• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

How to check in Scala the exact type the right hand side statement evaluates to?  RSS feed

 
Ranch Foreman
Posts: 1112
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

thanks.


 
Monica Shiralkar
Ranch Foreman
Posts: 1112
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 64179
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Master Rancher
Posts: 3189
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Monica Shiralkar
Ranch Foreman
Posts: 1112
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 13447
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If you use var or similar as the type


Neither var nor val are "types" in Scala. See https://www.scala-lang.org/files/archive/spec/2.11/04-basic-declarations-and-definitions.html. Rather, val and var are used to declare/define a value expression or variable expression, respectively.

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.

As to the OP's question, you need to understand the rules for determining the type for an expression. The Scala Language Specification has Chapter 6: https://www.scala-lang.org/files/archive/spec/2.11/06-expressions.html but like any language specification, it quite dense and not easy to understand.

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.
 
Monica Shiralkar
Ranch Foreman
Posts: 1112
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 6697
464
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!