Forum > General

Immutable data types

<< < (2/21) > >>

Warfley:
Also such a simple immutability concept must extend to methods:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$ModeSwitch AdvancedRecords}type  TTestRec = record    i: Integer;    procedure Increment;  end; procedure TTestRec.Increment;begin  Inc(Self.i);end; var  r: TTestRec = (i: 0); immutable;begin  Inc(r.i); // Should throw an error because r is immutable  r.Increment; // What about this?end;
So you need a notion which methods can alter (i.e. for which methods self is immutable). E.g.:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  TTestRec = record    i: Integer;    procedure Increment; immutable;  end;And thereby re-invent the C++ const system.

Bogen85:

--- Quote from: Zaher on September 29, 2022, 12:44:26 pm ---How the compiler will know it the value locked , you need to add AI  8)

--- End quote ---

Rhetorical answer, I know: So, C/C++/Rust all have AI's built into them? (And C/C++ has that AI for more than 20 years?)

But to get back to Pascal. And yes, a lot more involved then this, but at a basic level the compiler just has to keep track when emitting code that when it sees the the first assignment, it marks that identifier internally as already assigned. Any attempt to emit code that would result in another assignment, would result in an error.

FPC can already warn you (and you can make that warning an error) that you are trying to access a variable that has not been initialized yet. So at some level it is already do that kind of tracking.

Zaher:
It is exists also D lang , and they have good examples https://dlang.org/spec/const3.html

If you need add this feature you need to modify other syntax of language too not just adding new modifier (or AI  :D ).

Bogen85:

--- Quote from: MarkMLl on September 29, 2022, 01:34:02 pm ---The easiest way is to declare a value and allow constrained modification, followed by a custom block during which it's immutable. On exit from the block it's no longer in scope:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---for i: integer= 0 to 9 do begin...end; 
...and we all know what the developers think about /that/ sort of thing.

MarkMLl

--- End quote ---

And currently that is not possible. Something similar can already be achieved where the index is read only and the index can't be modified.
The following works (if you build and install FPC from current git sources), but there is a lot of overhead involved to support it.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode delphi}{$modeswitch anonymousfunctions}{$modeswitch functionreferences} program readonlyloop; type  tLoop = reference to procedure (const i, limit: integer);   procedure main;    var      L1: tLoop;     begin      L1 := procedure (const i, limit: integer) begin        if i >= limit then exit;        writeln ('at local iteration ', i);        L1 (i+1, limit);      end;      L1 (10, 25);    end; begin  main;end. 
So, FPC can already do this sort of immutablity, albeit the syntax looks odd, and there would need to be a lot of optimization added to make that work efficiently.

Bogen85:

--- Quote from: Zaher on September 29, 2022, 01:56:37 pm ---It is exists also D lang , and they have good examples https://dlang.org/spec/const3.html

--- End quote ---

Yes, I'm familiar with D lang, my language list in my rhetorical answer was not exhaustive...  :D

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version