45
votes

I want to declare a variable with name 'type' in a Play/Scala application, since my data has this field name and I'm using JSON transforms. It just makes more sense.

Fortunately I could just rename the field, but still curious if there is a way to make the compiler ignore the type reserved word when declaring variables.

1
Actually I wonder if this is a problem with the parser, as it should expect a valid variable name, not a reserved word. It says "identifier expected but 'type' found] - jmend
But nothing stops you from having "type" name as DB field (if not prohibited by DB) and different variable name for the related class field. - Leo
I'm building a REST API and the incoming data is JSON. I'm using JSON Macros to create my model classes, so the field in the incoming JSON data must match the variable name :).. I guess I could write my own Json.Reads to handle that specific case. - jmend

1 Answers

109
votes

Use backticks:

var `type` = 42