How to share an appender!string?

rikki cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 19 03:13:21 PDT 2016


On 19/05/2016 10:08 PM, Thorsten Sommer wrote:
> Dear community,
>
> I tried to create a kind of collector module which collects strings by
> using a shared appender!string. Why? To collect KPIs at a huge program
> across multiple classes and threads and store it later as e.g. CSV file
> in order to analyse it by using R.
>
> But I failed...
>
> Attempt #1: I tried what I would do in Java and C#: Using a Singelton
> pattern. But it was not as easy to create it in D and I found out, that
> today the Singleton is considered as anti-pattern. My conclusion was:
> Okay, that is right. Why should I force OOP in D to realize a Singleton?
> I can use different paradigm with D -- so just create a module with a
> shared appender!string ... cf. attempt #2.
>
>
> Attempt #2: https://dpaste.dzfl.pl/6035b6fdd4bd
> My second attempt was to make my appender "sb" shared. But I got this
> compiler error:
>
> std/array.d(3118): Error: 'auto' can only be used as part of 'auto ref'
> for template function parameters
>
> Do I used a wrong syntax or is this a bug?
>
>
> Attempt #3: https://dpaste.dzfl.pl/1e5df9cf08c6
> Okay, the third attempt. I searched around and found the
> Appender!(shared(string)) syntax. That version compiles and it runs
> fine. But the "sb" is not shared, because the "four" output is missing
> (cf. dpaste).
>
>
> Can anyone help me? I am stucked...
>
>
> Best regards,
> Thorsten

At this point I'd recommend you to just ignore Appender.
Write your own.

After all, all it does is this:

T[] data_;
size_t realLen;

void add(T v) {
	if (realLen + 1 > data.length) {
		data_.length += x;
	}
	data_[realLen] = v;
	realLen++;
}

T[] data() { return data_; }

Okay okay, I simplified things quite a bit.


More information about the Digitalmars-d-learn mailing list