Bug 9151 – opCast makes "this" alias ineffective

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-12-13T09:34:00Z
Last change time
2013-11-09T02:17:28Z
Assigned to
nobody
Creator
puneet

Comments

Comment #0 by puneet — 2012-12-13T09:34:14Z
The following code gives me an error saying: Error: template instance opCast!(bool) opCast!(bool) does not match template declaration opCast(T)() if (is(T == int)) The error disappears if the opCast is not defined. struct Foo { int t; bool get() { return (t == 0); } alias get this; T opCast(T) () if(is(T == int)) { return t; } } void main() { Foo f; if(f) {} }
Comment #1 by puneet — 2012-12-13T09:48:46Z
Also note that if both "this" alias and opCast to bool are present, the "this" alias is getting ignored in favor of opCast -- though I am not using explicit cast operation. struct Foo { int t; bool get() { return (t == 0); } alias get this; T opCast(T) () if(is(T == int)) { return t; } T opCast(T) () if(is(T == bool)) { import std.stdio; writeln("I am here"); return (t != 0); } } void main() { Foo f; if(f) {} }
Comment #2 by k.hara.pg — 2012-12-13T22:16:00Z
(In reply to comment #0) > The following code gives me an error saying: > Error: template instance opCast!(bool) opCast!(bool) does not match template > declaration opCast(T)() if (is(T == int)) (In reply to comment #0) > The following code gives me an error saying: > Error: template instance opCast!(bool) opCast!(bool) does not match template > declaration opCast(T)() if (is(T == int)) This is an intended behavior. 1. In IfStatement, opCast!bool is implicitly used. http://dlang.org/operatoroverloading#Cast 2. alias this works just like a class inheritance. If the derived type (== Foo) defines opCast, compiler does not look up the base type opCast (== implicit conversion from int to bool)
Comment #3 by puneet — 2012-12-13T22:19:47Z
Kenji, thanks for clarification.
Comment #4 by verylonglogin.reg — 2013-11-09T02:17:28Z
So this bug is invalid. Open an enhancement request if you need a language change.