Recent

Author Topic: Immutable data types  (Read 9417 times)

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: Immutable data types
« Reply #15 on: September 29, 2022, 02:24:26 pm »
And it's final value accessible after the loop, and still actually modifiable.

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.

Agreed (as far as the undefined portions). But it still accessible and modifiable in a nested procedure. Is that considered undefined as well?

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Immutable data types
« Reply #16 on: September 29, 2022, 03:34:20 pm »
Please don't bring that rusty language to the table.
If u need that kind of control then maybe u need scripture?

 I think a record with a few operator overloads will do.
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 14169
  • Probably until I exterminate Putin.
Re: Immutable data types
« Reply #17 on: September 29, 2022, 06:34:06 pm »
People got on for IONS before Zero...
https://en.wikipedia.org/wiki/0
Feel free to also donate to wikipedia..
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11353
  • FPC developer.
Re: Immutable data types
« Reply #18 on: September 29, 2022, 08:46:31 pm »
People got on for IONS before Zero...
https://en.wikipedia.org/wiki/0
Feel free to also donate to wikipedia..

And even aeons before that  >:D

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Immutable data types
« Reply #19 on: September 29, 2022, 08:53:29 pm »
People got on for IONS before Zero...
And even aeons before that  >:D

I think he meant there was a -ve charge...

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 14169
  • Probably until I exterminate Putin.
Re: Immutable data types
« Reply #20 on: September 29, 2022, 10:50:20 pm »
And you both can miss around 10 euro to the wikimedia foundation. We three of the age, we actually had real books, real touchable Encyclopedia, I still have them. (two of them,  over 60 volumes, Elsevier and Spectrum) Brittanica was not easily available back in the 50/60's... 8) And in the 70's I was either playing guitar or remembering everything I forgot by now. (inc girls) Not my current wife of course. I just married ONCE.
« Last Edit: September 29, 2022, 10:57:31 pm by Thaddy »
Specialize a type, not a var.

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: Immutable data types
« Reply #21 on: September 30, 2022, 12:27:01 am »
Alright...

Thanks for that humor.  :D

Functional programming (now possible with anonymous methods, references to them, and the full closure support they offer) as is can likely do most of what is necessary here.

With const parameters should give all needed compile time read-only access controls I'm needing for some particular use cases.

Both for certain types of iterators and read-only data structures for concurrent applications (yes, that could be abused/bypassed, I'm obviously aware of that, but as already pointed out, records with the applicable access controls and properties can enforce that at runtime, in addition to the compile time checks mentioned above.

Beyond that, the Pascal syntax for variable declaration and initial assignment don't seem to be conducive to this sort of "evolution" of the language.

Not being conducive to this sort of "evolution" of pascal, many would see as good thing.

FPC has already evolved to support this type of functional programming, which could be sufficient to provide the immutability "features" that many desire. I say "many", there are obviously "many" who have no desire to have these features, so they won't need to leverage functional programming to get access to them.

My intent for this was not for an iterator index, but exploring that aspect is what led to functional programming achieving what I'm look for.

We will see... Worst case @PascalDragon sees a lot of issue reports from me on anonymous method closure problems... (and related).

Here is an example a type of generic iterator for looping... But of course there are other things that apply similar mechanisms...

Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. {$modeswitch anonymousfunctions}
  3. {$modeswitch functionreferences}
  4.  
  5. program readonlyloop;
  6.  
  7. type
  8.   tLoop = reference to procedure (const i: integer; var leave: boolean);
  9.  
  10.   function looper (const initial_value, final_value: integer; const loopPr: tLoop): boolean;
  11.     var
  12.       i: integer;
  13.     begin
  14.       result := false;
  15.       for i := initial_value to final_value do begin
  16.         loopPr (i, result);
  17.         if result then exit;
  18.       end;
  19.     end;
  20.  
  21.   procedure main;
  22.     const
  23.       too_much = 10;
  24.  
  25.     begin
  26.       looper (4, 15,
  27.         procedure (const i: integer; var leave: boolean) begin
  28.           writeln('loop #1 i: ', i);
  29.           if i > too_much then begin
  30.             leave := true;
  31.             exit;
  32.           end;
  33.           writeln('still in loop.')
  34.         end);
  35.  
  36.       looper (2, 5,
  37.         procedure (const i: integer; var leave: boolean) begin
  38.           writeln('loop #2 i: ', i);
  39.           leave := false;
  40.         end);
  41.     end;
  42.  
  43. begin
  44.   main;
  45. end.
  46.  

Which results in:
Code: Text  [Select][+][-]
  1. loop #1 i: 4
  2. still in loop.
  3. loop #1 i: 5
  4. still in loop.
  5. loop #1 i: 6
  6. still in loop.
  7. loop #1 i: 7
  8. still in loop.
  9. loop #1 i: 8
  10. still in loop.
  11. loop #1 i: 9
  12. still in loop.
  13. loop #1 i: 10
  14. still in loop.
  15. loop #1 i: 11
  16. loop #2 i: 2
  17. loop #2 i: 3
  18. loop #2 i: 4
  19. loop #2 i: 5
  20.  

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: Immutable data types
« Reply #22 on: September 30, 2022, 03:04:37 am »
The reason why I say that anonymous methods solves this problem, is because my original source code musings from this initial post can use the following (now supported in FPC) to achive immutable variable assignments that the compiler enforces (but yes, as others have stated, could be bypassed with pointers.)

Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. {$modeswitch anonymousfunctions}
  3.  
  4. program immutable;
  5.  
  6.   procedure something (const a, b, c: integer);
  7.     begin
  8.       procedure (const d, e, f: integer) begin
  9.         writeln ('a: ', a);
  10.         writeln ('b: ', b);
  11.         writeln ('c: ', c);
  12.         writeln ('d: ', d);
  13.         writeln ('e: ', e);
  14.         writeln ('f: ', f);
  15.       end(a+4, b+5, c+6);
  16.     end;
  17.  
  18. begin
  19.   something (1, 2, 3);
  20. end.
  21.  

d, e, f are indeed one time assignable and compiler enforces that.

producing this output:

Code: Text  [Select][+][-]
  1. $ ./lazforum/immutable
  2. a: 1
  3. b: 2
  4. c: 3
  5. d: 5
  6. e: 7
  7. f: 9
  8.  

Joanna

  • Hero Member
  • *****
  • Posts: 702
Re: Immutable data types
« Reply #23 on: September 30, 2022, 08:18:13 am »
Hello I guess I got here late to discussion and bogen85 is away in irc.
Can anyone explain how the anonymous method works ? I’ve never seen
Code: Pascal  [Select][+][-]
  1.  procedure something (const a, b, c: integer);
  2.     begin
  3.       procedure (const d, e, f: integer) begin
Syntax before and didn’t know it was even possible.

Is this part the anonymous procedure calling itself?
Code: Pascal  [Select][+][-]
  1.  end(a+4, b+5, c+6);

What keeps this procedure from being called more than once?
« Last Edit: September 30, 2022, 08:26:16 am by Joanna »
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: Immutable data types
« Reply #24 on: September 30, 2022, 02:48:27 pm »
Hello I guess I got here late to discussion and bogen85 is away in irc.
Can anyone explain how the anonymous method works ? I’ve never seen
Code: Pascal  [Select][+][-]
  1.  procedure something (const a, b, c: integer);
  2.     begin
  3.       procedure (const d, e, f: integer) begin
Syntax before and didn’t know it was even possible.

Support for anonymous methods in free pascal is a recent addition, from May of this year and available if you clone FPC from source and build and install it.
PascalDragon knows a lot more of the details about this.

They are not in the current release of FPC, but will be available in the next release.
EDIT: Next major release, as pointed out by @marcov.

An anonymous method is, too make a long answer short, basically a function or procedure declared inline.

To be used it can be:
  • Called immediately
  • Assigned to a procedure or function reference variable to be called later
  • Passed as a parameter in a call that receives a procedure or function reference

An anonymous method has view of all variables and any other identifiers visible in the scope of where it is defined.
In other words, any identifiers that can be used before or after the anonymous method can be used in the anonymous method.
This is true even when the method is passed somewhere else to be called, where that somewhere else does not have the same variable/identifier scope.

This "capturing" of identifiers in it's view that is using so the anonymous method can be called somewhere else is referred to as a closure.
That "view" is part of the anonymous method definition along with the definition you do see.

The anonymous method being directly associated with this closure is what allows it be called elsewhere, where the variable/identifier scope is not necessarily.

From a usage standpoint, anonymous method are similar to procedure and function pointers.
What makes them "special" is the closure that is directly associated with them.

Is this part the anonymous procedure calling itself?
Code: Pascal  [Select][+][-]
  1.  end(a+4, b+5, c+6);
Yes

What keeps this procedure from being called more than once?

It is not assigned to a variable of type reference to procedure.
If it were it could be easily be called more than once.

It is only in the code once, and it only calls itself once.

It is also not in a loop and being called multiple times.

« Last Edit: September 30, 2022, 03:21:00 pm by Bogen85 »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11353
  • FPC developer.
Re: Immutable data types
« Reply #25 on: September 30, 2022, 03:15:39 pm »
They are not in the current release of FPC, but will be available in the next release.

Next major release. So not in 3.2.4, which will be a fixes (3.2 branch) release.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Immutable data types
« Reply #26 on: September 30, 2022, 03:27:19 pm »
We will see... Worst case @PascalDragon sees a lot of issue reports from me on anonymous method closure problems... (and related).

Well, the more bugs for anonymous functions and function references are found (and fixed) before the next major release, the better. :D

And it might be nitpicky, but in FPC they are called “anonymous functions”, not “methods” (I'm aware that Delphi calls them “methods”, but the construct itself has nothing per se that justifies that name).

Hello I guess I got here late to discussion and bogen85 is away in irc.
Can anyone explain how the anonymous method works ? I’ve never seen
Code: Pascal  [Select][+][-]
  1.  procedure something (const a, b, c: integer);
  2.     begin
  3.       procedure (const d, e, f: integer) begin
Syntax before and didn’t know it was even possible.

See also this announcement post.

Is this part the anonymous procedure calling itself?
Code: Pascal  [Select][+][-]
  1.  end(a+4, b+5, c+6);

What keeps this procedure from being called more than once?

The procedure has no name, so there is no way to call it again (or even inside itself) once it returns.

To be used it can be:
  • Called immediately
  • Assigned to a procedure or function reference variable to be called later
  • Passed as a parameter in a call that receives a procedure or function reference

Depending on what is captured by an anonymous function it can also be assigned to a function pointer or a method pointer or (always) a nested function pointer. That's an extension compared to what Delphi allows. :)

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: Immutable data types
« Reply #27 on: September 30, 2022, 04:07:54 pm »
We will see... Worst case @PascalDragon sees a lot of issue reports from me on anonymous method closure problems... (and related).

Well, the more bugs for anonymous functions and function references are found (and fixed) before the next major release, the better. :D

And it might be nitpicky, but in FPC they are called “anonymous functions”, not “methods” (I'm aware that Delphi calls them “methods”, but the construct itself has nothing per se that justifies that name).

Thanks.

I will try to use the correct nomenclature from now on.  :)

AlanTheBeast

  • Sr. Member
  • ****
  • Posts: 348
  • My software never cras....
Re: Immutable data types
« Reply #28 on: September 30, 2022, 05:22:41 pm »
And it's final value accessible after the loop, and still actually modifiable.

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.

Of course if i were not declared at all, and only in scope for the "for" loop, then it would not be accessible at all after the loop.

Code: Pascal  [Select][+][-]
  1.             x := 23;        // i does not exist
  2.             for i in Tmyrange do                          // i exists!  (and probably in a nice snug, fast register)
  3.                begin
  4.                        GreatBallofFire [i] := x + i -1;
  5.                end;                                            //  i ceases to exist
  6.            i := i + 1;                                       // compiler go boo!
  7.  
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: Immutable data types
« Reply #29 on: September 30, 2022, 07:15:20 pm »
But how can this be done in Free Pascal?

Of course if i were not declared at all, and only in scope for the "for" loop, then it would not be accessible at all after the loop.

Code: Pascal  [Select][+][-]
  1.             x := 23;        // i does not exist
  2.             for i in Tmyrange do                          // i exists!  (and probably in a nice snug, fast register)
  3.                begin
  4.                        GreatBallofFire [i] := x + i -1;
  5.                end;                                            //  i ceases to exist
  6.            i := i + 1;                                       // compiler go boo!
  7.  

I just now tried that.

Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2.  
  3. program immutable2;
  4.  
  5.   type
  6.     tSet1 = (a, b, c);
  7.  
  8.   procedure something_else;
  9.     //var
  10.     //  i: tSet1;
  11.     begin
  12.       for i in tSet1 do begin
  13.         writeln(ord(i));
  14.       end;
  15.     end;
  16.  
  17. begin
  18.   something_else;
  19. end.
  20.  

That results in:

Code: Text  [Select][+][-]
  1. /home/shared-development/fpc_usr/bin/fpc -Fu./src -Fu./lib -Xc -XD -Xi -Sm -Sg -Os -gl -Sd -vnewh -Senwh -Sh -FE./bin lazforum/immutable2.pas
  2. Hint: Start of reading config file ~/.fpc.cfg
  3. Hint: End of reading config file ~/.fpc.cfg
  4. Free Pascal Compiler version 3.3.1 [2022/09/26] for x86_64
  5. Copyright (c) 1993-2022 by Florian Klaempfl and others
  6. Target OS: Linux for x86-64
  7. Compiling lazforum/immutable2.pas
  8. immutable2.pas(12,11) Error: Identifier not found "i"
  9. immutable2.pas(13,21) Error: Identifier not found "i"
  10. immutable2.pas(20) Fatal: There were 2 errors compiling module, stopping
  11. Fatal: Compilation aborted
  12. Error: /home/shared-development/fpc_usr/lib/fpc/3.3.1/ppcx64 returned an error exitcode
  13.  

But the following does compile and run:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2.  
  3. program immutable2;
  4.  
  5.   type
  6.     tSet1 = (a, b, c);
  7.  
  8.   procedure something_else;
  9.     var
  10.       i: tSet1;
  11.     begin
  12.       for i in tSet1 do begin
  13.         writeln(ord(i));
  14.       end;
  15.     end;
  16.  
  17. begin
  18.   something_else;
  19. end.
  20.  

Which when run gives:

Code: Text  [Select][+][-]
  1. 0
  2. 1
  3. 2
  4.  

 

TinyPortal © 2005-2018