- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Surprising type inference
Tue, 2011-12-13, 21:08
Dear Scalarazzi,
The little code snippet at this paste gives a compilation behavior that surprises me. The compiler is perfectly happy with
which is the de-sugaring of the commented method
which the compiler sees are ill-typed:
[ERROR] /Users/lgm/work/src/projex/stellar/mdp4tw/SpecialK.master/src/main/scala/com/biosimilarity/lift/lib/http/JSONUtil.scala:31: error: type mismatch; [INFO] found : Unit[INFO] required: Option[T][INFO] rspData <- toMap( jsonRsp ).get( "data" );[INFO] ^[INFO] Unit <: Option[T]? [INFO] <notype> <: Option[T]?[INFO] false[INFO] false[ERROR] one error found
Does anyone see what my tired eyes are missing?
Best wishes,
--greg
--
L.G. Meredith
Managing Partner
Biosimilarity LLC
7329 39th Ave SWSeattle, WA 98136
+1 206.650.3740
http://biosimilarity.blogspot.com
The little code snippet at this paste gives a compilation behavior that surprises me. The compiler is perfectly happy with
def getRspData [T] ( jsonRsp : String, key : String ) : Option[T] = { import scala.collection.JavaConverters._ toMap( jsonRsp ).get( "data" ) match { case Some( rspData ) => { rspData.asInstanceOf[java.util.LinkedHashMap[String,Object]].asScala.get( key ) match { case Some( v ) => Some( v.asInstanceOf[T] ) case _ => None } } case _ => None } }
which is the de-sugaring of the commented method
// def getRspDataBug [T] ( jsonRsp : String, key : String ) : Option[T] = { // import scala.collection.JavaConverters._ // for( // rspData <- toMap( jsonRsp ).get( "data" ); // v <- rspData.asInstanceOf[java.util.LinkedHashMap[String,Object]].asScala.get( key ) // ) { // v.asInstanceOf[T] // } // }
which the compiler sees are ill-typed:
[ERROR] /Users/lgm/work/src/projex/stellar/mdp4tw/SpecialK.master/src/main/scala/com/biosimilarity/lift/lib/http/JSONUtil.scala:31: error: type mismatch; [INFO] found : Unit[INFO] required: Option[T][INFO] rspData <- toMap( jsonRsp ).get( "data" );[INFO] ^[INFO] Unit <: Option[T]? [INFO] <notype> <: Option[T]?[INFO] false[INFO] false[ERROR] one error found
Does anyone see what my tired eyes are missing?
Best wishes,
--greg
--
L.G. Meredith
Managing Partner
Biosimilarity LLC
7329 39th Ave SWSeattle, WA 98136
+1 206.650.3740
http://biosimilarity.blogspot.com
On Tue, Dec 13, 2011 at 12:07 PM, Meredith Gregory <lgreg.meredith@gmail.com> wrote:
--
L.G. Meredith
Managing Partner
Biosimilarity LLC
7329 39th Ave SWSeattle, WA 98136
+1 206.650.3740
http://biosimilarity.blogspot.com