Tuples as Return Types
Probably the next-best tuple use case is using them to return one-time structures. Tuples as return types makes sense when you need to return multiple return types.
The following function is required to return a
User from our database.
However, in order to speed up processing, the user may be loaded from the
cache instead of a query. If that's the case, the caller of the function
should also receive the information that the
User was cached, and how
long it was since the last update from the database. We can easily do this
This function returns three values, the actual user
user, whether the user is
cached and when the user was lasted
This also saves you from introducing a new
Since tuples can also be deconstructed on the fly, this even allows re-introducing the variables at the call site:
let (user, cached, lastUpdated) = userFromDatabase()
This will create three new variables in the current scope:
We will describe this
destructuring in more detail in the next chapter.