Re: How do I check if a type is assignable to null at compile time?

2021-02-26 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, Feb 27, 2021 at 01:03:56AM +, Jack via Digitalmars-d-learn wrote:
> On Friday, 26 February 2021 at 23:37:18 UTC, Murilo wrote:
> > On Friday, 26 February 2021 at 05:25:14 UTC, Jack wrote:
> > > I started with:
> > > 
> > > enum isAssignableNull(T) = is(T : Object) || isPointer(T);
> > > 
> > > but how do I cover all cases?
> > 
> > You can check if it's null with this `variable is null` and you
> > can test it with assert as in `assert(variable is null);`
> 
> I mean a give type T not variablee

Why not just:

enum isAssignableNull(T) = is(typeof((T t){ t = null; }));

?


T

-- 
What are you when you run out of Monet? Baroque.


Re: How do I check if a type is assignable to null at compile time?

2021-02-26 Thread Jack via Digitalmars-d-learn

On Friday, 26 February 2021 at 23:37:18 UTC, Murilo wrote:

On Friday, 26 February 2021 at 05:25:14 UTC, Jack wrote:

I started with:

enum isAssignableNull(T) = is(T : Object) || isPointer(T);

but how do I cover all cases?


You can check if it's null with this `variable is null` and you
can test it with assert as in `assert(variable is null);`


I mean a give type T not variablee


Re: How do I check if a type is assignable to null at compile time?

2021-02-26 Thread Murilo via Digitalmars-d-learn

On Friday, 26 February 2021 at 05:25:14 UTC, Jack wrote:

I started with:

enum isAssignableNull(T) = is(T : Object) || isPointer(T);

but how do I cover all cases?


You can check if it's null with this `variable is null` and you
can test it with assert as in `assert(variable is null);`


Re: Can Metaprogramming Help Here?

2021-02-26 Thread Mike Brown via Digitalmars-d-learn

On Friday, 26 February 2021 at 20:42:50 UTC, H. S. Teoh wrote:
On Fri, Feb 26, 2021 at 11:37:18AM -0800, H. S. Teoh via 
Digitalmars-d-learn wrote:

> [...]

[...]

Alright, here's an actual working example. Instead of using 
classes, I decided to use templates instead, but the underlying 
concept is the same:


[...]


Hi T,

Thank you so much for that, I appriciate the time and hard work 
you've put in for this! I'm sure the code examples above will be 
workable for my needs.


Thanks again
Mike


Re: Can Metaprogramming Help Here?

2021-02-26 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Feb 26, 2021 at 11:37:18AM -0800, H. S. Teoh via Digitalmars-d-learn 
wrote:
> On Wed, Feb 24, 2021 at 08:10:30PM +, Mike Brown via Digitalmars-d-learn 
> wrote:
> [...]
> > Thank you for the reply. Im struggling extending this to get the
> > nesting working.
[...]

Alright, here's an actual working example. Instead of using classes, I
decided to use templates instead, but the underlying concept is the
same:

-snip--
template branch(string _ident, _values...) {
enum ident = _ident;
alias values = _values;
}

// I used strings for easier concatenation to code, otherwise we have to use
// std.conv to convert it which is slow in CTFE.
static immutable string[] primes = [
"2", "3", "5", "7", "11", "13", "17", "19", "23", "29", "31", "37",
"41", // fill in more if you need to
];

string genPrimeId(size_t[] indices)
in (indices.length > 0)
{
string result = primes[indices[0]];
foreach (i; indices[1 .. $]) {
result ~= "*" ~ primes[i];
}
return result;
}

template primeIdsImpl(size_t[] indices, Args...)
if (indices.length > 0 && Args.length > 0)
{
static if (Args.length == 1) {
static if (is(typeof(Args[0]) == string)) {
enum primeIdsImpl = Args[0] ~ "=" ~ genPrimeId(indices) 
~ ",\n";
} else {
enum primeIdsImpl = Args[0].ident ~ "=" ~ 
genPrimeId(indices) ~ ",\n" ~
primeIdsImpl!(indices ~ [ indices[$-1] + 1 ],
Args[0].values);
}
} else {
enum primeIdsImpl = primeIdsImpl!(indices, Args[0]) ~
primeIdsImpl!(indices[0 .. $-1] ~ [ indices[$-1] + 1 ],
Args[1 .. $]);
}
}

