Okay, I guess question is already complete in the title. Nothing big, but I am just wondering. I have a method which returns either a correct value or an error code enum item. For example something like this:
def doMyStuff(): Future[_] = {
val result = db.queryMyData().map {
case some(data) =>
val modifiedData = data.doStuff()
modifiedData
case None =>
Errors.THIS_IS_FALSE
}
result
}
Where db.queryMyData()
returns a Future, and data.doStuff()
just modifies the data.
Now I have intuitively written Future[_]
, cause the return value is flexible. But when looking in other libraries I've seen Future[Any]
used. Which seems to be logic too, when you use a match-case on the return of the function to check which data it is.
The code which uses that is for example something like this:
doMyStuff().map {
case data: MyDataType => // Blah blah
case Errors.Value => // error handling
}
So, my questions is: What's the difference between the use of Any or _ here, and why should I use the correct one?
It is a matter of semantics:
The Existential TypeT[_]
means there is a class/type at the position of _
for which I do not care at all but it must be there.
T[Any]
means there has to be a subclass Any
present.
The difference comes into play when you want to serialize the underlying class.
If you just use _
without any typebounds you will not be able to use some of the many Scala JSON libraries.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With