SRL Archive - Other Language Help and Tutorials

Simba const-ness


Brandon

2014-02-23 21:00:06

How can I declare a constant array as the return type? Or constant pointer to constant data?

I tried:


Function Foo: const array[0..2] of integer; //does not compile.
var
myarr: array[0..2] of Integer = [2, 5];
begin
result := myarr;
end;

begin
Foo[0] := 10; //should fail. Result is constant.
end.


const
arr = ['1', '2'];

Function Foo: ^char;
begin
Result := @arr[0]; //can't.. expected variable..
end;

Function Foo2: const ^char; //can't..
begin
Result := @arr[0];
end;

I know you can do this:


type
PInt = ^Integer;

const
Foo = PInt(100);

but why is there no way to have const return types?

Even this fails:

type pInt = ^Integer;

Function Foo: PInt;
var
I: Integer;

const
Res: PInt := (@I);
begin
I := 10;
result := Res;
end;

const
I: PInt := Foo;
begin
I^ := 20; //biggest wtf..

writeln(I^); //prints 20 -__-
end.


Is there seriously no way to have data and pointed to data immutable? I want to have a const pointer. One that cannot be written to or even immutable pointers that cannot be changed once initialized..

Impossible?

Gu3st...

2014-02-24 03:21:24

Wow @ that "biggest wtf" script… :p

const
a := 1;

begin
@a; // Variable expected, like OP's script also tells us
end.

Yeah, I'm lost here as well. @nielsie95;?

nielsie95

2014-04-09 04:24:28

Sorry for the late response!
Only a value can be immutable, not a type. This is not possible in Pascal.