From b970344c2dd3919c943bff6d4494dd5a41cbbcba Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 6 Dec 2023 10:23:15 +0100 Subject: [PATCH 1/2] State all known limitations of right-associative extension methods --- .../right-associative-extension-methods.md | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/docs/_docs/reference/contextual/right-associative-extension-methods.md b/docs/_docs/reference/contextual/right-associative-extension-methods.md index 61f0beece6ed..af4c58429cdb 100644 --- a/docs/_docs/reference/contextual/right-associative-extension-methods.md +++ b/docs/_docs/reference/contextual/right-associative-extension-methods.md @@ -61,4 +61,36 @@ For instance, the `+::` method above would become This expansion has to be kept in mind when writing right-associative extension methods with inter-parameter dependencies. -An overall simpler design could be obtained if right-associative operators could _only_ be defined as extension methods, and would be disallowed as normal methods. In that case neither arguments nor parameters would have to be swapped. Future versions of Scala should strive to achieve this simplification. +This expansion also introduces some inconsistencies when calling the extension methods in non infix form. The user needs to invert the order of the arguments at call site manually. For instance: + +```scala + extension [T](x: T) + def *:(xs: List[T]): List[T] = ... + + y.*:(ys) // error when following the parameter definition order + ys.*:(y) + + *:(y)(ys) // error when following the parameter definition order + *:(ys)(y) +``` + +Another limitation of this representation is that it is impossible to pass the +type parameters of the `def` explicitly. For instance: + +```scala + extension (x: Int) + def *:[T](xs: List[T]): List[T] = ... + + xs.*:[Int](1) // error when trying to set T explicitly +``` + +The expansion of right-associative extension methods also affects the order in which contextual parameters can be passed explicitly. + +Group extension can also behave unintuitively, in general all extension in a +group are extension on the receiver. Except if one of these extensions is a +right-associative extension method, in which case that one is an extension on the type of its argument. For instance: +```scala + extension (a: Int) + def :+(b: Long): Long = ... // extension on Int + def +:(b: Long): Long = ... // extension on Long +``` From 3647acff81783fa439a8f05b039148870bf29e0c Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Mon, 11 Dec 2023 11:54:13 +0100 Subject: [PATCH 2/2] Update docs/_docs/reference/contextual/right-associative-extension-methods.md Co-authored-by: Jamie Thompson --- .../reference/contextual/right-associative-extension-methods.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/_docs/reference/contextual/right-associative-extension-methods.md b/docs/_docs/reference/contextual/right-associative-extension-methods.md index af4c58429cdb..0388d173cd2c 100644 --- a/docs/_docs/reference/contextual/right-associative-extension-methods.md +++ b/docs/_docs/reference/contextual/right-associative-extension-methods.md @@ -75,7 +75,7 @@ This expansion also introduces some inconsistencies when calling the extension m ``` Another limitation of this representation is that it is impossible to pass the -type parameters of the `def` explicitly. For instance: +type parameters of the `def` explicitly, (unless called in prefix form). For instance: ```scala extension (x: Int)