www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - overload/ride

reply Ant <duitoolkit yahoo.ca> writes:
I'm trying to use tango and I found Cout doesn't take int.
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?

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?

???

Ant
May 19 2007
parent reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
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
May 19 2007
parent Ant <duitoolkit yahoo.ca> writes:
Chris Nicholson-Sauls wrote:
 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.
thank you, I got that tip for the IRC also.
 
 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: alias Console.Output.opCall opCall; // <- the fix
[...]
 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?
 
[...]
 -- Chris Nicholson-Sauls
You're right, so the real fix is: MOutput opCall(char[] str) { super.opCall(str); return this; } MOutput opCall(int i) { // do something here return this; } of course, this has no real use, just helped me better understand D. thanks. Ant
May 19 2007