Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Clojure object-oriented at its heart? (Polymorphism in seqs)

Clojure is a functional lisp, reportedly not at all object-oriented, even though it runs on the JVM, a VM designed for an object oriented language. Clojure provides identical interfaces for iterating over lists and vectors by abstracting them to an interface called seq. This is even implemented internally using a Java interface called ISeq. Is this not an example of object-oriented abstraction? How can it be claimed that Clojure is not object-oriented?

I guess a corollary to this question--- when can polymorphism be considered distinct from object orientation?

like image 479
Steve Avatar asked Oct 10 '09 15:10

Steve


People also ask

Is Clojure object oriented language?

Clojure is a functional lisp, reportedly not at all object-oriented, even though it runs on the JVM, a VM designed for an object oriented language. Clojure provides identical interfaces for iterating over lists and vectors by abstracting them to an interface called seq.

What is object-oriented polymorphism?

Polymorphism is a feature of object-oriented programming languages that allows a specific routine to use variables of different types at different times. Polymorphism is the ability of a programming language to present the same interface for several different underlying data types.

Is clojure better than Java?

Clojure and Java can be categorized as "Languages" tools. "It is a lisp", "Concise syntax" and "Persistent data structures" are the key factors why developers consider Clojure; whereas "Great libraries", "Widely used" and "Excellent tooling" are the primary reasons why Java is favored.

What is polymorphism in object-oriented analysis and design?

In object-oriented paradigm, polymorphism implies using operations in different ways, depending upon the instance they are operating upon. Polymorphism allows objects with different internal structures to have a common external interface. Polymorphism is particularly effective while implementing inheritance.


2 Answers

Idiomatic Clojure favors defining independent functions that operate on a very small set of core data structures; this unbundling of methods and data is a strong statement against object orientation and in favour of a functional style. Rich Hickey (creator of Clojure) has repeatedly stated the importance of this; for example here: "Clojure eschews the traditional object-oriented approach of creating a new data type for each new situation, instead preferring to build a large library of functions on a small set of types.".

The reliance on the core data structures is even more important in Clojure than in other functional languages because you'll only reap the full benefits from Clojure's STM when you are using Clojure's persistent data structures.

I guess a corollary to this question--- when can polymorphism be considered distinct from object orientation?

I'm using Clojure's multimethods (i.e. polymorphic facilities) to dispatch to different implementations based on a filename's extension - not at all object oriented, but polymorphic.

like image 139
pmf Avatar answered Sep 22 '22 05:09

pmf


I guess a corollary to this question--- when can polymorphism be considered distinct from object orientation?

Polymorphism has absolutely no relation to object-orientation. It simply means that the same operation can behave differently depending on the type(s) of its operands.

Functional languages like ML or Haskell have had polymorphism for more than 30 years, and someone with a better knowledge of PL history can probably point out some examples pre-1962 (i.e. pre-OO).

Christopher Strachey described the distinction between parametric polymorphism and ad-hoc polymorphism in 1967, so polymorphism must have already existed then. Since polymorphism was only introduced in OO in Simula-67, my guess is that polymorphism must have existed before it was introduced in OO.

like image 44
Jörg W Mittag Avatar answered Sep 23 '22 05:09

Jörg W Mittag