Thread overview
Value-parameters in templates (Bug?)
Nov 17, 2004
Daniel Keep
Nov 17, 2004
Simon Buchan
Nov 17, 2004
Daniel Keep
Nov 17, 2004
Simon Buchan
Nov 17, 2004
Regan Heath
Nov 17, 2004
Lionello Lunesu
Nov 17, 2004
Regan Heath
Nov 17, 2004
Nick
Nov 17, 2004
Sean Kelly
November 17, 2004
I think I might have found a(nother) bug :P

#  5
#  6 template strCatMix(char[] S = " ")
#  7 {
#  8     char[] strCatMix(char[] lhs, char[] rhs)
#  9     {
# 10         return lhs ~ S ~ rhs;
# 11     }
# 12 }
# 13

Compiling this with w/ dmd, I get:

listtest.d(6): Error: integral type expected for value-parameter, not char[]

As far as I can tell from the D website, this should be perfectly fine, as it doesn't mention anywhere that the value-parameter must be an int.  Is the documentation just ambiguous, or is this not yet finished in the compiler?

	-- Daniel
November 17, 2004
On Thu, 18 Nov 2004 00:50:34 +1100, Daniel Keep <daniel.keep@dummy.gmail.com> wrote:

> I think I might have found a(nother) bug :P
>
> #  5
> #  6 template strCatMix(char[] S = " ")
> #  7 {
> #  8     char[] strCatMix(char[] lhs, char[] rhs)
> #  9     {
> # 10         return lhs ~ S ~ rhs;
> # 11     }
> # 12 }
> # 13
>
> Compiling this with w/ dmd, I get:
>
> listtest.d(6): Error: integral type expected for value-parameter, not char[]
>
> As far as I can tell from the D website, this should be perfectly fine, as it doesn't mention anywhere that the value-parameter must be an int.   Is the documentation just ambiguous, or is this not yet finished in the compiler?
>
> 	-- Daniel

I suspect dmd is expecting only one argument for the function strCatMix(),
as you only gave one for the template. How are you expecting to use this?
I dont know where dmd gets all its ints from, unfortunatly.

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
November 17, 2004
Well, it doesn't make any difference if I change the name of the function/template.  Basically, it's for a functional library I'm toying with.  It's used like this:

alias strCatMix!(", ")		StrCatComma;
stdout.writeLine("["~StrReduce(&StrCatComma, someListOfStrings)~"]");

Or something like that :P  As near as I can see, the number of parameters for the template doesn't have any bearing on the number of parameters in the function.

	-- Daniel

Simon Buchan wrote:
> On Thu, 18 Nov 2004 00:50:34 +1100, Daniel Keep  <daniel.keep@dummy.gmail.com> wrote:
> 
>> I think I might have found a(nother) bug :P
>>
>> #  5
>> #  6 template strCatMix(char[] S = " ")
>> #  7 {
>> #  8     char[] strCatMix(char[] lhs, char[] rhs)
>> #  9     {
>> # 10         return lhs ~ S ~ rhs;
>> # 11     }
>> # 12 }
>> # 13
>>
>> Compiling this with w/ dmd, I get:
>>
>> listtest.d(6): Error: integral type expected for value-parameter, not  char[]
>>
>> As far as I can tell from the D website, this should be perfectly fine,  as it doesn't mention anywhere that the value-parameter must be an int.    Is the documentation just ambiguous, or is this not yet finished in  the compiler?
>>
>>     -- Daniel
> 
> 
> I suspect dmd is expecting only one argument for the function strCatMix(),
> as you only gave one for the template. How are you expecting to use this?
> I dont know where dmd gets all its ints from, unfortunatly.
> 
November 17, 2004
> #  6 template strCatMix(char[] S = " ")

Hi there.
Never wrote a D program in my life, but I've noticed that char[] is actually
an array, so it's certainly not a "single value".

What happens if you write (char* S =" "), then it's a value. D supports C-style pointers, right?

Lionello.


November 17, 2004
On Thu, 18 Nov 2004 01:16:04 +1100, Daniel Keep <daniel.keep@dummy.gmail.com> wrote:

