你能在Scala中返回一个可赋值的左值吗?
(注意,左值实际上是C语法中的一个术语,我不知道它在Scala中叫什么!) 尝试学习Scala。。。今晚,我正在为一种动态范围语言开发一种内部DSL,这种语言可能类似于PHP语法 我的回复是:欢迎使用Scala版本2.7.6.final(JavaHotSpot(TM)客户端虚拟机,Java1.6.0) 我有一些虚构的示例代码: class $(any: Any) { def update(sym: Symbol, any: Any) { println("line 2 executed");} def ->(sym: Symbol) : $ = { println("line 1 executed"); return this } def update(any: Any) { println("line 3 executed");} } 类别$(任何:任何){ def更新(sym:Symbol,any:any){println(“第2行已执行”);} def->(sym:Symbol):$={println(“执行第1行”);返回此} def更新(any:any){println(“执行第3行”);} } 以下工作如预期: scala> var a = new $(0) a: $ = $@19238ad scala> a('x) = "blah" line 2 executed scala>var a=new$(0) a:$=$@19238ad scala>a('x)=“废话” 第2行已执行 另一方面,为什么下面不调用单参数更新方法 scala> a = 1 :6: error: type mismatch; found : Int(1) required: $ a = 1 ^ scala>a=1 :6:错误:类型不匹配; 发现:Int(1) 所需:$ a=1 ^ 在做一些尝试和错误时,我发现了这种句法上的好奇: scala> class A { def this_= { print("hello") } } defined class A scala> var a = new A a: A = A@9aca82 scala> a = 2 :6: error: type mismatch; found : Int(2) required: A a = 2 ^ scala> a.this_ :7: error: value this_ is not a member of A a.this_ ^ scala>class A{def this{print(“hello”)} 定义的A类 scala>var a=新a a:a=A@9aca82 scala>a=2 :6:错误:类型不匹配; 发现:Int(2) 所需:A a=2 ^ scala>a_ :7:错误:值此\不是 a、 这个_ ^ 覆盖上面的“这个”是什么意思?它去哪里了 最终,我希望这样做能够奏效: a->'x = "blah" a->'x=“诸如此类”你能在Scala中返回一个可赋值的左值吗?,scala,Scala,(注意,左值实际上是C语法中的一个术语,我不知道它在Scala中叫什么!) 尝试学习Scala。。。今晚,我正在为一种动态范围语言开发一种内部DSL,这种语言可能类似于PHP语法 我的回复是:欢迎使用Scala版本2.7.6.final(JavaHotSpot(TM)客户端虚拟机,Java1.6.0) 我有一些虚构的示例代码: class $(any: Any) { def update(sym: Symbol, any: Any) { println("line 2 executed"
谢谢我认为您需要的是隐式转换
scala> case class Test (val x: Int)
defined class Test
scala> implicit def testFromInt (x: Int) = new Test (x)
testFromInt: (Int)Test
scala> var a = new Test (3)
a: Test = Test(3)
scala> a = 10
a: Test = Test(10)
顺便说一句,我认为您不应该使用$作为标识符,它通常用于编译器生成的类/函数
def this_= { print("hello") }
你似乎认为这就是这个
方法,它等于{print(“hello”)}
。相反,这是方法this=
,它使用过程样式声明(即,无等号)
最常用的用法如下:
scala> class A {
| private var _x = ""
| def x = _x
| def x_=(s: String) = _x = s.toUpperCase
| }
defined class A
scala> new A
res0: A = A@1169fb2
scala> res0.x
res1: java.lang.String =
scala> res0.x = "abc"
scala> res0.x
res2: java.lang.String = ABC
id(key) = value // with update
id.key = value // with key_=, as long as key also exists and is public
id += value // or any other method containing "=" as part of its name
然而,虽然您碰巧使用了具有特殊含义的语法(id_=
),但它只是一个标识符。任何标识符都可以混合字母数字字符和其他符号,由下划线字符分隔
最后,不,Scala中没有可赋值的左值。你可以有这样的东西:
scala> class A {
| private var _x = ""
| def x = _x
| def x_=(s: String) = _x = s.toUpperCase
| }
defined class A
scala> new A
res0: A = A@1169fb2
scala> res0.x
res1: java.lang.String =
scala> res0.x = "abc"
scala> res0.x
res2: java.lang.String = ABC
id(key) = value // with update
id.key = value // with key_=, as long as key also exists and is public
id += value // or any other method containing "=" as part of its name
例如,您可以这样做:
scala> class A {
| private var _x = ""
| def :=(s: String) = _x = s.toUpperCase
| override def toString = "A("+_x+")"
| }
defined class A
scala> val x = new A
x: A = A()
scala> x := "abc"
scala> x
res4: A = A(ABC)
但是
=
本身是保留的。顺便说一句,Scala中没有“按引用传递”——您永远无法更改作为参数传递的变量的值。最后一个示例不起作用,因为该方法实际上是this
而不是this
定义方法时不需要=
(虽然如果不使用,该方法将返回单位
),并且两个scalac之间没有空格,只是假设您想要该方法此
。我相当确定=
方法不可能实现您想做的事情,因为它很特殊,但是它很容易用其他函数实现(例如谢谢,:=的想法实际上是一个很好的解决办法,如果裸“=”不起作用,实际上没有理由在Scala中避免$,它在生成的字节码中转义为$。