Re: mir.ndslice : multi dimensional associative array - Example/Docs

2020-10-26 Thread 9il via Digitalmars-d-learn

On Monday, 26 October 2020 at 14:31:00 UTC, Vino wrote:

Hi All,

  Is it possible to create a multi dimensional associative 
array using mir.ndslice,

  if yes,
   (1): request you to point me to some example / docs
   (2): below is an example multi dimensional associative array 
using the core d module, and

how can we to implement the same using mir.ndslice.
   (3): What are the pros and cons of using mir.ndslice over 
the core d module.


import std.stdio;
void main () {
   string[int][string] aa;
   aa["Name"] = [1: "test01", 2:"test02"];
   aa["Pool"] = [1: "Development", 2:"Quality"];
   foreach(i; aa["Pool"].byValue) { writeln(i); } 
}

From,
Vino.B


No. ndslice provides rectangular arrays. An associative array (as 
it defined in D) can't be really 2D, instead, it is an AA of AAs 
like in your example. A real 2D analog associative arrays are 
DataFrames. It is a postponed WIP ndslice feature.


Ilya


Re: Is there something I'm not getting here?

2020-10-26 Thread matheus via Digitalmars-d-learn

On Tuesday, 27 October 2020 at 02:21:39 UTC, matheus wrote:
On Tuesday, 27 October 2020 at 01:26:56 UTC, James Blachly 
wrote:

On 10/26/20 9:19 PM, Ruby The Roobster wrote:
Following code doesn't work(it's not the actual code but it 
represents it). Is there some rule about function overrides 
that I don't know about?



...
The error I keep getting no matter what says: Error: Multiple 
Overrides of Same Function. Anybody know what I should do?


It works for me.
...


I think he is referring to this:
...


I mean he may be having a duplicate method in the same class.

Matheus.


Re: Is there something I'm not getting here?

2020-10-26 Thread matheus via Digitalmars-d-learn

On Tuesday, 27 October 2020 at 01:26:56 UTC, James Blachly wrote:

On 10/26/20 9:19 PM, Ruby The Roobster wrote:
Following code doesn't work(it's not the actual code but it 
represents it). Is there some rule about function overrides 
that I don't know about?



...
The error I keep getting no matter what says: Error: Multiple 
Overrides of Same Function. Anybody know what I should do?


It works for me.
...


I think he is referring to this:

import std;

class B{
public override string toString(){
return null;
}

public override string toString(){
return toString(null);
}
}

void main() {
B b = new P();
}

Error: function `B.toString` multiple overrides of same function

You can view: https://www.jdoodle.com/iembed/v0/3rm

Matheus.


How is this an "Access Violation"