I think this is your problem: (from http://www.digitalmars.com/d/index.html, frame http://www.digitalmars.com/d/template.html)

Implicit Template Properties
If a template has exactly one member in it, and the name of that member is the same as the template name, that member is assumed to be referred to in a template instantiation:

template Foo(T)
{
    T Foo;// declare variable Foo of type T
}

void test()
{
    Foo!(int) = 6;// instead of Foo!(int).Foo
}

Haven't any idea what using & on a template instance would do...

> Well, it doesn't make any difference if I change the name of the function/template.  Basically, it's for a functional library I'm toying with.  It's used like this:
>
> alias strCatMix!(", ")		StrCatComma;
> stdout.writeLine("["~StrReduce(&StrCatComma, someListOfStrings)~"]");
>
> Or something like that :P  As near as I can see, the number of parameters for the template doesn't have any bearing on the number of parameters in the function.
>
> 	-- Daniel
>
> Simon Buchan wrote:
>> On Thu, 18 Nov 2004 00:50:34 +1100, Daniel Keep  <daniel.keep@dummy.gmail.com> wrote:
>>
>>> I think I might have found a(nother) bug :P
>>>
>>> #  5
>>> #  6 template strCatMix(char[] S = " ")
>>> #  7 {
>>> #  8     char[] strCatMix(char[] lhs, char[] rhs)
>>> #  9     {
>>> # 10         return lhs ~ S ~ rhs;
>>> # 11     }
>>> # 12 }
>>> # 13
>>>
>>> Compiling this with w/ dmd, I get:
>>>
>>> listtest.d(6): Error: integral type expected for value-parameter, not  char[]
>>>
>>> As far as I can tell from the D website, this should be perfectly fine,  as it doesn't mention anywhere that the value-parameter must be an int.    Is the documentation just ambiguous, or is this not yet finished in  the compiler?
>>>
>>>     -- Daniel
>>   I suspect dmd is expecting only one argument for the function strCatMix(),
>> as you only gave one for the template. How are you expecting to use this?
>> I dont know where dmd gets all its ints from, unfortunatly.
>>



-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
November 17, 2004
In article <cnfkpe$ud3$1@digitaldaemon.com>, Daniel Keep says...
>[...]
>listtest.d(6): Error: integral type expected for value-parameter, not char[]
>
>As far as I can tell from the D website, this should be perfectly fine, as it doesn't mention anywhere that the value-parameter must be an int.
>  Is the documentation just ambiguous, or is this not yet finished in
>the compiler?

It's hard to tell. The docs do not mention anything about it, but all the given examples are ints. I also hope there will be support for other types in the future, since there doesn't seem to be any clear reason why value parameters should be restricted to integers.

Here is the test case I used:

# import std.stdio;
#
# template hello(char[] s)
# {
#   void hello() { writefln(s); }
# }
#
# void main()
# {
#   hello!("Hello World!");
# }

It gives exactly the same error that you got.

Nick


November 17, 2004
In article <cnfkpe$ud3$1@digitaldaemon.com>, Daniel Keep says...
>
>I think I might have found a(nother) bug :P
>
>#  5
>#  6 template strCatMix(char[] S = " ")
>#  7 {
>#  8     char[] strCatMix(char[] lhs, char[] rhs)
>#  9     {
># 10         return lhs ~ S ~ rhs;
># 11     }
># 12 }
># 13
>
>Compiling this with w/ dmd, I get:
>
>listtest.d(6): Error: integral type expected for value-parameter, not char[]
>
>As far as I can tell from the D website, this should be perfectly fine, as it doesn't mention anywhere that the value-parameter must be an int.
>  Is the documentation just ambiguous, or is this not yet finished in
>the compiler?

This is correct behavior.  Since templates are evaluated at compile-time, you need to provide something that can be evaluated at compile-time such as an integer constant, etc.  C++ allows you to use static arrays as template parameters like so:

# template<char* T>
# class C
# {
#     char* val;
# public:
#     C() : val(T) {}
# };
#
# char x[] = "hi";
#
# C<x> c;

But D does not allow this.  Instead you need to use an alias parameter:

# template t(alias T)
# {
#     alias T val;
# }
#
# static char[] x = "hi";
#
# void main()
# {
#     printf( "%.*s\n", t!(x).val );
# }

Note that this is functionally the same as the C++ approach, as the char array must be a static global variable.


Sean


November 17, 2004
On Thu, 18 Nov 2004 01:16:04 +1100, Daniel Keep <daniel.keep@dummy.gmail.com> wrote:
> Well, it doesn't make any difference if I change the name of the function/template.  Basically, it's for a functional library I'm toying with.  It's used like this:
>
> alias strCatMix!(", ")		StrCatComma;
> stdout.writeLine("["~StrReduce(&StrCatComma, someListOfStrings)~"]");
>
> Or something like that :P  As near as I can see, the number of parameters for the template doesn't have any bearing on the number of parameters in the function.

Try this:

import std.stdio;

template test(T:T[],alias S) {
  char[] foo(T[] a, T[] b) {
    return a ~ S ~ b;
  }
}

char[] comma = ", ";
alias test!(char[],comma).foo catComma;

void main()
{
	char[] a = "Heath";
	char[] b = "Regan";
	
	writef(catComma(a,b));
}

Regan
November 17, 2004
On Wed, 17 Nov 2004 16:11:58 +0200, Lionello Lunesu <lionello.lunesu@crystalinter.remove.com> wrote:
>> #  6 template strCatMix(char[] S = " ")
>
> Hi there.
> Never wrote a D program in my life, but I've noticed that char[] is actually
> an array, so it's certainly not a "single value".
>
> What happens if you write (char* S =" "), then it's a value. D supports
> C-style pointers, right?

Yes, but..

import std.stdio;

template test(char *S = ", ") {
  char[] foo(char[] a, char[] b) {
    return a ~ S ~ b;
  }
}

alias test!().foo catComma;

void main()
{
	char[] a = "Heath";
	char[] b = "Regan";
	
	writef(catComma(a,b));
}

strcat.d(3): Error: integral type expected for value-parameter, not char*

apparently 'char *' is not an integral type.

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
November 18, 2004
"Daniel Keep" <daniel.keep@dummy.gmail.com> escribió en el mensaje
news:cnfkpe$ud3$1@digitaldaemon.com...
|I think I might have found a(nother) bug :P
|
| #  5
| #  6 template strCatMix(char[] S = " ")
| #  7 {
| #  8     char[] strCatMix(char[] lhs, char[] rhs)
| #  9     {
| # 10         return lhs ~ S ~ rhs;
| # 11     }
| # 12 }
| # 13
|
| Compiling this with w/ dmd, I get:
|
| listtest.d(6): Error: integral type expected for value-parameter, not char[]
|
| As far as I can tell from the D website, this should be perfectly fine,
| as it doesn't mention anywhere that the value-parameter must be an int.
|  Is the documentation just ambiguous, or is this not yet finished in
| the compiler?
|
| -- Daniel

From template.html (a couple of paragraphs below the grammar specification):

"Template parameters can be types, values, or symbols. Types can be any type. Value parameters must be of an integral type, and specializations for them must resolve to an integral constant. Symbols can be any non-local symbol."

-----------------------
Carlos Santander Bernal