www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9151] New: opCast makes "this" alias ineffective

reply d-bugmail puremagic.com writes:
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



---
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: -------
Dec 13 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9151




---
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: -------
Dec 13 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9151





 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 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: -------
Dec 13 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9151




---
Kenji, thanks for clarification.

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