Recent

Author Topic: Possible to do this? (academic exercise only)  (Read 585 times)

Ten_Mile_Hike

  • Jr. Member
  • **
  • Posts: 87
Possible to do this? (academic exercise only)
« on: November 05, 2024, 09:58:55 pm »
Code: Text  [Select][+][-]
  1.  I was playing around and began wondering, "I DO NOT EVER PLAN TO DO THIS",
  2. if there is a way to declare a variable's type based on an external condition.
  3. OBVIOUSLY the code below is an error. I'm just attempting to show what I am
  4. curious about.
  5.  
  6. I know that I could change the pointer pointing to "My_Proc1" to point to "@My_Proc2"
  7. where the two procedures would have the desired individual type assignments
  8. but would there be another way? AGAIN- I am only asking this as an academic curiosity...
  9.  
Code: Pascal  [Select][+][-]
  1. procedure My_Proc1(z:Shortint);
  2.   if Form1.color=clred then Var X:Integer
  3.                        else  Var X:Int64;
  4. Begin
  5.   DOSTUFF
  6. End;
When any government, or any church for that matter, undertakes to say to its subjects, This you may not read, this you
must not see, this you are forbidden to know, the end result is tyranny and oppression no matter how holy the motives.

Robert A. Heinlein

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11929
  • FPC developer.
Re: Possible to do this? (academic exercise only)
« Reply #1 on: November 05, 2024, 10:43:21 pm »
No. Not straight away.

This is outside the realm of a compiler, since the test for clred is done at runtime, and typing is used in the compilation process.

This is more something for an interpreter that knows runtime typing.

Warfley

  • Hero Member
  • *****
  • Posts: 1734
Re: Possible to do this? (academic exercise only)
« Reply #2 on: November 06, 2024, 01:49:49 am »
No. The type system of Pascal requires strict knowledge about the memory layout of each variable at compile time. That's just the nature of a statically typed language. Languages that allow types to change at runtime, like python, are only capable of doing so because, even when they are compiled, they still don't use real memory mapped variables, but basically emulate them through polymorphic classes (in python every variable is basically nothing other than a pointer to a variant like structure on the heap).

Pascal is a low level language that runs right on the metal, meaning Pascal types directly map onto the underlying memory. Therefore the memory layout must be k own at compile time

Zvoni

  • Hero Member
  • *****
  • Posts: 2737
Re: Possible to do this? (academic exercise only)
« Reply #3 on: November 06, 2024, 08:07:53 am »
What about Variant record?

as to the initial example (Integer/Int64): Use the "bigger" type and be done with it
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018