File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -51,5 +51,46 @@ object Test {
51
51
val c = xx(" hh" , 22 )
52
52
53
53
val c1 : Int = c
54
+
55
+ Contextual .main(args)
56
+ }
57
+ }
58
+
59
+ object Contextual {
60
+
61
+ class Key [+ V ]
62
+
63
+ class Context (bindings : Map [Key [Any ], Any ]) {
64
+ def binding [V ](key : Key [V ]): Option [V ] =
65
+ bindings.get(key).asInstanceOf [Option [V ]]
66
+ def withBinding [V ](key : Key [V ], value : V ): Context =
67
+ new Context (bindings + ((key, value)))
68
+ }
69
+
70
+ val rootContext = new Context (Map ())
71
+
72
+ val Source = new Key [String ]
73
+ val Options = new Key [List [String ]]
74
+
75
+ type Ctx [T ] = implicit Context => T
76
+
77
+ def ctx : Ctx [Context ] = implicitly[Context ]
78
+
79
+ def compile (s : String ): Ctx [Boolean ] =
80
+ runOn(new java.io.File (s))(ctx.withBinding(Source , s)) >= 0
81
+
82
+ def runOn (f : java.io.File ): Ctx [Int ] = {
83
+ val options = List (" -verbose" , " -explaintypes" )
84
+ process(f)(ctx.withBinding(Options , options))
85
+ }
86
+
87
+ def process (f : java.io.File ): Ctx [Int ] =
88
+ ctx.binding(Source ).get.length - ctx.binding(Options ).get.length
89
+
90
+ def main (args : Array [String ]) = {
91
+ implicit val context : Context = rootContext
92
+ assert(compile(" abc" ))
93
+ assert(compile(" ab" ))
94
+ assert(! compile(" a" ))
54
95
}
55
96
}
You can’t perform that action at this time.
0 commit comments