Question

I was writing a little helper function toString(TypeSymbol t, M3 m) when I encountered a weird parser error.

The function has a lot of statements like:

...
}else if(object() := t){
    return "object";
}else if(float() := t){
    return "float";
}else if(double() := t){
    return "double";
...

These work fine. However, when I try this same pattern for int() or void(), the compiler gives an error, specifically on the = sign.

if(int() := t){}
          ^ Parse error here
Was it helpful?

Solution

As it often happens, I found the answer to this question while I was typing it up. However, I think it'll be valuable for others so I will post it nonetheless.

I got the syntax for pattern matching in this answer: https://stackoverflow.com/a/21929342/451847

It seems that the 'proper' way of pattern matching is to prefix the type you want to test for with a \.

So, the code above becomes:

...
}else if(\object() := t){
    return "object";
}else if(\float() := t){
    return "float";
}else if(\double() := t){
    return "double";
...

The non-\ syntax works for most of the cases but I think int() and void() have a different definition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top