Difference between Future[Any] and Future[_]

Wolfsblvt

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?

Andreas Neumann

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Difference between a Future and a Mono

From Dev

Difference between a Future returning another Future and Not

From Dev

Is there any difference between Observable and Future.get

From Java

Difference between CompletableFuture, Future and RxJava's Observable

From Java

Difference between coroutine and future/task in Python 3.5?

From Java

What's the difference between a Future and a Promise?

From Dev

What's the difference between onComplete and flatMap of Future?

From Dev

What is the difference between onComplete and foreach for a future in Scala?

From Dev

KafkaProducer: Difference between `callback` and returned `Future`?

From Dev

Is there any difference between Observable and Future.get

From Dev

Difference between current time and future time in java

From Dev

What is the difference between "Future.successful(None)" and "Future(None)"

From Dev

Difference between Future callback methods and Promises [Success and Failure]?

From Java

Jquery is getting the incorrect difference between now and a future date

From Dev

Difference between / and /**

From Java

Is there a difference between "==" and "is"?

From Dev

Difference between 'or' and '||'?

From Dev

Difference between <- and <<-

From Dev

Difference between ./ and ~/

From Java

Difference between == and ===

From Dev

Difference between ./ and ../

From Dev

Difference between

From Dev

Difference between $ and ()

From Dev

Difference between <% and <%=

From Dev

Difference between << and =

From Dev

Difference between \% and %%

From Dev

Difference between /* and /**

From Dev

Difference between $@ and $*

From Dev

difference between != and !==

Related Related

HotTag

Archive