Re: Problem with map, reduce, ..

2015-06-24 Thread Adrian Matoga via Digitalmars-d-learn

On Wednesday, 24 June 2015 at 08:30:29 UTC, Adrian Matoga wrote:
input.byLine() yields char[]'s as range elements, while props 
is (correctly) indexed by strings, i.e. immutable(char)[].


Ooops, more precisely it's because of the second argument of 
add() being string, but the solution above still applies.




Re: Problem with map, reduce, ..

2015-06-24 Thread Adrian Matoga via Digitalmars-d-learn

On Wednesday, 24 June 2015 at 08:18:52 UTC, Stefan wrote:
I tried to refactor some existing code to use more of the 
functional patterns/style (map, filter, reduce, ..).
The task is to read in some sort of a simple property file and 
present the result as an associative array.

My attempt is:

import std.stdio;
import std.algorithm.iteration : filter, map, reduce;
import std.algorithm.mutation : split;
import std.string : indexOf, strip;
import std.stdio : File, writeln;
import std.algorithm : startsWith;

string[string] add( ref string[string] result, in string line) {
int assignmentIndex = indexOf( line, = );
if ( assignmentIndex  0 ) {
string key = line[ 0 .. assignmentIndex ];
string value = line[ assignmentIndex + 1 .. line.length 
];

result[ key ] = value;
   }
   return result;
}


