method with angle brackets (<>)

后端 未结 3 1321
不知归路
不知归路 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

    Edit: I was wrong; kassens's answer shows how to do it as you want.


    It is not possible to implement a method that would be called when you write v<1> = 4 (except, maybe, if you write a compiler plugin?). However, something like this would be possible:

    class Foo {
      def at(i: Int) = new Assigner(i)
      class Assigner(i: Int) {
        def :=(v: Int) = println("assigning " + v + " at index " + i)
      }
    }
    

    Then:

    val f = new Foo
    f at 4 := 6
    

提交回复
热议问题