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.
The variable is declared with the following syntax in Scala as follows: val or val variable_name: variable_datatype = value; In the above syntax, the variable can be defined in one of two ways by using either the 'var' or 'val' keyword. It consists of 'variable_name' as your new variable, followed by a colon.
Since your run() function does not take parameters, the contents of value can also be computed without it. Show activity on this post. you can add java classes in Scala , so you can create a class and put your variables in static { } method and you can get access using private get methods .
Use the getClass Method in Scala The getClass method in Scala is used to get the class of the Scala object. We can use this method to get the type of a variable.
Use backticks:
var `type` = 42
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With