Tech Tip: Instantiating singleton with .me before .new()
PRODUCT: 4D | VERSION: 20 R | PLATFORM: Mac & Win
Published On: May 2, 2025
When working with a singleton class, keep in mind that instantiating it with “.me” is like instantiating it with “.new()” without passing in any parameters. For example, suppose there is a singleton class “MySingletonClass”:
singleton Class constructor($param : Text) This.foo:=$param |
If this class is instantiated like so:
$mySingleton1:=cs.MySingletonClass.me |
Then, its “foo” property would be initialized to an empty (“”) string. Even if a call to “.new()” occurs after the instantiation, like this:
$mySingleton2:=cs.MySingletonClass.new("bar") |
In the above example, the singleton’s “foo” property would not be updated to “bar”, but instead remain an empty string, because “.me” was called first. Both variables “$mySingleton1” and “$mySingleton2” would refer to the same singleton with “foo” property as empty string. In this case, the subsequent call to “.new()” is essentially just another “.me” call. To update the “foo” property properly, key into it and assign it a value directly, like this:
$mySingleton1.foo:="bar" |