0

I try to understand what's the data type of scala.concurrent.Future?

I found the following types from the scala lang documentation, but still unsure the differences between them, and when to use which one?

trait Future[+T] extends Awaitable[T] //for concurrent programming
object Future extends AnyRef //not sure?

http://www.scala-lang.org/api/2.9.3/scala/concurrent/Future.html
http://www.scala-lang.org/api/2.9.3/scala/concurrent/Future$.html
John
  • 169
  • 1
  • 3
  • 10

4 Answers4

4

Sorry, but I got an impression that you need first to get some scala basis, what is trait, what is companion object, and other stuff.

Back to your question. When you want to execute something concurrently, you can wrap it in Future. Your code has some output type (SomeType, could be Unit - equivalent of void), after wrapping into Future you will get Future[SomeType] - it is extension of trait Future[+T]. Than you need some execution context (thread pool) to execute your Future.

Try to find and read "Programming in Scala" written by Martin Odersky, Lex Spoon and Bill Venners, very good for beginners.

Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
Evgeny
  • 1,760
  • 10
  • 11
1

Like a collection (List, Array, etc.), a Future is a type that works on/with another type. A useful comparison is the Option type.

Just as an Option[Int] might be an Int value (and it might not), a Future[Int] might not be an Int value yet. It could be that the Int value is still being calculated, or being extracted from a database table, or being retrieved from a distant network location. Whatever the cause, if it's a slow process there's no reason to wait for it. Turn it into a Future[Int] so that your program can go on with other important tasks.

As for the object Future, that is a singleton object that has a handful of methods for handling/manipulating existing Future elements. Future.sequence() is a useful example.

jwvh
  • 50,871
  • 7
  • 38
  • 64
  • Future is not higher-kinded. Neither is Option. They're just generic. https://stackoverflow.com/questions/6246719/what-is-a-higher-kinded-type-in-scala – Brian McCutchon Feb 10 '18 at 17:52
0

It is unclear whether you are talking about the trait Future or the singleton object Future. I will answer both.

The trait doesn't have a type. It is a type.

All singleton objects foo have the singleton type foo.type, so the singleton object Future has the singleton type Future.type.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
0

In Scala, object is a singleton class, which means, that there only exists a single instance during the runtime of the application. There are several ways to implement singletons in most languages, but most often, you risk some issues such as thread safety. Scala's object takes care of the implementation of this pattern for you.

A common pattern in Scala is creating an object that has the same name as a class, like the one in your example. This is called a companion object. A common use for these is for essentially defining the equivalents of static methods from Java. You can declare methods that are common for all instances, or methods that handle and manipulate instances of the class. In Java, for example, you would declare them as static in the body of the class itself. The companion object helps you with separation of concern in this case.