Jump to page: 1 2
Thread overview
template instantiation --- having trouble therewith
Sep 03, 2013
Carl Sturtivant
Sep 03, 2013
John Colvin
Sep 03, 2013
Carl Sturtivant
Sep 03, 2013
Manfred Nowak
Sep 03, 2013
Carl Sturtivant
Sep 03, 2013
Manfred Nowak
Sep 03, 2013
Manfred Nowak
Sep 04, 2013
Carl Sturtivant
Sep 04, 2013
Carl Sturtivant
Sep 04, 2013
Carl Sturtivant
Sep 03, 2013
Manfred Nowak
Sep 04, 2013
Carl Sturtivant
Sep 05, 2013
John Colvin
Sep 05, 2013
John Colvin
Sep 05, 2013
John Colvin
Sep 05, 2013
John Colvin
September 03, 2013
template Dptr( T, U...) {
	alias T delegate( U args) Dptr;
}

Dptr!(T,U) muddle( T, U...)( Dptr!(T,U) f) {
	return f; //or make another delegate in real code
}


unittest {
	import std.stdio;
	int x = 3;
	int scale( int s) { return x * s; }
	Dptr!(int,int) f = muddle( &scale);
	writeln( f(7));
}
============================================

The above technique seemed natural to me, but I get this message from dmd:

p1.d(15): Error: template p1.muddle does not match any function template declaration. Candidates are:
p1.d(7):        p1.muddle(T, U...)(Dptr!(T, U) f)
p1.d(15): Error: template p1.muddle(T, U...)(Dptr!(T, U) f) cannot deduce template function from argument types !()(int delegate(int))

and if I use muddle!(int,int) instead it doesn't help. This is likely a misunderstanding on my part --- please show me how to sort this out.

September 03, 2013
On Tuesday, 3 September 2013 at 03:49:52 UTC, Carl Sturtivant wrote:
>
> template Dptr( T, U...) {
> 	alias T delegate( U args) Dptr;
> }
>
> Dptr!(T,U) muddle( T, U...)( Dptr!(T,U) f) {
> 	return f; //or make another delegate in real code
> }
>
>
> unittest {
> 	import std.stdio;
> 	int x = 3;
> 	int scale( int s) { return x * s; }
> 	Dptr!(int,int) f = muddle( &scale);
> 	writeln( f(7));
> }
> ============================================
>
> The above technique seemed natural to me, but I get this message from dmd:
>
> p1.d(15): Error: template p1.muddle does not match any function template declaration. Candidates are:
> p1.d(7):        p1.muddle(T, U...)(Dptr!(T, U) f)
> p1.d(15): Error: template p1.muddle(T, U...)(Dptr!(T, U) f) cannot deduce template function from argument types !()(int delegate(int))
>
> and if I use muddle!(int,int) instead it doesn't help. This is likely a misunderstanding on my part --- please show me how to sort this out.

I'm confused as to what you're trying to do... your example code is equivalent to

  import std.stdio;
  int x = 3;
  int scale( int s) { return x * s; }
  auto f = &scale;
  writeln( f(7) );
September 03, 2013
>
> I'm confused as to what you're trying to do... your example code is equivalent to
>
>   import std.stdio;
>   int x = 3;
>   int scale( int s) { return x * s; }
>   auto f = &scale;
>   writeln( f(7) );

No it isn't according to dmd.

My code is a minimal piece that produces the same error as some real code. The higher order generic function muddle in the real code is supposed to transform one delegate into another, but I still get the template problem if muddle is the identity function (given here).

My example code isn't equivalent to the above according to the compiler. Why is that? And how can I make it work?
September 03, 2013
Carl Sturtivant wrote:

> No it isn't according to dmd.
dmd does not express this.

according to
> p1.d(15): Error: [...]
dmd "cannot deduce" that `Dptr!(T, U)' might be equal to
`int delegate(int)'

-manfred


September 03, 2013
On Tuesday, 3 September 2013 at 13:42:44 UTC, Manfred Nowak wrote:
> Carl Sturtivant wrote:
>
>> No it isn't according to dmd.
> dmd does not express this.
>
> according to
>> p1.d(15): Error: [...]
> dmd "cannot deduce" that `Dptr!(T, U)' might be equal to
> `int delegate(int)'
>
> -manfred

I understood that. How do I fix this? Writing muddle!(int,int) in
the unit test so there's no type inference needed gives the same error message.
September 03, 2013
Carl Sturtivant wrote:
> How do I fix this?

maybe it is currently not fixable because of restrictions in the deduction algorithm.

Obviously the deduction works if the instantion of the template is replaced by a symbol by `alias T delegate( U) Dptr;' or similar.

-manfred



September 03, 2013
Carl Sturtivant wrote:
> Writing muddle!(int,int)
[...]
> gives the same error message.

Not entirely true:

template muddle( T, U...){
  alias T delegate( U) Dptr;
  auto muddle1( T, U...)( Dptr f) {
	return f; //or make another delegate in real code
  }
  alias muddle1!( T, U) muddle;
}

unittest {
	import std.stdio;
	int x = 3;
	int scale( int s) { return x * s; }
	auto f= muddle!( int, int)( &scale);
	writeln( f(7));
}

-manfred
September 03, 2013
Carl Sturtivant wrote:
> is supposed to transform one delegate into another

Then please declare the template parameters to be delegates:

U muddle( T, U)( T f) {
	uint g( int fp){
		return cast(uint)( 5* f( fp));
        }
	auto gP= &g;
	return gP;
}

unittest {
     import std.stdio;
	      int x = 3;
	    int scale( int s) { return x * s; }
	  auto f= muddle!( int delegate( int), uint delegate( int))( &scale);
	writeln( f(7));
}
// no deduction problems

-manfred
September 04, 2013
On Tuesday, 3 September 2013 at 17:25:12 UTC, Manfred Nowak wrote:
> Carl Sturtivant wrote:
>> Writing muddle!(int,int)
> [...]
>> gives the same error message.
>
> Not entirely true:
>
> template muddle( T, U...){
>   alias T delegate( U) Dptr;
>   auto muddle1( T, U...)( Dptr f) {
> 	return f; //or make another delegate in real code
>   }
>   alias muddle1!( T, U) muddle;
> }
>
> unittest {
> 	import std.stdio;
> 	int x = 3;
> 	int scale( int s) { return x * s; }
> 	auto f= muddle!( int, int)( &scale);
> 	writeln( f(7));
> }

Can you point me at some documentation that explains this behavior?


September 04, 2013
On Tuesday, 3 September 2013 at 13:42:44 UTC, Manfred Nowak wrote:
> Carl Sturtivant wrote:
>
>> No it isn't according to dmd.
> dmd does not express this.
>
> according to
>> p1.d(15): Error: [...]
> dmd "cannot deduce" that `Dptr!(T, U)' might be equal to
> `int delegate(int)'

Indeed, dmd doesn't know the equivalence, and therefore won't compile my code.
« First   ‹ Prev
1 2