class.property = x 是否可以返回除 x 之外的其他内容?
假设我有一个 Ruby 类:
class MyClass
def self.property
return "someVal"
end
def self.property=(newVal)
# do something to set "property"
success = true
return success # success is a boolean
end
end
如果我尝试执行 MyClass.property=x
,则整个语句的返回值始终为 x。 在许多基于 C/受启发的语言中,返回布尔“成功”值是一种约定 - 是否可以使用 Ruby 中的“等于语法”为 setter 执行此操作?
此外 - 如果这不可能,为什么不呢? 允许“等于设置器”操作返回值是否有任何可以想象的缺点?
Let's say I have a Ruby class:
class MyClass
def self.property
return "someVal"
end
def self.property=(newVal)
# do something to set "property"
success = true
return success # success is a boolean
end
end
If I try and do MyClass.property=x
, the return value of the whole statement is always x. It is a convention in a lot of C-based/inspired languages to return a boolean "success" value - is it possible to do this for a setter using the "equals syntax" in Ruby?
Furthermore - if this isn't possible, why not? Is there any conceivable downside to allowing an "equals setter" operation return a value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如马丁所说,这将打破分配链。
ruby 赋值方法的定义方式将
MyClass.property = 3
扩展为等价于(lambda { |v| MyClass.send('property=', v); v }) [3]
(不是真的,但这显示了链接的工作原理)。 赋值的返回值始终是分配的值。如果您想查看
MyClass#property=
方法的结果,请使用#send
:但是,Ruby 方法有例外情况 - 如果出现问题期间
分配,引发异常。 那么如果出现问题,所有调用者都必须处理它
错误,与返回值不同,它很容易被忽略:
Like Martin says, this would break assignment chaining.
The way ruby assignment methods are defined to work expands
MyClass.property = 3
to the equivalent of(lambda { |v| MyClass.send('property=', v); v })[3]
(not really, but this shows how chaining works). The return value of the assignment is always the value assigned.If you want to see the result of your
MyClass#property=
method, then use#send
:However, the ruby way to do this is with exceptions - if something goes wrong during
the assignment, raise an exception. Then all invokers must handle it if something goes
wrong, unlike a return value, which can be easily ignored:
我不是 Ruby 专家,但恐怕我会拒绝这种情况。 属性设置器仅用于设置私有字段的值,不会产生任何副作用,例如返回结果代码。
如果您想要该功能,请忘记设置器并编写一个名为
TrySetProperty
的新方法或尝试设置属性并返回布尔值的方法。I'm not a Ruby expert but I'd say no for that case I'm afraid. A property setter is solely there to set the value of a private field, not to have any side effects like returning result codes.
If you want that functionality then forget the setter and write a new method called
TrySetProperty
or something which tries to set the property and returns a boolean.一个缺点是您会破坏链式赋值语义:
考虑一下:
如果这按您的预期工作(右关联性),则
x
将采用true
。 对于使用您的界面并习惯典型语义的人来说,这可能会感到惊讶。您还让我考虑并行分配,例如:
显然该表达式的返回值是特定于实现的...我想我不会链接并行作业:)
好问题!
One downside is that you would break the chained assignment semantics:
Consider:
Then
x
would taketrue
if this worked as you had expected (right-associativity). That could be a surprise for people using your interface and used to the typical semantics.You also got me thinking about parallel assignment, eg:
Apparently the return value from that expression is implementation specific... I guess I won't be chaining parallel assignments :)
Nice question!