template primeIds(string enumName, Args...) if (Args.length > 0) {
enum primeIds = "enum " ~ enumName ~ " {\n" ~
primeIdsImpl!([0], Args) ~
"}";
}

mixin(primeIds!("token_type",
"endOfFile",
"unknown",
"newline",
branch!("identifier",
"userDefined",
"var",
"uses",
"constructor",
"do_",
"end_",
),
branch!("operator",
"copyAssignment",
),
));

void main() {
import std;
writefln("%s", token_type.identifier);
writefln("%d", token_type.identifier);
}
-snip--


You can change the mixin line to `pragma(msg, ...)` instead to see the
generated code string.

I noticed that the definitions of the first nested identifiers are
different from your original post; I don't know if this is a
misunderstanding on my side or an oversight on your part?  After
identifier=7, the next prime should be 11, not 13, so userDefined should
start with 11*identifier rather than 13*identifier.


T

-- 
Shin: (n.) A device for finding furniture in the dark.


Re: How can I get the variable name passed as parameter from within a function?

2021-02-26 Thread Jack via Digitalmars-d-learn

On Friday, 26 February 2021 at 19:37:34 UTC, Adam D. Ruppe wrote:

On Friday, 26 February 2021 at 19:32:52 UTC, Jack wrote:

I managed to do this with alias parameter in a template:


this is the only way, it needs to be an alias template

Also, can I short this template function somehow to syntax 
f!(a) omitting the g?


rename g to f. If the function inside the template's name 
matches the template's own name, the compiler combines them.


of course at that point you can also just write it

void f(alias var)() {
// do your magic
}


thanks! i was confused about where put alias in the function 
parameters but of course it's in the first () meant to be used as 
template parameters


Re: How can I get the variable name passed as parameter from within a function?

2021-02-26 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 26 February 2021 at 19:32:52 UTC, Jack wrote:

I managed to do this with alias parameter in a template:


this is the only way, it needs to be an alias template

Also, can I short this template function somehow to syntax 
f!(a) omitting the g?


rename g to f. If the function inside the template's name matches 
the template's own name, the compiler combines them.


of course at that point you can also just write it

void f(alias var)() {
// do your magic
}


Re: Can Metaprogramming Help Here?

2021-02-26 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Feb 24, 2021 at 08:10:30PM +, Mike Brown via Digitalmars-d-learn 
wrote:
[...]
> Thank you for the reply. Im struggling extending this to get the
> nesting working.
> 
> I'm trying something like:
> 
> string entry(string i, string[] inherit = []) {
>   return i;
> }
> 
> alias token_type2 = PrimeEnum!(
>   entry("unknown"),
>   entry("newline"),
>   entry("identifier"),
>   entry("var", ["identifier"]),
>   entry("userDefined", ["identifier"])
> );
> 
> Its worth noting that multiple inherited bases are needed too.
> 
> But I can't get those functions contexts linking, can I pass a
> function pointer as lazy into the PrimeEnum!() template?
> 
> Would it be easier to just parse the text at once into a single
> templating function?
[...]

Ah, so sorry, I completely overlooked the nesting part.  PrimeEnum as I
defined it in my first reply does not handle this at all, so it will
need to be extended.

Since we're dealing with a tree structure here, I think the best way is
to express the tree structure explicitly in a compile-time data
structure. Thanks to CTFE, this works pretty much exactly the same as
normal runtime data structures; the only difference is that they will be
processed at compile-time.

Here's a rough sketch of how I'd do it:

class Entry {
string ident;
Entry[] subentries;
this(string _id, Entry[] _subs = []) {
ident = _id;
subentries = _subs;
}
}

Entry[] makeIdentTrees() {
return [
new Entry("endOfFile"),
new Entry("unknown"),
new Entry("newline"),
new Entry("identifier", [
new Entry("userDefined"),
new Entry("var"),
new Entry("uses"),
... // you get the idea
],
new Entry("operator", [
new Entry("copyAssignment"),
... // etc.
]));
]
}

