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

cgen of match expr #4401

Closed
danieldaeschle opened this issue Apr 14, 2020 · 1 comment
Closed

cgen of match expr #4401

danieldaeschle opened this issue Apr 14, 2020 · 1 comment
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@danieldaeschle
Copy link
Member

danieldaeschle commented Apr 14, 2020

Currently cgen uses C ternary expression to generate a match expression which returns a value. The disadvantage of the ternary operator is, that it doesn't support variables which results in no it variable on SumTypes and in general no variables in the body.

Not working example:

// sumtype
a := match b {
    c { it.foo }
    else { placeholdervalue }
}

The solution for this would be to use common if-else generation.
There is another Problem with this.
Let's say, the match expr assigns the result to a variable.
Then cgen generates an output of string myRes =. After this output comes the ternary operator, normally.
To return something using if and else we have to use a temp-variable. But because our myRes variable is defined before, there is no way to assign the value to it.

The perfect result of a match-expr would look like this:

#line
temp_type tmp; if (cond) { 
  ...
  tmp = ...
#line
}
#line
else {
  ...
  tmp = ...
#line
} final_type final = tmp;

Currently there is no way to get this result.

This should work:

type Abc = int | string

struct Foo {
    bar string
}

fn foo(bar string) {}

fn main() {
    a := Abc(1)
    len := match a {
        int { 'asdsad' }
        string { a }
    }

    x := Foo{
        bar: match a {
            int { 'asdsad' }
            string { a }
        }
    }

    // vlib/time/format.v:140
    foo(match a {
        int { 'asdsad' }
        string { a }
    })
}
@danieldaeschle danieldaeschle added the Bug This tag is applied to issues which reports bugs. label Apr 14, 2020
@danieldaeschle
Copy link
Member Author

the example works somehow no :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs.
Projects
None yet
Development

No branches or pull requests

1 participant