Skip to content

Struct enhancements #42

@Victorious3

Description

@Victorious3

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 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions