Jump to page: 1 2 3
Thread overview
Constants, Aliases
Dec 14, 2006
Xinok
Dec 14, 2006
Bill Baxter
Dec 14, 2006
nazo
Suggestion: alias should be single tuple type
Dec 14, 2006
nazo
Dec 14, 2006
Bill Baxter
Dec 14, 2006
John Reimer
Dec 15, 2006
nazo
[Patch] Variable Template
Dec 15, 2006
nazo
Dec 14, 2006
Alexander Panek
Dec 15, 2006
janderson
Dec 16, 2006
Alexander Panek
Dec 14, 2006
Lutger
Dec 14, 2006
Alexander Panek
Dec 14, 2006
Hasan Aljudy
Dec 14, 2006
Alexander Panek
Dec 14, 2006
Lutger
Dec 14, 2006
Hasan Aljudy
Dec 14, 2006
Lutger
Dec 14, 2006
Don Clugston
Dec 14, 2006
Lutger
Dec 14, 2006
Don Clugston
Dec 14, 2006
Lutger
Dec 14, 2006
Sean Kelly
Dec 14, 2006
Hasan Aljudy
December 14, 2006
In C++, you can define a constant by using a preprocessor: #define INT 30

In D, the standard is to use 'const':
const int INT = 30;

The problem with the const keyword is it doesn't guarantee the expression will
be constant.
const int* PTR = new int; // This technically isn't a constant, the compiler
just doesn't allow you to modify it's value.

Another solution is to use enumerators, but they don't aloow any type other
than int:
enum { val = 30 } // OK
enum { str = "Hello" } // Error


So I was thinking, why not give this job to aliases? Aliases must be constant,
you can use any type with them, and they're easy to write.
alias 30 VAL;
alias "Hello" STR;
alias 31.5 DEC;

Expressions can simply be put within parenthesis:
alias (15 * 99) EXP;
December 14, 2006
Xinok wrote:
> In C++, you can define a constant by using a preprocessor:
> #define INT 30
> 
> In D, the standard is to use 'const':
> const int INT = 30;
> 
> The problem with the const keyword is it doesn't guarantee the expression will
> be constant.
> const int* PTR = new int; // This technically isn't a constant, the compiler
> just doesn't allow you to modify it's value.
> 
> Another solution is to use enumerators, but they don't aloow any type other
> than int:
> enum { val = 30 } // OK
> enum { str = "Hello" } // Error
> 
> 
> So I was thinking, why not give this job to aliases? Aliases must be constant,
> you can use any type with them, and they're easy to write.
> alias 30 VAL;
> alias "Hello" STR;
> alias 31.5 DEC;
> 
> Expressions can simply be put within parenthesis:
> alias (15 * 99) EXP;

It would also make writing recursive templates a little bit simpler. Sometimes it takes me a minute to figure out whether I need
   alias template_name[1..$] template_name;
or
   const Type template_name = template_name[1..$];

Actually... I guess they're interchangeable till you get to the base case -- ferinstance:

template tuple_all_of(T, S...) {
    static if(S.length == 0) {
        const bool tuple_all_of = true;
    }
    else static if( is(S[0]: T) ) {
	// either version is ok here
        //const bool tuple_all_of = tuple_all_of!(T, S[1..$]);
        alias tuple_all_of!(T, S[1..$]) tuple_all_of;
    } else {
        const bool tuple_all_of = false;
    }
}


--bb
December 14, 2006
Expressions! Expressions!  (/ ~.)/

I think that the exchange of parameters of alias is necessary from:
  alias (x*2) exp;
to:
  alias exp=x*2;

And I need alias arguments as syntax sugar like:
  alias(alias i) exp=i*2;
same as:
  template exp(alias i){
    alias exp=i*2;
  }

#sorry for my poor English.
December 14, 2006
Xinok wrote:
> In C++, you can define a constant by using a preprocessor:
> #define INT 30
> 
> In D, the standard is to use 'const':
> const int INT = 30;
> 
> The problem with the const keyword is it doesn't guarantee the expression will
> be constant.
> const int* PTR = new int; // This technically isn't a constant, the compiler
> just doesn't allow you to modify it's value.

Technically, you can change a constant value in C++, too, if you want. Just get the address and write a little inline assembler to mov x, y something else there. I can't imagine what'd hold me off from that.

