Feature: Labeled Tuple Elements

TypeScript allows you to label tuple elements in type definitions

function getUserBirthday(): [month: number, day: number, year: number] {
  return [4, 30, 1990]
}

as opposed to a less expressive

function getUserBirthday(): [number, number, number] {
  return [4, 30, 1990]
}

where you must guess or analyze the internals to know what each position represents.

The labels are purely annotative and are not referenceable in the runtime.

Yes, you can use objects or maps instead, and often times that is better for additional reasons. But sometimes, you really do just want a tuple. It would be nice if Scala introduced this for improved readability in those cases.

Proposal:

def getUserBirthday(): (month: Double, day: Double, year: Double): = ???

It’s already been done (“named tuples”): Named Tuples

It’s still experimental, though.

1 Like