scala - Akka mapTo versus asInstanceOf -
i'm reading akka futures guide , see sentence:
also note future returned actor future[any] since actor dynamic. why asinstanceof used in above sample. when using non-blocking improve utilize mapto method safely seek cast future expected type
why mapto improve utilize asinstanceof non-blocking future?
the problem asinstanceof
here is, cast want
scala> val f = future { 2 } f: scala.concurrent.future[int] = scala.concurrent.impl.promise$defaultpromise@69b28a51 scala> f.asinstanceof[future[string]] res9: scala.concurrent.future[string] = scala.concurrent.impl.promise$defaultpromise@69b28a51 scala> f.asinstanceof[future[list[string]]] res10: scala.concurrent.future[list[string]] = scala.concurrent.impl.promise$defaultpromise@69b28a51 scala> res10.value res15: option[scala.util.try[list[string]]] = some(success(2))
because of type erasure jvm not know concrete inner type of value. if utilize mapto
instead, cast straight on value, 1 time available , in case of non-matching type, failed future back.
scala> f.mapto[list[string]] res11: scala.concurrent.future[list[string]] = scala.concurrent.impl.promise$defaultpromise@2828afbb scala> res11.value res14: option[scala.util.try[list[string]]] = some(failure(java.lang.classcastexception: cannot cast java.lang.integer scala.collection.immutable.list))
scala akka future
No comments:
Post a Comment