September 01, 2006
Walter Bright wrote:
> Don Clugston wrote:
>> Walter Bright wrote:
>>> There's a bit of chicken and egg problem with it - can't tell if it's referenced or not until code generation, which is intimately wrapped up with object file generation.
>>
>> But isn't it true that a template consisting only of
>> 'const' and 'static if' will *never* result in code generation?
> 
> There can be:
> 
> template Foo(T)
> {
>     static if (...)
>     const T x;
> }
> 
> int* p = &Foo!(int).x;
> 

Yes, because there are two very different meanings for "const" in D : there's "compile-time constant", and there's "variable that can only be assigned once", for which the keyword 'final' is used in every other modern C-like language, which is how it should be done in D as well (or with some other keyword).

(Not that that would be required for or even helpful to the compiler to be able to differentiate the two meanings of 'const', in order to see which templates can be discarded)

-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
September 06, 2006
Bruno Medeiros wrote:
> Walter Bright wrote:
>> Don Clugston wrote:
>>> Walter Bright wrote:
>>>> There's a bit of chicken and egg problem with it - can't tell if it's referenced or not until code generation, which is intimately wrapped up with object file generation.
>>>
>>> But isn't it true that a template consisting only of
>>> 'const' and 'static if' will *never* result in code generation?
>>
>> There can be:
>>
>> template Foo(T)
>> {
>>     static if (...)
>>     const T x;
>> }
>>
>> int* p = &Foo!(int).x;
>>
> 
> Yes, because there are two very different meanings for "const" in D : there's "compile-time constant", and there's "variable that can only be assigned once", for which the keyword 'final' is used in every other modern C-like language, which is how it should be done in D as well (or with some other keyword).

Bruno, I agree that 'final' would be much better for 'write-once' values; but the compiler can tell the difference anyway, based on whether it has an =.

If you change the line to

const T x = 6;

then the problematic line won't compile. By the time the compiler has finished instantiating the template, it should know if it contained 'const=' or 'write-once const'.

I really think that it is worth putting some thought into making this early discarding possible. If all else fails, one possibility would be to allow 'const' in front of 'template' to signify that the template should be discarded. But I don't think that's necessary.
The refined rule is:
Templates with only 'const =' and 'static if' can always be discarded.
September 07, 2006
Don Clugston wrote:
> Bruno Medeiros wrote:
>> Walter Bright wrote:
>>> Don Clugston wrote:
>>>> Walter Bright wrote:
>>>>> There's a bit of chicken and egg problem with it - can't tell if it's referenced or not until code generation, which is intimately wrapped up with object file generation.
>>>>
>>>> But isn't it true that a template consisting only of
>>>> 'const' and 'static if' will *never* result in code generation?
>>>
>>> There can be:
>>>
>>> template Foo(T)
>>> {
>>>     static if (...)
>>>     const T x;
>>> }
>>>
>>> int* p = &Foo!(int).x;
>>>
>>
>> Yes, because there are two very different meanings for "const" in D : there's "compile-time constant", and there's "variable that can only be assigned once", for which the keyword 'final' is used in every other modern C-like language, which is how it should be done in D as well (or with some other keyword).
> 
> Bruno, I agree that 'final' would be much better for 'write-once' values; but the compiler can tell the difference anyway, based on whether it has an =.
> 
> If you change the line to
> 
> const T x = 6;
> 
> then the problematic line won't compile. By the time the compiler has finished instantiating the template, it should know if it contained 'const=' or 'write-once const'.
> 
> I really think that it is worth putting some thought into making this early discarding possible. If all else fails, one possibility would be to allow 'const' in front of 'template' to signify that the template should be discarded. But I don't think that's necessary.
> The refined rule is:
> Templates with only 'const =' and 'static if' can always be discarded.

I know that, I did say it in the post:

Bruno Medeiros wrote:
>
> (Not that that would be required for or even helpful to the compiler to
> be able to differentiate the two meanings of 'const', in order to see
> which templates can be discarded)
>

;)


-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
1 2
Next ›   Last »