digitalmars.D.learn - template parameters
- Ellery Newcomer (6/6) Sep 25 2009 Hello.
- BCS (4/16) Sep 25 2009 That is correct. it would seem that expression should be used, but that ...
- Ellery Newcomer (5/26) Sep 25 2009 Okay, let me rephrase that:
- Christopher Wright (12/17) Sep 25 2009 void foo(T : int)(T value)
- Ellery Newcomer (3/25) Sep 25 2009 Great, it does something specified for a type parameter.
- Jarrett Billingsley (8/33) Sep 25 2009 r
- BCS (4/33) Sep 25 2009
Hello. Today I was looking at the grammar for template value parameters and realized I didn't know what the conditional expression is for in TemplateValueParameter => BasicType Declarator : ConditionalExpression So what does it do? (I assume = ConditionalExpression is the default value for the parameter)
Sep 25 2009
Hello Ellery,Hello. Today I was looking at the grammar for template value parameters and realized I didn't know what the conditional expression is for in TemplateValueParameter => BasicType Declarator : ConditionalExpression So what does it do? (I assume = ConditionalExpression is the default value for the parameter)That is correct. it would seem that expression should be used, but that includes an assignment expression so the next things down is used. http://www.digitalmars.com/d/1.0/expression.html#AssignExpression
Sep 25 2009
BCS wrote:Hello Ellery,Okay, let me rephrase that: When you have a <something> preceded by a colon in a template value parameter, what are its semantics? Is it the same as a default parameter value? Is it some sort of constraining expression? Is it a vestige?Hello. Today I was looking at the grammar for template value parameters and realized I didn't know what the conditional expression is for in TemplateValueParameter => BasicType Declarator : ConditionalExpression So what does it do? (I assume = ConditionalExpression is the default value for the parameter)That is correct. it would seem that expression should be used, but that includes an assignment expression so the next things down is used. http://www.digitalmars.com/d/1.0/expression.html#AssignExpression
Sep 25 2009
Ellery Newcomer wrote:Okay, let me rephrase that: When you have a <something> preceded by a colon in a template value parameter, what are its semantics? Is it the same as a default parameter value? Is it some sort of constraining expression? Is it a vestige?void foo(T : int)(T value) { // value is implicitly convertible to int } It can do some pattern matching: void foo(T : V[U], V, U)(T dictionary) { // I have an associative array } In D2, it's usually going to be easier to use constraints: template foo(T) if (isAssociativeArray!T) {}
Sep 25 2009
Christopher Wright wrote:Ellery Newcomer wrote:Great, it does something specified for a type parameter. Now what does it do for a VALUE parameter?Okay, let me rephrase that: When you have a <something> preceded by a colon in a template value parameter, what are its semantics? Is it the same as a default parameter value? Is it some sort of constraining expression? Is it a vestige?void foo(T : int)(T value) { // value is implicitly convertible to int } It can do some pattern matching: void foo(T : V[U], V, U)(T dictionary) { // I have an associative array } In D2, it's usually going to be easier to use constraints: template foo(T) if (isAssociativeArray!T) {}
Sep 25 2009
On Fri, Sep 25, 2009 at 6:48 PM, Ellery Newcomer <ellery-newcomer utulsa.edu> wrote:Christopher Wright wrote:rEllery Newcomer wrote:Okay, let me rephrase that: When you have a <something> preceded by a colon in a template value parameter, what are its semantics? Is it the same as a default paramete=Specialization for particular values. It's not really that useful in the face of static if. template fib(int n : 0) { const fib =3D 1; } template fib(int n : 1) { const fib =3D 1; } template fib(int n) { const fib =3D fib!(n - 2) + fib!(n - 1); }Great, it does something specified for a type parameter. Now what does it do for a VALUE parameter?value? Is it some sort of constraining expression? Is it a vestige?void foo(T : int)(T value) { =A0 =A0 // value is implicitly convertible to int } It can do some pattern matching: void foo(T : V[U], V, U)(T dictionary) { =A0 =A0 // I have an associative array } In D2, it's usually going to be easier to use constraints: template foo(T) if (isAssociativeArray!T) {}
Sep 25 2009
Hello Ellery,BCS wrote:You should check me on this but I think it's explicit instantiation: "This is the special case for when given some value".Hello Ellery,Okay, let me rephrase that: When you have a <something> preceded by a colon in a template value parameter, what are its semantics? Is it the same as a default parameter value? Is it some sort of constraining expression? Is it a vestige?Hello. Today I was looking at the grammar for template value parameters and realized I didn't know what the conditional expression is for in TemplateValueParameter => BasicType Declarator : ConditionalExpression So what does it do? (I assume = ConditionalExpression is the default value for the parameter)That is correct. it would seem that expression should be used, but that includes an assignment expression so the next things down is used. http://www.digitalmars.com/d/1.0/expression.html#AssignExpression
Sep 25 2009