1

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You need the \ in cases where the name you are using is also part of the Rascal syntax. In this case, object, float, and double are not, while both int and void are names of Rascal types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.