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

Conditionally enable generic functions #22

Open
Victorious3 opened this issue Feb 22, 2023 · 0 comments
Open

Conditionally enable generic functions #22

Victorious3 opened this issue Feb 22, 2023 · 0 comments
Labels
proposal New feature or request

Comments

@Victorious3
Copy link
Contributor

The simple way of making a normal function definition dependant on some sort of condition is to wrap it inside an #if

#if defined WIN32 {
    def foo -> int { return 10 }
}

However, for generic functions it isn't so simple. There is currently no way to "enable" a function if a condition is met.
Take index_of from Vector as an example:

export def index_of(vec: &Vector(type T), elem: T) -> int64 {
    for var i in 0..vec.length {
        if vec[i] == elem {
            return i
        }
    }
    return -1
}

This function only makes sense if equality is defined between element types.
Now there's certainly the option to codify this condition with a type, possibly using this syntax:

type Eq = interface {
    def __eq__(other: Eq) -> bool
}

export def index_of(vec: &Vector(type T > Eq), elem: T) -> int64 {

However more complex conditions can't be expressed in that way.
My idea is to wrap the function body in an #if statement like this:

export def index_of(vec: &Vector(type T), elem: T) -> int64
#if defined __eq__::(T, T) {
    ...
}
@Victorious3 Victorious3 added the proposal New feature or request label Feb 22, 2023
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