Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmings languages that are immutable by default?

Are there programming languages whose "variables" are immutable (const, final, etc) by default?

And, to make it variable, you need to declare an additional immutable qualifier ?

like image 403
sivabudh Avatar asked Jun 29 '10 00:06

sivabudh


People also ask

What is a immutable programming language?

In object-oriented and functional programming, an immutable object (unchangeable object) is an object whose state cannot be modified after it is created. This is in contrast to a mutable object (changeable object), which can be modified after it is created.

Is C++ immutable?

C++ only guarantees shallow immutability—that is, field values directly stored in a const-qualified class do not change. If a field has pointer type, C++ ensures that the pointer value does not change, but does not guarantee anything about the value pointed to.

Are Python variables immutable?

Most python objects (booleans, integers, floats, strings, and tuples) are immutable. This means that after you create the object and assign some value to it, you can't modify that value.

Are strings immutable in all languages?

In Java, C#, JavaScript, Python and Go, strings are immutable. Furthermore, Java, C#, JavaScript and Go have the notion of a constant: a “variable” that cannot be reassigned. (I am unsure how well constants are implemented and supported in JavaScript, however.) In Ruby and PHP, strings are mutable.


1 Answers

The philosophy of the Functional Programming paradigm is strongly geared towards all "variables" being immutable, and "mutable" ones being only allowed in extreme cases (ie, for I/O). Therefore, most functional programming languages like the various Lisp dialects and Haskell will (more often than not) reinforce immutable variables. Of course, some languages offer more flexibility than others, but the central paradigm/philosophy remains, that discourages it.


Edit: In response to your edit:

If you are looking for a C-with-friendlier-constants, then adopting an entirely new programming paradigm isn't your solution. In Functional programming, everything is immutable (in most cases), and you're generally looking at a whole new fundamental philosophy and approach to programming.

This isn't really a solution if all you want is to make it easier to declare constants.


Edit2: In response to people upvoting me:

While I did correctly (I hope) answer the asker's question, I'm not sure it was an answer that turned out to be useful, given his new edit/comment. However, I can hope to possibly open his eyes to a whole new beautiful world of programming =)


Edit3: Here is Wikipedia's List of functional programming languages:

  • APL
  • Charity (purely functional)
  • Clean (purely functional)
  • Curl
  • Curry
  • Erlang
  • F#
  • Haskell (purely functional)
    • CAL
  • Hop
  • J
  • Joy
  • Kite
  • Lisp
    • Clojure
    • Common Lisp
    • Dylan
    • Little b
    • Logo
    • Scheme
    • Tea
  • Lush
  • Mathematica
  • Miranda
  • ML
    • Standard ML
    • Alice
    • Ocaml
    • Mythryl
  • Nemerle
  • Opal
  • OPS5
  • Poplog
  • R
  • Q
  • REFAL
  • Russell
  • Scala
  • Spreadsheets

Most of these languages have some minor elements/influences of non-functional heresy; the ones labeled "purely functional" do not.

(To my knowledge, Functional Programming languages are the only ones that encourage immutable variables by philosophy. There may be languages that have variables immutable, by default, that are not Functional by paradigm. The concept sounds quite odd to me, but I can't guarantee a blanket "never ever" statement, given the vast, vast, vast number of programming languages out there. I'll just say that it is, to my knowledge, extremely unlikely)

A commenter has suggested that ADA is an Imperative/Object-Oriented programming language with immutable variables by default.

like image 179
Justin L. Avatar answered Sep 25 '22 15:09

Justin L.