Could Scala be considered an "inversion of abstraction"?

Hi,

This is a provocative question, the purpose of which is to open debate about how abstraction inversion is observed among the developer community. I am really interested to know what you think.

Firstly, here is a quote from examples of abstraction inversion given on Wikipedia: http://en.wikipedia.org/wiki/Abstraction_inversion

Creating an object to represent a function is cumbersome in object-oriented languages ​​such as Java and C ++, in which functions are not first-class objects. In C ++, you can make an object "callable" by overloading the () operator, but it is still often necessary to implement a new class, for example, "Functors" in STL.

For me, functions are first-class citizens in Scala, but when we use Scala to generate Java bytecode, Scala creates a specific class β€œon top” of Java to enable functional programming ... can we see this as an inversion of abstraction?

The same can be applied to Clojure or to any functional language for the JVM ... or even to Apache collections, for example:

http://commons.apache.org/collections/apidocs/org/apache/commons/collections/Closure.html


By the way, I'm not sure about the objectivity of the Wikipedia article. For example, when it comes to a possible inversion of abstraction in a microcell, the article says: β€œThe body of opinions holds the micronuclear design as an inversion of abstraction,” but such an operator for a functional type in OOP

+3
4

wiki ( ?:), . , - , .

, . , tan, sin cos, , sin cos tan, tan. , , tan sin cos, tan, .

, Scala. , , Scala, , , Scala.

, , Scala . , , , :

  • JVM "function".
  • , Scala.

, , ? ? "" , . , - , , , - , , -, .

, Java , Java . , . , " ", .

, , , 1 . .

Java , Scala ? , . Scala , . "", Java, , , "", .

, .

+9

, JVM, Scala.

- : , , .. , ( apply()), FunctionN.

API- , , , ( ) ( /).

Scala - , .

+5

, . : Scala , .

val f = 2 * (_:Int)
//--> f: (Int) => Int = 

f(21)
//--> res5: Int = 42

, "" :

val f = new Function1[Int,Int] { def apply(v:Int) = 2*v }
//--> f: java.lang.Object with (Int) => Int = 

f(21)
//--> res6: Int = 42

Since FunctionN functions are features, you can use mixing inheritance, which avoids situations similar to those used for functors in C ++. For instance. you could "modify" Java-Map as a function:

class JavaMap[K,V] extends java.util.HashMap[K,V] with Function1[K,V] {
    def apply(k:K) = get(k)
}

val m = new JavaMap[Int, String]
m.put(5,"five")
m(5)
//--> res8: String = five
+2
source

I believe not. The way first-class functions are implemented in Scala is just an implementation detail.

+1
source

Source: https://habr.com/ru/post/1775185/


All Articles