void main() {
   auto input = File( test.ini, r );
   auto lines = input.byLine()
  .filter!( line = !startsWith( line, # )  
!startsWith( line, !)  line.length  0 )

  .map!( line = strip( line ) );
   string[string] props;
   auto result = reduce!(add)( props, lines );
}

result should now contain the associative array.

However, it fails with an error in the reduce line:

c:\develop\dmd2.067.1\windows\bin\..\..\src\phobos\std\algorithm\iteration.d(2536): 
Error: static assert  Incompatible function/seed/element: 
prop.add/string[string]/char[]
c:\develop\dmd2.067.1\windows\bin\..\..\src\phobos\std\algorithm\iteration.d(2521):
instantiated from here: reduceImpl!(false, MapResult!(__lambda2, 
FilterResult!(__lambda1, ByLine!(char, char))), string[string])
c:\develop\dmd2.067.1\windows\bin\..\..\src\phobos\std\algorithm\iteration.d(2505):
instantiated from here: reducePreImpl!(MapResult!(__lambda2, 
FilterResult!(__lambda1, ByLine!(char, char))), string[string])
source\prop.d(30):instantiated from here: 
reduce!(string[string], MapResult!(__lambda2, 
FilterResult!(__lambda1, ByLine!(char, char


I have no idea how to resolve this.


input.byLine() yields char[]'s as range elements, while props is 
(correctly) indexed by strings, i.e. immutable(char)[]. Use .idup 
to create an immutable copy of the property name, e.g.:


   auto lines = input.byLine()
  .filter!( line = !startsWith( line, # )  !startsWith( 
line, !)  line.length  0 )

  .map!( line = strip( line.idup ) );



Re: Program exited with code -11

2015-06-24 Thread Charles Hawkins via Digitalmars-d-learn

On Wednesday, 24 June 2015 at 06:54:57 UTC, weaselcat wrote:

On Tuesday, 23 June 2015 at 06:50:28 UTC, Charles Hawkins wrote:

On Tuesday, 23 June 2015 at 03:31:37 UTC, weaselcat wrote:
On Tuesday, 23 June 2015 at 03:29:14 UTC, Charles Hawkins 
wrote:

[...]


Try to compile with either ldc or gdc and the -g flag, it 
should give you a backtrace. dmd seems to not like linux wrt 
backtraces.


Thanks.  I wish!  I haven't had any success in compiling with 
anything but dub.  gdc, dmd, rdmd always give me module mylib 
is in file 'mylib.d' which cannot be read on my import 
mylib; statement.  I've tried every permutation of -I and -L 
that I can think of.  It almost appears that one either uses 
dub for everything or nothing and I'm getting pretty 
frustrated with it as well.  Perhaps I should just go back to 
old-fashioned make files?


you can instruct dub to use other compilers with the --compiler 
option

valid options include dmd,ldc,gdc,gdmd,ldmd


Ah, a sort of hidden option.  I've only been typing dub and 
thus, dub --help.  Didn't think to do dub build --help.


Is there a quick way to get gdc to recognize 
std.experimental.logger?  I'm already spoiled by it.  Choosing 
between it and a backtrace is difficult.


Re: cannot use UDA with same name as one of field's name

2015-06-24 Thread sigod via Digitalmars-d-learn

On Tuesday, 23 June 2015 at 23:52:52 UTC, Adam D. Ruppe wrote:
On Tuesday, 23 June 2015 at 23:14:13 UTC, Steven Schveighoffer 
wrote:

I'm not completely sure on the syntax, try adding some parens.


Yeah, I'm pretty sure it needs to be

@(full.name.here) void foo()


Yep, something like this works.

```
@(vibe.data.serialization.name(_id)) int id;
```

But in this case `alias` looks better.


Re: Program exited with code -11

2015-06-24 Thread weaselcat via Digitalmars-d-learn

On Tuesday, 23 June 2015 at 06:50:28 UTC, Charles Hawkins wrote:

On Tuesday, 23 June 2015 at 03:31:37 UTC, weaselcat wrote:
On Tuesday, 23 June 2015 at 03:29:14 UTC, Charles Hawkins 
wrote:

[...]


Try to compile with either ldc or gdc and the -g flag, it 
should give you a backtrace. dmd seems to not like linux wrt 
backtraces.


Thanks.  I wish!  I haven't had any success in compiling with 
anything but dub.  gdc, dmd, rdmd always give me module mylib 
is in file 'mylib.d' which cannot be read on my import 
mylib; statement.  I've tried every permutation of -I and -L 
that I can think of.  It almost appears that one either uses 
dub for everything or nothing and I'm getting pretty frustrated 
with it as well.  Perhaps I should just go back to 
old-fashioned make files?


you can instruct dub to use other compilers with the --compiler 
option

valid options include dmd,ldc,gdc,gdmd,ldmd


Problem with map, reduce, ..

2015-06-24 Thread Stefan via Digitalmars-d-learn
I tried to refactor some existing code to use more of the 
functional patterns/style (map, filter, reduce, ..).
The task is to read in some sort of a simple property file and 
present the result as an associative array.

My attempt is:

import std.stdio;
import std.algorithm.iteration : filter, map, reduce;
import std.algorithm.mutation : split;
import std.string : indexOf, strip;
import std.stdio : File, writeln;
import std.algorithm : startsWith;

string[string] add( ref string[string] result, in string line) {
int assignmentIndex = indexOf( line, = );
if ( assignmentIndex  0 ) {
string key = line[ 0 .. assignmentIndex ];
string value = line[ assignmentIndex + 1 .. line.length ];
result[ key ] = value;
   }
   return result;
}


void main() {
   auto input = File( test.ini, r );
   auto lines = input.byLine()
  .filter!( line = !startsWith( line, # )  !startsWith( 
line, !)  line.length  0 )

  .map!( line = strip( line ) );
   string[string] props;
   auto result = reduce!(add)( props, lines );
}

result should now contain the associative array.

However, it fails with an error in the reduce line:

c:\develop\dmd2.067.1\windows\bin\..\..\src\phobos\std\algorithm\iteration.d(2536): 
Error: static assert  Incompatible function/seed/element: 
prop.add/string[string]/char[]
c:\develop\dmd2.067.1\windows\bin\..\..\src\phobos\std\algorithm\iteration.d(2521):
instantiated from here: reduceImpl!(false, MapResult!(__lambda2, 
FilterResult!(__lambda1, ByLine!(char, char))), string[string])
c:\develop\dmd2.067.1\windows\bin\..\..\src\phobos\std\algorithm\iteration.d(2505):
instantiated from here: reducePreImpl!(MapResult!(__lambda2, 
FilterResult!(__lambda1, ByLine!(char, char))), string[string])
source\prop.d(30):instantiated from here: 
reduce!(string[string], MapResult!(__lambda2, 
FilterResult!(__lambda1, ByLine!(char, char


I have no idea how to resolve this.


Re: Problem with map, reduce, ..

2015-06-24 Thread Stefan via Digitalmars-d-learn

On Wednesday, 24 June 2015 at 08:33:29 UTC, Adrian Matoga wrote:

On Wednesday, 24 June 2015 at 08:30:29 UTC, Adrian Matoga wrote:
input.byLine() yields char[]'s as range elements, while props 
is (correctly) indexed by strings, i.e. immutable(char)[].


Ooops, more precisely it's because of the second argument of 
add() being string, but the solution above still applies.


Thanks! That does it!

Any idea how to make the 'ugly' reduce step more 'pleasant'? I.e. 
make it a part of the filter, map, .. chain?


Re: Problem with map, reduce, ..

2015-06-24 Thread Stefan via Digitalmars-d-learn

On Wednesday, 24 June 2015 at 09:35:35 UTC, Adrian Matoga wrote:

On Wednesday, 24 June 2015 at 08:58:10 UTC, Stefan wrote:
On Wednesday, 24 June 2015 at 08:33:29 UTC, Adrian Matoga 
wrote:

[...]


Thanks! That does it!

Any idea how to make the 'ugly' reduce step more 'pleasant'? 
I.e. make it a part of the filter, map, .. chain?


What about:

   auto result = File(test.ini, r)
  .byLine()
  .filter!( line = !startsWith( line, # )  
!startsWith( line, !)  line.length  0 )

  .map!( line = line.idup.split('='))
  .filter!( fields = fields.length  1)
  .map!( fields = tuple(fields[0].strip(), 
fields[1].strip() ))

  .assocArray();


Wow! Very cool!

Thanks


Re: kill and thisProcessID

2015-06-24 Thread via Digitalmars-d-learn

On Wednesday, 24 June 2015 at 11:39:51 UTC, Nordlöw wrote:

What to do?


See also: Discussion at 
http://dlang.org/library/std/process/kill.html


Re: kill and thisProcessID

2015-06-24 Thread Steven Schveighoffer via Digitalmars-d-learn

On 6/24/15 7:39 AM, Nordlöw wrote:

I have a process that shall suspend itself using SIGTSTP or SIGSTOP.

My current plan is

 import std.process: thisProcessID, kill, Pid;
 import core.sys.posix.signal: SIGKILL, SIGSTOP, SIGTSTP;
 const thisPid = thisProcessID;
 // some call to kill()

but kill() needs a `Pid`

so

 kill(thisPid, SIGTSTP);

fails and constructing Pid cannot be done because

 kill(new Pid(thisPid), SIGTSTP);

errors as

 Error: class std.process.Pid member this is not accessible

What to do?


Why not use core.sys.posix.signal: kill instead? You're already 
importing the module anyway.


-Steve



Re: Problem with map, reduce, ..

2015-06-24 Thread Adrian Matoga via Digitalmars-d-learn

On Wednesday, 24 June 2015 at 08:58:10 UTC, Stefan wrote:

On Wednesday, 24 June 2015 at 08:33:29 UTC, Adrian Matoga wrote:
On Wednesday, 24 June 2015 at 08:30:29 UTC, Adrian Matoga 
wrote:
input.byLine() yields char[]'s as range elements, while props 
is (correctly) indexed by strings, i.e. immutable(char)[].


Ooops, more precisely it's because of the second argument of 
add() being string, but the solution above still applies.


Thanks! That does it!

Any idea how to make the 'ugly' reduce step more 'pleasant'? 
I.e. make it a part of the filter, map, .. chain?


What about:

   auto result = File(test.ini, r)
  .byLine()
  .filter!( line = !startsWith( line, # )  !startsWith( 
line, !)  line.length  0 )

  .map!( line = line.idup.split('='))
  .filter!( fields = fields.length  1)
  .map!( fields = tuple(fields[0].strip(), fields[1].strip() 
))

  .assocArray();




Re: kill and thisProcessID

2015-06-24 Thread nkpm via Digitalmars-d-learn

On Wednesday, 24 June 2015 at 11:39:51 UTC, Nordlöw wrote:
I have a process that shall suspend itself using SIGTSTP or 
SIGSTOP.


My current plan is

import std.process: thisProcessID, kill, Pid;
import core.sys.posix.signal: SIGKILL, SIGSTOP, SIGTSTP;
const thisPid = thisProcessID;
// some call to kill()

but kill() needs a `Pid`

so

kill(thisPid, SIGTSTP);

fails and constructing Pid cannot be done because

kill(new Pid(thisPid), SIGTSTP);

errors as

Error: class std.process.Pid member this is not accessible

What to do?


in std.process, add a `kill()` overload that takes `thisPid` as 
argument, it will create the `Pid` as a local scoped instance and 
it will call the normal `kill()` version that takes a Pid as 
argument with the scoped `Pid` instance.
propose a PR for this overloaded `kill()` and wait dmd 2.068 
release.


Pid is well locked BTW: private constructor, final class...


kill and thisProcessID

2015-06-24 Thread Nordlöw
I have a process that shall suspend itself using SIGTSTP or 
SIGSTOP.


My current plan is

import std.process: thisProcessID, kill, Pid;
import core.sys.posix.signal: SIGKILL, SIGSTOP, SIGTSTP;
const thisPid = thisProcessID;
// some call to kill()

but kill() needs a `Pid`

so

kill(thisPid, SIGTSTP);

fails and constructing Pid cannot be done because

kill(new Pid(thisPid), SIGTSTP);

errors as

Error: class std.process.Pid member this is not accessible

What to do?


Re: kill and thisProcessID

2015-06-24 Thread Nordlöw

On Wednesday, 24 June 2015 at 11:39:51 UTC, Nordlöw wrote:

What to do?


I believe the best solution is to add a new function

Pid thisProcessPid()

to std.process and refer to this from kill(Pid). Should I do PR?


Re: Compiles but does not link

2015-06-24 Thread Paul D Anderson via Digitalmars-d-learn

On Thursday, 25 June 2015 at 00:24:23 UTC, Paul D Anderson wrote:
The code snippet below compiles but the linker fails with Error 
42: Symbol undefined.


What am I doing wrong?

void main()
{
int foo(int a);
alias FP = int delegate(int);
FP fp = foo;
}

Paul


Uh, never mind. I got so focused on the delegate I forgot to give 
the function a body.


Re: core.exception.InvalidMemoryOperationError@(0) on File Reading.

2015-06-24 Thread David DeWitt via Digitalmars-d-learn

On Tuesday, 23 June 2015 at 18:49:59 UTC, David DeWitt wrote:

On Monday, 22 June 2015 at 20:30:40 UTC, David DeWitt wrote:

I am getting an core.exception.InvalidMemoryOperationError@(0)

 auto recs = f  // Open for reading
   .byLineCopy();
   .array;  //Here is where is appears 
to be happening.


[...]


I have had a chance to try the new phobos.  Dealing with the 
errors related to:
http://forum.dlang.org/thread/mm1fdo$q5u$1...@digitalmars.com in 
debian.


Gonna mess with it some more when I have a chance.


I pulled down the latest version on Github and everything ran 
fine.


Compiles but does not link

2015-06-24 Thread Paul D Anderson via Digitalmars-d-learn
The code snippet below compiles but the linker fails with Error 
42: Symbol undefined.


What am I doing wrong?

void main()
{
int foo(int a);
alias FP = int delegate(int);
FP fp = foo;
}

Paul



Casting from an enum type to another enum type

2015-06-24 Thread Roland Hadinger via Digitalmars-d-learn

Hi!

What is the straightest way to safely cast from one enum type A 
to another enum type B, when B, in terms of values as well as 
identifiers, is a strict subset of A?


Ideally, that should be as simple as to!B(a). So far I've tried 
a couple of things, and one way I found was to first cast A to 
int, then cast that to B, but that is not really straightforward.


Example:

import std.stdio;
import std.conv;

enum Color { r, o, y, g, b, i, v }

enum StyleColor : int { o = Color.o, b = Color.b }

void main()
{
auto c = Color.g;

// Problem: the result is not guaranteed to be a StyleColor.
// Does not throw, needs extra checks.
auto d1 = cast(StyleColor) c;

// Typesafe, but is not exactly straightforward.
auto d2 = to!StyleColor(cast(int) c);

// Error: template std.conv.toImpl cannot deduce
// function from argument types !(StyleColor)(Color)
//
// Changing enum StyleColor : Color to enum StyleColor : 
int

// in the definition above does not help either.
auto d3 = to!StyleColor(c);
}



Template mixin can not introduce overloads

2015-06-24 Thread Tofu Ninja via Digitalmars-d-learn

Is this intended or is it a bug?

void main(string[] args)
{
Test a;
a.foo(5); // Fails to compile
}

struct Test
{
mixin testMix;
void foo(string y){}
}

mixin template testMix()
{
void foo(int x){}
}


Suggested enhancements/changes to D

2015-06-24 Thread DLearner via Digitalmars-d-learn

Is there a place where these should be posted for discussion?


Re: Suggested enhancements/changes to D

2015-06-24 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 24 June 2015 at 16:44:06 UTC, DLearner wrote:

Is there a place where these should be posted for discussion?


The general forum:
http://forum.dlang.org/group/general


Re: Program exited with code -11

2015-06-24 Thread weaselcat via Digitalmars-d-learn

On Wednesday, 24 June 2015 at 07:52:10 UTC, Charles Hawkins wrote:

On Wednesday, 24 June 2015 at 06:54:57 UTC, weaselcat wrote:
On Tuesday, 23 June 2015 at 06:50:28 UTC, Charles Hawkins 
wrote:

[...]


you can instruct dub to use other compilers with the 
--compiler option

valid options include dmd,ldc,gdc,gdmd,ldmd


Ah, a sort of hidden option.  I've only been typing dub and 
thus, dub --help.  Didn't think to do dub build --help.


Is there a quick way to get gdc to recognize 
std.experimental.logger?  I'm already spoiled by it.  Choosing 
between it and a backtrace is difficult.


I believe it's available as a dub package albeit outdated, should 
be roughly similar though.


Filtering Associative Array Key-Values the D way

2015-06-24 Thread David DeWitt via Digitalmars-d-learn
I'm trying to understand filtering an Associative Array the D 
way.  I have the code below
(Using while readln cause problem failing on Debian using 
byLineCopy()).  When the byKeyValue().filter evaluates to reduce 
the number of Keys:Values to only the ones in the filtered 
header, what is the best way to transform the Pair back to an AA 
instead of having to run the foreach loop after?  Basically if 
the file has 300 columns and 300 in the full header but only 15 
in the filtered header I want the return of rec to only be the 15 
in an Associative Array.  Thanks :)



string line;
string[string][] records;
while ((line = f.readln()) !is null){
string[string] record;
auto rec = assocArray(zip(h1.fullHeader, 
splitter(line,',')))

.byKeyValue().filter!(a=h1.filteredHeader.canFind(a.key));


foreach(r;rec){
record[r.key] = r.value;
}
records ~= record;
}


Re: Template mixin can not introduce overloads

2015-06-24 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 25 June 2015 at 03:49:04 UTC, Tofu Ninja wrote:

Is this intended or is it a bug?


Intended, the mixin template works on the basis of names. This 
means that you can override methods from the mixin by writing a 
member with the same name - very useful thing - but also means no 
overloading happens without an extra step.


The extra step is easy though: alias the name in:

 void main(string[] args)
 {
Test a;
a.foo(5); // works!
a.foo(4qsda);
 }

 struct Test
 {
mixin testMix tm; // give it a name to reference later
void foo(string y){}
alias tm.foo foo; // alias the mixin thing too
 }

 mixin template testMix()
 {
void foo(int x){}
 }



By adding that explicit alias, it tells the compiler that yes, 
you do want it added to the set, not completely overridden by 
your replacement method.


function default parameters lost

2015-06-24 Thread Paul D Anderson via Digitalmars-d-learn
I'm trying to pass a function pointer while keeping the default 
parameter values intact. Given the following:


import std.traits;
import std.stdio;

int foo(int a, int b = 1)
{
  return a;
}

alias FOOP = int function(int, int = 1);

struct ST(POOF)
{
  FOOP fctn;

  this(POOF fctn)
  {
this.fctn = fctn;
  }

  void details()
  {
alias PDVA = ParameterDefaultValueTuple!fctn;
writefln(typeid(PDVA[0]) = %s, typeid(PDVA[0]));
writefln(typeid(PDVA[1]) = %s, typeid(PDVA[1]));
  }
}

void main()
{
  FOOP fp = foo;
  auto st = ST!FOOP(fp);
  st.details;
}

The default parameter value types are void, int: a has no default 
and b has an int value as its default.


If I change line 14 from
  FOOP fctn;
to
  POOF fctn;

The default parameter value types are void, void. In other words 
the default value for b is no longer there.


Why doesn't invoking the template (ST!FOOP) replace POOF in line 
14 with FOOP?


Paul



Re: Casting from an enum type to another enum type

2015-06-24 Thread Meta via Digitalmars-d-learn

On Wednesday, 24 June 2015 at 15:29:03 UTC, Roland Hadinger wrote:

Hi!

What is the straightest way to safely cast from one enum type A 
to another enum type B, when B, in terms of values as well as 
identifiers, is a strict subset of A?


Ideally, that should be as simple as to!B(a). So far I've 
tried a couple of things, and one way I found was to first cast 
A to int, then cast that to B, but that is not really 
straightforward.


Example:

import std.stdio;
import std.conv;

enum Color { r, o, y, g, b, i, v }

enum StyleColor : int { o = Color.o, b = Color.b }

void main()
{
auto c = Color.g;

// Problem: the result is not guaranteed to be a StyleColor.
// Does not throw, needs extra checks.
auto d1 = cast(StyleColor) c;

// Typesafe, but is not exactly straightforward.
auto d2 = to!StyleColor(cast(int) c);

// Error: template std.conv.toImpl cannot deduce
// function from argument types !(StyleColor)(Color)
//
// Changing enum StyleColor : Color to enum StyleColor : 
int

// in the definition above does not help either.
auto d3 = to!StyleColor(c);
}


std.conv.to really should be able to do this, but I guess not 
many people have needed to do this. You can write an extension 
to `to` which does it for you:


import std.traits;

auto to(To, From)(From f)
if (is(From == enum)  is(To == enum)  is(OriginalType!From : 
OriginalType!To))

{
return cast(To)f;
}

enum Color { r, o, y, g, b, i, v }

enum StyleColor : int { o = Color.o, b = Color.b }

void main()
{
auto c = Color.g;
auto s = c.to!StyleColor;
}


Re: Casting from an enum type to another enum type

2015-06-24 Thread Meta via Digitalmars-d-learn
Note that this is a very simple example. You need to check in the 
function that a valid StyleColor will actually be produced. 
Otherwise, it'll happily produce a StyleColor that's invalid.


Re: Filtering Associative Array Key-Values the D way

2015-06-24 Thread Ali Çehreli via Digitalmars-d-learn

On 06/24/2015 09:08 AM, David DeWitt wrote:

I'm trying to understand filtering an Associative Array the D way.  I
have the code below
(Using while readln cause problem failing on Debian using
byLineCopy()).  When the byKeyValue().filter evaluates to reduce the
number of Keys:Values to only the ones in the filtered header, what is
the best way to transform the Pair back to an AA instead of having to
run the foreach loop after?  Basically if the file has 300 columns and
300 in the full header but only 15 in the filtered header I want the
return of rec to only be the 15 in an Associative Array.  Thanks :)


 string line;
 string[string][] records;
 while ((line = f.readln()) !is null){
 string[string] record;
 auto rec = assocArray(zip(h1.fullHeader, splitter(line,',')))
.byKeyValue().filter!(a=h1.filteredHeader.canFind(a.key));

 foreach(r;rec){
 record[r.key] = r.value;
 }
 records ~= record;
 }


It would be more efficient to filter the input before creating a larger 
AA to be thrown away:


import std.stdio;
import std.range;
import std.algorithm;

void main()
{
auto keys = 10.iota;
auto values = keys.map!(k = 10 * k);

auto result = zip(keys, values)
  .filter!(t = t[0] % 2)
  .assocArray;

writefln(%(%s: %s\n%), result);
}

The AA contains elements with odd keys:

1: 10
5: 50
9: 90
3: 30
7: 70

Ali



Re: Check if template has been passed a reference type or value type?

2015-06-24 Thread sigod via Digitalmars-d-learn

On Sunday, 7 June 2015 at 15:39:17 UTC, Marc Schütz wrote:

On Sunday, 7 June 2015 at 15:17:27 UTC, 1967 wrote:
I've got a template that takes in a type. Sometimes the type 
is a class, sometimes a struct, sometimes just an int. It 
doesn't much matter what it is, but if it's a reference type I 
need to check if it's null so I can make it not null before 
using it. I get an error if I try to check if a value type is 
null so I'd like to put a static if to check if the type 
passed to the template is a nullable one so I can avoid 
checking the value types passed in. What do I have to do to 
check for nullability?


You can directly check whether it allows comparison with null:

static if(is(typeof(value is null) : bool)) {
if(value is null)
  ...
}


Don't forget [`Nullable`][0] type, which won't pass this check.

[0]: http://dlang.org/phobos/std_typecons.html#.Nullable


Re: Casting from an enum type to another enum type

2015-06-24 Thread Roland Hadinger via Digitalmars-d-learn

On Wednesday, 24 June 2015 at 18:16:42 UTC, Meta wrote:
std.conv.to really should be able to do this, but I guess not 
many people have needed to do this. You can write an 
extension to `to` which does it for you:


import std.traits;

auto to(To, From)(From f)
if (is(From == enum)  is(To == enum)  is(OriginalType!From 
: OriginalType!To))

{
return cast(To)f;
}


Thanks, that works!

I'll add this to my project's 'helpers' module, with the return 
line replaced by:


foreach (m; EnumMembers!To) {
if (m == f)
return m;
}
throw new ConvException(Value not in enum);