Forum > General

Immutable data types

<< < (3/21) > >>

Thaddy:
No AI involved.
Just a Pascal range, like
--- 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 test;type Tmyrange = 0..9;var  i: Tmyrange;begin  for i in Tmyrange do writeln(i);end.Here I is not an index, but content. The index itself is a good example of immutable. You can't even see it... :D

marcov:
Changing the type system might be a too big job for an initial project/feature in the compiler.

PascalDragon:

--- Quote from: Bogen85 on September 29, 2022, 01:59:07 pm ---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.

--- End quote ---

You can just as well use a nested function pointer together with an anonymous function. Much less overhead this way. ;)


--- 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 nestedprocvars} program readonlyloop; type  tLoop = procedure (const i, limit: integer) is nested;   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.
Regarding some immutable mechanism: for the JVM target FPC already supports immutability in the form of a separate section inside class and record declarations, but only for external classes:


--- 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";}};} ---  ANNfcEvent = class sealed external 'android.nfc' name 'NfcEvent' (JLObject)  public    final var      fnfcAdapter: ANNfcAdapter; external name 'nfcAdapter';  end; 
This is not supported on other platforms, because this would require full DFA support and thus this currently simply can not be supported.

Bogen85:

--- Quote from: Thaddy on September 29, 2022, 02:01:05 pm ---No AI involved.
Just a Pascal range, like
--- 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 test;type Tmyrange = 0..9;var  i: Tmyrange;begin  for i in Tmyrange do writeln(i);end.Here I is not an index, but content. The index itself is a good example of immutable. You can't even see it... :D

--- End quote ---

Yes, and it can't be modified inside that loop, agreed.

--- 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 test; type  Tmyrange = 0..9;var  i: Tmyrange;begin  for i in Tmyrange do writeln (i);   for i in Tmyrange do begin    writeln (i);    i := succ(i);  end;end. 

--- Code: Text  [+][-]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";}};} ---Free Pascal Compiler version 3.3.1 [2022/09/26] for x86_64Copyright (c) 1993-2022 by Florian Klaempfl and othersTarget OS: Linux for x86-64Compiling ./lazforum/test_loop.pastest_loop.pas(12,7) Error: Illegal assignment to for-loop variable "i"test_loop.pas(16) Fatal: There were 1 errors compiling module, stoppingFatal: Compilation abortedError: /home/shared-development/fpc_usr/lib/fpc/3.3.1/ppcx64 returned an error exitcode 

Also, i is exposed.

--- 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 test; type  Tmyrange = 0..9;var  i: Tmyrange;begin  writeln (i);  for i in Tmyrange do writeln (i);  writeln (i);end. 
And it's final value accessible after the loop, and still actually modifiable.


--- 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 test; type  Tmyrange = 0..9;var  i: Tmyrange;   procedure something;  begin    inc(i);    writeln ('something ', i);  end; begin  writeln ('before ', i);  for i in Tmyrange do begin    writeln ('inside ', i);    something;  end;  writeln ('after ', i);end. 

--- Code: Text  [+][-]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";}};} ---before 0inside 0something 1inside 2something 3inside 4something 5inside 6something 7inside 8something 9after 9 

PascalDragon:

--- Quote from: Bogen85 on September 29, 2022, 02:15:27 pm ---And it's final value accessible after the loop, and still actually modifiable.
--- End quote ---

But before and after the loop the value of the index variable is considered undefined (except for the later if the loop has been left with a Break or goto). This is more obvious if you use a function instead of the main body, because then it won't necessarily be 0 before the loop.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version