Forum > General

Immutable data types

(1/21) > >>

Bogen85:
Yes, I know, I've seen this discussed a few times...

It seems like some of the issues (primarily related to how it would fit with pascal syntax) are:

* What would it look like? Would it be a something other than var?
* Would it be an attribute to var?
* How would initial assignment be done?
Doing complex initial assignment in the var section (especially if it uses parameters from parent function or needs to make function calls is tedious...).

If with additional keywords it something like the following could work within existing pascal syntax (suggested names are just example names, not necessarily what I'm suggesting):


--- 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";}};} ---program immutable;   procedure something (const a, b, c: integer);    var      d: integer; immutable;      e: integer; immutable;      f: integer; immutable;    begin      d := a + 4; final;      e := b + 5; final;      f := c + 6; final;      writeln ('a: ', a);      writeln ('b: ', b);      writeln ('c: ', c);      writeln ('d: ', d);      writeln ('e: ', e);      writeln ('f: ', f);    end; begin  something (1, 2, 3);end. 
Using the immutable keyword, the final keyword would not likely not be used.
Basically with immutable the first assignment would lock in the value, and the compiler would not allow for further assignment.

If there was enough community and developer buy in to to such a feature change, I might be willing to do a lot of the legwork, providing I'd get sufficient assistance.



marcov:
I don't see a description of what final would do, but modifiers afaik don't apply to statement lines. The closest I can come up with as analogous syntax would be aligned() so the syntax probably would be more


--- 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";}};} ---  f := final(c + 6); 

Warfley:
Immutability must be transitive otherwise it is useless. If i have an immutable integer, but can make a mutable pointer to it, it is effectively not immutable.

So considering your example

--- 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";}};} ---var  i: Integer; immutable;  pi: PInteger; immutable;begin  i := 42; // Initial assignment is fine, any change afterwards would be violation  pi := @i; // Also fine because initial assignemnt  pi^ := 32; // This does not change pi but the pointer behind. As only pi is immutable not what it points towards this is fine, but violates the immutability of iend;And this is generally not decidable:

--- 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";}};} ---var  i: Integer; immutable;  j: Integer;  pi: PInteger; immutable;begin  i := 42; // Initial assignment is fine, any change afterwards would be violation  pi := ifThen(Random(2) = 1, @i, @j); // Initial assignment is fine  pi^ := 32; // In 50% of cases (when random is 1) pi will point to i, which would be a violation of it's immutability, but in the other 50% of cases this would point to j, which is mutable and therefore fine
So the immutability information must transitively be passed with the pointer information, so there must be the concept of a pointer to an immutable object. But that pointer could also be mutable and immutable. Also there can be a pointer to that pointer and so on.
Without this in a language like pascal, which heavily relies on pointers a concept of immutability would be pointless.

So what you end up with is having to incorporate this into your type system, so for example:

--- 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";}};} ---type  ImmutableInt = immutable Integer;  PImmutableInt = ImmutableInt^; var  i: ImmutableInt;  pi: PImmutableInt;begin  i := 42;  pi := @i;  pi^ := 32; // Error because pi dereferences to an immutable object 
And basically what we have here is the exact way the C typesystem works.

Zaher:
How the compiler will know it the value locked , you need to add AI  8)


--- Code: ---program immutable;


  procedure something(var d: integer);
    begin
      d := d + 1; //compiler will raise error here in second call
    end;

var
d: integer; immutable;
begin
  d := 1;
  something (d);

  d := 2; final;
  something (d);
end.
[code]

--- End code ---

MarkMLl:

--- Quote from: Warfley on September 29, 2022, 11:24:57 am ---Immutability must be transitive otherwise it is useless. If i have an immutable integer, but can make a mutable pointer to it, it is effectively not immutable.

--- End quote ---

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

Navigation

[0] Message Index

[#] Next page

Go to full version