Accessing static data of functions

Timon Gehr timon.gehr at gmx.ch
Tue Oct 19 17:04:32 UTC 2021


On 19.10.21 18:43, Imperatorn wrote:
> On Tuesday, 19 October 2021 at 08:31:47 UTC, Mike Parker wrote:
>> On Tuesday, 19 October 2021 at 08:15:01 UTC, Ogi wrote:
>>>
>>> What are we talking about is not a game but an engine to be used for 
>>> making different games, potentially by people other than me. So this 
>>> is a limitation that the users will have to be aware of and work 
>>> around it, and failing to do so will result in silent bugs in their 
>>> code because there’s no way to enforce it at compile time.
>>
>> Personally, this isn't something I would expect, nor would I worry 
>> about it. Static variables in functions are generally there because 
>> they are scoped to that function, to be used within that function over 
>> the run time of the program. There shouldn't be any reason whatsoever 
>> for them to be accessible outside the function.
>>
>> The idea that you are even considering serializing them is just a 
>> foreign concept to me. If I wanted to make something available to a 
>> serializer, I wouldn't declare it inside a function. I'd put it at 
>> module scope, or in an aggregate. I'm not saying no one would ever 
>> think of it (you obviously did), but in 25 years of following various 
>> online programming communities (C, Java, D, game development, etc.) I 
>> can't recall seeing anyone bring it up before now. I can't imagine it 
>> would be common enough to be an issue.
> 
> I was actually thinking something similar. Have never heard of that 
> requirement before. You're serializing the object, not the class, and 
> the static variable doesn't belong to the object (in many languages) but 
> the class. I guess D looked at Java? Have no evidence of that atm tho.
> 
> That doesn't mean D *should* "do what Java does" ofc.

This is about static variables at function scope. Java does not have 
static variables at function scope.

string uniqueName(){
     static int id = 0; // this is the value that OP wants to preserve 
on reload
     return text("__tmp", id++);
}


More information about the Digitalmars-d mailing list