Implicit conversion

Jiyan jiyan at jiyan.info
Wed Jan 17 23:15:33 UTC 2018


Hello,

I want to convert from ints implicit to a struct type, so for 
example:

struct use
{
	int x;

	int toInt()
	{
		return x;
	}

	use fromInt(int v)
	{
		return use(v);
	}

         alias toInt this; // implicit conversion to int value

	this(int v)
	{x = v;}
}

void useP(int v)
{
	v.writeln;
}

void useV(use v)
{
	v.writeln;
}

void main(string[] args)
{
	use a = use(2);
	//useP(a);
	useV(2); // how can i let this work?
}

Thanks :)



More information about the Digitalmars-d-learn mailing list