View mode: basic / threaded / horizontal-split · Log in · Help
October 27, 2005
Re: The power of static if! (A bit of fun)
In article <djq81i$1i11$1@digitaldaemon.com>, Don Clugston says...
>
>Indeed. Even better would be if a constant index of an array of 
>constants, was a literal.
>Then digittostr would just be
>
>template digittostr(int n) {
>   const char [] digittostr="0123456789"[n];
>}
>
>at which point it becomes a genuine functional programming language, 
>because you can manipulate lists. (Right now I've shown you can create 
>an arbitrary list, but AFAIK you can't peek inside it).
>But maybe there's some syntax it will accept right now.

This would be extremely cool.  And it seems like it shouldn't be terribly
difficult to implement, since ~ is already supported in this context.


Sean
October 27, 2005
Re: The power of static if! (A bit of fun)
In article <djq9pn$1jo5$1@digitaldaemon.com>, Ivan Senji says...
>
>But this works :)
>
>template digittostr(int n)
>{
>   const char[] digittostr;
>   static this()
>   {
>     digittostr = "0123456789"[n..n+1];
>   }
>}

Another surprise.  So constants can be initialized at run time via static this?
I thought the format always had to be:

const T t = val;

Interesting.


Sean
October 28, 2005
Re: The power of static if! (A bit of fun)
"Ivan Senji" <ivan.senji_REMOVE_@_THIS__gmail.com> wrote in message
news:djq3r9$1dfh$1@digitaldaemon.com...
> And when that promotion bug is fixed so things can be
> writen this way:

I'm not familiar with that bug?
October 28, 2005
Re: The power of static if! (A bit of fun)
"Don Clugston" <dac@nospam.com.au> wrote in message
news:djput7$1822$1@digitaldaemon.com...
> I've also made a template that calculates pi at compile time by summing
> a power series. I have hopes that I can make it work to arbitrary
> precision (it only does reals right now).
>
> Seriously, this compiler is *far* more advanced than the programming
> techniques we've developed to use it. Now that we know that a
> compile-time itoa() is possible, and even a basic compile-time printf,
> I can imagine a whole compile-time library.

This is perfect stuff for a magazine article. If you want to prepare one, I
can get you in with the publisher(s).
October 28, 2005
Re: The power of static if! (A bit of fun)
Walter Bright wrote:
> "Ivan Senji" <ivan.senji_REMOVE_@_THIS__gmail.com> wrote in message
> news:djq3r9$1dfh$1@digitaldaemon.com...
> 
>>And when that promotion bug is fixed so things can be
>>writen this way:
> 
> 
> I'm not familiar with that bug?
> 

Sorry not a bug, you said it was a limitation.

template factorial(int n)
{
  static if (1) {
      enum { factorial = n * .factorial!(n-1) }
  }
}

No promotion when using static if.
October 29, 2005
Re: The power of static if! (A bit of fun)
"Ivan Senji" <ivan.senji_REMOVE_@_THIS__gmail.com> wrote in message
news:dju3t8$2ida$1@digitaldaemon.com...
> Sorry not a bug, you said it was a limitation.
>
> template factorial(int n)
> {
>    static if (1) {
>        enum { factorial = n * .factorial!(n-1) }
>    }
> }
>
> No promotion when using static if.

I'm still not sure what you mean. Can you refresh my memory?
October 29, 2005
Re: The power of static if! (A bit of fun)
Walter Bright wrote:
> "Ivan Senji" <ivan.senji_REMOVE_@_THIS__gmail.com> wrote in message
> news:dju3t8$2ida$1@digitaldaemon.com...
> 
>>Sorry not a bug, you said it was a limitation.
>>
>>template factorial(int n)
>>{
>>   static if (1) {
>>       enum { factorial = n * .factorial!(n-1) }
>>   }
>>}
>>
>>No promotion when using static if.
> 
> 
> I'm still not sure what you mean. Can you refresh my memory?
> 

Sorry again, i don't know why i called it promotion, i should have said 
implicit template properties. Simpler example:

template Foo(int n)
{
  static if(n==0) const int Foo;
  else static if(n==1) const float Foo;
  else const char[] Foo;
}

writefln(Foo!(1).Foo); //works
writefln(Foo!(1));     //voids have no value
October 29, 2005
Re: The power of static if! (A bit of fun)
"Ivan Senji" <ivan.senji_REMOVE_@_THIS__gmail.com> wrote in message
news:djv7p7$gkq$1@digitaldaemon.com...
> Sorry again, i don't know why i called it promotion, i should have said
> implicit template properties. Simpler example:
>
> template Foo(int n)
> {
>    static if(n==0) const int Foo;
>    else static if(n==1) const float Foo;
>    else const char[] Foo;
> }
>
> writefln(Foo!(1).Foo); //works
> writefln(Foo!(1));     //voids have no value

Ok, I understand now. Don also posted this issue.
October 29, 2005
Re: The power of static if! (A bit of fun)
Walter Bright wrote:
> "Don Clugston" <dac@nospam.com.au> wrote in message 
> news:djput7$1822$1@digitaldaemon.com...
> 
>> I've also made a template that calculates pi at compile time by
>> summing a power series. I have hopes that I can make it work to
>> arbitrary precision (it only does reals right now).
>> 
>> Seriously, this compiler is *far* more advanced than the
>> programming techniques we've developed to use it. Now that we know
>> that a compile-time itoa() is possible, and even a basic
>> compile-time printf, I can imagine a whole compile-time library.
> 
> This is perfect stuff for a magazine article. If you want to prepare
> one, I can get you in with the publisher(s).

Guys, this is just awesome!

It took Don to show us what Walter has created!

I'm getting the impression it's time to throw my meta stuff out the 
window. ;-(
October 29, 2005
Re: The power of static if! (A bit of fun)
On Sun, 30 Oct 2005 01:38:17 +0300, Georg Wrede wrote:

> Walter Bright wrote:
>> "Don Clugston" <dac@nospam.com.au> wrote in message 
>> news:djput7$1822$1@digitaldaemon.com...
>> 
>>> I've also made a template that calculates pi at compile time by
>>> summing a power series. I have hopes that I can make it work to
>>> arbitrary precision (it only does reals right now).
>>> 
>>> Seriously, this compiler is *far* more advanced than the
>>> programming techniques we've developed to use it. Now that we know
>>> that a compile-time itoa() is possible, and even a basic
>>> compile-time printf, I can imagine a whole compile-time library.
>> 
>> This is perfect stuff for a magazine article. If you want to prepare
>> one, I can get you in with the publisher(s).
> 
> Guys, this is just awesome!
> 
> It took Don to show us what Walter has created!
> 
> I'm getting the impression it's time to throw my meta stuff out the 
> window. ;-(

Ok, I'll expose my ignorance here, but why is this such a fanfare item? Its
just a fancy way to declare literals, no? And who needs PI calculated at
compile time anyway - just hard code the literal - it ain't changing
anytime soon. What am I missing?

-- 
Derek Parnell
Melbourne, Australia
30/10/2005 10:01:06 AM
1 2 3 4
Top | Discussion index | About this forum | D home