overload/ride

Chris Nicholson-Sauls ibisbasenji at gmail.com
Sat May 19 20:33:22 PDT 2007


Ant wrote:
> I'm trying to use tango and I found Cout doesn't take int.

I believe that's by design.  If you are wanting more flexible output than flat strings can 
provide, I recommend trying tango.io.Stdout:Stdout instead.

> Before trying to figure out why I did this:
> 
> import tango.io.Console;
> 
> class MOutput : Console.Output
> {
>     this()
>     {
>         super(cast(tango.io.Console.Console.Conduit)Cout.conduit(), 
> Cout.redirected());
>     }
>     
> //    Console.Output opCall(char[] i)
> //    {
> //        return this;
> //    }
> 
>     Console.Output opCall(int i)
>     {
>         return this;
>     }
> 
> }
> 
> void main()
> {
>     (new MOutput()) ("Hello, sweetheart \u263a")(14).newline;
> }
> 
> looks fine right?
> but D doesn't like it I get:
> t1.d(24): Error: cannot implicitly convert expression ("Hello, 
> sweetheart \xe2\x98\xba") of type char[21] to int
> 
> Am I doing something wrong? is this a D limitation?
> where is the super.opCall(char[]) hiding?

Its hiding in the parent class, and this is a limitation.  The problem is that the lookup 
rules will not automatically look to the parent class if the symbol has matches in the 
current class.  Oy.  The fix is this:

class MOutput : Console.Output {

     this() {
         super(cast(tango.io.Console.Console.Conduit)Cout.conduit(), Cout.redirected());
     }

     alias Console.Output.opCall opCall; // <- the fix

     Console.Output opCall(int i) {
         return this;
     }

}


> adding the commented out method results in the error:
> function alias tango.io.Console.Console.Output.append (char[]) does not 
> match parameter types (int)
> 
> 
> now where did the overloaded opCall(int) go?
> 
> ???

The problem here is a more interesting one, and it has to do with your overloads.  They 
should be returning 'MOutput's rather than 'Console.Output's, other wise the chained calls 
to opCall() will happen in Console.Output's namespace...  isn't OOP fun?

But as I said before, you can just use the Stdout class if you need more than flat string 
printing.

-- Chris Nicholson-Sauls


More information about the Digitalmars-d-learn mailing list