Re: Nothrow std.conv.to with explicit default value

2018-06-21 Thread Per Nordlöw via Digitalmars-d-learn
On Wednesday, 20 June 2018 at 14:39:48 UTC, Per Nordlöw wrote: Is there a way to avoid compile-time-string-concat plus mixin here? Using __traits(getMember, ...) should compile faster, right? T toDefaulted(T)(scope const(char)[] value, T defaultValue) @safe pure nothrow @nogc if (is(T ==

Re: Nothrow std.conv.to with explicit default value

2018-06-20 Thread Per Nordlöw via Digitalmars-d-learn
On Wednesday, 20 June 2018 at 09:54:29 UTC, Per Nordlöw wrote: T toDefaulted(T)(scope const(char)[] value, T defaultValue) @safe pure nothrow @nogc if (is(T == enum)) { switch (value) { static foreach (index, member; __traits(allMembers, T)) {

Re: Nothrow std.conv.to with explicit default value

2018-06-20 Thread Jonathan M Davis via Digitalmars-d-learn
On Wednesday, June 20, 2018 09:37:00 Per Nordlöw via Digitalmars-d-learn wrote: > On Wednesday, 20 June 2018 at 09:27:14 UTC, Per Nordlöw wrote: > > On Monday, 18 June 2018 at 21:10:03 UTC, Steven Schveighoffer > > > > wrote: > >> It just means re-doing std.conv.to, which is pretty hairy, but >

Re: Nothrow std.conv.to with explicit default value

2018-06-20 Thread Per Nordlöw via Digitalmars-d-learn
On Wednesday, 20 June 2018 at 09:37:00 UTC, Per Nordlöw wrote: AFAICT, string-to-enum-conversion must include a switch containing a static foreach over all the enumerators, right? My suggestion for nothrow @nogc string-to-enum conversion with default value T toDefaulted(T)(scope

Re: Nothrow std.conv.to with explicit default value

2018-06-20 Thread Per Nordlöw via Digitalmars-d-learn
On Wednesday, 20 June 2018 at 09:52:04 UTC, Per Nordlöw wrote: My suggestion for nothrow @nogc string-to-enum conversion with default value T toDefaulted(T)(scope const(char)[] value, T defaultValue) @safe pure nothrow @nogc if (is(T == enum)) { switch (value) {

Re: Nothrow std.conv.to with explicit default value

2018-06-20 Thread Per Nordlöw via Digitalmars-d-learn
On Wednesday, 20 June 2018 at 09:27:14 UTC, Per Nordlöw wrote: On Monday, 18 June 2018 at 21:10:03 UTC, Steven Schveighoffer wrote: It just means re-doing std.conv.to, which is pretty hairy, but also pretty well-organized. Ok. Where in std.conv do the string-to-enum conversions take place?

Re: Nothrow std.conv.to with explicit default value

2018-06-20 Thread Per Nordlöw via Digitalmars-d-learn
On Monday, 18 June 2018 at 21:10:03 UTC, Steven Schveighoffer wrote: It just means re-doing std.conv.to, which is pretty hairy, but also pretty well-organized. Ok. Where in std.conv do the string-to-enum conversions take place?

Re: Nothrow std.conv.to with explicit default value

2018-06-18 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/18/18 4:48 PM, Per Nordlöw wrote: The problem with this code is that throwing exceptions for the default case is costly at least with dmd. Is there another way to do this? Yes, have an internal implementation which doesn't throw, but rather returns an error code. Then you can call that

Re: Nothrow std.conv.to with explicit default value

2018-06-18 Thread Adam D. Ruppe via Digitalmars-d-learn
On Monday, 18 June 2018 at 20:48:55 UTC, Per Nordlöw wrote: T toDefaulted(T, S, U)(S value, /*lazy*/ U defaultValue) if (is(typeof(() { T r = defaultValue; }))) // TODO use std.traits.isAssignable!(T, U) ? why not just make it T toDefaulted(T, S)(S value, T defaultValue) and forget U

Nothrow std.conv.to with explicit default value

2018-06-18 Thread Per Nordlöw via Digitalmars-d-learn
I have a nothrow variant of std.conv.to defined as follows: T toDefaulted(T, S, U)(S value, /*lazy*/ U defaultValue) if (is(typeof(() { T r = defaultValue; }))) // TODO use std.traits.isAssignable!(T, U) ? { try { import std.conv : to; return value.to!T; } catch