带尖括号(<>)的方法 [英] method with angle brackets (<>)

查看:171
本文介绍了带尖括号(<>)的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在方法名称中是否可以有尖括号,例如:

Is it possible to have angle brackets in method names , e.g. :

class Foo(ind1:Int,ind2:Int){...}
var v = new Foo(1,2)
v(1) = 3 //updates ind1
v<1> = 4 //updates ind2

实际情况显然比这复杂得多!提供方便的用户界面。

The real situation is obviously more complicated than this!!I am trying to provide a convenient user interface.

推荐答案

此响应并不意味着太严重,而只是证明可以使用一些hack几乎实现。

This response is not meant too serious and is just a proof that this can almost be achieved using some hacks.

class Vector(values: Int*) {
  val data = values.toArray
  def < (i:Int) = new {
    def `>_=`(x: Int) {
      data(i) = x
    }
    def > {
      println("value at "+ i +" is "+ data(i))
    }
  }
  override def toString = data.mkString("<", ", ", ">")
}

val v = new Vector(1, 2, 3)
println(v) // prints <1, 2, 3>
v<1> = 10
println(v) // prints <1, 10, 3>
v<1> // prints: value at 1 is 10

使用此类,我们可以得到一个使用<$的向量c $ c><> 代替()的读和写访问权限。
如果> 返回值,则编译器(2.9.0.1)崩溃。可能是错误,也可能是由于误用了>

Using this class we can have a vector that uses <> instead of () for "read" and write access. The compiler (2.9.0.1) crashes if > returns a value. It might be a bug or a result of misusing >.

这篇关于带尖括号(&lt;&gt;)的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