Scala loop returns as Unit and compiler points to "for" syntax?
Posted
by
DeLongey
on Stack Overflow
See other posts from Stack Overflow
or by DeLongey
Published on 2012-06-07T23:05:05Z
Indexed on
2012/06/08
4:40 UTC
Read the original article
Hit count: 197
scala
|return-type
Seems like Unit is the theme of my troubles today. I'm porting a JSON deserializer that uses Gson and when it comes to this for loop:
def deserialize(json:JsonElement,
typeOfT:Type,
context:JsonDeserializationContext) = {
var eventData = new EventData(null, null)
var jsonObject = json.getAsJsonObject
for(entry <- jsonObject.entrySet()) {
var key = entry.getKey()
var element = entry.getValue()
element
if("previous_attributes".equals(key)) {
var previousAttributes = new scala.collection.mutable.HashMap[String, Object]()
populateMapFromJSONObject(previousAttributes, element.getAsJsonObject())
eventData.setPreviousAttributes(previousAttributes)
eventData
} else if ("object".equals(key)) {
val `type` = element.getAsJsonObject().get("object").getAsString()
var cl = objectMap.get(`type`).asInstanceOf[StripeObject]
var `object` = abstractObject.retrieve(cl, key)
eventData.setObject(`object`)
eventData
}
}
}
The compiler spits out the error type mismatch; found : Unit required: com.stripe.EventData
and it points to this line here: for(entry <- jsonObject.entrySet())
Questions
- Confirm that it is indeed the Gson method
entrySet()
appearing as unit? - If not, what part of the code is creating the issue? I've set return types/values for
eventData
class methods - Is there a workaround for the Gson Unit issue?
Thanks!
© Stack Overflow or respective owner