* * *

Author Topic: Obtaining COMPILE time TYPE info to use in creatnig anothe variable!  (Read 2198 times)

jamie

  • Hero Member
  • *****
  • Posts: 767
I know I am having a brain fart here!!!!

 Can you remind me how to use a variable as a reference for a TYPE when creating another
variable ?

 Example

 Var
    MyArray:Array[1..10] of SomeOtherVariable's Type!

 In other words, lets say I have a record that contains  a LONGINT member, this record one day could
get changed or I don't want to dig deep inside to find out what the type is..

 even if this was a class, object etc, I may one day decide to change the type of data a TAG name is
assigned to.

 This is not Run time info, it is info the compiler already knows, I just want to know when I declare an
object/Variable what ever, to use the same TYPE as an already known object/variable at compile time..

Another example

 Var
   MyNumber:Integer;
------
elsewhere in code..

  Var
    MylocalNumber :Type of MyNumber;

get the idea?

Its bad getting old!

Pascal

  • Hero Member
  • *****
  • Posts: 664
Re: Obtaining COMPILE time TYPE info to use in creatnig anothe variable!
« Reply #1 on: March 14, 2018, 06:16:42 am »
You could use your own type:

Code: Pascal  [Select]
  1. type
  2.   TMyType = Integer;
  3.  

Code: Pascal  [Select]
  1.  Var
  2.    MyNumber:TMyType;
  3.  
  4. ...
  5.  
  6.   Var
  7.     MylocalNumber:TMyType;
  8.  

Later you can change the type to something else.
laz trunk - fpc trunk 32bit - Windows 10 Pro x64 (1803)

SymbolicFrank

  • Sr. Member
  • ****
  • Posts: 380
Re: Obtaining COMPILE time TYPE info to use in creatnig anothe variable!
« Reply #2 on: March 14, 2018, 09:19:44 am »
You can also use generics. That's probably what you're after.

Thaddy

  • Hero Member
  • *****
  • Posts: 6152
Re: Obtaining COMPILE time TYPE info to use in creatnig anothe variable!
« Reply #3 on: March 14, 2018, 10:25:36 am »
From the wiki:
Code: Pascal  [Select]
  1. {$mode objfpc}{$R+}
  2. type
  3.   TmyRange = 0..9;
  4. var
  5.   i:TMyRange;
  6.   anArray:array[TmyRange] of integer; // ten elements
  7. begin
  8.   for i := Low(TMyRange) to High(TMyRange) do
  9.   begin
  10.     anArray[i] := i;
  11.     write(anArray[i]:3);
  12.   end;
  13. end.

If the range needs to change, just change the range type... Code keeps working. Demo:
Code: Pascal  [Select]
  1. {$mode objfpc}{$R+}
  2. type
  3.   TmyRange = 2..11; // changed the type. The code needs no change at all
  4. var
  5.   i:TMyRange;
  6.   anArray:array[TmyRange] of integer; // ten elements
  7. begin
  8.   for i := Low(TMyRange) to High(TMyRange) do
  9.   begin
  10.     anArray[i] := i;
  11.     write(anArray[i]:3);
  12.   end;
  13. end.

« Last Edit: March 14, 2018, 10:32:26 am by Thaddy »
I might not give the answer that you want me to.. Peter Green 1969

Thaddy

  • Hero Member
  • *****
  • Posts: 6152
Re: Obtaining COMPILE time TYPE info to use in creatnig anothe variable!
« Reply #4 on: March 14, 2018, 10:42:48 am »
The suggested solution for generics would look like this (trunk!):
Code: Pascal  [Select]
  1. {$mode delphi}{$R+}
  2. uses generics.collections;
  3. type
  4.   TmyRange = 0..9;
  5. var
  6.   i:TMyRange;
  7.   anArray:Tarray<integer>; // needs dynamic array
  8. begin
  9.   SetLength(anArray,High(TMyRange)+1); // because of the specific behavior of dynamic arrays
  10.   for i := Low(TMyRange) to High(TMyRange) do
  11.   begin
  12.     anArray[i] := i;
  13.     write(anArray[i]:3);
  14.   end;
  15. end.

This code is not optimal and also not completely correct, but works. If your index always start with zero! (real programmers...) this code is a good template.
« Last Edit: March 14, 2018, 10:59:40 am by Thaddy »
I might not give the answer that you want me to.. Peter Green 1969

Thaddy

  • Hero Member
  • *****
  • Posts: 6152
Re: Obtaining COMPILE time TYPE info to use in creatnig anothe variable!
« Reply #5 on: March 14, 2018, 11:16:12 am »
Here's a better example, which is correct. Needs trunk:
Code: Pascal  [Select]
  1. {$mode delphi}{$R+}
  2. var
  3.   i:integer;
  4.   anArray:Tarray<integer>; // needs dynamic array
  5. begin
  6.   anArray:= [5,6,7,8,9,10,11,12,13,14]; // implicit constructor
  7.   for i in anArray do
  8.     write(i:3);
  9. end.

Change type:
Code: Pascal  [Select]
  1. {$mode delphi}{$R+}
  2. var
  3.   i:Single;
  4.   anArray:Tarray<Single>; // needs dynamic array
  5. begin
  6.   anArray:= [0.1,0.2,0.3,0.5,0.4,0.7,0.33,111.0,-0.33,10.0]; // implicit constructor  
  7.   for i in anArray do
  8.     writeln(i:1:4);
  9. end.
« Last Edit: March 14, 2018, 11:30:07 am by Thaddy »
I might not give the answer that you want me to.. Peter Green 1969

jamie

  • Hero Member
  • *****
  • Posts: 767
