jakt icon indicating copy to clipboard operation
jakt copied to clipboard

selfhost: Add bindings on is operator for enum variants

Open adleroliveira opened this issue 3 years ago • 5 comments

In codegen, it will de-sugar this:

function main() {
    let foo = Foo::Bar(5)
    let baz = Foo::Baz(m: "Hello")

    if foo is Bar(n) {
        if baz is Baz(m: different) {
            println("{} {}", n, different)
        }
    }
}

Into this:

ErrorOr<int> main(Array<String>)
{
    {
        const Foo foo = typename Foo::Bar(static_cast<i64>(5LL));
        const Foo baz = typename Foo::Baz(String("Hello"));
        if ((foo).has<Foo::Bar>()) {
            const i64 n = (foo.get<Foo::Bar>()).value;
            if ((baz).has<Foo::Baz>()) {
                const String different = (baz.get<Foo::Baz>()).m;
                outln(String("{} {}"), n, different);
            }
        }
    }
    return 0;
}

adleroliveira avatar Jul 27 '22 20:07 adleroliveira

@adleroliveira - couple thoughts:

Can you drop the Foo::?:

function main() {
    let foo = Foo::Bar(5)
    let baz = Foo::Baz(m: "Hello")

    if foo is Bar(n) {
        if baz is Baz(m: different) {
            println("{} {}", n, different)
        }
    }
}

also, can you add a test for the case I was wondering about the other day where it's compound in the if?

if foo is Bar(n) and n > 4 { ... }

sophiajt avatar Jul 27 '22 21:07 sophiajt

@adleroliveira - couple thoughts:

Can you drop the Foo::?:

function main() {
    let foo = Foo::Bar(5)
    let baz = Foo::Baz(m: "Hello")

    if foo is Bar(n) {
        if baz is Baz(m: different) {
            println("{} {}", n, different)
        }
    }
}

also, can you add a test for the case I was wondering about the other day where it's compound in the if?

if foo is Bar(n) and n > 4 { ... }

You can now use if foo is Foo::Bar(x) as well as if foo is Bar(x)

The chaining of bindings will go in a separate PR.

adleroliveira avatar Jul 27 '22 23:07 adleroliveira

Hello!

One or more of the commit messages in this PR do not match the Jakt code submission policy, please check the lint_commits CI job for more details on which commits were flagged and why. Please do not close this PR and open another, instead modify your commit message(s) with git commit --amend and force push those changes to update this PR.

BuggieBot avatar Jul 28 '22 16:07 BuggieBot

Please squash selfhost: Remove .ninja_log from staged commit into the relevant preceding commit.

awesomekling avatar Jul 29 '22 10:07 awesomekling

Please squash selfhost: Remove .ninja_log from staged commit into the relevant preceding commit.

awesomekling done :)

adleroliveira avatar Jul 29 '22 12:07 adleroliveira

Closing this one and opening a new one with total refactor

adleroliveira avatar Aug 02 '22 05:08 adleroliveira