How I may use "volatile from java" in GOSU?

272 views
Skip to first unread message

Will Hu

unread,
Oct 3, 2016, 8:25:20 AM10/3/16
to gosu-lang

Kyle Moore

unread,
Oct 3, 2016, 10:42:32 PM10/3/16
to gosu-lang
Hi Will and thanks for using Gosu!

We do not currently support the volatile keyword for fields of a Gosu class. However we will look into adding it.

I'm not sure about your use case, but for an alternative way to have control over how an instance variable is initialized you might consider gw.util.concurrent.LockingLazyVar<T>.

Example:
uses gw.util.concurrent.LockingLazyVar

class Foo {

 
var _bar = new LockingLazyVar<Integer>() {
   
override function init() : Integer {
     
print("initializing _bar")
     
return 2 + 2 //or do more complex initialization here
   
}
 
}

  property
get Bar() : Integer {
   
print("getting Bar")
   
return _bar.get()
 
}

}

Then simply new up Foo and call its Bar property from a Program:
print(new Foo().Bar)

And this should return:
getting Bar
initializing _bar
4


Note that the init() method is called after the getter is first accessed.

Again, I know this may not satisfy your use case but hopefully this can help.

- Kyle

Kyle Moore

unread,
Oct 3, 2016, 10:46:35 PM10/3/16
to gosu-lang
Created issue #86 on GitHub.


On Monday, October 3, 2016 at 5:25:20 AM UTC-7, Will Hu wrote:

Will Hu

unread,
Oct 4, 2016, 5:56:11 AM10/4/16
to gosu-lang
Hi Kyle,

Thanks for the explaining. I would write some concurrent tests using gw.util.concurrent.LockingLazyVar<T> to see if it works. 
Reply all
Reply to author
Forward
0 new messages