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

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


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))();
         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