views:

243

answers:

6

I thought the whole idea was to have only computation with NO state and NO side effects. Now if a Clojure app(or even worse, a reusable Clojure library ) can use and create any Java object, how can I be sure I don't get side effects or state ?

+4  A: 

You cannot be sure, apart from consulting documentation or using a java decompiler(?). This ability certainly defies the idea of pure functional programming, but the real world is not a particularly pure place and purely functional languages can't get much traction against it. Witness all the contortionism with monads in Haskell. Besides, mutable state is very powerful computationally — many algorithms become much faster and much more economical of memory when implemented with mutable state.

Anton Tykhyy
+5  A: 

FP is a paradigm, a concept, but not necessarily a dogma. Clojure trusts the programmer to make thoughtful decisions about where he'll depart from FP. In exchange, Clojure offers the staggering cornucopia of code that is available in the form of Java libraries. This makes it relatively easy and painless to write a GUI app in Clojure, say, or a Web server or any of the things covered by Java library code.

Note that the Java "hole" is not the only escape hatch Clojure offers from FP: References and atoms hold state and Clojure offers functions to change it under controlled conditions. I think this pragmatic approach makes Clojure useful and will help make it popular.

Carl Smotricz
What bothers me is that while we are told that concurrency should be much easier in Clojure because the lack of state, now you're saying that in practice the developer should (again) worry about threads because nothing is guaranteed to be stateless. BTW, how would Clojure's STM deal with Java objects? Does it even recognize that there is a "write" to a java object (how could he? it could be an implicit "write". Just a change of state) which requires rollback?
bugspy.net
I think you're overstating here. As long as you stay away from the stateful stuff (and it's pretty easy to be aware of it), everything IS guaranteed to be stateless. So you CAN program purely FP if you want to, you're just not forced to.I'm not knowledgeable enough to answer the question about Java code in a STM, but I'd guess you'd be wanting to wrap that kinda access at least in a dosync or one of the other mechanisms mentioned on http://clojure.org/refs .
Carl Smotricz
As I mentioned in my post, Clojure makes functional programming much easier than in languages like Java, but makes it so that when you really need to, you can move away from functional programming and program in a more imperative style. Clojure helps you manage state by making use of state explicit. This is something Java does not do.
Rayne
I guess I should have read more carefully Clojure's web page: "Clojure is predominantly a functional programming language, and features a rich set of immutable, persistent data structures." **predominantly** is the keyword here.
bugspy.net
A: 
Arthur Ulfeldt
+3  A: 

Clojure is not a pure functional programming language. What you said would stand in Haskell, but not in Clojure. Clojure encourages functional programming, but it doesn't force it. Clojure is built to help you program in a functional style, but also to allow you to actually get stuff done. Clojure makes sure that when you use state, you have to be explicit about it. If you want to be sure that you're programming purely functional, you have to make sure yourself. Clojure isn't pure, so it doesn't promise purity.

Rayne
+2  A: 

Because Clojure is meant for the real world it makes compromises, and therefore it isn't a pure functional language.

Haskell was made as a proof that it was even possible to make a pure functional programming language that could work in the real world, so if pureness is what you desire, your journey should take you there.

tomjen
+1  A: 

Referential transparency (which is a consequence of the lack of side effect) isn't the only motivation for functional programming. The concept of lazy evaluation is thought to be one of the central features of the functional style since it allows you to modularly construct programs.

In other words functional programming is at least as much about generic programming as it is about providing static safety guarantees. I'm pretty sure you already knew this, but I thought it might be appropriate to articulate the idea.

Allowing side effects is a bit of a trade-off which you need to justify for yourself. Many applications do need to deal with quite a lot of stateful computation, some languages are just more strict about dealing with this than others.

Rehno Lindeque