www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Implicit Casting

reply Jonathan <JonathanILevi gmail.com> writes:
I am trying to make a `Pos` type.  But I need it to implicitly 
cast from an `int[2]`.
I am using the `alias this` to get most of what I want but it 
still doesn't do all an implicit cast can do.

What I have now is this:

struct Pos {
	int[2] pos;
	alias pos this;

	this (int[2] pos) {
		this.pos = pos;
	}
}

This allows me to implicitly cast from type `Pos` to type 
`int[2]` but not the other way.  I can do a sort of cast when I 
define a `Pos` (`Pos pos = [2,3]` works).

But what I really want it to do is to implicitly cast an `int[2]` 
to a `Pos`.

Is this possible in D?
Feb 06 2018
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 02/06/2018 01:35 PM, Jonathan wrote:

 But what I really want it to do is to implicitly cast an `int[2]` to a
 `Pos`.

 Is this possible in D?
Simply, no. :) Ali
Feb 06 2018