www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - IFTI Bug

reply Kramer <Kramer_member pathlink.com> writes:
I imagine this should work.  The factorial code is straight from the docs.

import std.stdio;

void main()
{
     writefln(factorial(2));
}

template factorial(int n)
{
   static if (n == 1)
     const factorial = 1;
   else
     const factorial = n * factorial!(n-1);
}

C:\code\d\src>dmd template_ex_1.d
template_ex_1.d(8): template template_ex_1.factorial(int n) is not a 
function template
template_ex_1.d(5): template template_ex_1.factorial(int n) cannot 
deduce template function from argument types (int)

-Kramer
Jul 09 2006
parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Kramer wrote:
 I imagine this should work.  The factorial code is straight from the docs.
 
 import std.stdio;
 
 void main()
 {
     writefln(factorial(2));
 }
 
 template factorial(int n)
 {
   static if (n == 1)
     const factorial = 1;
   else
     const factorial = n * factorial!(n-1);
 }
 
 C:\code\d\src>dmd template_ex_1.d
 template_ex_1.d(8): template template_ex_1.factorial(int n) is not a 
 function template
 template_ex_1.d(5): template template_ex_1.factorial(int n) cannot 
 deduce template function from argument types (int)
 
 -Kramer

You need to instantiate the template with a bang: void main() { writefln(factorial!(2)); } IFTI only applies to function templates, which this is not. -- Kirk McDonald Pyd: Wrapping Python with D http://dsource.org/projects/pyd/wiki
Jul 09 2006
parent reply Kramer <Kramer_member pathlink.com> writes:
Kirk McDonald wrote:
 Kramer wrote:
 I imagine this should work.  The factorial code is straight from the 
 docs.

 import std.stdio;

 void main()
 {
     writefln(factorial(2));
 }

 template factorial(int n)
 {
   static if (n == 1)
     const factorial = 1;
   else
     const factorial = n * factorial!(n-1);
 }

 C:\code\d\src>dmd template_ex_1.d
 template_ex_1.d(8): template template_ex_1.factorial(int n) is not a 
 function template
 template_ex_1.d(5): template template_ex_1.factorial(int n) cannot 
 deduce template function from argument types (int)

 -Kramer

You need to instantiate the template with a bang: void main() { writefln(factorial!(2)); } IFTI only applies to function templates, which this is not.

Thanks. I knew about the bang, but figured IFTI would be able to handle this and would consider this a function so I thought it might work. I haven't worked with D in a while, so is there any reason why this isn't considered a function template? What would I need to do so IFTI would be invoked? Thanks in advance. -Kramer
Jul 09 2006
next sibling parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Kramer wrote:
 Kirk McDonald wrote:
 
 Kramer wrote:

 I imagine this should work.  The factorial code is straight from the 
 docs.

 import std.stdio;

 void main()
 {
     writefln(factorial(2));
 }

 template factorial(int n)
 {
   static if (n == 1)
     const factorial = 1;
   else
     const factorial = n * factorial!(n-1);
 }

 C:\code\d\src>dmd template_ex_1.d
 template_ex_1.d(8): template template_ex_1.factorial(int n) is not a 
 function template
 template_ex_1.d(5): template template_ex_1.factorial(int n) cannot 
 deduce template function from argument types (int)

 -Kramer

You need to instantiate the template with a bang: void main() { writefln(factorial!(2)); } IFTI only applies to function templates, which this is not.

Thanks. I knew about the bang, but figured IFTI would be able to handle this and would consider this a function so I thought it might work. I haven't worked with D in a while, so is there any reason why this isn't considered a function template? What would I need to do so IFTI would be invoked? Thanks in advance. -Kramer

Function templates accept both runtime and compile-time parameters. This factorial template accepts only a single integer literal (which in this case is a compile-time parameter). It is just a templated integer constant, not a function. A function template using IFTI might look something like this: T func(T)(T t) { return t * 2; } We can explicitly instantiate the template and call the function like this: writefln(func!(int)(20)); Or IFTI can derive the type of the function argument for us: writefln(func(20)); -- Kirk McDonald Pyd: Wrapping Python with D http://dsource.org/projects/pyd/wiki
Jul 09 2006
parent Kramer <Kramer_member pathlink.com> writes:
Kirk McDonald wrote:
 Kramer wrote:
 Kirk McDonald wrote:

 Kramer wrote:

 I imagine this should work.  The factorial code is straight from the 
 docs.

 import std.stdio;

 void main()
 {
     writefln(factorial(2));
 }

 template factorial(int n)
 {
   static if (n == 1)
     const factorial = 1;
   else
     const factorial = n * factorial!(n-1);
 }

 C:\code\d\src>dmd template_ex_1.d
 template_ex_1.d(8): template template_ex_1.factorial(int n) is not a 
 function template
 template_ex_1.d(5): template template_ex_1.factorial(int n) cannot 
 deduce template function from argument types (int)

 -Kramer

You need to instantiate the template with a bang: void main() { writefln(factorial!(2)); } IFTI only applies to function templates, which this is not.

Thanks. I knew about the bang, but figured IFTI would be able to handle this and would consider this a function so I thought it might work. I haven't worked with D in a while, so is there any reason why this isn't considered a function template? What would I need to do so IFTI would be invoked? Thanks in advance. -Kramer

Function templates accept both runtime and compile-time parameters. This factorial template accepts only a single integer literal (which in this case is a compile-time parameter). It is just a templated integer constant, not a function. A function template using IFTI might look something like this: T func(T)(T t) { return t * 2; } We can explicitly instantiate the template and call the function like this: writefln(func!(int)(20)); Or IFTI can derive the type of the function argument for us: writefln(func(20));

Ahhh, got it. Thanks for the explanation; that helps a lot.
Jul 09 2006
prev sibling parent Derek Parnell <derek nomail.afraid.org> writes:
On Mon, 10 Jul 2006 00:14:36 -0500, Kramer wrote:


 What would I need to do so IFTI would be invoked?

// ------------------ import std.stdio; void main() { writefln(factorial(2)); } template factorial(T) { T factorial(T n) { if (n <= 1) return cast(T)1; else return cast(T)( n * factorial(n-1)); } } // ------------------ The example you gave at first was using a template to generate a compile-time literal. To turn that into a template that generates a function instead, you need to define the function inside the template. If you give it the same name as the template, IFTI becomes easier to use too. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocrity!" 10/07/2006 3:33:48 PM
Jul 09 2006