Thread overview
[Issue 9151] New: opCast makes "this" alias ineffective
Dec 13, 2012
Puneet Goel
Dec 13, 2012
Puneet Goel
Dec 14, 2012
Kenji Hara
Dec 14, 2012
Puneet Goel
December 13, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9151

           Summary: opCast makes "this" alias ineffective
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: puneet@coverify.org


--- Comment #0 from Puneet Goel <puneet@coverify.org> 2012-12-13 09:34:14 PST ---
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) {}
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 13, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9151



--- Comment #1 from Puneet Goel <puneet@coverify.org> 2012-12-13 09:48:46 PST ---
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) {}
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 14, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9151



--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2012-12-13 22:16:00 PST ---
(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)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 14, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9151



--- Comment #3 from Puneet Goel <puneet@coverify.org> 2012-12-13 22:19:47 PST ---
Kenji, thanks for clarification.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------