D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 17552 - Cannot implicitly convert expression (struct this)
Summary: Cannot implicitly convert expression (struct this)
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 All
: P3 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-06-25 17:42 UTC by Andre
Modified: 2024-12-13 18:52 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andre 2017-06-25 17:42:58 UTC
I created a custom type which enables me to have enums which have as 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};

I asked this question in the learning forum and got the advice to create an issue.

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);
	}
}
Comment 1 dlangBugzillaToGithub 2024-12-13 18:52:50 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/17800

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB