Re: How do I check if this field is of this template struct?

2021-03-19 Thread Jack via Digitalmars-d-learn
On Saturday, 20 March 2021 at 00:16:06 UTC, Steven Schveighoffer 
wrote:

On 3/19/21 12:41 PM, Jack wrote:

On Friday, 19 March 2021 at 08:54:50 UTC, Paul Backus wrote:

On Friday, 19 March 2021 at 07:14:46 UTC, Jack wrote:
give below template struct, how can I list the members x, y 
and z? I've tried something with OriginalType and TemplateOf 
but no luck... it seems if I do foo!"str1" the "str1" became 
"part of type"? give .stringof from 
typeof(__traits(getMember, foo, field)) I thought the type 
would be foo!string or something.


You want std.traits.isInstanceOf:

    static if(!isType!m && isInstanceOf!(foo, typeof(m)))


thanks this works fine outside a method but not in a static 
method. what am I missing?


void main()
{
     // doesn't print anything
 Foo.list();
}

alias Foo = foo!();

template foo(string s = null)
{
 @nogc
 struct foo
 {
     int n;

     enum x = foo!"str1"(10);
     enum y = foo!"str2"(20);
     enum z = foo!"str3"(30);

     enum myEnum { none }

     void someMethod() { }

     static void list()
     {
     //writeln("members = ", [__traits(allMembers, 
foo!())]);
     static foreach(field; __traits(allMembers, 
foo!()))

     {{
     alias m = __traits(getMember, foo!(), field);
     static if(!isType!m && isInstanceOf!(foo, 
typeof(m)))


foo in this context is the *instantiated* foo (i.e. the struct 
foo above). What you want is `isIsntanceOf!(.foo, typeof(m))`


-Steve


Thank you Steve, that what I was looking for. Funny thing, I 
totally forget about the . operator


Re: How do I check if this field is of this template struct?

2021-03-19 Thread Jack via Digitalmars-d-learn

On Friday, 19 March 2021 at 17:40:39 UTC, frame wrote:

On Friday, 19 March 2021 at 16:41:11 UTC, Jack wrote:

thanks this works fine outside a method but not in a static 
method. what am I missing?


Reading the manual ;)

To use isInstanceOf to check the identity of a template while 
inside of said template, use TemplateOf.


indeed, it shwos an example on how to use that in a static 
function


Can I make this work?

2021-03-19 Thread Jack via Digitalmars-d-learn
there's a value passed as template parameter which is know at 
compile time (can be used with static if) but I don't know how 
can I pass it around and use in a property. Obviously enum 
deosn't work because it would be a local to the function. 
immutable would result in compiler can't read it at compile time. 
version doesn't work either because it isn't at module scope but 
a function.


full code:


struct S
{
enum x = init!(false, 10);
enum y = init!(true, 20);

this(string s)
{
type = Type.type1;
}

this(int n)
{
type = Type.type2;
}

static auto init(bool v, int val)()
{
auto s = S();

static if(v)
{
s.type = Type.type1;
}
else
{
s.type = Type.type2;
}

s.n = val;
return s;
}

int value()
{
// would to use static if here or just
// get rid of that check if type is Type.type1,
// information which is available at init()
// template function.
if(type == Type.type2) {
// do something
}

return n;
}

Type type;
int n;
}

enum Type
{
type1,
type2,
type3
}


Re: How do I check if this field is of this template struct?

2021-03-19 Thread Steven Schveighoffer via Digitalmars-d-learn

On 3/19/21 12:41 PM, Jack wrote:

On Friday, 19 March 2021 at 08:54:50 UTC, Paul Backus wrote:

On Friday, 19 March 2021 at 07:14:46 UTC, Jack wrote:
give below template struct, how can I list the members x, y and z? 
I've tried something with OriginalType and TemplateOf but no luck... 
it seems if I do foo!"str1" the "str1" became "part of type"? give 
.stringof from typeof(__traits(getMember, foo, field)) I thought the 
type would be foo!string or something.


You want std.traits.isInstanceOf:

    static if(!isType!m && isInstanceOf!(foo, typeof(m)))


thanks this works fine outside a method but not in a static method. what 
am I missing?


void main()
{
     // doesn't print anything
 Foo.list();
}

alias Foo = foo!();

template foo(string s = null)
{
 @nogc
 struct foo
 {
     int n;

     enum x = foo!"str1"(10);
     enum y = foo!"str2"(20);
     enum z = foo!"str3"(30);

     enum myEnum { none }

     void someMethod() { }

     static void list()
     {
     //writeln("members = ", [__traits(allMembers, foo!())]);
     static foreach(field; __traits(allMembers, foo!()))
     {{
     alias m = __traits(getMember, foo!(), field);
     static if(!isType!m && isInstanceOf!(foo, typeof(m)))


foo in this context is the *instantiated* foo (i.e. the struct foo 
above). What you want is `isIsntanceOf!(.foo, typeof(m))`


-Steve


Re: How to delete dynamic array ?

2021-03-19 Thread Kagamin via Digitalmars-d-learn
On Thursday, 18 March 2021 at 17:57:30 UTC, Patrick Schluter 
wrote:
It's important to understand that [] is just a practical syntax 
for a fat pointer.


Thinking of [] just as a fancy pointer helps imho to clarify 
that the pointed to memory nature is independant of the pointer 
itself.


I think they are arrays alright. What's missing is expression of 
ownership, because D follows traditional language design 
approach, and traditionally ownership wasn't expressed in 
language and was done by convention.


Re: How do I check if this field is of this template struct?

2021-03-19 Thread frame via Digitalmars-d-learn

On Friday, 19 March 2021 at 16:41:11 UTC, Jack wrote:

thanks this works fine outside a method but not in a static 
method. what am I missing?


Reading the manual ;)

To use isInstanceOf to check the identity of a template while 
inside of said template, use TemplateOf.


Re: How do I check if this field is of this template struct?

2021-03-19 Thread Jack via Digitalmars-d-learn

On Friday, 19 March 2021 at 08:54:50 UTC, Paul Backus wrote:

On Friday, 19 March 2021 at 07:14:46 UTC, Jack wrote:
give below template struct, how can I list the members x, y 
and z? I've tried something with OriginalType and TemplateOf 
but no luck... it seems if I do foo!"str1" the "str1" became 
"part of type"? give .stringof from typeof(__traits(getMember, 
foo, field)) I thought the type would be foo!string or 
something.


You want std.traits.isInstanceOf:

static if(!isType!m && isInstanceOf!(foo, typeof(m)))


thanks this works fine outside a method but not in a static 
method. what am I missing?


void main()
{
// doesn't print anything
Foo.list();
}

alias Foo = foo!();

template foo(string s = null)
{
@nogc
struct foo
{
int n;

enum x = foo!"str1"(10);
enum y = foo!"str2"(20);
enum z = foo!"str3"(30);

enum myEnum { none }

void someMethod() { }

static void list()
{
//writeln("members = ", [__traits(allMembers, foo!())]);
static foreach(field; __traits(allMembers, foo!()))
{{
alias m = __traits(getMember, foo!(), field);
static if(!isType!m && isInstanceOf!(foo, 
typeof(m)))
{
writefln("field = [%s]", field);
}
}}
}
}
}



Re: How do I check if this field is of this template struct?

2021-03-19 Thread Jack via Digitalmars-d-learn

On Friday, 19 March 2021 at 07:56:26 UTC, Panke wrote:

On Friday, 19 March 2021 at 07:14:46 UTC, Jack wrote:
give below template struct, how can I list the members x, y 
and z? I've tried something with OriginalType and TemplateOf 
but no luck... it seems if I do foo!"str1" the "str1" became 
"part of type"? give .stringof from typeof(__traits(getMember, 
foo, field)) I thought the type would be foo!string or 
something.


Template parameter cannot only be types but also values, 
including strings. If you instantiate a template with different 
values you get different types.


that design is a bit different but may be worth, gonna get used 
to it



--
struct foo(T) { }
struct bar(string s) {}

alias a = foo!string; // type of a is foo!string
alias b = bar!"str1"; // type of b is bar!"str1"
alias c = bar!"str2"; // typo of c is bar!"str2"
static assert (!is(typeof(c) == typeof(b)));
--




Re: Using YMM registers causes an undefined label error

2021-03-19 Thread z via Digitalmars-d-learn

On Tuesday, 9 March 2021 at 20:33:01 UTC, z wrote:

On Tuesday, 9 March 2021 at 20:23:48 UTC, z wrote:

On Friday, 5 March 2021 at 12:57:43 UTC, z wrote:

...


Then it seems the only way to get AVX-compatible inline 
assembly(ldc.llvmasm excluded) is to use an external assembler.


For example :

...
But i'm not really sure how to integrate that into a dub 
project, it seems «lflags "filename.obj"» and 
preGenerateCommands/preBuildCommands would work but i haven't 
tested that.(«dflags "filename.obj"» doesn't work for sure)


In dub.sdl :

lflags "source/asmfunctions.obj"
preGenerateCommands " cd source && *command or bat/sh file that 
builds the asm object file(s)*"



It works, but if the package is being imported by another then it 
will fail because the way lflags work mean that the linker will 
try to find source/asmfunctions.obj from the working directory of 
the importer.

This is circumventable with relative paths(if possible).

lflags "../importedpackagesname/source/asmfunctions.obj"


Re: How do I check if this field is of this template struct?

2021-03-19 Thread Paul Backus via Digitalmars-d-learn

On Friday, 19 March 2021 at 07:14:46 UTC, Jack wrote:
give below template struct, how can I list the members x, y and 
z? I've tried something with OriginalType and TemplateOf but no 
luck... it seems if I do foo!"str1" the "str1" became "part of 
type"? give .stringof from typeof(__traits(getMember, foo, 
field)) I thought the type would be foo!string or something.


You want std.traits.isInstanceOf:

static if(!isType!m && isInstanceOf!(foo, typeof(m)))


Re: noobie question, dub or meson?

2021-03-19 Thread Chris Piker via Digitalmars-d-learn

On Thursday, 18 March 2021 at 06:02:03 UTC, Elronnd wrote:
Meson doesn't track dependencies properly for d, so your dirty 
builds will be wrong if you go that route.


You might consider keeping the c and d code in the same 
repository, but with separate build systems; using dub to build 
the d code and and whatever tool you prefer for c.  Or try 
reggae.


Hi Elronnd

You might be right.  Since Visual D is highly spoken of on these 
forums I decided to give it a go.  I don't usually write software 
on Windows, and I have to say that once I stepped outside the D 
ecosystem the experience was not good.  There's no standard 
locations for
anything.  Meson worked well on Linux, but I can't figure out how 
to get it to find vcpkg dependencies on Windows.  Outside Visual 
D, the one thing that worked great on windows was:


   dub

I know everyone knocks the idea of downloading your dependencies 
off the web, but when starting from scratch you have to get them 
from somewhere and dub comes to the rescue,

and "add-path" is there for local packages.

Has there ever been talk of adding C source code support to dub, 
or is that a forbidden topic?  I know if dub supported C, all my 
C libs and all my necessary dependencies (openssl, expat, etc.) 
would have dub.json files before the weekend was over.




Re: How do I check if this field is of this template struct?

2021-03-19 Thread Panke via Digitalmars-d-learn

On Friday, 19 March 2021 at 07:14:46 UTC, Jack wrote:
give below template struct, how can I list the members x, y and 
z? I've tried something with OriginalType and TemplateOf but no 
luck... it seems if I do foo!"str1" the "str1" became "part of 
type"? give .stringof from typeof(__traits(getMember, foo, 
field)) I thought the type would be foo!string or something.


Template parameter cannot only be types but also values, 
including strings. If you instantiate a template with different 
values you get different types.


--
struct foo(T) { }
struct bar(string s) {}

alias a = foo!string; // type of a is foo!string
alias b = bar!"str1"; // type of b is bar!"str1"
alias c = bar!"str2"; // typo of c is bar!"str2"
static assert (!is(typeof(c) == typeof(b)));
--


How do I check if this field is of this template struct?

2021-03-19 Thread Jack via Digitalmars-d-learn
give below template struct, how can I list the members x, y and 
z? I've tried something with OriginalType and TemplateOf but no 
luck... it seems if I do foo!"str1" the "str1" became "part of 
type"? give .stringof from typeof(__traits(getMember, foo, 
field)) I thought the type would be foo!string or something.


Here's the code:

template foo(string s = null)
{
@nogc
struct foo
{
int n;

enum x = foo!"str1"(10);
enum y = foo!"str2"(20);
enum z = foo!"str3"(30);

enum myEnum { none }

void someMethod() { }
}
}

not working listener:

void list()
{
static foreach(field; __traits(allMembers, foo!()))
{{
alias m = __traits(getMember, foo!(), field);
		static if(!isType!m && __traits(isSame, 
OriginalType!(typeof(m)), foo))

{
writefln("field = [%s]", field);
}
}}
}