scalacenter / student-projects

The list of the available projects at the Scala Center for bachelor and master students.
8 stars 0 forks source link

Generalized vararg expansion in Scala 3 #3

Closed adpi2 closed 1 year ago

adpi2 commented 2 years ago

This task is probably too short to constitute an entire bachelor, or semester project but it could be combined with another task.

Description

A vararg parameter, or repeated parameter, is a way to declare a method (or pattern) that can take an arbitrary number of arguments. The value of the vararg parameter is converted into a Seq of all the arguments.

Example:

def sum(args: Int*): Int = {
  args.fold(0)(_ + _) // args is a Seq[Int]
}

sum()        // 0
sum(1)       // 1
sum(1, 2, 3) // 6

In the current version of the Scala language, it is also possible to pass a sequence argument into a vararg parameter using the postfix * operator. This is only valid if the sequence argument is the only argument of the parameter list.

val xs = Seq(2, 3)
sum(xs*)    // 5
sum(xs*, 1) // invalid: xs* must be the only argument

The goal of this project is to generalize the passing of multiple sequence arguments, combined with single arguments, in any order.

sum(
  1.to(3)*,                          // a sequence argument of type  Range[Int] <: Seq[Int]
  4,                                 // a single argument of type Int
  (intOption: Option[Int]).toSeq*,   // another sequence argument of type Seq[Int]
  Seq(6, 7)*                         // yet another sequence argument of type Seq[Int]
)

Supervisors

Adrien Piquerez(adrien.piquerez@epfl.ch): Tooling Engineer at the Scala Center Sébastien Doeraene(sebastien.doeraene@epfl.ch): Principal Engineer at the Scala Center and author of Scala.js

Prerequisites

Expected Outcome

A working and well-tested implementation of the generalized vararg expansion in Scala 3.