You'd then write a recursive function that traverses this tree, using a
compile-time array of prime numbers, and compute the enum values that
way.  Format that into D code as a string, and use mixin to actually
create the enum.  The function can be written just like any runtime D
code, as long as it does not use any CTFE-incompatible language
features.

string genEnum(Entry[] entries) {
string code;
... // traverse tree and generate D code here
return code;
}

// Create the enum
mixin(genEnum(makeIdentTrees()));


T

-- 
A mathematician learns more and more about less and less, until he knows 
everything about nothing; whereas a philospher learns less and less about more 
and more, until he knows nothing about everything.


Re: ldc on a raspberry pi 3 running freebsd

2021-02-26 Thread Jacob Carlborg via Digitalmars-d-learn

On 2021-02-23 16:34, Decabytes wrote:

ldc2 is the winner thank you! I'd like to get gdc and dmd up and running 
to at some point


Unfortunately, DMD doesn't support ARM.

--
/Jacob Carlborg


Re: DUB is not working correctly

2021-02-26 Thread Maxim via Digitalmars-d-learn

On Friday, 26 February 2021 at 19:00:57 UTC, H. S. Teoh wrote:
On Fri, Feb 26, 2021 at 06:53:32PM +, evilrat via 
Digitalmars-d-learn wrote:

On Friday, 26 February 2021 at 18:20:38 UTC, Maxim wrote:
> On Friday, 26 February 2021 at 17:57:12 UTC, ryuukk_ wrote:
> > "targetType": "executable",
> > 
> > and it should just run using "dub run"
> 
> Unfortunately, the problem remains :/


Looks like something specific to your machine.

The last thing I could think of is to run dub with -v flag 
(verbose mode), look for all steps performed and check the 
output for anything that potentially could cause a failure.

[...]

And possibly paste the output of -v here, somebody may be able 
to diagnose what went wrong then.



--T


This is an output:

D:\DEVELOPMENT\[ D ]\sandbox>dub run -v
Using dub registry url 'https://code.dlang.org/'
Refreshing local packages (refresh existing: true)...
Looking for local package map at 
C:\ProgramData\dub\packages\local-packages.json
Looking for local package map at 
C:\Users\MAX_PC\AppData\Local\dub\packages\local-packages.json
Try to load local package map at 
C:\Users\MAX_PC\AppData\Local\dub\packages\local-packages.json
Looking for local package map at D:\DEVELOPMENT\[ D 
]\sandbox\.dub\packages\local-packages.json
Note: Failed to determine version of package sandbox at .. 
Assuming ~master.

Refreshing local packages (refresh existing: false)...
Looking for local package map at 
C:\ProgramData\dub\packages\local-packages.json
Looking for local package map at 
C:\Users\MAX_PC\AppData\Local\dub\packages\local-packages.json
Try to load local package map at 
C:\Users\MAX_PC\AppData\Local\dub\packages\local-packages.json
Looking for local package map at D:\DEVELOPMENT\[ D 
]\sandbox\.dub\packages\local-packages.json

Refreshing local packages (refresh existing: false)...
Looking for local package map at 
C:\ProgramData\dub\packages\local-packages.json
Looking for local package map at 
C:\Users\MAX_PC\AppData\Local\dub\packages\local-packages.json
Try to load local package map at 
C:\Users\MAX_PC\AppData\Local\dub\packages\local-packages.json
Looking for local package map at D:\DEVELOPMENT\[ D 
]\sandbox\.dub\packages\local-packages.json

Generating using build
Configuration 'application' of package sandbox contains no source 
files. Please add {"targetType": "none"} to its package 
description to avoid building it.

Package with target type "none" must have dependencies to build.


How can I get the variable name passed as parameter from within a function?

2021-02-26 Thread Jack via Digitalmars-d-learn

int a = 10;
f(a); // print "a"
int b = 10;
f(b); // print "b"

I managed to do this with alias parameter in a template:

template f(alias s, string file = __FILE__, size_t line = 
__LINE__)

{
import std.exception : enforce;
import std.string : format;

void g()
{
  writeln(__traits(identifier, s));
}
}

so I can call like this:

f!(a).g; // print "a"

