Jack Cough on Software: Deeper Look at the Apply Method in Scala
Deeper Look at the Apply Method in Scala In Scala, there is a language feature generally referred to as "The Apply Method" that has the following rules: Any object that has an apply method can be called with the .apply omitted. Functions are no more than objects. Let's take a look at an example. Given the following abbreviated definition of class Array, and an instance a, class Array{ def apply(index:Int) = get(index) a.apply(7) a(7) a.get(7) (the call to get is only equivalent because apply simply calls get. apply could have any implementation, of course.) While apply is really useful in cleaning up syntax, the real beauty of it is hidden just below the surface. (Note: Some of the following might not be implemented exactly as I describe, I'm not 100% sure, but for the sake of understanding, I think it's okay.) Scala'a apply is a concept that can be considered universally across functions or methods, objects, anonymous functions, case classes, and the like. In Scala,Read full article from Jack Cough on Software: Deeper Look at the Apply Method in Scala
No comments:
Post a Comment