Singleton in Action?

Jacob Carlborg doob at me.com
Mon Feb 4 19:23:26 UTC 2019


On 2019-02-04 11:36, Ron Tarrant wrote:

> I've seen comments similar to this in several examples. When you say "no 
> one else" you're personifying callers? 

Yes.

> And so this means: No caller 
> outside the object? Which really amounts to: Since no one INside the 
> object WILL call this() and no one OUTside CAN call this(), it won't 
> ever get called.

Yes, well it's called once. Technically since the protections in D work 
on module level a caller inside the same file can access the 
constructor. But one is usually in control of the whole file.

>>     writeln(DSingleton.instance);
> 
> No '()' needed for the call to DSingleton.instance?

No need. It's the way D to implement properties.

> If it's called again from somewhere else, say from within an object 
> function several levels of scope away, it's called the same way?

You can call it with or without parentheses. It applies to all functions 
that don't take any arguments or functions taking a single argument and 
are called using UFCS [1].

[1] https://dlang.org/spec/function.html#pseudo-member

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list