does
does

Reputation: 107

scala generic double brackets literal

I'm learning Scala's generic content, and I don't know the meaning of literal in the code

sealed trait Natural

sealed trait Vect[N <: Natural, +A]:
  def length: Int

  def map[B](f: A => B): Vect[N, B]

  def zip[B](that: Vect[N, B]): Vect[N, (A, B)]

  def concat[M <: Natural, B >: A](that: Vect[M, B]): Vect[Plus[N, M], B]

What does Vect[N, (A, B)] mean, especially (A, B)?

Upvotes: 3

Views: 91

Answers (2)

echo
echo

Reputation: 3134

(A,B) is type for tuple.

scala api has definition for zip, which is similar.

ref: https://docs.scala-lang.org/tour/tuples.html

Upvotes: 3

jwvh
jwvh

Reputation: 51271

A Vect is a type with 2 type parameters. The 1st must be some form of (sub-type of) Natural, we'll call it N. The 2nd is some unrestricted type defined at the call-site, we'll call it A.

The zip() method receives a different Vect. It must have the same 1st-parameter type (not just any Natural, it has to be the same) but the 2nd-parameter type might be different, we'll call it B. (It might be the same as A but it might not so it needs a different identifier.)

The zip() method returns a new Vect with the same 1st-parameter type but the 2nd type parameter is a 2-element tuple (a 2-ple) with the 1st element of type A and the 2nd element of type B.

The A and B have been "zipped" together.

Upvotes: 4

Related Questions