Casting to union type?
    cc via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Mon Aug 25 19:33:23 PDT 2014
    
    
  
Is it possible to allow implicit casting from a base type to a
union type?  For example, considering the following basic vector
union:
union vec2 {
	struct {
		float x = 0.0f;
		float y = 0.0f;
	}
	float[2] v;
	enum length = v.length;
	ref auto opIndex(size_t idx) {
		assert(idx >= 0 && idx < length, "Bounds error on index");
		return v[idx];
	}
	auto opAssign(float[] f) {
		assert(f.length == length, "Bounds error on assignment");
		v[0..length] = f[0..length];
		return this;
	}
}
vec2 a = vec2(1.0, 2.0); // fine
vec2 b;
b = [3.0, 4.0]; //fine
vec2 c = [5.0, 6.0]; // cannot cast float[] to vec2
Overloading opCast() seems to be only for "outbound" casting, and
unions can't have this() constructors like structs.  Is there any
way to accomplish this?
    
    
More information about the Digitalmars-d-learn
mailing list