May 13, 2005
Just a thought. Instead of using static, would using UPPERCASE for static keywords be easier on the eyes? A bit like a C preprocessor macro.

template Integer(int nbits)
{
    IF (nbits <= 8)
       alias byte Integer;
    ELSE IF (nbits <= 16)
       alias short Integer;
    ELSE IF (nbits <= 32)
       alias int Integer;
    ELSE IF (nbits <= 64)
       alias long Integer;
    ELSE
       ASSERT(0);
}

Walter wrote:
> "Bill Baxter" <Bill_member@pathlink.com> wrote in message
> news:d5c1mu$640$1@digitaldaemon.com...
> 
>>Yikes!  All that is, really, is just an if-else, but dressed up in C++
> 
> template
> 
>>metaprogramming it takes about a page of code!  My thought is that if the
>>language actually had first-class support for metaprogramming, then you
> 
> could
> 
>>just do something like write a short metafunction that returns a type:
>>
>>metafun type integer(int numbits)
>>{
>>if (numbits<=sizeof(char)) return char;
>>if (numbits<=sizeof(short)) return short;
>>if (numbits<=sizeof(int)) return int;
>>if (numbits<=sizeof(long)) return long;
>>if (numbits<=sizeof(cent)) return cent;
>>
>>metathrow "Compiler error";
>>}
> 
> 
> You've inspired me:
> 
> template Integer(int nbits)
> {
>     static if (nbits <= 8)
>        alias byte Integer;
>     else static if (nbits <= 16)
>        alias short Integer;
>     else static if (nbits <= 32)
>        alias int Integer;
>     else static if (nbits <= 64)
>        alias long Integer;
>     else
>        static assert(0);
> }
> 
> int main()
> {
>     Integer!(8) i ;
>     Integer!(16) j ;
>     Integer!(29) k ;
>     Integer!(64) l ;
>     printf("%d %d %d %d\n", i.sizeof, j.sizeof, k.sizeof, l.sizeof);
>     return 0;
> }
> 
> 
May 14, 2005
"Walter" <newshound@digitalmars.com> wrote in message news:d62m56$2h67$1@digitaldaemon.com...
>
> "Bill Baxter" <Bill_member@pathlink.com> wrote in message news:d5c1mu$640$1@digitaldaemon.com...
>> Yikes!  All that is, really, is just an if-else, but dressed up in C++
> template
>> metaprogramming it takes about a page of code!  My thought is that if the language actually had first-class support for metaprogramming, then you
> could
>> just do something like write a short metafunction that returns a type:
>>
>> metafun type integer(int numbits)
>> {
>> if (numbits<=sizeof(char)) return char;
>> if (numbits<=sizeof(short)) return short;
>> if (numbits<=sizeof(int)) return int;
>> if (numbits<=sizeof(long)) return long;
>> if (numbits<=sizeof(cent)) return cent;
>>
>> metathrow "Compiler error";
>> }
>
> You've inspired me:
>
> template Integer(int nbits)
> {
>    static if (nbits <= 8)
>       alias byte Integer;
>    else static if (nbits <= 16)
>       alias short Integer;
>    else static if (nbits <= 32)
>       alias int Integer;
>    else static if (nbits <= 64)
>       alias long Integer;
>    else
>       static assert(0);
> }
>
> int main()
> {
>    Integer!(8) i ;
>    Integer!(16) j ;
>    Integer!(29) k ;
>    Integer!(64) l ;
>    printf("%d %d %d %d\n", i.sizeof, j.sizeof, k.sizeof, l.sizeof);
>    return 0;
> }
>

Does that mean we'll be able to do something like this?

;---
import std.stdio;

struct S
{
    double x = 0, y = 0;
    static S opCall(int i) { S r; r.x = i; return r; }
    S opMul(S s)
    {
        S r;
        r.x = x * s.x;
        r.y = y * s.y;
        return r;
    }
}

template sqr(F) { F sqr(F x) { return x * x; } }

template pow(F) {
    F pow(F x, int i) {
        if(i < 1)
        {
            // sure would be nice if there was a 'TypeInfo.udt' property or
some such..
            static if(typeid(F) == typeid(int)
                   || typeid(F) == typeid(float)
                   || typeid(F) == typeid(double)
                   || typeid(F) == typeid(real))
                return 1;
            else return F(1);
        }
        if(i & 1) if(i == 1) return x; else return x * pow(x,i-1);
        return sqr!(F)(pow(x,i/2));
    }
}

void main()
{
    S s = S(10);
    S val = pow!(S)(s,2);
    writefln("x = %2.2f, y = %2.2f", val.x, val.y);
}


May 14, 2005
Walter wrote:
> 
> "Bill Baxter" <Bill_member@pathlink.com> wrote in message news:d5c1mu$640$1@digitaldaemon.com...
>> Yikes!  All that is, really, is just an if-else, but dressed up in C++
> template
>> metaprogramming it takes about a page of code!  My thought is that if the language actually had first-class support for metaprogramming, then you
> could
>> just do something like write a short metafunction that returns a type:
>>
>> metafun type integer(int numbits)
>> {
>> if (numbits<=sizeof(char)) return char;
>> if (numbits<=sizeof(short)) return short;
>> if (numbits<=sizeof(int)) return int;
>> if (numbits<=sizeof(long)) return long;
>> if (numbits<=sizeof(cent)) return cent;
>>
>> metathrow "Compiler error";
>> }
> 
> You've inspired me:
> 
> template Integer(int nbits)
> {
>     static if (nbits <= 8)
>        alias byte Integer;
>     else static if (nbits <= 16)
>        alias short Integer;
>     else static if (nbits <= 32)
>        alias int Integer;
>     else static if (nbits <= 64)
>        alias long Integer;
>     else
>        static assert(0);
> }
> 
> int main()
> {
>     Integer!(8) i ;
>     Integer!(16) j ;
>     Integer!(29) k ;
>     Integer!(64) l ;
>     printf("%d %d %d %d\n", i.sizeof, j.sizeof, k.sizeof, l.sizeof);
>     return 0;
> }

