Two problems with op overload

Philippe Sigaud philippe.sigaud at gmail.com
Sun Mar 14 07:32:37 PDT 2010


On Sun, Mar 14, 2010 at 14:14, bearophile <bearophileHUGS at lycos.com> wrote:

> I have tried to use the new operators of D2 and I have found several
> problems. This small program shows two of those problems (some of my
> problems can be caused by my improper usage, I'm trying to tell apart the
> operator overloading bugs from my improper usage cases).
> void main() {
>    Foo f = Foo(5);
>    f++; // line 16
>    auto a = new int[f]; // line 17
> }
>
> Errors:
> temp2.d(16): Error: var has no effect in expression (__tmp1)
>

Try ++f, it works. I guess ++f is rewritten f.opUnary!"++"(), but f++ must
have create a temporary variable.



> temp2.d(17): Error: cannot implicitly convert expression (f) of type Foo to
> uint
>
> Note: the line 17 works if I write it this way, but it's not nice:
> auto a = new int[cast(uint)f];
>
>
Note the error message: cannot *implicitly* convert expresion (f).
opCast defines the explicit cast operation, it has no effect on implicit
casting. Having an opImplicitCast(T) would be nice, I agree.

For now, this also does not work:

void bar(int i) {}

(in main:)
   bar(f); // Error: cannot implicitly convert expresion (f) of type Foo to
int.

That's sad, I'd like to have std.variant.Variant defining an opCast. That
way, you could do:

void foo(int i) {}
void bar(string s) {}

Variant v;
v = 3;
foo(v);       // try to cast v to int implicitly: works, move along.
v = "abc";
bar(v);       // implicit cast to string, works.


  Philippe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20100314/82c83fd3/attachment.htm>


More information about the Digitalmars-d-learn mailing list