开发者

OZ Programming language: Boolean guard

I am taking a subject at school which require us to use the Mozart Programming Interface. I do not really think mu开发者_开发技巧ch 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