为什么不能在 Scala 中创建没有 getter 的 setter? [英] Why can't you create a setter without getter in scala?

查看:51
本文介绍了为什么不能在 Scala 中创建没有 getter 的 setter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Scala:没有getter就不能写setter?没有 getter 就不能创建 setter:

I found in Scala: can't write setter without getter? that you can't create a setter without getter:

对赋值的解释一个简单的变量 x = e 取决于x 的定义.如果 x 表示一个可变变量,然后赋值将 x 的当前值更改为评估结果表达式 e.e 的类型是预期符合 x 的类型.如果 x 是一个无参数函数在某个模板中定义,并且相同模板包含一个 setter 函数x_= 作为成员,然后赋值 x =e 被解释为调用该 setter 函数的 x_=(e ).类似地,赋值 f.x = e 到无参数函数 x 是解释为调用 f.x_=(e).赋值 f(args) = e 与左侧的功能应用程序‘=’ 运算符被解释为 f.update(args, e ),即调用由 f 定义的更新函数.

The interpretation of an assignment to a simple variable x = e depends on the definition of x. If x denotes a mutable variable, then the assignment changes the current value of x to be the result of evaluating the expression e. The type of e is expected to conform to the type of x. If x is a parameterless function defined in some template, and the same template contains a setter function x_= as member, then the assignment x = e is interpreted as the invocation x_=(e ) of that setter function. Analogously, an assignment f.x = e to a parameterless function x is interpreted as the invocation f.x_=(e ). An assignment f(args) = e with a function application to the left of the ‘=’ operator is interpreted as f.update(args, e ) , i.e. the invocation of an update function defined by f .

因此,不允许没有 getter 的 setter 是一个设计决定.但为什么?只是更难实施还是根本不可能做到?

So it is a design decision to not allow setters without getters. But why? Is it just be harder to implement or is it fundamentally impossible to do?

我确实有一个有效的用例,将它用作(有点复杂的)setter,不使用这种语法糖会破坏项目中任何地方的相同语法.

I do have a valid use case for it, using it as a (somewhat complex) setter, where not using this syntactic sugar would break having the same syntax everywhere in the project.

推荐答案

您可以尝试从 API 中排除访问器:

You might try this to exclude the accessor from your API:

scala> class C { def c_=(i: Int) = println(i) ; private def c: Int = ??? }
defined class C

scala> val c = new C
c: C = C@289fdb08

scala> c.c = 42
<console>:14: error: method c in class C cannot be accessed in C
val $ires0 = c.c
               ^
<console>:12: error: method c in class C cannot be accessed in C
       c.c = 42
         ^

scala> def f = { c.c = 42 ; 0 }
<console>:12: error: method c in class C cannot be accessed in C
       def f = { c.c = 42 ; 0 }
                   ^

在第一个错误中,REPL 尝试使用访问器报告值.

In the first error, the REPL is trying to report the value by using the accessor.

这种成对的访问器和修改器的概念称为通用访问原则,因此被访问的成员看起来像一个属性.

This notion of paired accessor and mutator is called the universal access principle, so that accessed member looks like a property.

表达式 c.c 在进一步脱糖之前必须进行类型检查.否则,转换(对 c.c_= 的调用)必须是纯粹的语法.

The expression c.c must type-check before further desugaring. Otherwise, the transform (to an invocation of c.c_=) must be purely syntactic.

例如,可以使用提供扩展方法 c_= 的隐式转换.

For example, an implicit conversion that supplies an extension method c_= could come into play.

这篇关于为什么不能在 Scala 中创建没有 getter 的 setter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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