Hacker News new | ask | show | jobs
Ask HN: Any OOP language which enforces / encourages purity / immutability?
2 points by nawfalhasan 3395 days ago
I'm a c# developer and working on large projects made me realise how hard is it to maintain projects with side effects everywhere.

Which made me wonder if there are any OOP languages out there which has a strong emphasis on immutability. For e.g. syntax that lets user construct an object only on instantiation, syntax to easily clone an object, syntax to differentiate between a pure function and an impure one etc.

Note:

1. I do realise many people define OOP as stateful and what I am describing above is fundamentally against such OOP, and also this problem is solved by functional languages. But what I mean by object oriented is a paradigm that keeps classes/types/structs as fundamental building blocks to model problem domain, which has behaviour (methods) on state, and one which has essentially '.' (dot) syntax. For e.g. a language where I can do:

class Person (name, bday) { ...

    func int age()
    { 
        // calculated from bday state
    }
}

// And later var p = new Person (...) var age = p.age()

2. By mutability I mean mutating an object (changing its state), not reinitializing the variable. I'm OK with latter.

First time on HN, hopefully my question is within the rules. Thanks everyone!

2 comments

Some random internet remarks:

0. This looks like the why in 'why are there new languages?'

1. Objects are a place based abstraction that provides the ability to distinguish between 'this' and 'that' in the same way that two identically optioned BMW 325i's may be distinguished, e.g. mine and yours.

2. Immutability is a value based abstraction in which two identically optioned BMW's are fungible (e.g. mine and yours while they are sitting on the dealer's lot awaiting sale).

3. The simplest object like data structure is a hash/hash map/hash table. An immutable hash table can probably provide all of the semantics you describe.

4. There are lots of languages with immutable hash tables.

5. Ruby is one of them and:

   a. Ruby uses the . syntax.
   b. Everything in Ruby is an object.
   c. To a first approximation, any object in Ruby
      may have any of its methods over-ridden/shadowed/redefined
   d. The Ruby ecosystem supports writing new embedded languages,
      e.g. Rails.
   c. The downside is that, historically,
      a first class Windows experience has
      not been a priority of the Ruby community.
My random advice is to not get hung up on the dot syntax until it is clear that the semantics actually do what you want and therefore to mock up the semantics using hash-tables in a language that expects them to be immutable such as Clojure...hash-tables are more or less why Clojure does not have an object system.

Good luck.

1. I get the difference you're talking about between an instance (identity based) and a value. My question is exactly about a value based oop language, more like 'Value Oriented Programming Language' with modelling and patterns similar to oop languages. C# has struct types, so does many languages which have mechanisms to disallow state mutation but they are all optional. You can pretty much do oop modelling with them all.

2. On Ruby, is it possible to have functions on hashtables, just like methods on objects? Is it possible to define a Person hashtable and a Car hashtable and type system pick up the difference? It would be a hack to code like that if idiomatic Ruby code is not built around hashtables.

3. I see your point not to get hung up on dot syntax, my question was out of curiosity. I do like object modelling and wished if I could continue with a paradigm I'm so familiar with without its warts (side effects).

Have a look at Swift.
Thanks. Will play around with it.. Heard similar things about Rust, F#, etc. Will spend some time on them all.