OZ 编程语言:布尔守卫
我在学校选修的科目要求我们使用莫扎特编程接口。到目前为止我还没有想太多。但无论如何,这里有一个问题:
在 OZ 中,你只能分配一个变量一次(它不能被重新分配,但可以在当前范围内重新声明,如果我是对的?)。我遇到了一个问题,我想使用布尔保护,但 OZ 不允许我。我现在的情况是:
declare
BrowserObject = {New Browser.'class' init}
BrowserSetup = false
proc {Browse Bs}
if BrowserSetup == false then
{BrowserObject option(representation strings:true)}
{BrowserObject option(representation virtualStrings:true)}
BrowserSetup = true
end
{BrowserObject browse(Bs)}
end
有人知道如何做到这一点吗?感谢您抽出时间。
I am taking a subject at school which require us to use the Mozart Programming Interface. I do not really think much of it so far. But anyways, here is the question:
In OZ you are only allowed to assign a variable once (it can't be reassigned but redeclared in the current scope, if I am right?). I came across a problem where I want to use a boolean guard, but OZ wont let me. I have the current:
declare
BrowserObject = {New Browser.'class' init}
BrowserSetup = false
proc {Browse Bs}
if BrowserSetup == false then
{BrowserObject option(representation strings:true)}
{BrowserObject option(representation virtualStrings:true)}
BrowserSetup = true
end
{BrowserObject browse(Bs)}
end
Does anyone have any ideas of how to do this? Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有很多方法可以做到这一点。
最简单的方法是在创建浏览器对象后立即设置它。不需要看守。
如果您想使用可变变量,请查看单元格。例如:
但是,这不是线程安全的,即如果您从多个线程使用 Browse 过程,则会出现问题。
最好的方法可能是对 BrowserObject 使用延迟初始化,即仅在第一次使用时创建并初始化它。如果您想使用它,请查看“Value.byNeed”的文档。
我建议给语言一个机会。它与大多数语言有很大不同,您可能永远不会“在现实世界中”使用它。但还有很多东西需要学习,越来越多的研究语言概念进入了主流语言。
There are many ways to do this.
The easiest is to setup the browser object right after creating it. No need for a guard.
If you want to use a mutable variable, look at cells. For example:
However, this is not thread-safe, i.e. it is a problem if you use the Browse procedure from multiple threads.
The best way would probably be to use lazy initialization for BrowserObject, i.e. create and initialize it only when it is used the first time. Take a look at the documentation for "Value.byNeed" if you want to use this.
I recommend to give the language a chance. It is quite different from most languages and you will probably never use it "in the real world". But there is a lot to learn, and more and more concepts of research languages find their way into mainstream languages.