Skip to content
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

Allow named tuple syntax for case class constructors. #22400

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Jan 17, 2025

This is a tweak to the named tuple spec and implementation. If the expected type is a case class,
expand a named tuple to a call of the apply method of the case class.

Based on #21993. Only the last commit is relevant.

A test case that shows that we can have an "inline type class" that allows
to use a typeclass-based scheme for sequence literals where instances can
be created with macros.
Also: Move test to run
We now don't try to instantiate selected type variables. Instead, we use a default as fallback if
the expected type is underspecified according to the definition in Implicits. This is
simpler and more expressive.
Also, test infix operations taking [...] as a right operand.
Some corrections in docs, fix typos, drop unused code.
This is a tweak to the named tuple spec. If the expected type is a case class
expand to a call of its apply method.
val anotherLit: IArray[Double] = [math.Pi, math.cos(2.0), math.E * 3.0]
val diag: Array[Array[Int]] = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
val empty: ArrayBuffer[Object] = []
val mapy: HashMap[Int, String] = [1 -> "one", 2 -> "two", 3 -> "three"]
Copy link

Choose a reason for hiding this comment

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

I more like Dartlang's syntax for this, here, [] is an Array, which makes me think the KEY can be REPEATED, but actually, it isn't.

val mapy: HashMap[Int, String] = [1 -> "one", 2 -> "two", 3 -> "three"] can be
val mapy: Array[(Int, String)] = [1 -> "one", 2 -> "two", 3 -> "three"]

but if you chose Dart's syntax, then the HashMap[Int, String] can be omitted.

val mapy = {1 -> "one", 2 -> "two", 3 -> "three"} is a map or
val mapy = {1 : "one", 2 : "two", 3 :"three"} is a map too.

Copy link

Choose a reason for hiding this comment

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

When all are [], you lose the information of if the element or key can be repeated, and then you have to annotate the result type explicitly, which is not how Scala's works, a well static typed language as a dynamic language, and now, with [] everywhere, we are writing plain old Java.

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.

2 participants