Given following code:
case class User(name: String, email: String, age: Int)

val userLikeData = ("John", "[email protected]", 33)
Select all correct ways to instantiate User from userLikeData?
val user = (User.apply _).tupled(userLikeData)
In short it takes User.apply as a function first. On every function (here of type Function3) you can invoke tupled to convert its parameters list to corresponding tuple type. Because it’s all about Function the example above can also be written as:
Function.tupled(User.apply _)(userLikeData)
or even simpler:

//if companion object exists it must extend FunctionX e.g.:
object User extends Function3[String, String, Int, User]
This one is possible only in two cases: either if User doesn’t have companion object or if its companion object extends appropriate FunctionX. Read more

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Scala quizzes
Sign Up Now
or Subscribe for future quizzes