Jump to page: 1 2 3
Thread overview
Accessing static data of functions
Oct 14, 2021
Ogi
Oct 14, 2021
Paul Backus
Oct 19, 2021
Ogi
Oct 19, 2021
Mike Parker
Oct 19, 2021
Timon Gehr
Oct 19, 2021
Kagamin
Oct 19, 2021
Mike Parker
Oct 19, 2021
WebFreak001
Oct 19, 2021
Imperatorn
Oct 19, 2021
Timon Gehr
Oct 19, 2021
Imperatorn
Oct 19, 2021
Imperatorn
Oct 19, 2021
Ogi
Oct 19, 2021
Kagamin
Oct 19, 2021
Timon Gehr
Oct 19, 2021
Timon Gehr
Oct 14, 2021
Ali Çehreli
Oct 19, 2021
Tejas
Oct 19, 2021
Kagamin
Oct 19, 2021
Timon Gehr
October 14, 2021

Is there any way to get/set static variables defined in functions from the outside? Also, is there any way to access the types defined inside in function to get/set their static members?

I’m writing a game engine that should support a dynamic plugin system similar to what Manu Evans described in his Using D Alongside a Game Engine talk. Basically, game objects are defined in modules to be compiled as DLLs and loaded dynamically. This allows making modifications without even restarting the game. Plugin manager detects a modification in a plugin source file, serializes all game objects into text, unloads a DLL, recompiles it, loads it again and deserialize the game objects back.

So far so good, I’m now able to serialize/deserialize game objects (including private fields), and any global variables defined in a plugin, and any static members of the types defined in a plugin. But I don’t know what to do with the stuff that could be defined inside of a function, I can’t find any trait or a template that could access it.

October 14, 2021

On Thursday, 14 October 2021 at 14:21:21 UTC, Ogi wrote:

>

Is there any way to get/set static variables defined in functions from the outside? Also, is there any way to access the types defined inside in function to get/set their static members?

No.

>

So far so good, I’m now able to serialize/deserialize game objects (including private fields), and any global variables defined in a plugin, and any static members of the types defined in a plugin. But I don’t know what to do with the stuff that could be defined inside of a function, I can’t find any trait or a template that could access it.

Avoid storing any of your game's state in static variables.

You can refactor any function with a static variable into a struct method:

// Before:
int fun()
{
    static int n = 123;
    return n;
}

// After:
struct S
{
    int n;
    int fun() { return n; }
}

auto s = S(123);
// call s.fun()
October 14, 2021
On 10/14/21 7:21 AM, Ogi wrote:

> is there any way to access the types defined inside
> in function to get/set their static members?

Possible through instances of those types (not through type names because those type names are not visible from the outside):

auto foo() {
  struct S {
    static int s;
  }

  return S();
}

void main() {
  auto s = foo();
  s.s = 42;

  foo().s++;
  assert(foo().s == 43);
}

Ali

October 19, 2021

On Thursday, 14 October 2021 at 14:32:49 UTC, Paul Backus wrote:

>

No.

Is there any solid reasoning behind this gaping hole in the language reflection capabilities? I understand that in general it is not a good idea to meddle with static variables but serialization/deserialization is a good example where this is required.

>

Avoid storing any of your game's state in static variables.

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.

October 19, 2021

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.

October 19, 2021

On Tuesday, 19 October 2021 at 08:15:01 UTC, Ogi wrote:

>

On Thursday, 14 October 2021 at 14:32:49 UTC, Paul Backus wrote:

>

No.

Is there any solid reasoning behind this gaping hole in the language reflection capabilities? I understand that in general it is not a good idea to meddle with static variables but serialization/deserialization is a good example where this is required.

You can't serialize everything. The standard approach to serialization is to use DTOs - types tailored for serialization.

October 19, 2021
On 19.10.21 10:31, 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.

The OP has a well-motivated use case. I don't understand at all why you are being this dismissive. The inability to serialize static variables scoped inside functions is clearly an arbitrary limitation that should be lifted.
October 19, 2021
On 19.10.21 10:15, 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.

Without the necessary language extension, what you could do as a workaround is require all functions in the plugin to be `pure`. Then they can't access static variables. Of course that also rules out accessing globals, so you'd have to pass around global state explicitly within some object.
October 19, 2021
On 19.10.21 11:19, Kagamin wrote:
> On Tuesday, 19 October 2021 at 08:15:01 UTC, Ogi wrote:
>> On Thursday, 14 October 2021 at 14:32:49 UTC, Paul Backus wrote:
>>> No.
>>
>> Is there any solid reasoning behind this gaping hole in the language reflection capabilities? I understand that in general it is not a good idea to meddle with static variables but serialization/deserialization is a good example where this is required.
> 
> You can't serialize everything.

Why not?

> The standard approach to serialization is to use DTOs - types tailored for serialization.

Clearly the ambition of the OP is to do something that's better than the existing standard.
October 19, 2021
On Tuesday, 19 October 2021 at 11:12:12 UTC, Timon Gehr wrote:
> The OP has a well-motivated use case. I don't understand at all why you are being this dismissive. The inability to serialize static variables scoped inside functions is clearly an arbitrary limitation that should be lifted.

As an option a virtual machine should be able to serialize everything or at least RAM.
« First   ‹ Prev
1 2 3