Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do casting in Scala?

Tags:

casting

scala

For example suppose I have

val myDouble = 25.7 val myInt = 5 

How would I do something like

val divide = (Int) (myDouble / myInt) 
like image 745
deltanovember Avatar asked Jul 16 '11 02:07

deltanovember


People also ask

What is asInstanceOf?

asInstanceOf[Bar] is a type cast, which is primarily a runtime operation. It says that the compiler should be coerced into believing that foo is a Bar . This may result in an error (a ClassCastException ) if and when foo is evaluated to be something other than a Bar at runtime.

What is some () in Scala?

Scala some class returns some value if the object is not null, it is the child class of option. Basically, the option is a data structure which means it can return some value or None. The option has two cases with it, None and Some. We can use this with the collection.

What is AnyRef in Scala?

AnyRef represents reference types. All non-value types are defined as reference types. Every user-defined type in Scala is a subtype of AnyRef . If Scala is used in the context of a Java runtime environment, AnyRef corresponds to java. lang.

What is casting in OOP?

Casting objects in OOP is usually the product of either class inheritance or interface inheritance. One has to always remember that class inheritance denotes an "is a" relationship, and interface inheritance denotes a "has a" relationship.


1 Answers

You can do (myDouble/myInt).toInt. You can also do toDouble, toLong, and toFloat.

like image 119
Moe Matar Avatar answered Oct 12 '22 12:10

Moe Matar