|
7 | 7 | outof: 6
|
8 | 8 | ---
|
9 | 9 |
|
10 |
| -## Scala Reflection |
| 10 | +**Eugene Burmako, Heather Miller** |
11 | 11 |
|
12 |
| -And what it provides over Java reflection. |
| 12 | +*Reflection* is the ability of a program to inspect, and possibly even modify |
| 13 | +itself at runtime. It has a long history across object-oriented, functional, |
| 14 | +and logic programming paradigms, with each paradigm evolving its own, |
| 15 | +sometimes markedly different, *status quo* for reflection. While more |
| 16 | +functional programming languages like Lisp/Scheme have focused primarily on |
| 17 | +reification to enable tasks like dynamic interpretation, object-oriented |
| 18 | +programming languages like Java have focused primarily on runtime reflection to |
| 19 | +enable tasks like the inspection and/or invocation of class members at runtime. |
13 | 20 |
|
14 |
| -## Macros |
| 21 | +Three principal use cases of reflection across languages and paradigms are: |
15 | 22 |
|
16 |
| -Relation between Scala Reflection and macros |
| 23 | +1. **Runtime Reflection**. The ability to inspect and invoke runtime types and their members. |
| 24 | +2. **Compile-time Reflection**. The ability to access and manipulate abstract syntax trees at compile time. |
| 25 | +3. **Reification**. The generation of abstract syntax trees at runtime in the case of (1), or at compile time in the case of (2). |
17 | 26 |
|
18 |
| -## Environment (subsection title TBD) |
| 27 | +Until 2.10, Scala has not had any reflection capabilities of its own. Instead, |
| 28 | +one could use Java reflection which provided a very limited subset of runtime |
| 29 | +reflection capabilities from point (1) above. Many Scala-specific types are |
| 30 | +simply unrecoverable under standalone Java reflection; including info about |
| 31 | +runtime existential, higher-kinded, path-dependent and abstract types. In |
| 32 | +addition to these Scala-specific types, Java reflection is also unable to |
| 33 | +recover runtime type info of Java types that are generic at compile-time; a |
| 34 | +restriction that carried through to runtime reflection on generic types in |
| 35 | +Scala. |
19 | 36 |
|
20 |
| -Some high-level picture of how to think about reflection- which inevitably involves universes and mirrors. |
| 37 | +In 2.10, Scala Reflection was introduced not only to address the shortcomings |
| 38 | +of Java's runtime reflection on Scala-specific and generic types, but to also |
| 39 | +add a more powerful toolbox of general reflective capabilities to Scala. Along |
| 40 | +with full-featured runtime reflection for Scala types and generics (1), 2.10 also |
| 41 | +ships with compile-time reflection capabilities, in the form of |
| 42 | +[Scala Macros]({{site.baseurl }}/overviews/macros.html) (2), as well as the |
| 43 | +ability to *reify* Scala expressions into Scala abstract syntax trees (3). |
| 44 | + |
| 45 | +## Runtime Reflection |
| 46 | +In Scala runtime reflection, you can get types. Whereas in Java, you can only get classes. |
| 47 | +`isAsssignableFrom` - is B a subclass of A (you can do basic subtyping checks with Java) |
| 48 | +`getType` returns a `Class` object |
| 49 | +^ is this a valid point to make still? |
| 50 | + |
| 51 | +- Get around erasure with Scala reflection. |
| 52 | + |
| 53 | +#### Classes vs Types |
| 54 | + |
| 55 | +Why are types more powerful than classes |
| 56 | +^ is this a valid or worthwhile point to make? |
| 57 | + |
| 58 | +Java's type system is so simple that a class is essentially a type- the only exception is when you have generic types. This use case isn't fully supported by Java reflection. |
| 59 | + |
| 60 | +In Scala, one can create an array of a generic type in Scala but not in Java- it uses reflection (i.e. ClassTags) only internally. |
| 61 | + |
| 62 | +If you use Java reflection, you get a view that's restricted to what can be represented in a Java `Class` instance. |
| 63 | +For example, I can't find the self type of a trait that I'm examining using Java reflection. |
| 64 | + |
| 65 | +### Some Examples |
| 66 | + |
| 67 | +What is meant by runtime reflection. Examples. |
| 68 | +Some of the typical things that you can do with Scala reflection. This should also list things that aren't possible to do in Java. |
| 69 | + |
| 70 | +## Compile-time Reflection |
| 71 | + |
| 72 | +Macros. Brief intro. |
| 73 | +Reflection is the window to *Metaprogramming*, the ability for programs to modify themselves at runtime. Scala macros are an example of this. |
| 74 | + |
| 75 | +### Some Examples |
| 76 | + |
| 77 | +## Preliminaries |
| 78 | +### Universes |
| 79 | +### Mirrors |
| 80 | + |
| 81 | +## TypeTags/ClassTags Generic Arrays |
| 82 | + |
| 83 | + def m[T: TypeTag](x: T): Unit = { |
| 84 | + ... |
| 85 | + } |
| 86 | + |
| 87 | +This is a use of reflection because you're explicitly requesting to get runtime type information for T. |
0 commit comments