Singleton Object, calling member functions using UFCS (an "ugly?" example) ... better way?
james.p.leblanc
james.p.leblanc at gmail.com
Wed Sep 1 20:59:15 UTC 2021
On Wednesday, 1 September 2021 at 19:54:14 UTC, user1234 wrote:
> On Wednesday, 1 September 2021 at 16:02:47 UTC, james.p.leblanc
> wrote:
>> Dear D-ers,
>>
>> For simple plotting using a gnuplot process, I have created a
>> singleton object (a stripped
>> down minimal working example is below.)
>>
>> [...]
>>
>> **However, those wrapper functions in the gnuplot_mod module
>> looks a bit silly.**
>>
>> [...]
>>
>> Any suggestions on a better way are greatly appreciated.
>>
>> Best Regards,
>> James
>
> hello, I'd suggest this:
>
> ```d
> shared static this() {
> get(); // cache instance before main(){}
> // to get rid of the synchronized() stmt
> }
>
> static Gnuplot get() {
> __gshared Gnuplot instance;
> return instance ? instance : (instance = new Gnuplot());
> }
> ```
user1234,
Thanks for your reply, I will need to look at it deeper.
I also realize that my post was not so clear, to clarify:
I needed to implement two wrapper functions ... that are **in**
the
module, but **outside** of the object, copied below:
**void cmd(string txt){**
**Gnuplot gp;**
**gp = gp.get();**
**gp.cmd(txt);**
**}**
**void plot(double[] x, string txt){**
**Gnuplot gp;**
**gp = gp.get();**
**gp.plot(x, txt);**
**}**
These enable use of the UFCS in main() (without prefixing with
instantiated object's name
**gp** ). The gp prefix only needs to be in these wrappers that
exist in the module, not in the main().
The question is if there is a way to enable UFCS without such
wrapper
(see example in main() in original posting).
Thanks again, and now I need to play a bit with your suggestion
to learn
from it.
Best Regards,
James
More information about the Digitalmars-d-learn
mailing list