Are there other way to do this? Also, can I short this template 
function somehow to syntax f!(a) omitting the g? I couldn't find 
a way to set the template's body like it's a function


Re: DUB is not working correctly

2021-02-26 Thread Maxim via Digitalmars-d-learn

On Friday, 26 February 2021 at 18:53:32 UTC, evilrat wrote:

On Friday, 26 February 2021 at 18:20:38 UTC, Maxim wrote:

On Friday, 26 February 2021 at 17:57:12 UTC, ryuukk_ wrote:

"targetType": "executable",

and it should just run using "dub run"


Unfortunately, the problem remains :/


Looks like something specific to your machine.

The last thing I could think of is to run dub with -v flag 
(verbose mode), look for all steps performed and check the 
output for anything that potentially could cause a failure.
Could be related to paths with spaces, path with non-ascii 
symbols, antivirus software, FS permissions, missing C++ SDK's 
and runtime libs, compiler toolchain installation, basically 
anything...


It works! Thank you so much! The problem was in spaces in the 
path.


Re: DUB is not working correctly

2021-02-26 Thread Maxim via Digitalmars-d-learn

On Friday, 26 February 2021 at 19:00:57 UTC, H. S. Teoh wrote:
On Fri, Feb 26, 2021 at 06:53:32PM +, evilrat via 
Digitalmars-d-learn wrote:

On Friday, 26 February 2021 at 18:20:38 UTC, Maxim wrote:
> On Friday, 26 February 2021 at 17:57:12 UTC, ryuukk_ wrote:
> > "targetType": "executable",
> > 
> > and it should just run using "dub run"
> 
> Unfortunately, the problem remains :/


Looks like something specific to your machine.

The last thing I could think of is to run dub with -v flag 
(verbose mode), look for all steps performed and check the 
output for anything that potentially could cause a failure.

[...]

And possibly paste the output of -v here, somebody may be able 
to diagnose what went wrong then.



--T


Sure, I will paste it as soon as I'll have free time.


Re: DUB is not working correctly

2021-02-26 Thread Maxim via Digitalmars-d-learn

On Friday, 26 February 2021 at 18:53:32 UTC, evilrat wrote:

On Friday, 26 February 2021 at 18:20:38 UTC, Maxim wrote:

On Friday, 26 February 2021 at 17:57:12 UTC, ryuukk_ wrote:

"targetType": "executable",

and it should just run using "dub run"


Unfortunately, the problem remains :/


Looks like something specific to your machine.

The last thing I could think of is to run dub with -v flag 
(verbose mode), look for all steps performed and check the 
output for anything that potentially could cause a failure.
Could be related to paths with spaces, path with non-ascii 
symbols, antivirus software, FS permissions, missing C++ SDK's 
and runtime libs, compiler toolchain installation, basically 
anything...


This really might be helpful... I'll try it, thanks!


Re: How do I check if a type is assignable to null at compile time?

2021-02-26 Thread Jack via Digitalmars-d-learn

On Friday, 26 February 2021 at 05:45:39 UTC, Nathan S. wrote:

On Friday, 26 February 2021 at 05:34:26 UTC, Paul Backus wrote:

On Friday, 26 February 2021 at 05:25:14 UTC, Jack wrote:

I started with:

enum isAssignableNull(T) = is(T : Object) || isPointer(T);

but how do I cover all cases?


Something like this should work:

enum isAssignableNull(T) = __traits(compiles, (T t) => t = 
null);


`isAssignableNull!(immutable void*)` is true with his 
definition but false with yours. Of course you are correct that 
you cannot assign to an immutable pointer.


yep, it must be true for pointers too. Thank you all guys.  
is(typeof(null) : T) works like a charm


Re: DUB is not working correctly

2021-02-26 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Feb 26, 2021 at 06:53:32PM +, evilrat via Digitalmars-d-learn wrote:
> On Friday, 26 February 2021 at 18:20:38 UTC, Maxim wrote:
> > On Friday, 26 February 2021 at 17:57:12 UTC, ryuukk_ wrote:
> > > "targetType": "executable",
> > > 
> > > and it should just run using "dub run"
> > 
> > Unfortunately, the problem remains :/
> 
> Looks like something specific to your machine.
> 
> The last thing I could think of is to run dub with -v flag (verbose
> mode), look for all steps performed and check the output for anything
> that potentially could cause a failure.
[...]

