November 11, 2009
Yigal Chripun wrote:
> bearophile wrote:
>> Yigal Chripun:
>>
>>> Regardless of usefulness (or good design) of such variables, this sounds
>>> extremely dangerous. The compiler must not change semantics of the
>>> program based on optimization. optimizing away such variables most
>>> definitely alters the semantics.
>>
>> Maybe you have misunderstood, or I have explained the things badly. So I explain again.
>>
>> I have seen that LDC (when it performs link-time optimization, that's not done in all situations) keeps just one copy of constants inside the binary even if such constants are present in more than one template instance. In the situations where LTO is available I think this doesn't cause problems.
>>
>> Then I am half-seriously proposing a syntax like:
>> T foo(T)(T x) {
>>   static static int y;
>>   // ...
>> }
>>
>> Where the y is now static to (shared among) all instances of the templated function foo. This may be a little error-prone and maybe not that useful, but again here the compiler doesn't change the semantics of the program, because using a double static keyword the programmer has stated such intention.
>>
>> Bye,
>> bearophile
> 
> Oh. ok. I seems I completely misunderstood you. It wasn't clear to me before that your were talking about constants. Of course it's perfectly OK to optimize _constants_ like that.
> 
> IMO, static is harmful and should be avoided. some newer languages recognize this and completely remove this from the language. I'd like to see D going in that path rather than adding even more ways to use static.
> 
> regarding your concrete proposal - as others said, you can use global variables for that or put this inside a struct if you want to limit the scope.

One option that hasn't been mentioned:

private ref int myInt() {
    static int theInt;
    return theInt;
}

void fun(T)(T arg) {
    ... use myInt() ...
}


Andrei
November 11, 2009
On Wed, Nov 11, 2009 at 1:16 PM, Andrei Alexandrescu <SeeWebsiteForEmail@erdani.org> wrote:

> One option that hasn't been mentioned:
>
> private ref int myInt() {
>    static int theInt;
>    return theInt;
> }
>
> void fun(T)(T arg) {
>    ... use myInt() ...
> }

Is that a joke?  That just replaces global symbol theInt with global symbol myInt.  I don't see the win.

--bb
November 11, 2009
Bill Baxter wrote:
> On Wed, Nov 11, 2009 at 1:16 PM, Andrei Alexandrescu
> <SeeWebsiteForEmail@erdani.org> wrote:
> 
>> One option that hasn't been mentioned:
>>
>> private ref int myInt() {
>>    static int theInt;
>>    return theInt;
>> }
>>
>> void fun(T)(T arg) {
>>    ... use myInt() ...
>> }
> 
> Is that a joke?  That just replaces global symbol theInt with global
> symbol myInt.  I don't see the win.
> 
> --bb

Forgot to mention that the function gives you the opportunity to initialize the object in the general case.

Andrei
November 11, 2009
On Wed, Nov 11, 2009 at 1:36 PM, Andrei Alexandrescu <SeeWebsiteForEmail@erdani.org> wrote:
> Bill Baxter wrote:
>>
>> On Wed, Nov 11, 2009 at 1:16 PM, Andrei Alexandrescu <SeeWebsiteForEmail@erdani.org> wrote:
>>
>>> One option that hasn't been mentioned:
>>>
>>> private ref int myInt() {
>>>   static int theInt;
>>>   return theInt;
>>> }
>>>
>>> void fun(T)(T arg) {
>>>   ... use myInt() ...
>>> }
>>
>> Is that a joke?  That just replaces global symbol theInt with global symbol myInt.  I don't see the win.
>>
>> --bb
>
> Forgot to mention that the function gives you the opportunity to initialize the object in the general case.

Ah, ok.  I thought you were proposing that as another way to limit the scope of the variable.

--bb
November 12, 2009
On Tue, 10 Nov 2009 16:56:11 -0500, bearophile <bearophileHUGS@lycos.com> wrote:

> Steven Schveighoffer:
>
>> What's the advantage over:
>>
>> static int y;
>>
>> T foo(T)(T x) {
>>     // ...
>> }
>
> That the global name space is kept clean, the "y" name can be seen only inside foo. Reducing the visibility of names is useful to keep things tidy. (Inside normal functions in D static has just that purpose).

Doesn't static do that already?  I mean keep y within the module namespace.  Sure, it's visible to other functions in foo's module, but not the global namespace.  Or does that not work in D...

-Steve
November 12, 2009
Steven Schveighoffer:

> Doesn't static do that already?  I mean keep y within the module namespace.  Sure, it's visible to other functions in foo's module, but not the global namespace.  Or does that not work in D...

I think that 'private' is able to keep a name private to a module... But I put lot of stuff in certain D modules, so I'd like to reduce the scope of the name to something smaller of a whole module. (Walter has shown how to use a struct for this).

Bye,
bearophile
1 2
Next ›   Last »