Scala compiler issue or JVM boxing subtlty? -


the following function part of code have interface particularly nasty (as in untyped) java api. intended safely perform casts context 'knows' type should be, want make sure won't have bad failure modes in unknown edge case @ runtime:

def safecast[a](o: any): option[a] =   try(o.asinstanceof[a]).tooption 

now happens when it's used in repl session:

scala> val testdouble: double = 1.0  testdouble: double = 1.0  scala> safecast[int](testdouble)  res0: option[int] = some(1.0) 

res0 claims have type option[int] value some(1.0) (i.e. - some[double]). class cast exception follows if try map on option.

this behavior happens polymorphic safecast. if tighten specific type:

def safeintcast(o: any): option[int] = try(o.asinstanceof[int]).tooption 

then get:

scala> safeintcast(testdouble)  res1: option[int] = none 

hence polymorphism somehow interacting boxing (i suspect??) or compiler issue (bug??). compiler version used 2.12.2

can provide explanation this?

please, see here , here further reference. explains lot scala asinstanceof.

hope helps!


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -