Implicit Casting

Jonathan JonathanILevi at gmail.com
Tue Feb 6 21:35:08 UTC 2018


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?


More information about the Digitalmars-d-learn mailing list