It is a well known fact that structural types are implemented through reflection. Are there maybe any other language constructs which use reflection?
-
Thank you all for your answers! I am not really sure what to do at this point. It wouldn't be fair to accept a single one in this case, but it is also not possible to accept multiple of them. – Nermin Serifovic Jan 07 '11 at 15:54
-
accept the one about anonymous objects because it's the only one (aside from structural types in general) where reflection can get you into serious performance trouble. (Maybe it's just me, but I can't leave one of my questions without an accepted answer.) – Ken Bloom Jan 07 '11 at 15:58
6 Answers
This is closely related to structural types, but any anonymous object instance, ie
new { def print = ("hello world") }.print
will use reflection.
http://scala-programming-language.1934581.n4.nabble.com/Structural-types-reflection-td3071599.html

- 5,232
- 2
- 27
- 42
-
4+1: This has implications when defining implicit conversions, so it's well worth pointing out separately. In short, always define a *named* class outside of the `implicit` function, and define a *separate* implicit function to convert to that class. – Ken Bloom Jan 02 '11 at 17:39
-
This is exactly what I do when I define an implicit conversion in order to add a method to a class - time to change. – Russell Apr 17 '12 at 10:57
Enumerations use reflection to find out about all of the possible values for the enumeration for the nameOf
function. (See the populateNameMap
method in Enumeration.scala). This is done once, the first time you call nameOf
for a particular Enumeration
type.

- 57,498
- 14
- 111
- 168
If you consider isInstanceOf/asInstanceOf as reflection, then pattern matching relies on them

- 28,271
- 28
- 124
- 178
-
1That's not reflection, that's type checking & casting and is very fast. – vadipp Oct 29 '12 at 04:08
-
@vadipp - yes, looks like we should not be afraid to use it whenever we are inclined to do so: http://stackoverflow.com/questions/103564/the-performance-impact-of-using-instanceof-in-java – bbarker Feb 29 '16 at 16:00
Method invocation in structural types depends on reflection:
type T = { def startsWith(x:String):Boolean }
def doSomethingWith(x:T) = x.startsWith("abc")
doSomethingWith("abcdef")

- 57,498
- 14
- 111
- 168
-
2OP specifically mentioned structural types and asked for other examples. – Tom Crockett Jan 02 '11 at 11:28
-
-
-
-
2Yeah, I probably deserved the downvote for stating the obvious. That aside, all of you should be aware that on the question list on the front page, StackOverflow doesn't tell you the original poster, rather it tells you the last person who answered (or maybe even commented on) that question. If you scroll up and look at the question, it tells you who the original poster was (on the right), and who the last editor/tagger was (in the center). – Ken Bloom Jan 02 '11 at 17:37
It's not a language construct, but ScalaTest includes Suite.execute
, which uses reflection to find and invoke test methods.
Does Scala's pattern matching use any reflection behind the scenes?

- 13,265
- 5
- 41
- 50
-
Afaik, pattern matching uses only type checking, type casting and `unapply` methods. For standard library classes the compiler can also generate native JVM switches, `if-else` constructs and such. Reflection is not needed here. – vadipp Oct 29 '12 at 04:10
-