www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - template interface and delegates

reply "anonymous" <non trash-mail.com> writes:
Hi,
I'm new to D and played a bit with templates and delegates.
Now i discovered some behaviore that i don't understand.
Can somebody explain me why i get two different outputs?


import std.stdio;


interface A(T){
	bool GetBool();
	T getT();
}

class C:A!(double){
	override bool GetBool(){
		return false;
	}
	override double getT(){
		return 1;
	}
}

void mwriteln(T)( A!T delegate() dg){
	writeln(dg().getT());
}

void main()
{
	auto c = new C();
	writeln(c.getT());
	mwriteln!double({return new C();});
}
Mar 31 2014
next sibling parent Justin Whear <justin economicmodeling.com> writes:
On Mon, 31 Mar 2014 18:58:30 +0000, anonymous wrote:

 Hi,
 I'm new to D and played a bit with templates and delegates.
 Now i discovered some behaviore that i don't understand. Can somebody
 explain me why i get two different outputs?
 
 
 import std.stdio;
 
 
 interface A(T){
 	bool GetBool();
 	T getT();
 }
 
 class C:A!(double){
 	override bool GetBool(){
 		return false;
 	}
 	override double getT(){
 		return 1;
 	}
 }
 
 void mwriteln(T)( A!T delegate() dg){
 	writeln(dg().getT());
 }
 
 void main()
 {
 	auto c = new C(); writeln(c.getT()); mwriteln!double({return new
 	C();});
 }
Looks like a bug. Here's a cleaned-up test case: ----------------------------------------------------------- interface A(T) { T getT(); } class C : A!double { double getT(){ return 1; } } void mwriteln(T)(A!T delegate() dg) { import std.stdio; auto a = dg(); writeln("Checkpoint 1"); assert(a !is null); writeln(a); writeln("Checkpoint 2"); } void main() { import std.traits; static assert(isImplicitlyConvertible!(C, A!double)); mwriteln!double({return new C();}); } ----------------------------------------------------------- Backtrace: ----------------------------------------------------------- Program received signal SIGSEGV, Segmentation fault. #0 0x000000000042ac8c in _d_interface_cast () #1 0x0000000000427ec6 in std.format.__T11formatValueTS3std5stdio4File17LockingTextWriterTC4test8__T1ATdZ1ATaZ.formatValue () () #2 0x0000000000427e23 in std.format.__T13formatGenericTS3std5stdio4File17LockingTextWriterTC4test8__T1ATdZ1ATaZ.formatGeneric () () #3 0x0000000000427d29 in std.format.__T14formattedWriteTS3std5stdio4File17LockingTextWriterTaTC4test8__T1ATdZ1AZ.formattedWrite () () #4 0x0000000000427871 in std.stdio.File.__T5writeTC4test8__T1ATdZ1ATaZ.write() () #5 0x00000000004277d5 in std.stdio.__T7writelnTC4test8__T1ATdZ1AZ.writeln () () #6 0x000000000042771f in test.__T8mwritelnTdZ.mwriteln() () #7 0x00000000004276a9 in D main () #8 0x000000000042b61c in rt.dmain2._d_run_main() () #9 0x000000000042b576 in rt.dmain2._d_run_main() () #10 0x000000000042b5dc in rt.dmain2._d_run_main() () #11 0x000000000042b576 in rt.dmain2._d_run_main() () #12 0x000000000042b4f7 in _d_run_main () #13 0x0000000000429c5f in main () -----------------------------------------------------------
Mar 31 2014
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 31 Mar 2014 14:58:30 -0400, anonymous <non trash-mail.com> wrote:

 Hi,
 I'm new to D and played a bit with templates and delegates.
 Now i discovered some behaviore that i don't understand.
 Can somebody explain me why i get two different outputs?


 import std.stdio;


 interface A(T){
 	bool GetBool();
 	T getT();
 }

 class C:A!(double){
 	override bool GetBool(){
 		return false;
 	}
 	override double getT(){
 		return 1;
 	}
 }

 void mwriteln(T)( A!T delegate() dg){
 	writeln(dg().getT());
 }

 void main()
 {
 	auto c = new C();
 	writeln(c.getT());
 	mwriteln!double({return new C();});
 }
This is definitely a bug. Reduced case: import std.stdio; interface A{ void foo(); } class C:A{ override void foo(){ writeln("here"); } } void x( A delegate() dg){ dg().foo(); } void main() { A c = new C; c.foo(); // prints "here" x({A a = new C; return a;}); // prints "here" x({return new C;}); // does not print } This is really an issue with delegate return type inferrence not working properly. -Steve
Mar 31 2014
parent reply "anonymous" <anonymous trash-mail.com> writes:
Ok, thought i did something wrong or got some wrong idea how it 
should work.
Mar 31 2014
parent reply "anonymous" <non trash-mail.com> writes:
Is this bug allready reported? or can somebody who has a deeper 
insight to this report it?

On Tuesday, 1 April 2014 at 05:51:46 UTC, anonymous wrote:
 Ok, thought i did something wrong or got some wrong idea how it 
 should work.
Apr 01 2014
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 01 Apr 2014 15:47:42 -0400, anonymous <non trash-mail.com> wrote:

 Is this bug allready reported? or can somebody who has a deeper insight  
 to this report it?
I don't know. I think you should report it. If it's already reported, someone will close it as a "duplicate" -Steve
Apr 01 2014
parent "Kenji Hara" <k.hara.pg gmail.com> writes:
On Tuesday, 1 April 2014 at 19:55:05 UTC, Steven Schveighoffer 
wrote:
 On Tue, 01 Apr 2014 15:47:42 -0400, anonymous 
 <non trash-mail.com> wrote:

 Is this bug allready reported? or can somebody who has a 
 deeper insight to this report it?
I don't know. I think you should report it. If it's already reported, someone will close it as a "duplicate" -Steve
I filed it. https://d.puremagic.com/issues/show_bug.cgi?id=12508 Kenji Hara
Apr 02 2014