This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Surprising type inference

1 reply
Meredith Gregory
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Dear Scalarazzi,
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
Meredith Gregory
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Surprising type inference
P.S. Nevermind -- i see the missing yield!

On Tue, Dec 13, 2011 at 12:07 PM, Meredith Gregory <lgreg.meredith@gmail.com> wrote:
Dear Scalarazzi,
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



--
L.G. Meredith
Managing Partner
Biosimilarity LLC
7329 39th Ave SWSeattle, WA 98136

+1 206.650.3740

http://biosimilarity.blogspot.com

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland