# Clause Interweaving, allowing \`def f\[T\](x: T)\[U\](y: U)\`

**URL:** <https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525>\
**Category:** Language Design\
**Created:** [January 5, 2022, 10:36pm UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525 "2022-01-05T22:36:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [January 5, 2022, 10:36pm UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/1 "2022-01-05T22:36:20Z")

</div>

Hello,  
In the context of my master semester project, I’m implementing Clause Interweaving, I would like to have your feedback!

### What it is:

It allows methods to have more than one type parameter clause, and for them to be arbitrarily interwoven with other clauses, basically a generalization of type currying:

```scala
//from
def foo[T,U](x: T)(y: U): Z
//to:
def bar[T](x: T)[U](y: U): Z

```

Note that at the use site, this behave very similarly to something already valid, but inelegant like:

```scala
def bar[T](x: T): { def apply[U](y: U): Z }

```

The only difference being overloading resolution, where the new system allows access to `U` and `y` where the old one doesn’t.

The full technical details can be found it this PR to the scala 2 doc (as there is no scala 3 doc yet):

> <https://github.com/Sporarum/scala/pull/2>
>
> This edits the scala 2 specification to document the changes that will be added …by \[Adding Interweaved Clauses\](https://github.com/lampepfl/dotty/pull/14019)
> It is not designed to be merged with the scala 2 specification as those changes target scala 3,
> But to instead document those changes, and allow an easier generation of the scala 3 docs when the time comes.
> 
> Please refer to the other PR for any remarks/questions/suggestions not linked with the documentation.

### Upsides:

#### Dependent methods:

The new syntax allows a clean way to have type parameters depend on values like this:

```scala
trait Key { type Value }

trait DB {
  def getOrElse(k: Key)[V >: k.Value](default: V): V
}

```

#### Omitting obvious types:

It allows us to separate types parameters the compiler can infer from the ones it can’t, for example:

```scala
trait Function[Arity <: Int & Singleton, Return]
def constant[A <: Int & Singleton][R](x: R): Function[A,R] = ???

```

In `constant`, `A` is impossible to infer if there is no expected type, so we have to specify it, however, the `R` can always be inferred, since we curried them, we can specify only `A` like so:

```scala
val oneInputToZero = constant[1](0) // A = 1, inferred: R = Int 
// Type: Function[1, Int]

```

### Restricions

Classes and types must still have at most one type parameter clause, and it has to be the first one, this might be confusing to users.

The behaviour at constructor-call-site can still be emulated for classes through a constructor proxy:

```scala
class MyClass[T,U](x: T, y: U){...}
object MyClass{
  def apply[T](x: T)[U](y: U) = new MyClass(x, y)
}

```

### Conclusion

I hope you find this feature useful, and am happy to answer any questions !

I believe this feature represents a somewhat dramatic change, and as such I expect people will have strong opinions about it, if you have any thoughts or feelings about this change, please share them here.  
This will help getting a feel for how the community at large would react !

---

<div class="post-metadata">

**Author:** ![tpolecat](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/tpolecat/32/41_2.png) [@tpolecat](https://contributors.scala-lang.org/u/tpolecat)\
**Post date:** [January 6, 2022, 1:11am UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/2 "2022-01-06T01:11:06Z")

</div>

This looks really useful. Can you say a little bit about the difference between

```scala
def bar[T](x: T)[U](y: U): Z

```

and

```scala
def bar[T](x: T): [U] => U => Z

```

?

---

<div class="post-metadata">

**Author:** ![milessabin](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/milessabin/32/115_2.png) [@milessabin](https://contributors.scala-lang.org/u/milessabin)\
**Post date:** [January 6, 2022, 4:02pm UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/3 "2022-01-06T16:02:11Z")

</div>

The latter allocates a function value whereas the former erases to `def bar(x: Any, y: Any): Z` and does not.

---

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [January 6, 2022, 5:08pm UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/4 "2022-01-06T17:08:07Z")

</div>

There’s another benefit:

### Overloading Resolution

Returned:

```scala
def bar[T](x: T): [U >: Int] => (y: Int) => U
def bar[T](x: T): [U >: String] => (y: String) => U

bar[Char]('c')(0) // error

```

Interweaved:

```scala
def bar[T](x: T)[U >: Int](y: Int): U
def bar[T](x: T)[U >: String](y: String): U

bar[Char]('c')(0) // finds correct method by looking at the type of y

```

---

<div class="post-metadata">

**Author:** ![jducoeur](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jducoeur/32/32_2.png) [@jducoeur](https://contributors.scala-lang.org/u/jducoeur)\
**Post date:** [January 6, 2022, 5:27pm UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/5 "2022-01-06T17:27:27Z")

</div>

From an end-user POV, it feels delightfully “Scala-ish” – it has that characteristic of just “doing the right thing”, and while I don’t expect I would use it _often_, that Dependent-methods use case is common enough to be compelling. I like it at first blush, and hope that it works out.

---

<div class="post-metadata">

**Author:** ![robmwalsh](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/robmwalsh/32/2288_2.png) [@robmwalsh](https://contributors.scala-lang.org/u/robmwalsh)\
**Post date:** [January 10, 2022, 11:53am UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/6 "2022-01-10T11:53:17Z")

</div>

I’ve wanted the ability to do something like the dependent method for ages, and separately wanted a way to omit obvious types. This is killing 2 birds with one stone. Great.

---

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [January 10, 2022, 5:21pm UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/7 "2022-01-10T17:21:01Z")

</div>

Hello,  
Thank you for your excitement ^^

In some cases, you might not even need those changes, as:  
Dependent _result_ types are already possible (for both methods and functions)

> **[Dependent Function Types](https://docs.scala-lang.org/scala3/book/types-dependent-function.html)**
>
> This section introduces and demonstrates dependent function types in Scala 3.

What this would add is the ability to have dependent _parameter_ types for methods

For functions it’s also already possible, but a bit awkward:

```auto
val getOrElse: (k: Key) => [V >: k.Value] => (default: V) => V = ???

```

---

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [February 17, 2022, 6:25pm UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/8 "2022-02-17T18:25:29Z")

</div>

Hello,  
I have cleaned and opened the PR, it now links and explains everything, and is more up to date that this,  
It also contains the implementation details, so I invite you to give it a look and give me some feedback on there !

---

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [February 18, 2022, 9:27pm UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/9 "2022-02-18T21:27:28Z")

</div>

Oops, forgot to link the PR, it’s [here](https://github.com/lampepfl/dotty/pull/14019)

---

<div class="post-metadata">

**Author:** ![sjrd](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sjrd/32/24_2.png) [@sjrd](https://contributors.scala-lang.org/u/sjrd)\
**Post date:** [April 29, 2022, 9:08am UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/10 "2022-04-29T09:08:56Z")

</div>

Since I’m in a streak of reviewing proposed language changes: I fully support these changes. The proposal looks really strong to me. I don’t see any issue with the design nor with backward compatibility.

---

<div class="post-metadata">

**Author:** ![odd](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/odd/32/690_2.png) [@odd](https://contributors.scala-lang.org/u/odd)\
**Post date:** [April 30, 2022, 1:57pm UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/11 "2022-04-30T13:57:15Z")

</div>

Perhaps we could allow identifiers to be part of the interleaving as well?

```auto
def a[T](x: T) b[U](y: U) c d[V](z: V): T | U | V

```

Call site:

```auto
a(1) b(2) c d(3)

```

---

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [April 30, 2022, 2:20pm UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/12 "2022-04-30T14:20:36Z")

</div>

On the whole I feel like it would create a lot of problems with parsing at call-site

Do you have an example of something that would work better with extra identifiers ?

---

<div class="post-metadata">

**Author:** ![sjrd](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sjrd/32/24_2.png) [@sjrd](https://contributors.scala-lang.org/u/sjrd)\
**Post date:** [April 30, 2022, 3:30pm UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/13 "2022-04-30T15:30:43Z")

</div>

That is an entire different beast, which would require a separate proposal.

---

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [February 3, 2023, 12:40pm UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/14 "2023-02-03T12:40:11Z")

</div>

Good news, the SIP has been accepted, and the implementation is complete !  
(You can find the PR [here](https://github.com/lampepfl/dotty/pull/14019))

It will therefore be available as experimental in the next release of Scala 3  
(As any experimental feature, you will need to be on an unstable release of Scala, and to `import scala.language.experimental.clauseInterleaving`)

When it does, do try it out, and give us feedback on what works and what doesn’t  
(While keeping in mind the feature has some [restrictions](https://github.com/scala/improvement-proposals/blob/main/content/clause-interleaving.md#restrictions))

We look forward to your feedback, and hope this will make the language better

PS: As described [here](https://github.com/lampepfl/dotty/blob/main/docs/_docs/reference/experimental/generalized-method-syntax.md#when-to-use), we recommend not using clause interleaving unless necessary, as it can make signatures harder to read

---

<div class="post-metadata">

**Author:** ![TomasMikula](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/tomasmikula/32/121_2.png) [@TomasMikula](https://contributors.scala-lang.org/u/TomasMikula)\
**Post date:** [February 3, 2023, 12:47pm UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/15 "2023-02-03T12:47:00Z")

</div>

Awesome, looking forward to it! 🎉

---

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [February 6, 2023, 1:59pm UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/16 "2023-02-06T13:59:22Z")

</div>

I was somewhat mistaken, the next release of Scala will be 3.3.0,  
whereas clause interleaving will be available in 3.3.1

---

<div class="post-metadata">

**Author:** ![rcano](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/rcano/32/3073_2.png) [@rcano](https://contributors.scala-lang.org/u/rcano)\
**Post date:** [May 12, 2023, 10:09pm UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/17 "2023-05-12T22:09:29Z")

</div>

I’m curious on whether this would extend to enum ADTs. Today if an entry of the enum requires an extra type not defined in the base ones, you are forced to declare all the types and manually extend and pass them along, which is terrible. With clause interweaving, syntax like

```scala
enum SomeAdt[A] {
  case Base(a: A)
  case Mapped[B](b: B => A)
}

```

could be then interpreted like

```scala
enum SomeAdt[A] {
  case class Base[A](a: A) extends SomeAdt[A]
  case class Mapped[A][B](b: B => A) extends SomeAdt[A]
}

```

which makes all the difference for some ADTs that are heavy on generics

---

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [May 30, 2023, 1:48pm UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/18 "2023-05-30T13:48:38Z")

</div>

I think that is a very cool idea, however clause interleaving only applies to methods right now, so the following is not allowed:

```scala
class MyClass[A](a: A)[B](b: B)

```

Do note the following is also not allowed:

```scala
def curriedTypes[A][B](a: A, b: B)

```

More information can be found in the SIP:

> <https://github.com/scala/improvement-proposals/blob/main/content/clause-interleaving.md#restrictions>

---

<div class="post-metadata">

**Author:** ![TomasMikula](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/tomasmikula/32/121_2.png) [@TomasMikula](https://contributors.scala-lang.org/u/TomasMikula)\
**Post date:** [June 11, 2023, 2:46pm UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/19 "2023-06-11T14:46:50Z")

</div>

[3.3.1-RC1 is out](https://github.com/lampepfl/dotty/releases/tag/3.3.1-RC1), with no mention of clause interleaving. Is this still planned for 3.3.1?

---

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [June 12, 2023, 11:47am UTC](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525/20 "2023-06-12T11:47:01Z")

</div>

I’m not sure, my guess is that experimental changes are not mentioned in these patch-notes ?

You should still be able to try it out on that version, but I’m not an expert in how the releases work

[Next page](https://contributors.scala-lang.org/t/clause-interweaving-allowing-def-f-t-x-t-u-y-u/5525.md?page=2)
