Singleton Object, calling member functions using UFCS (an "ugly?" example) ... better way?

user1234 user1234 at 12.de
Wed Sep 1 22:11:29 UTC 2021


On Wednesday, 1 September 2021 at 22:01:12 UTC, user1234 wrote:
> On Wednesday, 1 September 2021 at 20:59:15 UTC, james.p.leblanc 
> wrote:
>> [...]
>> The question is if there is a way to enable UFCS without such 
>> wrapper
>
> sorry my attention got stuck on the singleton.
>
> So my suggestion is rather to only enable the ufcs style. This 
> highlights the fact that the class is useless, you can use a 
> simple getter that initializes a hidden global:
>
> ```d
> module gnuplot_mod;
>
> import std.stdio;
> import std.process;
> import std.algorithm : each;
> import std.range : enumerate;
>
> ProcessPipes gnuplot () {
>     __gshared ProcessPipes pipe;
>     return pipe.pid ? pipe : (pipe = 
> pipeProcess("/usr/local/bin/gnuplot"));
> }
>
> static void cmd(string txt) {
>     with (gnuplot()) {
>         stdin.writeln(txt);
>         stdin.flush();
>     }
> }
>
> static void plot(double[] x, string txt = "ls 21 lw 2"){
>     with (gnuplot()) {
>         stdin.writeln("plot '-' u 1:2 with lines ", txt);
>         enumerate(x).each!((i,v) => stdin.writefln!"%s %f"(i, 
> v))();

sorry there was a format mistake not detected at compile time.

```diff
- enumerate(x).each!((i,v) => stdin.writefln!"%s %f"(i, v))();
+ enumerate(x).each!(a => stdin.writefln!"%s %f"(a.index, 
a.value))();
```

>         stdin.writeln("e");
>         stdin.flush();
>     }
> }
>
> void main() {
>   double[] x = [1.1, 0.2, 3.1, 2.2, 3.1, 0.6];
>   double[] y = [-1.1, 0.2, -3.1, 2.2, 3.1, -0.6];
>   y.plot("ls 41 lw 8");
>   cmd("pause 2");
> }
>
> ```




More information about the Digitalmars-d-learn mailing list