Skip to content
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

what size should an empty object be? #1318

Open
theSherwood opened this issue May 22, 2024 · 3 comments
Open

what size should an empty object be? #1318

theSherwood opened this issue May 22, 2024 · 3 comments
Labels
language-design Language design syntax, semantics, types, statics and dynamics.

Comments

@theSherwood
Copy link
Contributor

Should empty objects be of size 0? Maybe in some contexts that won't even work.

Example

type
  Empty = object
  Wrapper*[K, V] = object
    node*: (K, V)
echo sizeof(Empty)
echo sizeof(Wrapper[uint8, Empty])

Actual Output

1
2

Expected Output

0
1

Possible Solution

Additional Information

References

@zerbina zerbina added the language-design Language design syntax, semantics, types, statics and dynamics. label May 22, 2024
@zerbina
Copy link
Collaborator

zerbina commented May 22, 2024

It's possible to implement empty objects/tuples/arrays having a size of 0, but I personally don't think it's worth the added compiler/language complexity.

@starsiderfox
Copy link
Contributor

It looks like there's already a type of size 0 that add nothing to any structure it's part of.

type Empty = distinct array[0, byte]

It must be based on an 8 bit type so it doesn't alter the layout (e.g. if it's an array[0,int64] it will instead force any adjacent type into 8 byte size and alignment).

However seqs seem to allocate 1 byte for each Empty. Similarly, tuples and objects that only contain tuples are equivalent to empty tuples and objects, all of which have size 1.

@zerbina
Copy link
Collaborator

zerbina commented Aug 6, 2024

It looks like there's already a type of size 0 that add nothing to any structure it's part of.

type Empty = distinct array[0, byte]

The size being reported as 0 is a compiler bug -- in the generated code, arrays with a length <= 0 always have a length of 1.

In any case, there's been discussion about a unit type since this question was originally asked. The unit type will most likely end up becoming the type to use for fields/locals when they should not take up any space nor alter alignment requirements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
language-design Language design syntax, semantics, types, statics and dynamics.
Projects
None yet
Development

No branches or pull requests

3 participants