-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add refined tupled records prototype #7731
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
Add refined tupled records prototype #7731
Conversation
Co-authored-by: Nicolas Stucki <[email protected]>
fedc8f0
to
18c6f97
Compare
compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala
Outdated
Show resolved
Hide resolved
Co-Authored-By: Nicolas Stucki <[email protected]>
I'm not super-keen on the aesthetics of the val person: Person = Record("name" -> "Emma", "age" -> 42).asInstanceOf[Person] I appreciate that we need to mention Could, val person: Person = Record("name" -> "Emma", "age" -> 42).as[Person] be made to work? |
Probably a record should have a better constructor. One that could figure out the refined type. |
@nicolasstucki do you mean a smarter implementation of the constructor or something like |
I meant |
After a quick brainstorming with @liufengyun we propose some functionality on tasty-reflect like the following where object Record extends SelectableRecordCompanion[Record] {
def fromUntypedTuple(elems: (String, Any)*): Record = Record(elems: _*)
inline def apply(elems: (String, Any)*) <: Record = ${ applyImpl('elem)) }
def applyImpl(elems: Expr[Seq[(String, Any)]])(given qctx: QuoteContext) = {
type TT
implicit val ttype: quoted.Type[TT] = qctx.expected.seal.asInstanceOf[quoted.Type[TT]]
'{ new Record(elems).asInstanceOf[TT] }
}
} 😃 😃 |
That is horribly unsound |
You probably want something in the lines of inline def apply[R <: Record](elems: (String, Any)*) <: R = ${ applyImpl('elem, '[R])) } |
I assumed we wanted to avoid the type parameter 😛! |
Why can’t the following work? val person: Person = Record("name" -> "Emma", "age" -> 42) |
Hmm the compiler cannot infer this ATM 🤷♂ I guess it would need some tweaking in the Typer. |
No, the apply method would just be another macro |
We are getting off-topic. This PR is about prototyping the core macro logic for refinement types. Any API design concerns are for later. |
After the last commit I have two observations to share:
If I don't, the compiler infers |
Yes, typeCheckErrors may have the same error many times. That's because it takes raw error list from the reporters of the parser and typer phases and that's what the reporters have. I don't know why precisely they may have duplicates so I didn't do any filtering. |
The only filtering done in |
95e1738
to
b0027c6
Compare
This PR adds two macros,
toTuple
andfromTuple
that transform a refinement type into a tuple and vice-versa:To enable this functionality the user would need to
SelectableRecord
instead ofSelectable
, e.g., :