On Thursday, 19 April 2012 at 15:48:29 UTC, Namespace wrote:
It seems that i can't cast if i want a const Vector2D, neither
with castFromtObject nor with opCast. If i do it i get a very
long list of compile errors. It's no problem because i can deal
without cast to const but it would be usefull if you can show
me how i can use both, const and normal casts.
For example in this case
void foo(T)(const Vector2D!(T) vec) {
bar(cast(Vector2s) vec);
}
void bar(const Vector2s vec) {
}
const Vector2f vf = Vector2f(23, 42);
Vector2s vs = Vector2s(vf);
Vector2s vs_ = cast(Vector2s) vf;
Vector2f vf_ = cast(Vector2f) vs;
Vector2s vs2 = Vector2s(23, 42);
if (vs2 == vf) {
writeln("equal!");
}
foo(vf);
foo(vs);
i get this compiler errors:
cast.d(323): Error: template
cast.Vector2D!(short).Vector2D.opCast matches more
than one template declaration, cast.d(285):opCast(U) if
(is(Unqual!(U) == Vector
2D!(byte)) || is(Unqual!(U) == Vector2D!(ubyte)) || is(Unqual!(U)
== Vector2D!(s
hort)) || is(Unqual!(U) == Vector2D!(ushort)) || is(Unqual!(U) ==
Vector2D!(int)
) || is(Unqual!(U) == Vector2D!(uint)) || is(Unqual!(U) ==
Vector2D!(long)) || i
s(Unqual!(U) == Vector2D!(ulong)) || is(Unqual!(U) ==
Vector2D!(float)) || is(Un
qual!(U) == Vector2D!(double)) || is(Unqual!(U) ==
Vector2D!(real))) and cast.d(
306):opCast(T) if
(isImplicitlyConvertible!(typeof(this),const(T)))
cast.d(360): Error: template instance cast.foo!(short) error
instantiating
I can avoid them if i change bar to
void foo(T)(const Vector2D!(T) vec) {
static if (is(typeof(vec.x) == const float)) {
bar(cast(Vector2s) vec);
} else {
bar(vec);
}
}
Of course i must check for more then only "const float" but it is
only a test case.
So it seems i have to change the cast operator. But i have no
idea how.
I thougth that "inout" would help me, but i was wrong. So maybe
you have some ideas?