Skip to content

Fix #9033, fix #9056: Add EmptyTuple #9049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2020
Merged

Conversation

nicolasstucki
Copy link
Contributor

@nicolasstucki nicolasstucki commented May 26, 2020

With this PR we change to the following hierarchy where all generic tuple types erase to Product.

Product -- Tuple -+- EmptyTuple
                  |
                  +- NonEmptyTuple -- *:[Head, Tail <: Tuple]

These are encoded using the following compile-time only classes

sealed trait Tuple extends Product { ... }
object EmptyTuple extends Tuple { ... }
type EmptyTuple = EmptyTuple.type
sealed trait NonEmptyTuple extends Tuple { ... }
sealed abstract class *:[+H, +T <: Tuple] extends NonEmptyTuple { ... }

TupleN classes for 1 < N <= 22 are made subtypes of *: with precise type (as done before). But Unit is not anymore related to Tuple.

Construction of empty tuples can be done with Tuple(), tuples with one element with Tuple(e) while any other tuple are created with (e1, ..., en). These syntactic forms can also be used to match over tuples.

Fixes #9033 and fixes #9056.

@nicolasstucki nicolasstucki self-assigned this May 26, 2020
@nicolasstucki nicolasstucki changed the title Fix #9033: Generic tuples instances of Product Fix #9033: Make generic tuples instances of Product May 26, 2020
@nicolasstucki nicolasstucki force-pushed the fix-#9033 branch 8 times, most recently from 0bf02ff to 4e61497 Compare May 27, 2020 07:58
@nicolasstucki nicolasstucki force-pushed the fix-#9033 branch 5 times, most recently from 18a04c8 to fcdcfdb Compare May 27, 2020 14:06
@nicolasstucki nicolasstucki marked this pull request as ready for review May 29, 2020 08:17
@nicolasstucki nicolasstucki requested a review from odersky May 29, 2020 08:17
@odersky odersky assigned nicolasstucki and unassigned odersky May 29, 2020
@nicolasstucki nicolasstucki force-pushed the fix-#9033 branch 5 times, most recently from 51e6298 to 1eadb03 Compare June 8, 2020 07:11
@nicolasstucki nicolasstucki changed the title Fix #9033: Make generic tuples instances of Product community-build/community-projects/shapeless Jun 8, 2020
We change to the following hierarchy where all generic tuple types erase to `Product`.

```
Product -- Tuple -+- EmptyTuple
                  |
                  +- NonEmptyTuple -- *:[Head, Tail <: Tuple]
```

These are encoded using the following compile-time only classes

```scalla
sealed trait Tuple extends Product { ... }
object EmptyTuple extends Tuple { ... }
type EmptyTuple = EmptyTuple.type
sealed trait NonEmptyTuple extends Tuple { ... }
sealed abstract class *:[+H, +T <: Tuple] extends NonEmptyTuple { ... }
```
`TupleN` classes for `1 < N <= 22` are made subtypes of `*:` with precise type (as done before). But `Unit` is not anymore related to `Tuple`.

Construction of empty tuples can be done with `Tuple()`, tuples with one element with `Tuple(e)` while any other tuple are created with `(e1, ..., en)`.
@nicolasstucki nicolasstucki changed the title community-build/community-projects/shapeless Fix #9033, fix #9056: Add EmptyTuple Jun 8, 2020
@nicolasstucki nicolasstucki requested a review from odersky June 8, 2020 11:01
// ()
Literal(Constant(()))
// scala.EmptyTuple
ref(defn.EmptyTupleModule.termRef)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe define a type in Definitions for defn.EmptyTupleModule.termRef.

@odersky odersky merged commit b301e4c into scala:master Jun 8, 2020
@odersky odersky deleted the fix-#9033 branch June 8, 2020 16:47
@smarter
Copy link
Member

smarter commented Jun 10, 2020

FWIW, it looks like this PR lead to a 30% slowdown in the "Tuple reverse (Runtime)" benchmark: http://dotty-bench.epfl.ch/

@nicolasstucki
Copy link
Contributor Author

@liufengyun how can I run this benchmark locally?

@nicolasstucki
Copy link
Contributor Author

Found it dotty-bench-run/jmh:run 3 10 1 .*reverse.*

@nicolasstucki
Copy link
Contributor Author

The performance regression comes from the sound type test to fix #9056. We will be able to recover this performance when we brack the API in 3.1 by not erasing Tuple and NonEmptyTuple.

https://github.com/lampepfl/dotty/pull/9049/files#diff-69edc152a7680e521f5bfba13aef8220R462

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unsound generic tuple type test Empty Tuple is not instance of Product
4 participants