www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - const argument

reply TSalm <TSalm free.fr> writes:
Hello,

Is there a way to specifie a constant argument ( I would say an argument  
for which his value is evaluate at compile time )

For example, something like this :

/* -------- CODE --------- */
import tango.io.Stdout;

void func(const bool constArg)
{
     static if (constArg)
         Stdout("Yes").newline;
     else
         Stdout("No").newline;
}

void main()
{
     func( true );
     func( false );
}
/* -------- END CODE --------- */

Thanks in advance,
TSalm
Mar 28 2009
parent reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Sat, Mar 28, 2009 at 6:12 AM, TSalm <TSalm free.fr> wrote:
 Hello,

 Is there a way to specifie a constant argument ( I would say an argument =
for
 which his value is evaluate at compile time )

 For example, something like this :

 /* -------- CODE --------- */
 import tango.io.Stdout;

 void func(const bool constArg)
 {
 =A0 =A0static if (constArg)
 =A0 =A0 =A0 =A0Stdout("Yes").newline;
 =A0 =A0else
 =A0 =A0 =A0 =A0Stdout("No").newline;
 }

 void main()
 {
 =A0 =A0func( true );
 =A0 =A0func( false );
 }
 /* -------- END CODE --------- */
You have to do it with templates: void func(bool constArg)() { static if(constArg) ... else ... } func!(true)(); func!(false)(); Walter suggested, in the D2 presentation at the conference in 2007, that there should be "static" parameters which would work the way your code should work.
Mar 28 2009
parent reply TSalm <TSalm free.fr> writes:
Le Sat, 28 Mar 2009 12:21:52 +0100, Jarrett Billingsley  
<jarrett.billingsley gmail.com> a écrit:

 On Sat, Mar 28, 2009 at 6:12 AM, TSalm <TSalm free.fr> wrote:
 Hello,

 Is there a way to specifie a constant argument ( I would say an  
 argument for
 which his value is evaluate at compile time )

 For example, something like this :

 /* -------- CODE --------- */
 import tango.io.Stdout;

 void func(const bool constArg)
 {
    static if (constArg)
        Stdout("Yes").newline;
    else
        Stdout("No").newline;
 }

 void main()
 {
    func( true );
    func( false );
 }
 /* -------- END CODE --------- */
You have to do it with templates: void func(bool constArg)() { static if(constArg) ... else ... } func!(true)(); func!(false)();
Thanks.
 Walter suggested, in the D2 presentation at the conference in 2007,
 that there should be "static" parameters which would work the way your
 code should work.
good idea IMO.
Mar 28 2009
parent TSalm <TSalm free.fr> writes:
Le Sat, 28 Mar 2009 17:52:54 +0100, TSalm <TSalm free.fr> a écrit:

 Le Sat, 28 Mar 2009 12:21:52 +0100, Jarrett Billingsley  
 <jarrett.billingsley gmail.com> a écrit:

 On Sat, Mar 28, 2009 at 6:12 AM, TSalm <TSalm free.fr> wrote:
 Hello,

 Is there a way to specifie a constant argument ( I would say an  
 argument for
 which his value is evaluate at compile time )

 For example, something like this :

 /* -------- CODE --------- */
 import tango.io.Stdout;

 void func(const bool constArg)
 {
    static if (constArg)
        Stdout("Yes").newline;
    else
        Stdout("No").newline;
 }

 void main()
 {
    func( true );
    func( false );
 }
 /* -------- END CODE --------- */
You have to do it with templates: void func(bool constArg)() { static if(constArg) ... else ... } func!(true)(); func!(false)();
Thanks.
 Walter suggested, in the D2 presentation at the conference in 2007,
 that there should be "static" parameters which would work the way your
 code should work.
good idea IMO.
I've just think that, since templated methods don't treat with respect rules of object inheritance, this could be a cause of mistakes...
Mar 29 2009