OZ 编程语言:布尔守卫

发布于 2024-12-03 04:57:18 字数 522 浏览 4 评论 0原文

我在学校选修的科目要求我们使用莫扎特编程接口。到目前为止我还没有想太多。但无论如何,这里有一个问题:

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗心遗梦遗幸福 2024-12-10 04:57:18

有很多方法可以做到这一点。

最简单的方法是在创建浏览器对象后立即设置它。不需要看守。

如果您想使用可变变量,请查看单元格。例如:

BrowserSetup = {NewCell false}
...
if @BrowserSetup == false ...
BrowserSetup := true

但是,这不是线程安全的,即如果您从多个线程使用 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:

BrowserSetup = {NewCell false}
...
if @BrowserSetup == false ...
BrowserSetup := true

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文