www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Deducing types for function templates

reply Magnus Lie Hetland <magnus hetland.org> writes:
I've got a function template along these lines:

  Foo!T foo(T)(T[] bar, real function(T,T) baz) { ... }

The main reason I'm using this is that it seems necessary to use a 
function, rather than a type (such as Foo), if you want the compiler to 
deduce the compile-time parameters. (Right?)

Now ... this works just fine. However, if I try to add "const" before 
"T[]", DMD no longer groks it. Is that how it shold be? Seems awkward 
to me... (Or maybe I'm just doing it wrong?)

And a followup question: Writing out the type of baz like this (in 
several places) is also a bit awkward. I'd like to have a template so I 
could just do

  Foo!T foo(T)(T[] bar, baz_t!T baz) { ... }

However, I haven't been able to define such a template without running 
into the same problem (i.e., that DMD no longer can deduce what T 
should be from my arguments).

Any pointers?

-- 
Magnus Lie Hetland
http://hetland.org
Mar 15 2011
parent reply Michel Fortin <michel.fortin michelf.com> writes:
On 2011-03-15 10:42:46 -0400, Magnus Lie Hetland <magnus hetland.org> said:

 I've got a function template along these lines:
 
   Foo!T foo(T)(T[] bar, real function(T,T) baz) { ... }
 
 The main reason I'm using this is that it seems necessary to use a 
 function, rather than a type (such as Foo), if you want the compiler to 
 deduce the compile-time parameters. (Right?)
 
 Now ... this works just fine. However, if I try to add "const" before 
 "T[]", DMD no longer groks it. Is that how it shold be? Seems awkward 
 to me... (Or maybe I'm just doing it wrong?)

I'd say add it to bugzilla. It's probably related to this bug: <http://d.puremagic.com/issues/show_bug.cgi?id=5531>
 And a followup question: Writing out the type of baz like this (in 
 several places) is also a bit awkward. I'd like to have a template so I 
 could just do
 
   Foo!T foo(T)(T[] bar, baz_t!T baz) { ... }
 
 However, I haven't been able to define such a template without running 
 into the same problem (i.e., that DMD no longer can deduce what T 
 should be from my arguments).

My feeling is that it should work. Feel free to file a bug or enhancement request in the bug tracker. -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Mar 15 2011
parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
On Tue, 15 Mar 2011 17:36:04 +0100, Michel Fortin  
<michel.fortin michelf.com> wrote:

 On 2011-03-15 10:42:46 -0400, Magnus Lie Hetland <magnus hetland.org>  
 said:

 I've got a function template along these lines:
    Foo!T foo(T)(T[] bar, real function(T,T) baz) { ... }
  The main reason I'm using this is that it seems necessary to use a  
 function, rather than a type (such as Foo), if you want the compiler to  
 deduce the compile-time parameters. (Right?)
  Now ... this works just fine. However, if I try to add "const" before  
 "T[]", DMD no longer groks it. Is that how it shold be? Seems awkward  
 to me... (Or maybe I'm just doing it wrong?)

I'd say add it to bugzilla. It's probably related to this bug: <http://d.puremagic.com/issues/show_bug.cgi?id=5531>

Oh, it's much older than that: http://d.puremagic.com/issues/show_bug.cgi?id=2128 -- Simen
Mar 15 2011
parent reply Michel Fortin <michel.fortin michelf.com> writes:
On 2011-03-15 13:20:30 -0400, "Simen kjaeraas" <simen.kjaras gmail.com> said:

 On Tue, 15 Mar 2011 17:36:04 +0100, Michel Fortin  
 <michel.fortin michelf.com> wrote:
 
 On 2011-03-15 10:42:46 -0400, Magnus Lie Hetland <magnus hetland.org>  said:
 
 I've got a function template along these lines:
    Foo!T foo(T)(T[] bar, real function(T,T) baz) { ... }
  The main reason I'm using this is that it seems necessary to use a  
 function, rather than a type (such as Foo), if you want the compiler to 
  deduce the compile-time parameters. (Right?)
  Now ... this works just fine. However, if I try to add "const" before  
 "T[]", DMD no longer groks it. Is that how it shold be? Seems awkward  
 to me... (Or maybe I'm just doing it wrong?)

I'd say add it to bugzilla. It's probably related to this bug: <http://d.puremagic.com/issues/show_bug.cgi?id=5531>

Oh, it's much older than that: http://d.puremagic.com/issues/show_bug.cgi?id=2128

I'm not sure it's the same thing, but I realize it's hard to say given Magnus in his original post didn't mention how he attempts to instantiate the template. -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Mar 15 2011
parent Magnus Lie Hetland <magnus hetland.org> writes:
On 2011-03-15 21:13:36 +0100, Michel Fortin said:

 Oh, it's much older than that:
 
 http://d.puremagic.com/issues/show_bug.cgi?id=2128

I'm not sure it's the same thing, but I realize it's hard to say given Magnus in his original post didn't mention how he attempts to instantiate the template.

Hm. I've gone back to it now, and, sadly (as opposed to for the other bugs I've encountered :) I didn't isolate and save the related code -- so now I'm unable to reproduce the const problem. (Who knows ... maybe I was just trying to pass it on as an un-const parameter or something? Might very well have been a bug on my part.) Here's some sample code for the other part of my question, though (which might not have been as unclear in the first place?). class Foo(T) { T foo; } template baz_t(T) { alias real function(T, T) baz_t; } Foo!T foo(T)(T[] bar, baz_t!T baz) { return new Foo!T; } real frozz(uint x, uint y) { return 4.2; } void main() { uint[] x = [1, 2, 3]; auto f = foo(x, &frozz); } The error is: inferencebug.d(19): Error: template inferencebug.foo(T) does not match any function template declaration inferencebug.d(19): Error: template inferencebug.foo(T) cannot deduce template function from argument types !()(uint[],real function(uint x, uint y)) -- Magnus Lie Hetland http://hetland.org
Mar 21 2011