digitalmars.D.learn - Cannot implicitly convert expression (struct this)
- Andre Pany (60/62) Jun 22 2017 Hi,
- Kagamin (2/8) Jun 23 2017 I think these should be equivalent, report a bug.
- Andre Pany (5/14) Jul 05 2017 Issue 17552 created:
Hi,
I created a custom type which enables me to have enums which have
in their initial state, the init value of their base type.
Something similiar to Nullable...
enum Reason : string {CO = "Co", FU = "Fu", CA = "Ca"}
struct TestStruct {InitialEnum!Reason reason;}
This line raises the error:
TestStruct s2 = TestStruct(Reason.FU);
Error: cannot implicitly convert expression ("Fu") of type
Reason to InitialEnum!(Reason)
While this line is working fine:
TestStruct s1 = {reason: Reason.FU};
What do I miss?
Kind regards
André
struct InitialEnum(T)
{
import std.traits: OriginalType, EnumMembers;
import std.conv: to;
private T _value;
private bool _isEmpty = true;
alias EnumBaseType = OriginalType!T;
property EnumBaseType baseTypeValue() { return (_isEmpty) ?
EnumBaseType.init : _value; }
property T value() { return _value; }
property bool isEmpty() { return _isEmpty; }
alias baseTypeValue this;
void opAssign(EnumBaseType value)
{
if (value == EnumBaseType.init)
{
_isEmpty = true;
return;
}
foreach (member; EnumMembers!T)
{
if (value == member)
{
_value = member;
_isEmpty = false;
return;
}
}
throw new Exception("Value "~value.to!string~" is not a valid
enum value");
}
this(T t)
{
_isEmpty = false;
_value = t;
}
this(EnumBaseType value)
{
opAssign(value);
}
}
Jun 22 2017
On Thursday, 22 June 2017 at 09:57:44 UTC, Andre Pany wrote:This line raises the error: TestStruct s2 = TestStruct(Reason.FU);I think these should be equivalent, report a bug.While this line is working fine: TestStruct s1 = {reason: Reason.FU};Error: cannot implicitly convert expression ("Fu") of type Reason to InitialEnum!(Reason)
Jun 23 2017
On Friday, 23 June 2017 at 15:52:10 UTC, Kagamin wrote:On Thursday, 22 June 2017 at 09:57:44 UTC, Andre Pany wrote:Issue 17552 created: http://forum.dlang.org/post/mailman.3688.1498412595.31550.digitalmars-d-bugs puremagic.com Kind regards AndréThis line raises the error: TestStruct s2 = TestStruct(Reason.FU);I think these should be equivalent, report a bug.While this line is working fine: TestStruct s1 = {reason: Reason.FU};Error: cannot implicitly convert expression ("Fu") of type Reason to InitialEnum!(Reason)
Jul 05 2017








Andre Pany <andre s-e-a-p.de>