Bug 2814 – implicit cast (implemented via alias this) is not triggered when passing to function

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2009-04-06T15:26:00Z
Last change time
2015-06-09T05:11:36Z
Keywords
rejects-valid
Assigned to
nobody
Creator
2korden

Comments

Comment #0 by 2korden — 2009-04-06T15:26:31Z
struct Proxy { private Object o; Object get() { return o; } alias get this; } void foo(Object o) { } void main() { Proxy proxy; //foo(proxy); // should work, but doesn't Object o = proxy; // workaround foo(o); } test.d(17): Error: function test.foo (Object o) does not match parameter types ( Proxy) test.d(17): Error: cannot implicitly convert expression (proxy) of type Proxy to object.Object
Comment #1 by jarrett.billingsley — 2009-08-05T18:48:39Z
Strangely, the compiler's behavior differs based on the type of the result of get(). While the workaround works in this instance (returning Object), it doesn't seem to work for any other type. For instance, given this struct: struct Proxy { int get() { return 0; } alias get this; } If you do: Proxy p; int x = p; you get an error saying it can't convert type Proxy to int. But here's the real kicker: Object x = p; _Now_ the compiler says that it can't convert p.get() of type int to Object! So for some reason, it's using the alias this when it shouldn't, and not using it when it should.
Comment #2 by yebblies — 2012-02-03T23:31:26Z
This works in current dmd (2.058)