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?

9
  • A Kotlin Int is a JVM int, a primative type Commented May 31, 2018 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. Commented May 31, 2018 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? Commented May 31, 2018 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 … Commented May 31, 2018 at 13:37
  • 2
    Kotlin is not a functional language. Commented May 31, 2018 at 18:48

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.