我可以检查 Scala 中是否已计算惰性 val 吗?
例如,在 toString 方法中,我想提供有关类的惰性 val 成员是否已被求值的信息,如果是,则打印其值。这可能吗?
For instance, in a toString
method, I would like to give info on whether a lazy val member of a class has been evaluated, and if so, print its value. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,你不能。但你可以提供帮助:
当然,可见性和访问修饰符必须进行调整。
As far as I know, you can´t. But you can help with that:
Of course, visibility and access modifier have to be adapted.
如果您想直接访问编译器生成的字段,请尝试以下代码。
If you want direct access to the compiler generated field, please try the following code.
我还发现检查惰性值是否确实在某些棘手的情况下被评估很有用。
这是一个您可以使用的通用类:
I have also found it useful to check if the lazy value is in fact evaluated in some tricky situations.
And this is a generic class you could use: