Skip to content

Improved type testing #71

@oscbyspro

Description

@oscbyspro

Joseph Groff mentioned a potential type testing feature:

Ultimately, though, I'd like to see us expose that kind of more expressive efficient type testing in the language too. It would be great if you could do things like [...] without the overhead of the extraneous checks, the need for a value of a type to ask for properties of the type itself, or the implied wrapping of casting something as? P.

func foo<T>(x: T, y: T) -> T {
    if  where T == Int {
        return x + y // we can assume x and y are Int here
    }

    if <U> where T == Array<U> {
        return x + y // we can assume x and y are some kind of Array here
    }

    if  where T: Addable {
        return x.add(y) // we can assume x and y conform to Addable here
    }

    return x
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    awaitcan't be done yetbrrrsuch code, much wow

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions