0

From https://stackoverflow.com/a/44981969/156458

There are two common approaches for a language to support both functional and object oriented programming.

  • Every callable is a method and functions are just methods of a hidden class, which is the approach adopted by Ruby;

  • or every callable is a function and object methods are just functions with an implicit or explicit parameter that receives the object the method was called from, which is the approach adopted by Python.

Is it an accurate statement? What is the source of the statement?

What are the approaches for the following languages to support both functional and OO programming:

  • Scala
  • Java
  • C#?

Thanks.

Tim
  • 1
  • 141
  • 372
  • 590
  • There are *other* approaches, but the *claim* was these are two common approaches... which is then substantiated with two examples. Scala, Java and C# are implemented with virtual machines (and the first two use the same virtual machine) - which means it's a combination of the programming language and runtime environment that enable it. – Elliott Frisch Jul 08 '17 at 03:06
  • @NathanHughes do you mean scala uses a different approach than the two mentioned? – Tim Jul 08 '17 at 03:40
  • I think the quote is observing how language designers tend to bolt on rudimentary support for FP as an afterthought. As a practical matter Scala would have to use the constructs, like classes, that are supported by the jvm. But nonOO jvm languages like clojure have to do that too – Nathan Hughes Jul 08 '17 at 03:43
  • If you get what is a functional programming and OOP you will start to notice the aproaches the languages uses to support these features. I really recomend this video from Master Venka about funcional programming (https://www.youtube.com/watch?v=fWKp-v0De9g) – rafaelim Jul 09 '17 at 21:44

1 Answers1

3

Functional Programming is based on concepts below:

  • High order functions: You can pass functions to functions and return functions from functions. More details here.
  • Immutable objects: Objects that can't change the state after creation. More details here.
  • Pure functions: Functions with no side-effects, in another words, when a function receives an input, it will produce the same output everytime. More details here.

If you can do all this things, the language supports functional programming.

Scala:

  • Is based on JVM, so supports OOP
  • Has a val syntax that creates an immutable object.
  • Has support for high order function
  • Programmer is responsible for creating pure functions

Java 8:

  • Supports OOP
  • Has the 'final' keyword and an effective final to make an object immutable
  • Has support for high order functions with lambda expression
  • Programmer is responsible for creating pure functions

C# supports functional programming too but I don't have the expertise to answer your question!

rafaelim
  • 644
  • 6
  • 13