1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
//############################################################################
// Type Representation at runtime
//############################################################################
// $Id: $
import TypeRep._
object Test extends Application {
testPrimitives
testOptions
testLists
testArrays
testTuples
testFuncs
testClasses
}
object serialize {
import java.io._
@throws(classOf[IOException])
def write[A](o: A): Array[Byte] = {
val ba = new ByteArrayOutputStream(512)
val out = new ObjectOutputStream(ba)
out.writeObject(o)
out.close()
ba.toByteArray()
}
@throws(classOf[IOException])
@throws(classOf[ClassNotFoundException])
def read[A](buffer: Array[Byte]): A = {
val in =
new ObjectInputStream(new ByteArrayInputStream(buffer))
in.readObject().asInstanceOf[A]
}
}
object testPrimitives {
println(getType(true))
val b = false; val bt = getType(b)
println(bt)
val bt1: TypeRep[Boolean] = serialize.read(serialize.write(bt))
println(bt1 == bt)
println(getType(16.toByte))
println(getType('a'))
println(getType(3))
println(getType(3l))
println(getType(0.0f))
println(getType(0.0d))
println(getType("abc"))
println(getType(())) // Unit
// println(getType(classOf[Int])) // Class
println
}
object testOptions {
println(getType(Some(2)))
val x: Option[Int] = Some(2)
println(getType(x))
println(getType(Some(Some(3))))
println(getType(None: Option[Int]))
val y: Option[Int] = None
println(getType(y))
println
}
object testLists {
println(getType(List(3)))
println(getType(3 :: Nil))
println(getType(List(List(3))))
println(getType(Nil: List[Int]))
println(getType(List(1, "abc")))
println
}
object testArrays {
println(getType(Array(3)))
println(getType(Array(Array(3), Array(4))))
println(getType(new Array[Int](0)))
println(getType(List(1).toArray))
println(getType(List[Int]().toArray))
println(getType(Array(3).drop(1).toArray)) // empty
println
}
object testTuples {
println(getType((3, "abc")))
println(getType(Triple('a', 'b', "c")))
println(getType(((3, "abc"), (4, "xyz"))))
println(getType(((Some('b'), 3), (Some('a'), 4))))
//println(getType(((Some('b'), 3), (None, 4))))
println
}
object testFuncs {
def f1(x: Int): Int = 2 * x
println(getType(f1 _))
println(getType(f1(2)))
val f2 = (x: Int) => 2 * x
println(getType(f2))
println(getType(f2(2)))
val f3 = (x: Int) => (y: Int) => x + y
println(getType(f3))
println(getType(f3(2)))
println(getType(f3(2)(2)))
def f4(b: Boolean, c: List[Char], i: Int): Int = i
println(getType(f4 _))
def f5(f: Int => Int, x: Int) = f(x)
println(getType(f5 _))
println(getType(f5(f1, 1)))
println
}
class Foo {
class Bar(x: Int)
}
object foo extends Foo
package pkg1 {
class C1
object c1 extends C1
}
object testClasses {
/*
case object FooRep extends TypeRep[Foo] {
override def toString = "Foo"
}
implicit def fooRep[A](x: A)(implicit rep: TypeRep[foo.type]): TypeRep[foo.type] = rep
println(getType(foo))
println(getType(new foo.Bar(0)))
val foo2 = new Foo
println(getType(foo2))
println(getType(new foo2.Bar(1)))
println
println(getType(pkg1.c1))
val c1 = new pkg1.C1
println(getType(c1))
println
*/
}
/**
* @author Stephane Micheloud
* @version 1.0
*/
abstract class TypeRep[A]
object TypeRep {
def getType[A](x: A)(implicit rep: TypeRep[A]): TypeRep[A] = rep
def getType[A](x: Option[A])(implicit rep: TypeRep[A]): TypeRep[Option[A]] = (x match {
case Some(v) => SomeRep(rep)
case None => NoneRep // or NoneRep(rep)
}).asInstanceOf[TypeRep[Option[A]]]
def getType[A](x: List[A])(implicit rep: TypeRep[A]): TypeRep[List[A]] = (x match {
case h :: t => ListRep(getType(h))
case Nil => NilRep
}).asInstanceOf[TypeRep[List[A]]]
implicit def boolRep: TypeRep[Boolean] = BooleanRep
implicit def byteRep: TypeRep[Byte] = ByteRep
implicit def charRep: TypeRep[Char] = CharRep
implicit def shortRep: TypeRep[Short] = ShortRep
implicit def intRep: TypeRep[Int] = IntRep
implicit def longRep: TypeRep[Long] = LongRep
implicit def floatRep: TypeRep[Float] = FloatRep
implicit def doubleRep: TypeRep[Double] = DoubleRep
implicit def unitRep: TypeRep[Unit] = UnitRep
// implicit def classRep: TypeRep[Class] = ClassRep
implicit def stringRep: TypeRep[String] = StringRep
implicit def noneRep: TypeRep[Option[Nothing]] = NoneRep[Nothing](NothingRep.asInstanceOf[TypeRep[Nothing]])
implicit def anyRep: TypeRep[Any] = AnyRep
implicit def nothingRep: TypeRep[Nothing] = NothingRep
implicit def someRep[A](implicit elemrep: TypeRep[A]): TypeRep[Some[A]] =
SomeRep(elemrep)
implicit def listRep[A](implicit elemrep: TypeRep[A]): TypeRep[List[A]] =
ListRep(elemrep)
implicit def arrayRep[A](implicit elemrep: TypeRep[A]): TypeRep[Array[A]] =
ArrayRep(elemrep)
implicit def tuple2Rep[A1, A2](implicit _1: TypeRep[A1], _2: TypeRep[A2]): TypeRep[Tuple2[A1, A2]] =
Tuple2Rep(_1, _2)
implicit def tuple3Rep[A1, A2, A3](implicit _1: TypeRep[A1], _2: TypeRep[A2], _3: TypeRep[A3]): TypeRep[Tuple3[A1, A2, A3]] =
Tuple3Rep(_1, _2, _3)
implicit def tuple4Rep[A1, A2, A3, A4](implicit _1: TypeRep[A1], _2: TypeRep[A2], _3: TypeRep[A3], _4: TypeRep[A4]): TypeRep[Tuple4[A1, A2, A3, A4]] =
Tuple4Rep(_1, _2, _3, _4)
implicit def tuple5Rep[A1, A2, A3, A4, A5](implicit _1: TypeRep[A1], _2: TypeRep[A2], _3: TypeRep[A3], _4: TypeRep[A4], _5: TypeRep[A5]): TypeRep[Tuple5[A1, A2, A3, A4, A5]] =
Tuple5Rep(_1, _2, _3, _4, _5)
implicit def tuple6Rep[A1, A2, A3, A4, A5, A6](implicit _1: TypeRep[A1], _2: TypeRep[A2], _3: TypeRep[A3], _4: TypeRep[A4], _5: TypeRep[A5], _6: TypeRep[A6]): TypeRep[Tuple6[A1, A2, A3, A4, A5, A6]] =
Tuple6Rep(_1, _2, _3, _4, _5, _6)
implicit def tuple7Rep[A1, A2, A3, A4, A5, A6, A7](implicit _1: TypeRep[A1], _2: TypeRep[A2], _3: TypeRep[A3], _4: TypeRep[A4], _5: TypeRep[A5], _6: TypeRep[A6], _7: TypeRep[A7]): TypeRep[Tuple7[A1, A2, A3, A4, A5, A6, A7]] =
Tuple7Rep(_1, _2, _3, _4, _5, _6, _7)
implicit def tuple8Rep[A1, A2, A3, A4, A5, A6, A7, A8](implicit _1: TypeRep[A1], _2: TypeRep[A2], _3: TypeRep[A3], _4: TypeRep[A4], _5: TypeRep[A5], _6: TypeRep[A6], _7: TypeRep[A7], _8: TypeRep[A8]): TypeRep[Tuple8[A1, A2, A3, A4, A5, A6, A7, A8]] =
Tuple8Rep(_1, _2, _3, _4, _5, _6, _7, _8)
implicit def tuple9Rep[A1, A2, A3, A4, A5, A6, A7, A8, A9](implicit _1: TypeRep[A1], _2: TypeRep[A2], _3: TypeRep[A3], _4: TypeRep[A4], _5: TypeRep[A5], _6: TypeRep[A6], _7: TypeRep[A7], _8: TypeRep[A8], _9: TypeRep[A9]): TypeRep[Tuple9[A1, A2, A3, A4, A5, A6, A7, A8, A9]] =
Tuple9Rep(_1, _2, _3, _4, _5, _6, _7, _8, _9)
implicit def func1Rep[A1, B](implicit a1: TypeRep[A1], b: TypeRep[B]): TypeRep[Function1[A1, B]] =
Function1Rep(a1, b)
implicit def func2Rep[A1, A2, B](implicit a1: TypeRep[A1], a2: TypeRep[A2], b: TypeRep[B]): TypeRep[Function2[A1, A2, B]] =
Function2Rep(a1, a2, b)
implicit def func3Rep[A1, A2, A3, B](implicit a1: TypeRep[A1], a2: TypeRep[A2], a3: TypeRep[A3], b: TypeRep[B]): TypeRep[Function3[A1, A2, A3, B]] =
Function3Rep(a1, a2, a3, b)
implicit def func4Rep[A1, A2, A3, A4, B](implicit a1: TypeRep[A1], a2: TypeRep[A2], a3: TypeRep[A3], a4: TypeRep[A4], b: TypeRep[B]): TypeRep[Function4[A1, A2, A3, A4, B]] =
Function4Rep(a1, a2, a3, a4, b)
implicit def func5Rep[A1, A2, A3, A4, A5, B](implicit a1: TypeRep[A1], a2: TypeRep[A2], a3: TypeRep[A3], a4: TypeRep[A4], a5: TypeRep[A5], b: TypeRep[B]): TypeRep[Function5[A1, A2, A3, A4, A5, B]] =
Function5Rep(a1, a2, a3, a4, a5, b)
implicit def func6Rep[A1, A2, A3, A4, A5, A6, B](implicit a1: TypeRep[A1], a2: TypeRep[A2], a3: TypeRep[A3], a4: TypeRep[A4], a5: TypeRep[A5], a6: TypeRep[A6], b: TypeRep[B]): TypeRep[Function6[A1, A2, A3, A4, A5, A6, B]] =
Function6Rep(a1, a2, a3, a4, a5, a6, b)
implicit def func7Rep[A1, A2, A3, A4, A5, A6, A7, B](implicit a1: TypeRep[A1], a2: TypeRep[A2], a3: TypeRep[A3], a4: TypeRep[A4], a5: TypeRep[A5], a6: TypeRep[A6], a7: TypeRep[A7], b: TypeRep[B]): TypeRep[Function7[A1, A2, A3, A4, A5, A6, A7, B]] =
Function7Rep(a1, a2, a3, a4, a5, a6, a7, b)
implicit def func8Rep[A1, A2, A3, A4, A5, A6, A7, A8, B](implicit a1: TypeRep[A1], a2: TypeRep[A2], a3: TypeRep[A3], a4: TypeRep[A4], a5: TypeRep[A5], a6: TypeRep[A6], a7: TypeRep[A7], a8: TypeRep[A8], b: TypeRep[B]): TypeRep[Function8[A1, A2, A3, A4, A5, A6, A7, A8, B]] =
Function8Rep(a1, a2, a3, a4, a5, a6, a7, a8, b)
implicit def func9Rep[A1, A2, A3, A4, A5, A6, A7, A8, A9, B](implicit a1: TypeRep[A1], a2: TypeRep[A2], a3: TypeRep[A3], a4: TypeRep[A4], a5: TypeRep[A5], a6: TypeRep[A6], a7: TypeRep[A7], a8: TypeRep[A8], a9: TypeRep[A9], b: TypeRep[B]): TypeRep[Function9[A1, A2, A3, A4, A5, A6, A7, A8, A9, B]] =
Function9Rep(a1, a2, a3, a4, a5, a6, a7, a8, a9, b)
/*
implicit def objectRep[A <: AnyRef](obj: A)(implicit rep: TypeRep[A]): TypeRep[AnyClass] =
ObjectRep(obj.getClass)
*/
case object BooleanRep extends TypeRep[Boolean] {
override def toString = "Boolean"
}
case object ByteRep extends TypeRep[Byte] {
override def toString = "Byte"
}
case object CharRep extends TypeRep[Char] {
override def toString = "Char"
}
case object ShortRep extends TypeRep[Short] {
override def toString = "Short"
}
case object IntRep extends TypeRep[Int] {
override def toString = "Int"
}
case object LongRep extends TypeRep[Long] {
override def toString = "Long"
}
case object FloatRep extends TypeRep[Float] {
override def toString = "Float"
}
case object DoubleRep extends TypeRep[Double] {
override def toString = "Double"
}
case object UnitRep extends TypeRep[Unit] {
override def toString = "Unit"
}
// case object ClassRep extends TypeRep[AnyClass] {
// override def toString = "Class"
// }
case object StringRep extends TypeRep[String] {
override def toString = "String"
}
case object NoneRep extends TypeRep[Option[Nothing]] {
override def toString = "None"
}
case object NilRep extends TypeRep[Nil.type] {
override def toString = "Nil"
}
case object AnyRep extends TypeRep[Any] {
override def toString = "Any"
}
case object NothingRep extends TypeRep[Nothing] {
override def toString = "Nothing"
}
@serializable
case class SomeRep[A](elemRep: TypeRep[A]) extends TypeRep[Some[A]] {
override def toString = "Some[" + elemRep + "]"
}
@serializable
case class NoneRep[A](elemRep: TypeRep[A]) extends TypeRep[Option[A]] {
override def toString = "None[" + elemRep + "]"
}
@serializable
case class ListRep[A](elemRep: TypeRep[A]) extends TypeRep[List[A]] {
override def toString = "List[" + elemRep + "]"
}
@serializable
case class ArrayRep[A](elemRep: TypeRep[A]) extends TypeRep[Array[A]] {
override def toString = "Array[" + elemRep + "]"
}
@serializable
case class Tuple2Rep[A1, A2](_1: TypeRep[A1], _2: TypeRep[A2]) extends TypeRep[Tuple2[A1, A2]] {
override def toString = "Tuple2[" + _1 + ", " + _2 + "]"
}
@serializable
case class Tuple3Rep[A1, A2, A3](_1: TypeRep[A1], _2: TypeRep[A2], _3: TypeRep[A3]) extends TypeRep[Tuple3[A1, A2, A3]] {
override def toString = "Tuple3[" + _1 + ", " + _2 + ", " + _3 + "]"
}
@serializable
case class Tuple4Rep[A1, A2, A3, A4](_1: TypeRep[A1], _2: TypeRep[A2], _3: TypeRep[A3], _4: TypeRep[A4]) extends TypeRep[Tuple4[A1, A2, A3, A4]] {
override def toString = "Tuple4[" + _1 + ", " + _2 + ", " + _3 + ", " + _4 + "]"
}
@serializable
case class Tuple5Rep[A1, A2, A3, A4, A5](_1: TypeRep[A1], _2: TypeRep[A2], _3: TypeRep[A3], _4: TypeRep[A4], _5: TypeRep[A5]) extends TypeRep[Tuple5[A1, A2, A3, A4, A5]] {
override def toString = "Tuple5[" + _1 + ", " + _2 + ", " + _3 + ", " + _4 + ", " + _5 + "]"
}
@serializable
case class Tuple6Rep[A1, A2, A3, A4, A5, A6](val _1: TypeRep[A1], val _2: TypeRep[A2], val _3: TypeRep[A3], val _4: TypeRep[A4], val _5: TypeRep[A5], val _6: TypeRep[A6]) extends TypeRep[Tuple6[A1, A2, A3, A4, A5, A6]] {
override def toString = "Tuple6[" + _1 + ", " + _2 + ", " + _3 + ", " + _4 + ", " + _5 + ", " + _6 + "]"
}
@serializable
case class Tuple7Rep[A1, A2, A3, A4, A5, A6, A7](val _1: TypeRep[A1], val _2: TypeRep[A2], val _3: TypeRep[A3], val _4: TypeRep[A4], val _5: TypeRep[A5], val _6: TypeRep[A6], val _7: TypeRep[A7]) extends TypeRep[Tuple7[A1, A2, A3, A4, A5, A6, A7]] {
override def toString = "Tuple7[" + _1 + ", " + _2 + ", " + _3 + ", " + _4 + ", " + _5 + ", " + _6 + ", " + _7 + "]"
}
@serializable
case class Tuple8Rep[A1, A2, A3, A4, A5, A6, A7, A8](val _1: TypeRep[A1], val _2: TypeRep[A2], val _3: TypeRep[A3], val _4: TypeRep[A4], val _5: TypeRep[A5], val _6: TypeRep[A6], val _7: TypeRep[A7], val _8: TypeRep[A8]) extends TypeRep[Tuple8[A1, A2, A3, A4, A5, A6, A7, A8]] {
override def toString = "Tuple8[" + _1 + ", " + _2 + ", " + _3 + ", " + _4 + ", " + _5 + ", " + _6 + ", " + _7 + ", " + _8 + "]"
}
@serializable
case class Tuple9Rep[A1, A2, A3, A4, A5, A6, A7, A8, A9](val _1: TypeRep[A1], val _2: TypeRep[A2], val _3: TypeRep[A3], val _4: TypeRep[A4], val _5: TypeRep[A5], val _6: TypeRep[A6], val _7: TypeRep[A7], val _8: TypeRep[A8], val _9: TypeRep[A9]) extends TypeRep[Tuple9[A1, A2, A3, A4, A5, A6, A7, A8, A9]] {
override def toString = "Tuple9[" + _1 + ", " + _2 + ", " + _3 + ", " + _4 + ", " + _5 + ", " + _6 + ", " + _7 + ", " + _8 + ", " + _9 + "]"
}
@serializable
case class Function1Rep[A1, B](a1: TypeRep[A1], b: TypeRep[B]) extends TypeRep[Function1[A1, B]] {
override def toString = "Function1[" + a1 + ", " + b + "]"
}
@serializable
case class Function2Rep[A1, A2, B](a1: TypeRep[A1], a2: TypeRep[A2], b: TypeRep[B]) extends TypeRep[Function2[A1, A2, B]] {
override def toString = "Function2[" + a1 + ", " + a2 + ", " + b + "]"
}
@serializable
case class Function3Rep[A1, A2, A3, B](a1: TypeRep[A1], a2: TypeRep[A2], a3: TypeRep[A3], b: TypeRep[B]) extends TypeRep[Function3[A1, A2, A3, B]] {
override def toString = "Function3[" + a1 + ", " + a2 + ", " + a3 + ", " + b + "]"
}
@serializable
case class Function4Rep[A1, A2, A3, A4, B](a1: TypeRep[A1], a2: TypeRep[A2], a3: TypeRep[A3], a4: TypeRep[A4], b: TypeRep[B]) extends TypeRep[Function4[A1, A2, A3, A4, B]] {
override def toString = "Function4[" + a1 + ", " + a2 + ", " + a3 + ", " + a4 + ", " + b + "]"
}
@serializable
case class Function5Rep[A1, A2, A3, A4, A5, B](a1: TypeRep[A1], a2: TypeRep[A2], a3: TypeRep[A3], a4: TypeRep[A4], a5: TypeRep[A5], b: TypeRep[B]) extends TypeRep[Function5[A1, A2, A3, A4, A5, B]] {
override def toString = "Function5[" + a1 + ", " + a2 + ", " + a3 + ", " + a4 + ", " + a5 + ", " + b + "]"
}
@serializable
case class Function6Rep[A1, A2, A3, A4, A5, A6, B](a1: TypeRep[A1], a2: TypeRep[A2], a3: TypeRep[A3], a4: TypeRep[A4], a5: TypeRep[A5], a6: TypeRep[A6], b: TypeRep[B]) extends TypeRep[Function6[A1, A2, A3, A4, A5, A6, B]] {
override def toString = "Function6[" + a1 + ", " + a2 + ", " + a3 + ", " + a4 + ", " + a5 + ", " + a6 + ", " + b + "]"
}
@serializable
case class Function7Rep[A1, A2, A3, A4, A5, A6, A7, B](a1: TypeRep[A1], a2: TypeRep[A2], a3: TypeRep[A3], a4: TypeRep[A4], a5: TypeRep[A5], a6: TypeRep[A6], a7: TypeRep[A7], b: TypeRep[B]) extends TypeRep[Function7[A1, A2, A3, A4, A5, A6, A7, B]] {
override def toString = "Function7[" + a1 + ", " + a2 + ", " + a3 + ", " + a4 + ", " + a5 + ", " + a6 + ", " + a7 + ", " + b + "]"
}
@serializable
case class Function8Rep[A1, A2, A3, A4, A5, A6, A7, A8, B](a1: TypeRep[A1], a2: TypeRep[A2], a3: TypeRep[A3], a4: TypeRep[A4], a5: TypeRep[A5], a6: TypeRep[A6], a7: TypeRep[A7], a8: TypeRep[A8], b: TypeRep[B]) extends TypeRep[Function8[A1, A2, A3, A4, A5, A6, A7, A8, B]] {
override def toString = "Function8[" + a1 + ", " + a2 + ", " + a3 + ", " + a4 + ", " + a5 + ", " + a6 + ", " + a7 + ", " + a8 + b + "]"
}
@serializable
case class Function9Rep[A1, A2, A3, A4, A5, A6, A7, A8, A9, B](a1: TypeRep[A1], a2: TypeRep[A2], a3: TypeRep[A3], a4: TypeRep[A4], a5: TypeRep[A5], a6: TypeRep[A6], a7: TypeRep[A7], a8: TypeRep[A8], a9: TypeRep[A9], b: TypeRep[B]) extends TypeRep[Function9[A1, A2, A3, A4, A5, A6, A7, A8, A9, B]] {
override def toString = "Function9[" + a1 + ", " + a2 + ", " + a3 + ", " + a4 + ", " + a5 + ", " + a6 + ", " + a7 + ", " + a8 + ", " + b + "]"
}
/*
@serializable
case class ObjectRep[A](c: Class) extends TypeRep[A] {
override def toString = c.getName
}
*/
}
|