If you really are so afraid constant values could be changed during runtime, then wright an immutable class, that does for you what you think you need more than D's const.
> 
> Another solution is to use enumerators, but they don't aloow any type other
> than int:
> enum { val = 30 } // OK
> enum { str = "Hello" } // Error
> 

enum : uint { } // Yay, we've got uint as enum!
enum : char { } // Yay, char too! (this works.)

The only thing that does not work is an array. Apart from that, you can use any (basic?) type.

> 
> So I was thinking, why not give this job to aliases? Aliases must be constant,
> you can use any type with them, and they're easy to write.
> alias 30 VAL;
> alias "Hello" STR;
> alias 31.5 DEC;
> 

This is neither type-safe nor anyhow what alias is meant to be.

> Expressions can simply be put within parenthesis:
> alias (15 * 99) EXP;

Same goes here.


Sorry, if I sound a bit harsh, but I really dislike preprocessors and is really great /without/ preprocessors. I don't see any need for this. You got version() statements, static if's and what not. It's way cleaner and  simpler to read, why insert something in the language just half a month before the language goes 1.0, that is *actually* deprecated and (IIRC) has been discussed in other forms already a few times?

Really. No need for that. D is not C++.

Kind Regards,
Alex
December 14, 2006
Xinok wrote:
> The problem with the const keyword is it doesn't guarantee the expression will
> be constant.

It doesn't? How can you modify a const var then? (in D)
December 14, 2006
Lutger wrote:
> Xinok wrote:
>> The problem with the const keyword is it doesn't guarantee the expression will
>> be constant.
> 
> It doesn't? How can you modify a const var then? (in D)

You can modify const variables that are not set directly at declaration (write-once constants) just by getting their address and modifying them through another variable/pointer.
December 14, 2006
I think you're proposing a preprocessor .. something that D deliberately dropped from C/C++

Xinok wrote:
> In C++, you can define a constant by using a preprocessor:
> #define INT 30
> 
> In D, the standard is to use 'const':
> const int INT = 30;
> 
> The problem with the const keyword is it doesn't guarantee the expression will
> be constant.
> const int* PTR = new int; // This technically isn't a constant, the compiler
> just doesn't allow you to modify it's value.
> 
> Another solution is to use enumerators, but they don't aloow any type other
> than int:
> enum { val = 30 } // OK
> enum { str = "Hello" } // Error
> 
> 
> So I was thinking, why not give this job to aliases? Aliases must be constant,
> you can use any type with them, and they're easy to write.
> alias 30 VAL;
> alias "Hello" STR;
> alias 31.5 DEC;
> 
> Expressions can simply be put within parenthesis:
> alias (15 * 99) EXP;
December 14, 2006

Alexander Panek wrote:
> Lutger wrote:
>> Xinok wrote:
>>> The problem with the const keyword is it doesn't guarantee the expression will
>>> be constant.
>>
>> It doesn't? How can you modify a const var then? (in D)
> 
> You can modify const variables that are not set directly at declaration (write-once constants) just by getting their address and modifying them through another variable/pointer.

So what!!
You can violate class encapsulation using pointers too, in fact you can even mess the v-table.
December 14, 2006
Lutger wrote:

> Xinok wrote:
> > The problem with the const keyword is it doesn't guarantee the expression will be constant.
> 
> It doesn't? How can you modify a const var then? (in D)

This actually feels more like a bug, but here's an example:

module noconst;
import std.stdio;
struct S {
    float f;
}
const S s = {3.1415};
void main() {
    writefln(s.f);
    s.f = 666;
    writefln(s.f);
}

(DMD.177)
December 14, 2006
Hasan Aljudy wrote:
> 
> 
> Alexander Panek wrote:
>> Lutger wrote:
>>> Xinok wrote:
>>>> The problem with the const keyword is it doesn't guarantee the expression will
>>>> be constant.
>>>
>>> It doesn't? How can you modify a const var then? (in D)
>>
>> You can modify const variables that are not set directly at declaration (write-once constants) just by getting their address and modifying them through another variable/pointer.
> 
> So what!!
> You can violate class encapsulation using pointers too, in fact you can even mess the v-table.

Hahaha, yea. I don't get what const would change there, either. X-P
« First   ‹ Prev
1 2 3