Skip to content

TIP: Implementation of pattern matching #2

@ljmf00

Description

@ljmf00

Idea

There will be two matchers. One for type matching and the other for value-based matching:

Generic matchers will be implemented on taurus.match.

Type matching

On this type of matchers you need to specify the matched type on a list of lambda functions.

Using class typeid:

class A {}
class C {}

Object a = new C;
string ret = atum.match!(
	(C c) => c.toString(),
    (A a) => a.toString(),
	() => ""
);

Could be also implemented with other type matchers that use typeid for type discovery.

Using Optional types:

auto s = some(7);

int ret = s.match!(
	(Some!int s) => s.get(),
    (None) => 0,
);

You can even omit None, or Some, if that happens you need to strictly define a default, otherwise, you need to use finalMatch!() template. This behavior mimics the D switch and final switch respectively.

Value-based matching

You will have two generic value-based matchers:

bool atum;

/// associative array
int ret1 = atum.match!([
    true: (bool x) => x,
    false: (bool x) => x
]);

/// quasiquotes syntax based on associative array body initializer
int ret2 = atum.match!(q{
    true: x => x,
    false: x => x
});

This description may change on new proposals

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions