Forum > General
Dynamic array as a typed constant
avk:
Is this intended?
--- 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;{$mode objfpc}{$j-}const Items: array of Integer = (1, 2, 3);var I: Integer;begin for I := 0 to High(Items) do begin Inc(Items[I]); WriteLn(Items[I]); end;end.
It compiles and runs for at least the current Linux64 compiler.
Bogen85:
const often seems to act more like static in C
--- 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;{$mode objfpc}{$j-}{$WriteableConst off}const Items: array of Integer = (1, 2, 3);var I: Integer;begin for I := 0 to High(Items) do begin Inc(Items[I]); WriteLn(Items[I]); end;end.
Compiles and run for me as well.... (Also on current Linux64 compiler)
Bogen85:
This also works:
--- 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 test2;{$mode objfpc}{$j-}{$WriteableConst off} type TArrayInteger = array of Integer; procedure process_array(const Items: TArrayInteger);var I: Integer;begin for I := 0 to High(Items) do begin Inc(Items[I]); WriteLn(Items[I]); end;end; begin process_array([1,2,3]);end.
As I've said before, const is cursory and shallow in Free Pascal.
Bogen85:
This fails:
--- 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 test3;{$mode objfpc}{$j-}{$WriteableConst off} type TArrayInteger = array of Integer; procedure process_array(const Items: TArrayInteger);var I: Integer;begin Items := [10, 11, 12]; for I := 0 to High(Items) do begin Inc(Items[I]); WriteLn(Items[I]); end;end; begin process_array([1,2,3]);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";}};} ---$ fpc test3.pasFree Pascal Compiler version 3.3.1 [2022/11/02] for x86_64Copyright (c) 1993-2022 by Florian Klaempfl and othersTarget OS: Linux for x86-64Compiling test3.pastest3.pas(12,3) Error: Can't assign values to const variabletest3.pas(22) Fatal: There were 1 errors compiling module, stoppingFatal: Compilation abortedError: /home/dev/fpc_usr/lib/fpc/3.3.1/ppcx64 returned an error exitcode
Whereas this succeeds:
--- 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 test4;{$mode objfpc}{$j-}{$WriteableConst off} type TArrayInteger = array of Integer; procedure process_array(Items: TArrayInteger);var I: Integer;begin Items := [10, 11, 12]; for I := 0 to High(Items) do begin Inc(Items[I]); WriteLn(Items[I]); end;end; begin process_array([1,2,3]);end.
const only seems to apply to the variable itself, not the contents if a pointer or an array.
Bogen85:
For const records it is enforced.
--- 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 test6;{$mode objfpc}{$j-}{$WriteableConst off} type SomeRec = record a: integer; end; const some: SomeRec = (a: 4); begin some.a := 5;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";}};} ---$ fpc test6.pasFree Pascal Compiler version 3.3.1 [2022/11/02] for x86_64Copyright (c) 1993-2022 by Florian Klaempfl and othersTarget OS: Linux for x86-64Compiling test6.pastest6.pas(14,3) Error: Can't assign values to const variabletest6.pas(16) Fatal: There were 1 errors compiling module, stoppingFatal: Compilation abortedError: /home/dev/fpc_usr/lib/fpc/3.3.1/ppcx64 returned an error exitcode
--- 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 test5;{$mode objfpc}{$j-}{$WriteableConst off} type SomeRec = record a: integer; end; procedure process_rec(const rec: SomeRec);begin rec.a := 20; WriteLn(rec.a);end; var some: SomeRec = (a: 4); begin some.a := 5; process_rec(some);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";}};} ---$ fpc test5.pas Free Pascal Compiler version 3.3.1 [2022/11/02] for x86_64Copyright (c) 1993-2022 by Florian Klaempfl and othersTarget OS: Linux for x86-64Compiling test5.pastest5.pas(12,2) Error: Can't assign values to const variabletest5.pas(23) Fatal: There were 1 errors compiling module, stoppingFatal: Compilation abortedError: /home/dev/fpc_usr/lib/fpc/3.3.1/ppcx64 returned an error exitcode
Navigation
[0] Message Index
[#] Next page