And possibly paste the output of -v here, somebody may be able to
diagnose what went wrong then.


--T


Re: DUB is not working correctly

2021-02-26 Thread evilrat via Digitalmars-d-learn

On Friday, 26 February 2021 at 18:20:38 UTC, Maxim wrote:

On Friday, 26 February 2021 at 17:57:12 UTC, ryuukk_ wrote:

"targetType": "executable",

and it should just run using "dub run"


Unfortunately, the problem remains :/


Looks like something specific to your machine.

The last thing I could think of is to run dub with -v flag 
(verbose mode), look for all steps performed and check the output 
for anything that potentially could cause a failure.
Could be related to paths with spaces, path with non-ascii 
symbols, antivirus software, FS permissions, missing C++ SDK's 
and runtime libs, compiler toolchain installation, basically 
anything...


Re: DUB is not working correctly

2021-02-26 Thread Maxim via Digitalmars-d-learn

On Friday, 26 February 2021 at 17:57:12 UTC, ryuukk_ wrote:

"targetType": "executable",

and it should just run using "dub run"


Unfortunately, the problem remains :/


Re: DUB is not working correctly

2021-02-26 Thread ryuukk_ via Digitalmars-d-learn

"targetType": "executable",

and it should just run using "dub run"


Re: Optimizing for SIMD: best practices?(i.e. what features are allowed?)

2021-02-26 Thread Guillaume Piolat via Digitalmars-d-learn
On Thursday, 25 February 2021 at 14:28:40 UTC, Guillaume Piolat 
wrote:

On Thursday, 25 February 2021 at 11:28:14 UTC, z wrote:
How does one optimize code to make full use of the CPU's SIMD 
capabilities?
Is there any way to guarantee that "packed" versions of SIMD 
instructions will be used?(e.g. vmulps, vsqrtps, etc...)


https://code.dlang.org/packages/intel-intrinsics


A bit of elaboration on why you might want to prefer 
intel-intrinsics:

- it supports all D compilers, including DMD 32-bit target
- targets arm32 and arm64 with same code (LDC only)
- core.simd just give you the basic operators, but not say, 
pmaddwd or any of the complex instructions. Some instructions 
need very specific work to get them.
- at least with LLVM, optimizers works reliably over subsequent 
versions of the compiler.




Re: DUB is not working correctly

2021-02-26 Thread Maxim via Digitalmars-d-learn

On Friday, 26 February 2021 at 09:15:02 UTC, Siemargl wrote:

On Thursday, 25 February 2021 at 17:38:11 UTC, Maxim wrote:

On Thursday, 25 February 2021 at 17:34:29 UTC, Maxim wrote:

On Wednesday, 24 February 2021 at 16:13:48 UTC, Maxim wrote:

[...]


I think, I need to rephrase the question for the present 
situtation: how can I force DUB to change targetName? It 
doesn't read my changes in dub.json, and I don't know why. In 
my opinion, that happens because of DUB can't accept my 
changes to the file. But how to make the manager admit it? I 
apologize to everyone who understood me wrong.


And my problem has been substituted by another! Now DUB says 
to me to set targetName with this problem:

'No target name set.'


Just study dub.pm site (it's not obvious to find it)


It's a good idea! Thanks!


Re: DUB is not working correctly

2021-02-26 Thread Siemargl via Digitalmars-d-learn

On Thursday, 25 February 2021 at 17:38:11 UTC, Maxim wrote:

On Thursday, 25 February 2021 at 17:34:29 UTC, Maxim wrote:

On Wednesday, 24 February 2021 at 16:13:48 UTC, Maxim wrote:

[...]


I think, I need to rephrase the question for the present 
situtation: how can I force DUB to change targetName? It 
doesn't read my changes in dub.json, and I don't know why. In 
my opinion, that happens because of DUB can't accept my 
changes to the file. But how to make the manager admit it? I 
apologize to everyone who understood me wrong.


And my problem has been substituted by another! Now DUB says to 
me to set targetName with this problem:

'No target name set.'


Just study dub.pm site (it's not obvious to find it)