Skip to content

Add struct as a main language feature #2101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
CXCubeHD opened this issue Feb 8, 2022 · 2 comments
Closed

Add struct as a main language feature #2101

CXCubeHD opened this issue Feb 8, 2022 · 2 comments
Labels
feature Proposed language feature that solves one or more problems state-duplicate This issue or pull request already exists

Comments

@CXCubeHD
Copy link

CXCubeHD commented Feb 8, 2022

Dart should add structs the same way C# did. Structs are in-place, meaning that the place for the members is pre-reserved, like for native types.

Here an example on how the memory structure may look like:

Class

class MyClass {
    int a;
    MyOtherClass b;
    MyStruct s;
}

struct MyStruct {
    int sA;
    int sB;
}
--- MyClass begin ---
a: INT (8 bytes)
b: PTR (8 bytes (64 Bit))
c -> All the members from MyStruct (-> 16 bytes)
    sA: INT (8 bytes)
    sB: INT (8 bytes)
--- MyClass end ---

Structs are passed by value, meaning they are being copied on each assignment. This can (or cannot) improve performance in certain situations. Also, the GC does not track structs on its own.

Structs should be used on small datatypes like: (fixed) Vectors -> Vector2 or similar.

This can heavily improve performance in games, as no additional allocations are needed and as no GC tracking is required.

This feature should be high priority, as Flutter apps and game engines rely on it (like Flame).

@CXCubeHD CXCubeHD added the feature Proposed language feature that solves one or more problems label Feb 8, 2022
@mraleph
Copy link
Member

mraleph commented Feb 8, 2022

This has been discussed for many years now, one of the more recent discussions / approaches are in #1467

@munificent
Copy link
Member

I'm going to close this as a duplicate of #1467.

@munificent munificent added the state-duplicate This issue or pull request already exists label Feb 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems state-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants