File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ package example
2+
3+ object RecMap {
4+
5+ object Record {
6+ // use this scope to bound who can see inside the opaque type
7+ opaque type Rec [A <: Tuple ] = Map [String , Any ]
8+
9+ object Rec {
10+ type HasKey [A <: Tuple , K ] =
11+ A match
12+ case (K , t) *: _ => t
13+ case _ *: t => HasKey [t, K ]
14+
15+ val empty : Rec [EmptyTuple ] = Map .empty
16+
17+ extension [A <: Tuple ](toMap : Rec [A ])
18+ def fetch [K <: String & Singleton ](key : K ): HasKey [A , K ] =
19+ toMap(key).asInstanceOf [HasKey [A , K ]]
20+ }
21+ }
22+
23+ def main (args : Array [String ]) =
24+ import Record ._
25+
26+ val foo : Any = Rec .empty.fetch(" foo" ) // error
27+ // ^
28+ // Match type reduction failed since selector EmptyTuple.type
29+ // matches none of the cases
30+ //
31+ // case (("foo" : String), t) *: _ => t
32+ // case _ *: t => example.RecMap.Record.Rec.HasKey[t, ("foo" : String)]
33+
34+ end main
35+ }
You can’t perform that action at this time.
0 commit comments