Bug 3968 – Some way to do certain implicit casts with operator overloading

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-03-15T10:29:34Z
Last change time
2018-02-01T07:05:28Z
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-03-15T10:29:34Z
Here an implicit cast to uint or int can be handy, to avoid the cast (to be able to create a generic "fake int" struct, so for example it can be possible to create a multiprecision integer that acts like a built-in integer), is this possible? struct Foo { int x; int opCast(T:int)() { return this.x; } } void main() { Foo f = Foo(5); auto a1 = new int[cast(int)f]; // OK auto a2 = new int[f]; // ERR } An implicit cast can be useful in other situations (but I don't know about its possible bad side effects): struct Foo { int x; int opCast(T:int)() { return this.x; } } void bar(int i) {} void main() { Foo f = Foo(5); bar(f); // Error } Steven Schveighoffer has suggested some possible solutions, like: struct Foo { int x; uint opCast(T:uint)() { return this.x; } int opCast(T:int)() { return this.x; } alias opCast this; } void main() { Foo f = Foo(5); int[] a = new int[f]; // line 9 } Or: struct Foo { uint x; uint castToUint() { return x; } alias castToUint this; } But they don't work. He says at least something like this should: alias opCast!uint this; I think that an implicit cast can also lead to bugs, so this has to be designed with care.
Comment #1 by simen.kjaras — 2018-02-01T07:05:28Z
This works today: struct Foo { int x; int opCast(T:int)() { return this.x; } alias fn = opCast!size_t; alias fn this; } unittest { Foo f = Foo(5); auto a1 = new int[cast(int)f]; // OK auto a2 = new int[f]; // Also OK } The syntax 'alias opCast!size_t this;' fails to compile, citing 'no identifier for declarator opCast!size_t'.