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

Struct enhancements #42

Open
Victorious3 opened this issue Dec 2, 2023 · 2 comments
Open

Struct enhancements #42

Victorious3 opened this issue Dec 2, 2023 · 2 comments
Labels
proposal New feature or request

Comments

@Victorious3
Copy link
Contributor

Victorious3 commented Dec 2, 2023

Currently there's no way to set default values for strucs. I don't want this to turn into fully fledged constructors, but it should work for constants in the same way as default arguments work. They would be substituted verbatim inside the struct literal when creating the struct.

Additionally I would allow const values which don't take up space inside the stucture but actually behave like functions. Those would work with polymorphism tho, this is mainly useful for being able to override functions in interfaces with a constant value without having to write out the whole function.

It would look something like this:

type S = struct {
    const constant: int = 20
    foo: int = 20
}

Maybe it makes sense to only allow these const parameters at the beginning/end of the structure because otherwise when you mix them with regular values the semantics for assigning values by index becomes really weird. struct inititalization by index was removed

One use case for these const values are setting flags for an interface like this:

type Kind = enum {
    KIND_A
    KIND_B
}

type I = interface {
    def kind -> Kind
    ...
}

type S = struct {
    const kind = Kind::KIND_B
    ...
}

// As opposed to writing something like this:
def kind(s: S) -> Kind { return Kind::KIND_B }
@Victorious3 Victorious3 added the proposal New feature or request label Dec 2, 2023
@Victorious3
Copy link
Contributor Author

Initializing structs by index might have to go because of these interactions.

@Victorious3 Victorious3 changed the title Struct enhancemenets Struct enhancements Dec 7, 2023
@Victorious3
Copy link
Contributor Author

Update: the intitalization by index is gone now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant