0

I feel a tendency that most functional languages, like Kotlin and Scala, is handling all data types as objects.

Double, Float, Long, Int.. They are all actual objects and they don't offer any primitive alternative?

Why does functional languages favor objects? Is it just because it is easier to offer operational overloading and polymorphism? Or are there any deeper meaning in this?

Theis F. Hinz
  • 183
  • 1
  • 3
  • 10
  • A Kotlin Int is a JVM int, a primative type – jrtapsell May 31 '18 at 13:08
  • 1
    In addition to my answer below, I want to point out that Scala is fundamentally an object-oriented language. It is in fact much more object-oriented than Java is, whereas its support for functional programming is more or less the same as Java's. The difference is mostly in the design of the libraries which eschew mutation and side-effects. You could make Java about as functional by replacing its standard library without making changes to the language. – Jörg W Mittag May 31 '18 at 13:26
  • Scala *does* offer primitive Ints, Doubles, Longs etc. , but it hides it very carefully, and does all the boxing-unboxing in the background, if necessary. @JörgWMittag I'm a bit skeptical about that: how would you express something like monad transformers in Java if its type system does not support higher kinded type constructors? – Andrey Tyukin May 31 '18 at 13:29
  • @AndreyTyukin: the fact that `scala.Int` compiles to a primitive on the JVM is an implementation detail. Which is essentially the point I am making in my answer: you can have objects and efficiently compile them nonetheless without having to split them. You can't express the type of monad transformers in Java, but that doesn't mean you can't implement them. You can't express the type `Monad` either, but you can still write monads. BTW, my claim that Scala is no more functional than Java is meant as a compliment. The fact that it *looks* like it is, is a testament to Odersky's great taste in … – Jörg W Mittag May 31 '18 at 13:37
  • … language design. – Jörg W Mittag May 31 '18 at 13:38
  • @JörgWMittag You can't make Java remotely as functional as Scala without Higher Kinded Types and some kind of machinery that lets you express type classes (implicits in Scala). – TheInnerLight May 31 '18 at 16:27
  • 2
    Kotlin is not a functional language. – sarveshseri May 31 '18 at 18:48
  • @SarveshKumarSingh Kotlin, like any other language, has some FP features and lacks others. Simply slapping a label on it, like you imply, is an empty statement. – Marko Topolnik Jun 01 '18 at 08:24
  • I disagree with the premise of the question. The languages you listed are heavily multi-paradigm. Most languages that are primarily functional either don't have objects at all (Haskell, SML, Scheme, older Lisps, Erlang) or do, but not as something the language is built around (Common Lisp, OCaml). – sepp2k Jun 03 '18 at 00:16

1 Answers1

4

This has nothing to do with functional languages. This is in fact true for almost all object-oriented languages as well.

Artificially splitting values into two different kinds of things only creates complications, why would you want that?

It is perfectly possible to generate efficient code for arithmetic operations on number objects. In fact, most high-performance OO implementations generate code dealing with primitive native machine number types even for "object numbers". So, if you can generate the same machine code for both cases, but one of the cases is simpler because it doesn't have this artificial split, then it seems obvious what is the better design, doesn't it?

Now, if you want to ask me why the designers of Java made this particular choice, I can't tell you. They certainly should have been aware of the work of the Self team, who after all worked at Sun.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653