Enforced immutability

Revision as of 05:05, 2 January 2011 by rosettacode>Jhuni (Initial draft of enforced immutability)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Demonstrate any means your language has to prevent the modification of values, or to create objects that cannot be modified after they have been created.

Enforced immutability is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

C

You can create simple constants using the C preprocessor:

<lang c>

  1. define PI 3.14159265358979323
  2. define MINSIZE 10
  3. define MAXSIZE 100

</lang>

Alternatively, you can modify parameters and variables with the const keyword to make them immutable:

<lang c> const char foo = 'a'; const double pi = 3.14159; const double minsize = 10; const double maxsize = 10;

// On pointers int const * ptrToConst; int * const constPtr; int const * const constPtrToConst;

// On parameters int main(const int argc,

        const char** argv)

{

   /* ... */

} </lang>

C++

In C++ you can also use the const keyword on methods to indicate that they can be applied to immutable objects:

<lang cpp> class MyClass { private:

   int x;
 

public:

   int getX() const
   {
       return x;
   }

} </lang>

D

<lang d> immutable double pi = 3.1415; </lang>

JavaScript

You can create constants with the Mozilla-specific extension const. This is not supported by IE and it only works on simple scalars and not on arrays, objects, or parameters.

<lang javascript> const pi = 3.1415; const msg = "Hello World"; </lang>

Perl

The constant pragma allows you to create subroutines that always return the same value and that cannot be modified:

<lang perl> use constant PI => 3.14159; use constant MSG => "Hello World"; </lang>

The module Readonly.pm provides a means of enforcing immutablity upon scalars and arrays, however, this imposes a considerable performance penalty:

<lang perl> use Readonly;

Readonly::Scalar my $pi => 3.14159; Readonly::Scalar my $msg => "Hello World";

Readonly::Array my @arr => (1, 2, 3, 4, 5); Readonly::Hash my %hash => (

   "a" => 1,
   "b" => 2,
   "c" => 3

); </lang>

Perl 6

You can create constants in Perl 6 with constant:

<lang perl6> constant $pi = 3.14159; constant $msg = "Hello World";

constant @arr = (1, 2, 3, 4, 5); </lang>

Unlike variables, parameters are immutable by default. You can directly specify that you want them to be immutable with the readonly trait:

<lang perl6> sub sum (Num $x is readonly, Num $y is readonly) { $x + $y; } </lang>

PHP

You can create constants using the define function. This only works with scalars.

<lang php> define("PI", 3.14159265358); define("MSG", "Hello World"); </lang>

Ruby

You can make things immutable at run-time with Ruby using the built-in freeze method:

<lang ruby> msg = "Hello World" msg.freeze </lang>

Scala

<lang scala> val pi = 3.14159 val msg = "Hello World"