It looks really good. In C++ there is two *differect* styles for normal
programing (imperative style) and metaprograming (functional style by means
of haskell). Just compare function that evaluates factorial and template
template that evaluates factorial. It's cool to have ONE approach for
programing and metaprograming in D.
The only thing is that 'static' keyword seems to be overused. It has at
least three different meanings in D, and even more in other languages. What
about using something like 'meta' or 'compile_time', or UPPERCASE letters ?

-- 
          Vladimir
May 14, 2005
>It looks really good. In C++ there is two *differect* styles for normal programing (imperative style) and metaprograming (functional style by means of haskell). Just compare function that evaluates factorial and template template that evaluates factorial. It's cool to have ONE approach for programing and metaprograming in D.

I don't get you. Have you ever seen any functional language without an if
statement? If is nigther functional nor imperative.
And as long as there is nothing like a compile-time variable that can be
re-assigned the style will stay functional and not imperative.



May 14, 2005
Walter wrote:
> "Bill Baxter" <Bill_member@pathlink.com> wrote in message
> news:d5c1mu$640$1@digitaldaemon.com...
> 
>>Yikes!  All that is, really, is just an if-else, but dressed up in C++
> 
> template
> 
>>metaprogramming it takes about a page of code!  My thought is that if the
>>language actually had first-class support for metaprogramming, then you
> 
> could
> 
>>just do something like write a short metafunction that returns a type:
>>
>>metafun type integer(int numbits)
>>{
>>if (numbits<=sizeof(char)) return char;
>>if (numbits<=sizeof(short)) return short;
>>if (numbits<=sizeof(int)) return int;
>>if (numbits<=sizeof(long)) return long;
>>if (numbits<=sizeof(cent)) return cent;
>>
>>metathrow "Compiler error";
>>}
> 
> 
> You've inspired me:
> 
> template Integer(int nbits)
> {
>     static if (nbits <= 8)
>        alias byte Integer;
>     else static if (nbits <= 16)
>        alias short Integer;
>     else static if (nbits <= 32)
>        alias int Integer;
>     else static if (nbits <= 64)
>        alias long Integer;
>     else
>        static assert(0);
> }

I think this is showing up a previous design error.  This use of static isn't logically consistent:

    if (nbits <= 8)
    static if (nbits <= 8)

Versus:

    int c = 4;
    static int c = 4;

So this should be "const" instead.  Rather than overloading the meaning of the keyword as is done with static, it would just be extending it to act a little more uniform.
May 14, 2005
"Burton Radons" <burton-radons@smocky.com> wrote in message news:d65at9$1flf$1@digitaldaemon.com...
> I think this is showing up a previous design error.  This use of static isn't logically consistent:
>
>      if (nbits <= 8)
>      static if (nbits <= 8)
>
> Versus:
>
>      int c = 4;
>      static int c = 4;
>
> So this should be "const" instead.  Rather than overloading the meaning of the keyword as is done with static, it would just be extending it to act a little more uniform.

I thought it would be consistent with the use of assert(exp) versus static
assert(exp).


May 14, 2005
Walter wrote:

> "Burton Radons" <burton-radons@smocky.com> wrote in message
> news:d65at9$1flf$1@digitaldaemon.com...
> 
>>I think this is showing up a previous design error.  This use of static
>>isn't logically consistent:
>>
>>     if (nbits <= 8)
>>     static if (nbits <= 8)
>>
>>Versus:
>>
>>     int c = 4;
>>     static int c = 4;
>>
>>So this should be "const" instead.  Rather than overloading the meaning
>>of the keyword as is done with static, it would just be extending it to
>>act a little more uniform.
> 
> 
> I thought it would be consistent with the use of assert(exp) versus static
> assert(exp).

No, no, I mean that "static assert" was the wrong design because it shows an inconsistency when extended to apply to more statements.  So it should be "const assert" as well as "const if" instead, while "static assert" should be an error.
May 15, 2005
In article <d636va$2v71$1@digitaldaemon.com>, Walter says...
>
>
>"Thomas Kuehne" <thomas-dloop@kuehne.thisisspam.cn> wrote in message news:0lofl2-jd3.ln1@lnews.kuehne.cn...
>> .123 doesn't support this but I see no reason why "static if" would be hard to implement. Just enhance the version/debug condition implementation a bit and you get the same features as as "static if".
>
>Yup, that's nearly all there is to it. What's interesting, though, it how it transforms template metaprogramming. C++ template metaprogramming was 'discovered' rather than designed, and as Bill Baxter's reference shows, it is very hard to use.

Does this paragraph mean you can do metaprogamming with static if, or am I reading too much into this?

Kevin


May 15, 2005
"Burton Radons" <burton-radons@smocky.com> wrote in message news:d65uvo$1sv1$1@digitaldaemon.com...
> No, no, I mean that "static assert" was the wrong design because it shows an inconsistency when extended to apply to more statements.  So it should be "const assert" as well as "const if" instead, while "static assert" should be an error.

"static assert" comes from C++ terminology for the same thing.


May 15, 2005
"Dave" <Dave_member@pathlink.com> wrote in message news:d63ssp$cqs$1@digitaldaemon.com...
> Does that mean we'll be able to do something like this?

Yes.