Scala, checking for type and then cast to it


patheems
25/12/2015 9:47 am
HOME ยป Programming Languages

I have sometimes the usecase that I need to check if a variable is of a specific type and if so cast to it to access a property/function of it.

Example:

    if (state.isInstanceOf[StateA] &&
state.asInstanceOf[StateA].isFlagBlaBla)

How can this be improved ? I could use match/case but is there a better option (shorter) ?


Related to : Scala, checking for type and then cast to it
Scala, checking for type and then cast to it
25/12/2015 9:47 am by patheems in Programming Languages

I have sometimes the usecase that I need to check if a variable is of a specific type and if so cast to it to access a property/function of it.

Example:

    if (state.isInstanceOf[StateA] &&
state.asInstanceOf[StateA].isFlagBlaBla)

How can this be


Scala cast type with match
11/12/2014 7:40 pm by Guid in Programming Languages

Frequently, I find myself doing pattern matching such as this

val foo: Foo = ...
foo match {
  case bar: Bar => Some(bar)
  case _ => None
}

I want to make this shorter.

val foo: Foo = ...
optInstance[Foo, Bar](foo)

TAGS: Scala cast type with match

How to do simple type cast in Scala?
by TheStu in Programming Languages

This should be a silly question.


scala> val aFloat = 1.5f
aFloat: Float = 1.5

How to cast aFloat to an Int in a simple way?


I already know to use a.asInstanceOf[Int]. But it needs too much keystrokes.


How do you type cast Char/Int in Scala?
by LinnheCreative in Programming Languages

I am having issues getting this cast to work. The compiler tells me
value aNumber is not a member of object Char


def runCastTest() {
val aNumber = 97
val aChar = (Char)aNumber


println(aChar) // Should be 'a'
}


What am I doing wrong?


Scala short and type safe cast operator
by eskimospy in Programming Languages

I don't like Scala isInstanceOf and asInstanceOf methods - they are long and asInstanceOf can throw exception so we need to use it in couple. Better way is to use pattern matching: Scala: How do I cast a variable? but for really simple operations it can be relatively long too. In C# we have 'is


Scala 'java.lang.ClassCastException' on cast to generic type
30/11/2014 1:00 pm by k19k in Programming Languages

I'm trying to implement a generic function that wraps a mathematical Java function. For simplicity, we can assume that the Java function (Java 7) takes one parameter and returns a result, both of type java.lang.Double. Of course, the wrapper function should take a parameter and a result, both of


Better type checking on match in Scala
by Justin Bowers in Programming Languages
scala> class A
defined class A
scala> class B
defined class B
scala> val a: A = new A
a: A = A@551510e8
scala> a match {
| case _: B => println("unlikely")
| case _ => println("no match")
| }
no match
TAGS: Better type checking match Scala
Checking Function Type in Scala
by Knoxy in Programming Languages

In Haskell, I can check a function's type in ghci by using :t:


Prelude> :t odd
odd :: Integral a => a -> Bool

This says that the function odd takes an Integral type and returns a Bool TAGS: Checking Function Type Scala

Scala equality with type checking?
by GreenChile in Programming Languages

Is there a uniform method to perform equality with type checking?
Unfortunately


val objectA:String = "test"
val objectB:Int = 2
objectA == objectB

the equality operator == doesn't complain if objectB is a Int while objectA is a String.
I wo


How to structure a Scala mixin trait to cast a return to an extended type?
by juma in Programming Languages

I want to mixin a trait so that I can use a method from to return my own trait type. For example,


> trait M {
trait foo {def blah = "foo" }
def name:foo = { new foo { override def blah = "name"}}}
> trait N extends M {
trait bar extends foo
Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2014 © bighow.org All Rights Reserved .