In Java if we assign a statement to a variable by not giving its type, the Eclipse IDE gives suggestion and helps us automatically add the correct type. For example, if I write the below java statement in Eclipse, it will give suggestion and automatically help add the correct type to String:
The above will not compile but eclipse will give suggestion to add String as its type
How to do the similar thing in Scala and come to know the exact type of right hand side statement?
posted 4 months ago
I came to know. The way to do this is to move curser over the variable name after assigning it to var. It will show the actual variable type like this in eclipse.
but to test yourself, you can specify the type yourself as well:
And if you get it wrong, you get an error (either from your IDE or from the compiler.
In Java and Netbeans, I use 'var' as often as I can, saves quite some typing. But sometimes I do get an error somewhere down the code, since the var is not what I expected. That usually happens when I expected to be dealing with a List<SomeClass>, while Java infers it to be a List<Object>, or similar. But a press of the button is enough to change the 'var' to the real type, so that you can spot what you did wrong. Very educational!
There are three kinds of actuaries: those who can count, and those who can't.
posted 4 months ago
Is this a question about Scala or about Eclipse? If you use var or similar as the type, do you actually need to know the correct runtime type in Scala?
This question as related to Scala. Now I have come to know the answer. The right hand side evaluates to a result which can be stored as var or val. I needed to know that what type does right hand side evaluate to. This is required to better understand what it does. It does this and evaluates to this type. That is easy to understand.
Declaration syntax in Scala is actually more like in Kotlin, Pascal, and Go, where the type comes after the name. If you use var or val, you can omit the type and let the compiler infer the type based on the type of the expression on the right-hand side.
Your realization that "The way to do this is to move curser over the variable name after assigning it to var. It will show the actual variable type like this in eclipse" belies your assertion that it's a Scala-related question and your understanding of what's happening. If you're talking about moving the cursor over a variable name in Eclipse, then it's really about understanding how Eclipse can help you, not about understanding the expression type rules defined in the SLS. Obviously, Eclipse has been programmed to "understand" what those rules are.
Apart from detecting the type right hand side evaluates to, eclipse has been programmed for a lot of things. We do not need to understand each one of those things but can directly make use of them in eclipse. Similarly, we do not need to know how eclipse internally detects the type but need to know the type using eclipse for our understanding while doing coding.
Junilu Lacar wrote:Neither var nor val are "types" in Scala... Rather, val and var are used to declare/define a value expression or variable expression, respectively.
They also define whether variable can be re-assigned its value. "var" let's to do so, while "val" doesn't let re-assign value once variable gets initialized (i.e. similar to Java's final variable; Scala however also has "final" concept, which is taken even further).
machines help you to do more, but experience less. Experience this tiny ad:
create, convert, edit or print DOC and DOCX in Java