2020-10-26 Thread Ruby The Roobster via Digitalmars-d-learn
Following function when called throws an access violation. I 
think it has to do with the assert statements, but I don't know 
why.


   void construct(string type,atom base,atom bonded)
{
base = new 
atom(base.name.idup,base.mass,base.electro_negativity,base.valence_electrons,base.electrons,base.protons,base.neutrons,base.pos);
bonded = new 
atom(bonded.name.idup,bonded.mass,bonded.electro_negativity,bonded.valence_electrons,bonded.electrons,bonded.protons,bonded.neutrons,bonded.pos);

if(type == "single")
{
assert(this.base.valence >= 1 && this.bonded.valence 
>=1 && this.base.electro_negativity >= 0 && 
this.bonded.electro_negativity >= 0,"For a single bond, both 
atoms need to have at least one free electron and have to have 
electro negativity.");

this.type = "single".dup;
}
else if(type == "double")
{
assert(this.base.valence >= 2 && this.bonded.valence 
>=2 && this.base.electro_negativity >= 0 && 
this.bonded.electro_negativity >= 0,"For a double bond, both 
atoms need to have at least one free electron and have to have 
electro negativity.");

this.type = "double".dup;
}
else if(type == "triple")
{
assert(this.base.valence >= 3 && this.bonded.valence 
>=3 && this.base.electro_negativity >= 0 && 
this.bonded.electro_negativity >= 0,"For a triple bond, both 
atoms need to have at least one free electron and have to have 
electro negativity.");

this.type = "triple".dup;
}
else if(type == "ionic")
{
bool this_electro_negativity_greater;
if((this.base.electro_negativity > 
this.bonded.electro_negativity))

this_electro_negativity_greater = true;
else if((this.base.electro_negativity < 
this.bonded.electro_negativity))

this_electro_negativity_greater = false;
if(this_electro_negativity_greater)
{
assert(((this.base._valence_electrons == 
this.base.valence) && (this.bonded._valence_electrons == 
this.bonded.valence)),"Atoms in an ionic bond can't already be 
ions.");

int fullValence;
if(this.base.electrons < 2)
fullValence = 2;
else
fullValence = 8;
assert((fullValence - 
this.base.valence_electrons) == 
(this.bonded.valence_electrons),"The amount valence electrons in 
the atom with less valence electrons must be the same as the 
value of (8 - (the amount of valence electrons in the atom with 
more valence electrons))");

}
else
{
assert(((this.base._valence_electrons == 
this.base.valence) && (this.bonded._valence_electrons == 
this.bonded.valence)),"Atoms in an ionic bond can't already be 
ions.");

int thatFullValence;
if(this.bonded.electrons < 2)
thatFullValence = 2;
else
thatFullValence = 8;
assert((thatFullValence - 
this.bonded.valence_electrons) == 
(this.base.valence_electrons),"The amount valence electrons in 
the atom with less valence electrons must be the same as the 
value of (8 - (the amount of valence electrons in the atom with 
more valence electrons))");

}
}
}


Re: Is there something I'm not getting here?

2020-10-26 Thread Ruby The Roobster via Digitalmars-d-learn
On Tuesday, 27 October 2020 at 01:19:58 UTC, Ruby The Roobster 
wrote:
Following code doesn't work(it's not the actual code but it 
represents it). Is there some rule about function overrides 
that I don't know about?


class a {
public override string toString() {
//...
}
}

class b : a {
public override string toString() {
//...
}
}

The error I keep getting no matter what says: Error: Multiple 
Overrides of Same Function. Anybody know what I should do?


Okay. Realized this wasn't actually affecting my program, but is 
there any explanation for why this doesn't work?


Re: Is there something I'm not getting here?

2020-10-26 Thread James Blachly via Digitalmars-d-learn

On 10/26/20 9:19 PM, Ruby The Roobster wrote:
Following code doesn't work(it's not the actual code but it represents 
it). Is there some rule about function overrides that I don't know about?



...
The error I keep getting no matter what says: Error: Multiple Overrides 
of Same Function. Anybody know what I should do?


It works for me.

The "shorten" link on run.dlang.io no longer works, but here is a 
working example:


```
import std;

class A {
public override string toString() {
return "a";
}
}

class B : A {
public override string toString() {
return "b";
}
}

void main()
{
A a = new A();
B b = new B();
writeln(a);
writeln(b);
}
```


Is there something I'm not getting here?

2020-10-26 Thread Ruby The Roobster via Digitalmars-d-learn
Following code doesn't work(it's not the actual code but it 
represents it). Is there some rule about function overrides that 
I don't know about?


class a {
public override string toString() {
//...
}
}

class b : a {
public override string toString() {
//...
}
}

The error I keep getting no matter what says: Error: Multiple 
Overrides of Same Function. Anybody know what I should do?


How Stop Worker Thread if Owner Thread is Finished?

2020-10-26 Thread Marcone via Digitalmars-d-learn
Because when the main thread is completed the worker thread 
continues to run.


Re: String Template Package

2020-10-26 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Oct 26, 2020 at 11:36:12PM +, Per Nordlöw via Digitalmars-d-learn 
wrote:
> I need a string template system for generation of parsers in D code to
> compiled in separate phase (not Pegged).
> 
> What packages is there for this?

Adela Vais is working on D support for GNU bison, a parser generator
that uses a separate compile phase.  I don't know how far she has gotten
and how usable the current support is, but it's something to keep an eye
out for.


T

-- 
Let's call it an accidental feature. -- Larry Wall


String Template Package

2020-10-26 Thread Per Nordlöw via Digitalmars-d-learn
I need a string template system for generation of parsers in D 
code to compiled in separate phase (not Pegged).


What packages is there for this?


C++ code to D (multi dem 3d mesh)

2020-10-26 Thread Joel via Digitalmars-d-learn

```
struct vec3d
{
float x, y, z;
}

struct triangle
{
vec3d[3] p;
}

struct mesh
{
triangle[] tris;
}

// This here
meshCube.tris = {

// SOUTH
{ 0.0f, 0.0f, 0.0f,0.0f, 1.0f, 0.0f,1.0f, 1.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f,1.0f, 1.0f, 0.0f,1.0f, 0.0f, 0.0f },

// EAST
{ 1.0f, 0.0f, 0.0f,1.0f, 1.0f, 0.0f,1.0f, 1.0f, 1.0f },
{ 1.0f, 0.0f, 0.0f,1.0f, 1.0f, 1.0f,1.0f, 0.0f, 1.0f },

// NORTH
{ 1.0f, 0.0f, 1.0f,1.0f, 1.0f, 1.0f,0.0f, 1.0f, 1.0f },
{ 1.0f, 0.0f, 1.0f,0.0f, 1.0f, 1.0f,0.0f, 0.0f, 1.0f },

// WEST
{ 0.0f, 0.0f, 1.0f,0.0f, 1.0f, 1.0f,0.0f, 1.0f, 0.0f },
{ 0.0f, 0.0f, 1.0f,0.0f, 1.0f, 0.0f,0.0f, 0.0f, 0.0f },

// TOP
{ 0.0f, 1.0f, 0.0f,0.0f, 1.0f, 1.0f,1.0f, 1.0f, 1.0f },
{ 0.0f, 1.0f, 0.0f,1.0f, 1.0f, 1.0f,1.0f, 1.0f, 0.0f },

// BOTTOM
{ 1.0f, 0.0f, 1.0f,0.0f, 0.0f, 1.0f,0.0f, 0.0f, 0.0f },
{ 1.0f, 0.0f, 1.0f,0.0f, 0.0f, 0.0f,1.0f, 0.0f, 0.0f },

};
```

See:
https://youtu.be/ih20l3pJoeU
https://github.com/OneLoneCoder/videos/blob/master/OneLoneCoder_olcEngine3D_Part1.cpp



Re: Given a TypeInfo_Class, instance a new object of its type

2020-10-26 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 26 October 2020 at 21:57:18 UTC, Not A Rectangle wrote:

Is this possible to do?


Only if you know the type ahead of time, then you can cast it.

Normally you'd probably just know an interface or base class it 
implements then you can cast to that, with the exact derived type 
being only known at runtime.


Given a TypeInfo_Class, instance a new object of its type

2020-10-26 Thread Not A Rectangle via Digitalmars-d-learn

Hi everyone,
Say i have a TypeInfo_Class representing a custom type, and i 
want a new instance of this class with its custom type, is there 
a way to do that?


I am aware that TypeInfo_Class has the methods create() and 
factory(), both of which return a new object of the type Object.
I have tried to figure out a way to cast to the custom type using 
cast(...), but it seems i can't get the custom type held in 
TypeInfo in a way cast() will accept.


Is this possible to do?
Thanks


Re: How can I use class and wasm?

2020-10-26 Thread Jack via Digitalmars-d-learn

On Friday, 16 October 2020 at 03:42:22 UTC, Adam D. Ruppe wrote:

On Friday, 16 October 2020 at 03:04:25 UTC, Jack wrote:

How can I allocate memory for this class?


It is possible but not easy without druntime.

If you are using -betterC, you can use extern(C++) classes with 
extern(D) members. The compiler will let you declare that. But 
then you need to allocate it. `__traits(classInstanceSize, 
Whatever)` will tell you the size to malloc, but you also need 
to copy an initializer over before you call the constructor.


I have a technique here that works on dmd...

http://dpldocs.info/this-week-in-d/Blog.Posted_2020_07_27.html#zero-runtime-classes


I did consider do something like this but the class would only 
live in the function's lifetime, right? that wouldn't work if I 
need to pass the class around


but ldc is more strict about the type definition and I don't 
know the magic it expects there... like it should be doable but 
idk how so this might not be of much use.


Which type definition are you refering to? you mean the class 
members/memory layout of a class?


Personally, I prefer to just not use betterC and make my own 
mini runtime:


I was thinking the same, this seems the way to go.


http://dpldocs.info/this-week-in-d/Blog.Posted_2020_08_10.html

in particular


This is great! I'll try write a small druntime too for my case. 
Do you have links/resources where things like __heap_base, 
__data_end are defined? assuming there's such documentation 
somewhere, if you did into the compiler to get those, let me know 
where you can it too. I didn't understand the interation of 
webassembly-core.js with D. for example, you have this:


// placeholder to be filled in by the loader
var memory;

How did you put the compiler/linker to set memory's location 
properly so you can do things like:


memorySize: function() { return memory.buffer.byteLength; },

?

in the same way that ldc defines __headp_base and __data_end, 
doesn't it define some variables where the implementation of 
memorySize() and growMemory() could be done in D? also, where can 
I find this Sebastiaan's project that you mentioned in the 
article? it's a druntime like yours?




https://github.com/adamdruppe/webassembly/blob/master/arsd-webassembly/object.d#L74


this is great, good enough to put me in the direction on how to 
do that. Also, I found C++ has this EMSCRIPTEN_BINDINGS() from 
emscripten/bind.h header, see[1] for example. I haven't tried 
yet. But maybe marking the class as extern(C) that would work 
with D's class too, right? I don't know about the C++'s or 
compiler's dependences that would prevent it from working with D. 
In fact, that seems quite a hacky. Do you think we are better off 
write our own small druntime? if I got an working nice one, I'll 
put on github for those who would like to use this.


[1]: 
https://stackoverflow.com/questions/15865923/interaction-with-c-classes-in-emscripten


Thanks for your answer, that was really helpful.



But that's also not easy, lots of unfinished cases in my thing, 
but I did manage to make it work... for my specific case.





Foreach output into a multi dimensional associative array.

2020-10-26 Thread Vino via Digitalmars-d-learn

Hi All,

  Request your help on the below on how to store the output to a 
multi dimensional associative array.


Code:

import std.stdio: writeln;
import asdf: parseJson;
import std.conv: to;

void main()
{
 string[int][string] aa;
 string apidata = `{"items":
  [
{"name":"T01","hostname":"test01","pool":"Development"}
{"name":"T02","hostname":"test02","pool":"Quality"},
{"name":"T03","hostname":"test03","pool":"Production"}
  ]
  }`;
 auto jv = parseJson(apidata);
  foreach(j; jv["items"].byElement()){
  aa["Name"] = j["name"].get!string("default");
   i++;
  }
  writeln(aa);
}

Expected Output
aa["Name"] = [T01, T01, T03]
aa["Hostname"] = [test01, test02, test03]
aa["Pool"] = [Development, Quality, Production]

From,
Vino.B


Re: static alias this and if/do/while/for

2020-10-26 Thread Q. Schroll via Digitalmars-d-learn
On Thursday, 22 October 2020 at 21:55:59 UTC, Jack Applegame 
wrote:

There is a funny feature (or bug) in the D language:

static alias this and static operator overloading.

For example


interface Foo {
static {
int value;
void opAssign(int v) { value = v; }
int get() { return value; }
alias get this;
}
}


Now we can use type Foo as if it were an lvalue/rvalue:


Foo = 5;
int a = Foo;
int b = Foo + a;


I heavily use this feature in my MCU library.


It doesn't surprise me that much. What actually did was that 
static opIndex() works. Consider this:


struct X
{
static int opIndex() { return 1; }
}

alias SliceOf(T) = T[];
enum ValueOf(T) = T[];

static assert( ! is(int[SliceOf!X] == int[ValueOf!X]));

But static opIndex and friends can be useful: 
https://run.dlang.io/is/s15zS0 I'd actually find it awesome if we 
had opCallAssign and opCallOpAssign; the reason I used opIndex 
and not opCall is that way, one gets access to the right-hand 
side and to-assign parameters by reference, so no internal 
pointer stuff's needed.



But it doesn't work inside conditionals:


if(Foo) {}// Error: type Foo is not an expression
while(Foo) {} // Error: type Foo is not an expression


Even if we define `static opCast!bool()`. It doesn't help.


Only using Foo.opCast!bool works; cast(bool) won't compile.


Should this be fixed?


The bool stuff should be fixed. The other direction would be a 
breaking change.


Re: `unittest` placement in code?

2020-10-26 Thread Anonymouse via Digitalmars-d-learn
On Monday, 26 October 2020 at 13:16:32 UTC, Vladimirs Nordholm 
wrote:

Hello.
[...]


Additionally, if you care about generating documentation, then 
placing them after whatever is being tested can make them end up 
as examples (of how to use them). Handy if your unittests shows 
how they're used and not only tests that they work; not so much 
otherwise.


class Foo
{
/// asdf
int multiplier;

/++
Multiplies stuff by `multiplier`.

Params:
n = Number to multiply.

Returns:
The integer passed, multiplied by `multiplier`.
 +/
int mul(int n)
{
return multiplier * n;
}

///
unittest
{
// Will end up in examples section, provided /// is 
placed above unittest

Foo foo = new Foo;
foo.multiplier = 2;

int four = foo.mul(2);
int six = foo.mul(3);
int eight = foo.mul(4);
int ten = foo.mul(5);
}
}

https://i.imgur.com/zjuJQkR.jpg

Even so I keep my larger test suite in a separate tests/ 
directory next to source/, but mostly because they'd swamp the 
original source files. You also lose the ability to unittest 
private stuff this way.


mir.ndslice : multi dimensional associative array - Example/Docs

2020-10-26 Thread Vino via Digitalmars-d-learn

Hi All,

  Is it possible to create a multi dimensional associative array 
using mir.ndslice,

  if yes,
   (1): request you to point me to some example / docs
   (2): below is an example multi dimensional associative array 
using the core d module, and

how can we to implement the same using mir.ndslice.
   (3): What are the pros and cons of using mir.ndslice over the 
core d module.


import std.stdio;
void main () {
   string[int][string] aa;
   aa["Name"] = [1: "test01", 2:"test02"];
   aa["Pool"] = [1: "Development", 2:"Quality"];
   foreach(i; aa["Pool"].byValue) { writeln(i); } 
}

From,
Vino.B


Re: `unittest` placement in code?

2020-10-26 Thread Vladimirs Nordholm via Digitalmars-d-learn
On Monday, 26 October 2020 at 13:36:58 UTC, Steven Schveighoffer 
wrote:

On 10/26/20 9:16 AM, Vladimirs Nordholm wrote:

[...]


Wherever you want. Generally people put it right after the 
thing being tested to keep files organized.


When the compiler runs unittests it runs them all in the 
sequence they are in the file.


One place to be cautious though -- if you put them inside a 
template, they will be created for every instantiation of that 
template.


-Steve


Thanks for the explanation (and the warning) Steve :)


Re: `unittest` placement in code?

2020-10-26 Thread Steven Schveighoffer via Digitalmars-d-learn

On 10/26/20 9:16 AM, Vladimirs Nordholm wrote:

Hello.

I have a class which I have written some tests for, to ensure if I ever 
change some code it will still work as intended.


The documentation https://dlang.org/spec/unittest.html says it is can be 
placed both in the class or outside it.


I come from a background of having a completely separate tests folder 
with only tests, so I do not know what the general best-practice is with 
D. Where should the `unittest` code block go?


Wherever you want. Generally people put it right after the thing being 
tested to keep files organized.


When the compiler runs unittests it runs them all in the sequence they 
are in the file.


One place to be cautious though -- if you put them inside a template, 
they will be created for every instantiation of that template.


-Steve


Re: Can we do compile time reading part of a file using import?

2020-10-26 Thread Steven Schveighoffer via Digitalmars-d-learn

On 10/23/20 12:42 PM, data pulverizer wrote:

Hi all,

the `import` function allows a file to be read at compile time, which 
opens up great opportunities for (mostly binary) file IO, where data 
types can be coded into files - the user doesn't need to know data types 
ahead of time. As specified in my old blog article: 
https://www.active-analytics.com/blog/reading-idx-files-in-d/.


Binary files can be very large and reading the whole file at compile 
time is often unnecessary.


This isn't exactly the intended use for the function, but there it is. 
Since compile time read capability already exists and is useful, adding 
capability to be able to read only a portion of the file at compile time 
in a given location is advantageous and has utility.


For me it's not make-or-break, it just something very useful and I think 
has clear use case. Please let me know if there are aspects or 
alternatives I am missing.


I see no reason why the compiler can't do something special with:

enum slicedFile = import("foo.bin")[0 .. 100];

One thing that comes to mind -- most OSes support memory-mapped files. 
If import simply did a memory mapped file internally, and duplicated the 
string once actually used somewhere, then you can avoid full reading of 
the data until it's necessary.


-Steve


Re: static alias this and if/do/while/for

2020-10-26 Thread Vladimirs Nordholm via Digitalmars-d-learn
On Thursday, 22 October 2020 at 21:55:59 UTC, Jack Applegame 
wrote:

Now we can use type Foo as if it were an lvalue/rvalue:


Foo = 5;
int a = Foo;
int b = Foo + a;


Haha, that's pretty neat!


`unittest` placement in code?

2020-10-26 Thread Vladimirs Nordholm via Digitalmars-d-learn

Hello.

I have a class which I have written some tests for, to ensure if 
I ever change some code it will still work as intended.


The documentation https://dlang.org/spec/unittest.html says it is 
can be placed both in the class or outside it.


I come from a background of having a completely separate tests 
folder with only tests, so I do not know what the general 
best-practice is with D. Where should the `unittest` code block 
go?


Re: this T / variadic template and interfaces

2020-10-26 Thread Jacob Carlborg via Digitalmars-d-learn

On Monday, 26 October 2020 at 11:14:47 UTC, frame wrote:

Is there any way to get this working? I know, I could use a 
known object to feed the arguments and use that instead - but I 
want to keep things simple as possible.


As Simen mentioned, templates cannot be virtual. But you don't 
need to use a template, you can use a regular variadic method 
[1]. It's a bit more clunky to work with than template variadic 
functions. Or if all the arguments will be of the same type, you 
can use type safe variadic functions [2], which are easier to 
work with.


[1] 
https://dlang.org/spec/function.html#d_style_variadic_functions
[2] 
https://dlang.org/spec/function.html#typesafe_variadic_functions


--
/Jacob Carlborg



Re: Template pattern delegate?

2020-10-26 Thread frame via Digitalmars-d-learn

On Monday, 26 October 2020 at 09:25:03 UTC, Jacob Carlborg wrote:

On Monday, 26 October 2020 at 00:56:26 UTC, frame wrote:


If you pass the delegate as a template parameter/alias 
parameter, it's more likely to be inlined:


auto myStuff(alias fn)() {
try return fn();
catch (Exception e) { }
}

myStuff!( { /* some code */ } );

--
/Jacob Carlborg


Does not work in my case. Seems to conflict with overloads, but 
thanks for the hint.





Re: this T / variadic template and interfaces

2020-10-26 Thread frame via Digitalmars-d-learn

On Monday, 26 October 2020 at 11:48:48 UTC, Simen Kjærås wrote:



This makes sense if you consider that the user of the interface 
has no knowledge of the types that implement it, and vice 
versa: the implementing class has no idea which instantiations 
to make, and the user has no idea which implementing classes to 
create instantiations for. Templates require that the user have 
full knowledge of the templates to be instantiated.


There are some workarounds of sorts, but they depend heavily on 
what you're trying to achieve. Can you use an array of 
std.variant.Variant, for instance?


--
  Simen


Yes, the user/coder does not know of other types by any chance. 
The interface must be used.


Well I guess I let the interface do the variadic stuff and pass 
the argument as Variant[] to the wrapper in a new interface 
method. Thanks.




Re: this T / variadic template and interfaces

2020-10-26 Thread Simen Kjærås via Digitalmars-d-learn

On Monday, 26 October 2020 at 11:14:47 UTC, frame wrote:

Did not find this topic:

I have an interface and some wrapper classes that use it. The 
wrapper's methods should accept variadic arguments. The runtime 
should only work with the interface, trying casting to a 
wrapper is not an option, because it's a plugin design.


- defining a variadic template in wrapper does not work, 
because we are working with the interface only and compiler 
complains method is not callable with argument X


- defining a variadic template without body in interface causes 
linker errors, which makes sense somehow


- defining a variadic template with body in interface could 
work if the compiler would get the right "this" type but sadly, 
"this" refers to interface and also "this T" refers to 
interface too.


Is there any way to get this working? I know, I could use a 
known object to feed the arguments and use that instead - but I 
want to keep things simple as possible.


Templates can't be virtual, so even if they can be defined in an 
interface, you can't override them in a class that implements 
said interface - the implementation needs to be in the interface 
itself.


This makes sense if you consider that the user of the interface 
has no knowledge of the types that implement it, and vice versa: 
the implementing class has no idea which instantiations to make, 
and the user has no idea which implementing classes to create 
instantiations for. Templates require that the user have full 
knowledge of the templates to be instantiated.


There are some workarounds of sorts, but they depend heavily on 
what you're trying to achieve. Can you use an array of 
std.variant.Variant, for instance?


--
  Simen


this T / variadic template and interfaces

2020-10-26 Thread frame via Digitalmars-d-learn

Did not find this topic:

I have an interface and some wrapper classes that use it. The 
wrapper's methods should accept variadic arguments. The runtime 
should only work with the interface, trying casting to a wrapper 
is not an option, because it's a plugin design.


- defining a variadic template in wrapper does not work, because 
we are working with the interface only and compiler complains 
method is not callable with argument X


- defining a variadic template without body in interface causes 
linker errors, which makes sense somehow


- defining a variadic template with body in interface could work 
if the compiler would get the right "this" type but sadly, "this" 
refers to interface and also "this T" refers to interface too.


Is there any way to get this working? I know, I could use a known 
object to feed the arguments and use that instead - but I want to 
keep things simple as possible.




Re: Help on asdf json module

2020-10-26 Thread Vino via Digitalmars-d-learn

On Monday, 26 October 2020 at 09:43:44 UTC, Vino wrote:

On Sunday, 25 October 2020 at 08:22:06 UTC, 9il wrote:

[...]


Hi All,

  Thank you for your help, and now need your suggestion as the 
below code is working and it also prints an additional value 
which is not expected, so request your help on how to avoid the 
additional value,


[...]


Hi All,

  Sorry, I was able to find the issue as there was an additional 
writeln in the code hence the additional output was printed.


From,
Vino.B


Re: Help on asdf json module

2020-10-26 Thread Vino via Digitalmars-d-learn

On Sunday, 25 October 2020 at 08:22:06 UTC, 9il wrote:

On Sunday, 25 October 2020 at 06:05:27 UTC, Vino wrote:

Hi All,

   Currently we are testing various json module such as 
"std.json, std_data_json, vibe.data.json and asdf", the below 
code works perfectely while use  "std_data_json or 
vibe.data.json" but not working as expected when we use "asdf" 
and throwing the below error, hence request your help on the 
same.


[...]


Hi Vino,

byElement should be used here for ASDF.

foreach(j; jv["items"].byElement)

http://asdf.libmir.org/asdf_asdf.html#.Asdf.byElement


Hi All,

  Thank you for your help, and now need your suggestion as the 
below code is working and it also prints an additional value 
which is not expected, so request your help on how to avoid the 
additional value,


Code:
import std.stdio: writeln;
import asdf: parseJson;
import std.conv: to;
import std.range: takeExactly;
import std.array: split;

void main()
{
 string apidata = `{"items":
  [
{"name":"T01","hostname":"test01","pool":"Development","type":
[{"Value":"D,d...@dev.com,DEV"},{"Value":"000"}]},
{"name":"T02","hostname":"test02","pool":"Quality","type":
[{"Value":"Q,q...@qas.com,QAS"},{"Value":"100"}]},
{"name":"T03","hostname":"test03","pool":"Production","type":
[{"Value":"P,p...@prd.com,PRD"},{"Value":"100"}]}
  ]
}`;
 auto jv = parseJson(apidata);
 foreach(j; jv["items"].byElement()){
foreach(i; j["type"].byElement().takeExactly(1)) {
   writeln(i["Value"].get!string("default").split(",")[1]);
}
 }
}

Output:
d...@dev.com
q...@qas.com
p...@prd.com
[]   // additional value which is not expected

From,
Vino.B



Re: Template pattern delegate?

2020-10-26 Thread Jacob Carlborg via Digitalmars-d-learn

On Monday, 26 October 2020 at 00:56:26 UTC, frame wrote:

I see that your approach can handle functions and delegates but 
isn't that not equivalent like this template for a function?


auto myStuff(T)(T function() fn) {
try {
return fn();
}
catch (Exception e) {
//
}
}


You cannot pass a delegate to something that expects a function 
pointer.


I wonder if I could use such a template as static variant and 
the compiler just expands the code? Just thinking that using a 
function or delegate is an overhead. Maybe not a function but a 
delegate will allocate GC memory I think.


If you pass the delegate as a template parameter/alias parameter, 
it's more likely to be inlined:


auto myStuff(alias fn)() {
try return fn();
catch (Exception e) { }
}

myStuff!( { /* some code */ } );

--
/Jacob Carlborg




Re: Can we do compile time reading part of a file using import?

2020-10-26 Thread Jacob Carlborg via Digitalmars-d-learn

On Sunday, 25 October 2020 at 16:50:09 UTC, Jack wrote:

Which build tool are you refering to? an existing one or build 
one oneself to do this job?


It should work with any build tool that has hooks to execute 
arbitrary commands.


--
/Jacob Carlborg