You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I spotted some possibly inconsistent behaviour when passing companion objects as the shorthand parameter to functions accepting a block, such as map, compared to when passing the automatically created companion object of a case class. StackOverflow question can be found here.
Basically, it works when the class is a case class, but not if it's not, and the companion object is manually defined.
For example
classModelclassModelWrapper(valm: Model)
objectModelWrapper { defapply(model: Model) = newModelWrapper(model) }
valm1 = newModel; valm2 = newModel; valm3 = newModelList(m1, m2, m3).map(ModelWrapper)
<console>:14: error: typemismatch;
found : ModelWrapper.type (withunderlyingtypeobjectModelWrapper)
required: Model => ?
List(m1, m2, m3).map(ModelWrapper)
//however List(m1, m2, m3).map(ModelWrapper.apply) and List(m1, m2, m3).map(ModelWrapper.(_)) both work
Serj on SO posted a workaround - making the companion object extend Function[Model, ModelWrapper]. However, according to the Scala specification, as far as I can see, the built-in companion object doesn't extend Function - should this difference in behaviour exist?
The text was updated successfully, but these errors were encountered:
@paulp said:
The behavior is expected. That the companion extends FunctionN should be added to the spec; I also think we could/should eta-expand naked applies. This was discussed at some point, I forget the resolution if any.
@adriaanm said:
originally case classes gave rise to factory methods, then we switched to companion objects with an apply (and an unapply, which triggered the move to companion objects); to keep backwards compatibility, we consider case class companion objects as functions (but not others)
I spotted some possibly inconsistent behaviour when passing companion objects as the shorthand parameter to functions accepting a block, such as
map
, compared to when passing the automatically created companion object of a case class. StackOverflow question can be found here.Basically, it works when the class is a case class, but not if it's not, and the companion object is manually defined.
For example
Whereas with a case class there's no problem
Serj on SO posted a workaround - making the companion object extend Function[Model, ModelWrapper]. However, according to the Scala specification, as far as I can see, the built-in companion object doesn't extend Function - should this difference in behaviour exist?
The text was updated successfully, but these errors were encountered: