method with angle brackets (<>)

后端 未结 3 1233
不知归路
不知归路 2021-02-05 14:11

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 //up         


        
3条回答
  •  梦如初夏
    2021-02-05 14:30

    With a little trickery you can actually get quite close to what you want.

    object Foo {
      val a:Array[Int] = new Array(100)
      def <(i:Int) = new Updater(a, i)
    }
    
    class Updater(a:Array[Int], i:Int) {
      def update(x:Int) {
        a(i) = x
      }
      def >() = this
    }
    
    Foo<1>() = 123
    

    I am not sure why Scala requires the () though. And yes, this is a bit of a hack...

提交回复
热议问题