forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix definition of enclosingExtensionMethod
Now also finds enclosing methods outside the current class. Fixes scala#13075
- Loading branch information
1 parent
9a41298
commit 9adecd5
Showing
2 changed files
with
41 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
object Implementing_Tuples: | ||
|
||
sealed trait Tup | ||
case class ConsTup[T, H <: Tup](head: T, tail: H) extends Tup | ||
case object EmptyTup extends Tup | ||
|
||
val *: = ConsTup // for unapply | ||
type *:[H, T <: Tup] = ConsTup[H, T] // for type matching | ||
type EmptyTup = EmptyTup.type // for type matching | ||
|
||
extension [H](head: H) | ||
def *:[T <: Tup](tail: T) = ConsTup(head, tail) | ||
|
||
type Fold[T <: Tup, Seed, F[_,_]] = T match | ||
case EmptyTup => Seed | ||
case h *: t => Fold[t, F[Seed, h], F] | ||
|
||
extension [T <: Tup](v: T) | ||
def fold[Seed, F[_,_]](seed: Seed)( | ||
fn: [C, Acc] => (C, Acc) => F[C, Acc] | ||
): Fold[T, Seed, F] = | ||
(v match | ||
case EmptyTup => seed | ||
case h *: t => t.fold(fn(h, seed))(fn) | ||
).asInstanceOf[Fold[T, Seed, F]] | ||
|
||
extension [T <: Tup](v: T) def reversed: Tup = | ||
v.fold[EmptyTup, [C, Acc] =>> Acc match { | ||
case h *: t => C *: h *: t | ||
}](EmptyTup)( | ||
[C, Acc] => (c: C, acc: Acc) => acc match | ||
case _@(_ *: _) => c *: acc // error | ||
) | ||
|
||
@main def testProperFold = | ||
val t = (1 *: '2' *: "foo" *: EmptyTup) | ||
val reversed: (String *: Char *: Int *: EmptyTup) = t.reversed // error | ||
println(reversed) | ||
|
||
end Implementing_Tuples |