Skip to content

Pattern matching with Matchable<T> #75

@SamTebbs33

Description

@SamTebbs33

When an expression that is of a type that implements Matchable<T> where T is an enum, should be matchable to members of that enum.

enum ListEnum {
    EMPTY, NON_EMPTY
}
class AshList<T> : LinkedList<T>, Matchable<ListEnum> {
    override func matches(e : ListEnum) -> bool = (e == EMPTY) == isEmpty()
}
var list = new AshList()
match list {
    EMPTY -> println("Empty")
    NON_EMPTY -> println("Not empty")
}

Another proposed structure is the following.

class AshList<T> : LinkedList<T>, Matchable<ListEnum> {
    override func getMatchEnum() -> ListEnum = isEmpty() ? EMPTY : NON_EMPTY
}
var list = new AshList()
match list.getMatchEnum() {
    EMPTY -> println("Empty")
    NON_EMPTY -> println("Not empty")
}

The second is of course more verbose and less pleasing to the eye, but would not require any changes to the compiler in its current state.

This could then be extended to work with optional types.

match obj {
    NONE -> println("none")
    SOME -> println(obj!)
}

enum EnumOptional {
    NONE, SOME
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions