Recent

Author Topic: Why Pascal?  (Read 44572 times)

metis

  • Sr. Member
  • ****
  • Posts: 302
Re: Why Pascal?
« Reply #45 on: May 08, 2018, 01:10:45 pm »
@Handoko

No, I mean Vars, that are declared in a Routine and keep their Value until the next Call. 

 :)
Life could be so easy, if there weren't those f*** Details.
My FFmpeg4Lazarus = FFPlay4Laz + FFGrab4Laz + FFInfo4Laz

Handoko

  • Hero Member
  • *****
  • Posts: 5551
  • My goal: build my own game engine using Lazarus
Re: Why Pascal?
« Reply #46 on: May 08, 2018, 01:16:36 pm »
Have you read the link? Typed constant is not the constant that you thought.


Contrary to ordinary constants, a value can be assigned to them at run-time.

Source: https://freepascal.org/docs-html/ref/refse10.html
« Last Edit: May 08, 2018, 04:08:47 pm by Handoko »

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: Why Pascal?
« Reply #47 on: May 08, 2018, 01:16:48 pm »
@Handoko

No, I mean Vars, that are declared in a Routine and keep their Value until the next Call. 

 :)

That's what he meant! It's possible:

Code: Pascal  [Select][+][-]
  1. procedure xyz;
  2. const
  3.   lLock: boolean = False;
  4. begin
  5.   if lLock then exit;
  6.   lLock := True;
  7.   try
  8.     ...
  9.   finally
  10.     lLock := False;
  11.   end;
  12. end;
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

Thaddy

  • Hero Member
  • *****
  • Posts: 19279
  • Glad to be alive.
Re: Why Pascal?
« Reply #48 on: May 08, 2018, 01:45:25 pm »
Indeed, the most use for a typed const is to maintain state over function calls, it behaves like a persistent (global) var, not a local var but is hidden from global access..
This is a slightly more revealing example that I have given before:
Code: Pascal  [Select][+][-]
  1. program untitled;
  2. {$ifdef fpc}{$mode delphi}{$H+}{$J+}{$endif}
  3. function MaintainStateOverCall:integer;
  4. const x:integer = 0;
  5. begin
  6.   Result := x;
  7.   inc(x);
  8. end;
  9.  
  10. var i:integer;
  11. begin
  12.   for i := 0 to 9 do
  13.    writeln(MaintainStateOverCall);
  14. end.
It is a very powerful tool once you know how to use it.
The alternative is writing a class, but this feature is much older and a precursor to Object Pascal.
« Last Edit: May 08, 2018, 02:04:16 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: Why Pascal?
« Reply #49 on: May 08, 2018, 01:52:00 pm »
This is a slightly more revealing example

Sure!  :D
laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

Thaddy

  • Hero Member
  • *****
  • Posts: 19279
  • Glad to be alive.
Re: Why Pascal?
« Reply #50 on: May 08, 2018, 02:03:53 pm »
Sure!  :D
Note you need to have {$writeableconst on} or the equivalent {$J+} for this to work.
objects are fine constructs. You can even initialize them with constructors.

Handoko

  • Hero Member
  • *****
  • Posts: 5551
  • My goal: build my own game engine using Lazarus
Re: Why Pascal?
« Reply #51 on: May 08, 2018, 02:08:27 pm »
It works on {$mode objfpc} too. Every times I start a new project using Lazarus, I get {$mode objfpc} and I can use typed constant without problem.

Kays

  • Hero Member
  • *****
  • Posts: 632
  • Whasup!?
    • KaiBurghardt.de
Re: Why Pascal?
« Reply #52 on: May 08, 2018, 02:21:08 pm »
Indeed, the most use for a typed const is to maintain state over function calls, it behaves like a persistent (global) var, not a local var but is hidden from global access.. […]
The alternative is writing a class, but this feature is much older and a precursor to Object Pascal.
Or a unit where the var is allocated in the implementation section.

[…] {$writeableconst on} […]
One of the worst compiler switches, ever (besides {$COperators on}). I mean constants, hence their name are supposed to remain constant, i.e. you can't assign any value to them.
Yours Sincerely
Kai Burghardt

Thaddy

  • Hero Member
  • *****
  • Posts: 19279
  • Glad to be alive.
Re: Why Pascal?
« Reply #53 on: May 08, 2018, 02:21:29 pm »
That would actually be a bug. Same goes for mode delphi. In delphi it should be off by default. Oh, minor issue.
objects are fine constructs. You can even initialize them with constructors.

Thaddy

  • Hero Member
  • *****
  • Posts: 19279
  • Glad to be alive.
Re: Why Pascal?
« Reply #54 on: May 08, 2018, 02:26:59 pm »
One of the worst compiler switches, ever (besides {$COperators on}). I mean constants, hence their name are supposed to remain constant, i.e. you can't assign any value to them.
No it is not: it is a constant over calls, not a traditional constant without type (which many people will probably also do not understand: how to determine the type of a constant....).
Actually this is THE compiler feature (internally) that allowed OOP in pascal. And sometime later lead to Object Pascal and subsequently Delphi and FreePascal.
And the difference is actually quite well explained in the manuals.
Being able to hide from global scope is a feature that allows classes and objects.

That was a really silly remark, Kai. You can do much better.
« Last Edit: May 08, 2018, 02:31:15 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

Zoran

  • Hero Member
  • *****
  • Posts: 1988
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Why Pascal?
« Reply #55 on: May 08, 2018, 03:16:50 pm »
However, the syntax is quite unfortunate. I never understood why are these declared with const keyword.
Perhaps we should call these global variables with local scope (although accessible from local scope only, they are allocated in global memory region).

Of course, as we know what we are doing, there is nothing wrong with using this feature.
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

Thaddy

  • Hero Member
  • *****
  • Posts: 19279
  • Glad to be alive.
Re: Why Pascal?
« Reply #56 on: May 08, 2018, 03:29:52 pm »
However, the syntax is quite unfortunate. I never understood why are these declared with const keyword.
Perhaps we should call these global variables with local scope (although accessible from local scope only, they are allocated in global memory region).

Of course, as we know what we are doing, there is nothing wrong with using this feature.
I somewhat agree with you, but note typed consts are typed. So although const necessarily  has a different meaning it is quite natural.
I would worry more about open array parameters vs dynamic array parameters. Now that's a clash that is hard to understand for most.
objects are fine constructs. You can even initialize them with constructors.

Zoran

  • Hero Member
  • *****
  • Posts: 1988
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Why Pascal?
« Reply #57 on: May 08, 2018, 04:43:01 pm »
but note typed consts are typed. So although const necessarily  has a different meaning it is quite natural.

Typed? Natural? What makes it typed?
Seriously, why do we call these typed?
A true const also have its type. Only you don't always have to type its type. :D
Is that what makes it not typed?

Not to mention that you can declare true constant's type explicitly, as well:
Code: Pascal  [Select][+][-]
  1. const
  2.   N = Int64(123); // a true const
  3.  

« Last Edit: May 08, 2018, 04:44:45 pm by Zoran »
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

Zoran

  • Hero Member
  • *****
  • Posts: 1988
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Why Pascal?
« Reply #58 on: May 08, 2018, 04:46:24 pm »
@Zoran
Quote
"Don't declare variable outside of exact block they belong to" is what I really miss in Pascal.
How would You declare global Vars then ?


Are you serious?

I'd do it this way:
Code: Pascal  [Select][+][-]
  1. var
  2.   N: Integer;
  3.  
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

lainz

  • Hero Member
  • *****
  • Posts: 4743
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Why Pascal?
« Reply #59 on: May 08, 2018, 06:54:13 pm »
@Zoran
Quote
"Don't declare variable outside of exact block they belong to" is what I really miss in Pascal.
How would You declare global Vars then ?


Are you serious?

I'd do it this way:
Code: Pascal  [Select][+][-]
  1. var
  2.   N: Integer;
  3.  

He means in each scope. With your code you are creating a variable for all function scope, that doesn't means is the local scope, for example a 'for scope' or an 'if scope', in other languages variables are local to the if or local to the for.

Code: Pascal  [Select][+][-]
  1. function
  2. begin // function scope
  3.  
  4. for ... do
  5. begin // for scope
  6.  
  7. if ... then
  8. begin // if scope
  9.  
  10. end // if scope
  11.  
  12. end // for scope
  13.  
  14. end // function scope

 

TinyPortal © 2005-2018