Re: Obtaining COMPILE time TYPE info to use in creatnig anothe variable!
« Reply #6 on: March 14, 2018, 10:35:42 pm »
no, that is not what I am looking for...

 I know you can define a type and use that all over, and just change the type at some point and all
will follow..

 I just would like the compiler to give me at declaration time the TYPE of a variable name elsewhere,
all I need to do is just specify it..

The other day I wanted to create a local array with a type  of an item in a  record, This array
was to be used as a type case over the record, but then I got thinking, what if one day I change that
type then the array would not have the correct casting type...

 This record in question has a group of LongInt, but it could turn into Int64 one day.. so the idea is
to simply have the compiler supply the immediate type of a variable I reference so I can be sure the
array created is proper at that point..

 I don't fancy the idea of using generics because it totally difference interface, on that does not
fit my fancy and code generation.

In C++ you can do this, create a Variable using another variable as the type..
can't remember now how that was done but I used to do it..

 I'll live with it for now..
thanks
.

Zoran

  • Hero Member
  • *****
  • Posts: 1241
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Obtaining COMPILE time TYPE info to use in creatnig anothe variable!
« Reply #7 on: March 14, 2018, 11:55:30 pm »
In C++ you can do this, create a Variable using another variable as the type..
can't remember now how that was done but I used to do it..

It's decltype, which is added in C++11. In its simplest form:

Code: C++  [Select]
  1. int n;
  2. decltype(n) m; // m is int

I like it, perhaps fpc will add something like that in future.

jamie

  • Hero Member
  • *****
  • Posts: 767
Re: Obtaining COMPILE time TYPE info to use in creatnig anothe variable!
« Reply #8 on: March 15, 2018, 12:20:34 am »
yes that's it...

Just take the type of another object/variable etc.. and use it ;)

Thanks, but I guess that means Fpc does not have something of the same..

I could of sworn back in my days of Delphi there was a way to do this..


jamie

  • Hero Member
  • *****
  • Posts: 767
Re: Obtaining COMPILE time TYPE info to use in creatnig anothe variable!
« Reply #9 on: March 15, 2018, 12:31:30 am »
Looking back, I think it was "TypeOf" but it seems it is not supported in Fpc...

oh well.

Thaddy

  • Hero Member
  • *****
  • Posts: 6152
Re: Obtaining COMPILE time TYPE info to use in creatnig anothe variable!
« Reply #10 on: March 15, 2018, 08:19:54 am »
Looking back, I think it was "TypeOf" but it seems it is not supported in Fpc...

oh well.
Sigh..... https://www.freepascal.org/docs-html/rtl/system/typeof.html so that is supported and fully Delphi compatible. But it is not a compile time check. It is a function call.
Note TypeInfo is slightly more useful, but again that does not meet your requirements.
e.g.:
Code: Pascal  [Select]
  1. {$ifdef fpc}{$mode delphi}{$H+}{$I-}{$endif}
  2. uses typinfo;
  3. var
  4.   Ti:PTypeInfo;
  5. begin
  6.   Ti := TypeInfo(integer);
  7.   writeln(Ti.Kind);
  8.   writeln(Ti.Name);
  9. end.
« Last Edit: March 15, 2018, 08:37:51 am by Thaddy »
I might not give the answer that you want me to.. Peter Green 1969

SymbolicFrank

  • Sr. Member
  • ****
  • Posts: 380
Re: Obtaining COMPILE time TYPE info to use in creatnig anothe variable!
« Reply #11 on: March 15, 2018, 10:20:45 am »
jamie, what you describe is exactly what generics are for.

The easiest way to do what you want is make a custom type and use that:

Code: Pascal  [Select]
  1. type
  2.   TMyType = Integer;
  3.  
  4.   TMyClass<TMyType> = class
  5.   end;

If at some point you want to change it, you can just change they type of TMyType.

But the main benefit is in things like this:

Code: Pascal  [Select]
  1. type
  2.   TMyType = class
  3.   end;
  4.  
  5.   TMyClass<T: TMyType> = class
  6.   end;
  7.  
  8.   TMyNewType = class(TMyType)
  9.   end;
  10.  
  11. var
  12.   SomeVar: TMyClass<TMyNewType>;

Thaddy

  • Hero Member
  • *****
  • Posts: 6152
Re: Obtaining COMPILE time TYPE info to use in creatnig anothe variable!
« Reply #12 on: March 15, 2018, 10:44:09 am »
You mean of course:
Code: Pascal  [Select]
  1. type
  2.   TMyType = type Integer;
Otherwise you will run into trouble at some point. Specifically with generics it is better to strongly type: gives you more options and the compiler knows more. A typed type is better than a type alias, which is what you just wrote.
I might not give the answer that you want me to.. Peter Green 1969

Zoran

  • Hero Member
  • *****
  • Posts: 1241
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Obtaining COMPILE time TYPE info to use in creatnig anothe variable!
« Reply #13 on: March 15, 2018, 11:30:51 am »
jamie, what you describe is exactly what generics are for.

No, it is not. The best he can do is declare a type alias first, and then use it for all variables. However, there is nothing like C++'s decltype.
Generics are completely different language feature, so called class templates in C++.

Thaddy

  • Hero Member
  • *****
  • Posts: 6152
Re: Obtaining COMPILE time TYPE info to use in creatnig anothe variable!
« Reply #14 on: March 15, 2018, 11:47:22 am »
Indeed. OTOH decltype() is fairly recent and I have not seen much of it in real code bases. (almost none at all!)
Unlike many new C++ features such a feature seems parseable in LLR(1) so would fit in the Pascal language if one chooses to implement it.
« Last Edit: March 15, 2018, 11:51:20 am by Thaddy »
I might not give the answer that you want me to.. Peter Green 1969

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus