-
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
Merged
nicolasstucki
merged 10 commits into
scala:master
from
dotty-staging:refined-selectable
Dec 20, 2019
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
18c6f97
Add basic structure for refined selectable macros
biboudis e2998b9
Fix checkfile
biboudis b44002c
Add apply method for object Refinement in tasty reflect
biboudis 8b17ed0
Fix toSelectableImpl
biboudis 3b71297
Rename directory and HMap to toTuple
biboudis 7992f6f
Check all tuple structures
biboudis da2f05b
Apply suggestions from code review
da393a4
Fix indentation
biboudis b8075f7
Add a small aliased test
biboudis b0027c6
Add negtests and apply as macro to remove the need for asInstanceOf
biboudis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,5 @@ | ||
((name,Emma),(age,42)) | ||
|
||
Record() | ||
Record(field1=1, field2=2, field3=3, field4=4, field5=5, field6=6, field7=7, field8=8, field9=9, field10=10, field11=11, field12=12, field13=13, field14=14, field15=15, field16=16, field17=17, field18=18, field19=19, field20=20, field21=21, field22=22, field23=23, field24=24, field25=25) | ||
Record(name=Emma, age=42) |
This file contains hidden or 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,95 @@ | ||
import scala.quoted._ | ||
|
||
object Macro { | ||
|
||
trait SelectableRecord extends Selectable { | ||
inline def toTuple <: Tuple = ${ toTupleImpl('this)} | ||
} | ||
|
||
trait SelectableRecordCompanion[T] { | ||
protected def fromUntypedTuple(elems: (String, Any)*): T | ||
inline def fromTuple[T <: Tuple](s: T) <: Any = ${ fromTupleImpl('s, '{ (x: Array[(String, Any)]) => fromUntypedTuple(x: _*) } ) } | ||
} | ||
|
||
private def toTupleImpl(s: Expr[Selectable])(given qctx:QuoteContext): Expr[Tuple] = { | ||
import qctx.tasty.{given, _} | ||
|
||
val repr = s.unseal.tpe.widenTermRefExpr.dealias | ||
|
||
def rec(tpe: Type): List[(String, Type)] = { | ||
tpe match { | ||
case Refinement(parent, name, info) => | ||
info match { | ||
case _: TypeBounds => | ||
rec(parent) | ||
case _: MethodType | _: PolyType | _: TypeBounds => | ||
qctx.warning(s"Ignored $name as a field of the record", s) | ||
rec(parent) | ||
case info: Type => | ||
(name, info) :: rec(parent) | ||
} | ||
|
||
case _ => Nil | ||
} | ||
} | ||
|
||
def tupleElem(name: String, info: Type): Expr[Any] = { | ||
val nameExpr = Expr(name) | ||
info.seal match { case '[$qType] => | ||
Expr.ofTuple(Seq(nameExpr, '{ $s.selectDynamic($nameExpr).asInstanceOf[$qType] })) | ||
} | ||
} | ||
|
||
val ret = rec(repr).reverse.map(e => tupleElem(e._1, e._2)) | ||
|
||
Expr.ofTuple(ret) | ||
} | ||
|
||
private def fromTupleImpl[T: Type](s: Expr[Tuple], newRecord: Expr[Array[(String, Any)] => T])(given qctx:QuoteContext): Expr[Any] = { | ||
import qctx.tasty.{given, _} | ||
|
||
val repr = s.unseal.tpe.widenTermRefExpr.dealias | ||
|
||
def isTupleCons(sym: Symbol): Boolean = sym.owner == defn.ScalaPackageClass && sym.name == "*:" | ||
|
||
def extractTuple(tpe: TypeOrBounds, seen: Set[String]): (Set[String], (String, Type)) = { | ||
tpe match { | ||
// Tuple2(S, T) where S must be a constant string type | ||
case AppliedType(parent, ConstantType(Constant(name: String)) :: (info: Type) :: Nil) if (parent.typeSymbol == defn.TupleClass(2)) => | ||
if seen(name) then | ||
qctx.error(s"Repeated record name: $name", s) | ||
(seen + name, (name, info)) | ||
case _ => | ||
qctx.error("Tuple type was not explicit expected `(S, T)` where S is a singleton string", s) | ||
(seen, ("<error>", defn.AnyType)) | ||
} | ||
} | ||
def rec(tpe: Type, seen: Set[String]): List[(String, Type)] = { | ||
if tpe =:= defn.UnitType then Nil | ||
else tpe match { | ||
// head *: tail | ||
case AppliedType(parent, List(head, tail: Type)) if isTupleCons(parent.typeSymbol) => | ||
val (seen2, head2) = extractTuple(head, seen) | ||
head2 :: rec(tail, seen2) | ||
// Tuple1(...), Tuple2(...), ..., Tuple22(...) | ||
case AppliedType(parent, args) if defn.isTupleClass(parent.typeSymbol) => | ||
(args.foldLeft((seen, List.empty[(String, Type)])){ case ((seenAcc, acc), arg) => | ||
val (seen3, arg2) = extractTuple(arg, seenAcc) | ||
(seen3, arg2 :: acc) | ||
})._2 | ||
// Tuple | ||
case _ => | ||
qctx.error("Tuple type must be of known size", s) | ||
Nil | ||
} | ||
} | ||
|
||
val r = rec(repr, Set.empty) | ||
|
||
val refinementType = r.foldLeft('[T].unseal.tpe)((acc, e) => Refinement(acc, e._1, e._2)).seal | ||
|
||
refinementType match { case '[$qType] => | ||
'{ $newRecord($s.toArray.map(e => e.asInstanceOf[(String, Any)])).asInstanceOf[${qType}] } | ||
} | ||
} | ||
} |
This file contains hidden or 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,70 @@ | ||
import Macro._ | ||
|
||
object Test { | ||
|
||
// TODO should elems of `new Record` and `Record.fromUntypedTuple` be IArray[Object] | ||
// This would make it possible to keep the same reference to the elements when transforming a Tuple into a Record (or vice versa) | ||
|
||
case class Record(elems: (String, Any)*) extends SelectableRecord { | ||
def selectDynamic(name: String): Any = elems.find(_._1 == name).get._2 | ||
override def toString(): String = elems.map(x => x._1 + "=" + x._2).mkString("Record(", ", ", ")") | ||
} | ||
|
||
object Record extends SelectableRecordCompanion[Record] { | ||
def fromUntypedTuple(elems: (String, Any)*): Record = Record(elems: _*) | ||
} | ||
|
||
type Person = Record { | ||
val name: String | ||
val age: Int | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
val person: Person = Record("name" -> "Emma", "age" -> 42).asInstanceOf[Person] | ||
|
||
val res = person.toTuple | ||
|
||
val p0 = person.asInstanceOf[Record { | ||
val name: String | ||
def age: Int // ignored | ||
}] | ||
p0.toTuple | ||
|
||
val p2: Record { | ||
val age: Int | ||
val name: String | ||
} = person | ||
|
||
p2.toTuple : (("age", Int), ("name", String)) | ||
|
||
println(res) | ||
println() | ||
|
||
res: (("name", String), ("age", Int)) | ||
|
||
val res2 = Record.fromTuple(res) | ||
|
||
val emptyTuple = () | ||
println(Record.fromTuple(emptyTuple)) | ||
|
||
// println(Record.fromTuple((1, 2))) // error | ||
|
||
val xxl: (("field1", Int),("field2", Int),("field3", Int),("field4", Int),("field5", Int),("field6", Int),("field7", Int),("field8", Int),("field9", Int),("field10", Int),("field11", Int),("field12", Int),("field13", Int),("field14", Int),("field15", Int),("field16", Int),("field17", Int),("field18", Int),("field19", Int),("field20", Int),("field21", Int),("field22", Int),("field23", Int),("field24", Int),("field25", Int)) = ("field1" -> 1,"field2" -> 2,"field3" -> 3,"field4" -> 4,"field5" -> 5,"field6" -> 6,"field7" -> 7,"field8" -> 8,"field9" -> 9,"field10" -> 10,"field11" -> 11,"field12" -> 12,"field13" -> 13,"field14" -> 14,"field15" -> 15,"field16" -> 16,"field17" -> 17,"field18" -> 18,"field19" -> 19,"field20" -> 20,"field21" -> 21,"field22" -> 22,"field23" -> 23,"field24" -> 24,"field25" -> 25) | ||
println(Record.fromTuple(xxl)) | ||
// println(Record.fromTuple(("field1" -> 1,"field2" -> 2,"field3" -> 3,"field4" -> 4,"field5" -> 5,"field6" -> 6,"field7" -> 7,"field8" -> 8,"field9" -> 9,"field10" -> 10,"field11" -> 11,"field12" -> 12,"field13" -> 13,"field14" -> 14,"field15" -> 15,"field16" -> 16,"field17" -> 17,"field18" -> 18,"field19" -> 19,"field20" -> 20,"field21" -> 21,"field22" -> 22,"field23" -> 23,"field24" -> 24,"field25" -> 25))) | ||
|
||
println(res2) | ||
|
||
// Record.fromTuple[(("name", String), ("name", Int))]("name" -> "aa", "name" -> 3) // error | ||
biboudis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
res2: Record { | ||
val name: String | ||
val age: Int | ||
} | ||
|
||
res2: Record { | ||
val age: Int | ||
val name: String | ||
} | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.