Recent

Author Topic: first generic attempt  (Read 1937 times)

Paolo

  • Hero Member
  • *****
  • Posts: 538
first generic attempt
« on: May 27, 2023, 10:08:34 am »
Maybe it is not possible.
Starting palying with generics I have an object that is something like (code reduced to the essential parts)

Code: Pascal  [Select][+][-]
  1. TFirstGeneric <T1>=class
  2. Private
  3. ..
  4.    A:T1;
  5. ..
  6. End;
  7.  

My need is to play with T1 both integer and double type.
 
Everything is going well up to the point I need to do a conversion from an input string, and I have to properly select one of the two alternatives
Code: Pascal  [Select][+][-]
  1.  
  2. (S is a string)
  3. ..
  4. A:=S.ToInteger;
  5. A:=S.ToDouble;
  6. ..
  7.  

How can do that ?
alternatives ?

avk

  • Hero Member
  • *****
  • Posts: 769
Re: first generic attempt
« Reply #1 on: May 27, 2023, 10:25:15 am »
Maybe
Code: Pascal  [Select][+][-]
  1. ...
  2. uses
  3.   ... SysUtils, ...
  4. ...
  5.   A := T1.Parse(S);
  6. ...
  7.  

Paolo

  • Hero Member
  • *****
  • Posts: 538
Re: first generic attempt
« Reply #2 on: May 27, 2023, 10:40:13 am »
thanks. What PARSE do ? nothing found in SysUtils.

TRon

  • Hero Member
  • *****
  • Posts: 3623
« Last Edit: May 27, 2023, 10:47:07 am by TRon »
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

Paolo

  • Hero Member
  • *****
  • Posts: 538
Re: first generic attempt
« Reply #4 on: May 27, 2023, 10:46:52 am »
Thank you !

avk

  • Hero Member
  • *****
  • Posts: 769
Re: first generic attempt
« Reply #5 on: May 27, 2023, 10:53:08 am »
More precisely LongInt.Parse()/Integer.Parse() and Double.Parse().

TRon

  • Hero Member
  • *****
  • Posts: 3623
Re: first generic attempt
« Reply #6 on: May 27, 2023, 10:57:41 am »
Yeah, sorry avk and Paolo as I interpreted the question the other way around (convert to string).

links:
https://www.freepascal.org/docs-html/rtl/sysutils/tdoublehelper.parse.html
https://www.freepascal.org/docs-html/rtl/sysutils/tintegerhelper.parse.html
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10546
  • Debugger - SynEdit - and more
    • wiki
Re: first generic attempt
« Reply #7 on: May 27, 2023, 11:47:41 am »
Code: Pascal  [Select][+][-]
  1. generic TFirstGeneric <T1>=class
  2.   function ParseString(s:string): T1; virtual; abstract;
  3. end;
  4.  
  5. TFoo = class(specialize TFirstGeneric<single>)
  6.   function ParseString(s:string): single; override;
  7. end;
  8.  


If you must do it in the base, maybe this will help (not the nicest code in my opinion....)

Code: Pascal  [Select][+][-]
  1.   if TypeInfo(T1) = TypeInfo(Single) then writeln('s');
  2.   if TypeInfo(T1) = TypeInfo(Double) then writeln('d');
  3.  

Paolo

  • Hero Member
  • *****
  • Posts: 538
Re: first generic attempt
« Reply #8 on: May 28, 2023, 04:59:25 pm »
Thaks to all for the suggestions. I think I'll rearrange the code logic (same problem to distinguish between 'div' and '/'

Paolo

  • Hero Member
  • *****
  • Posts: 538
Re: first generic attempt
« Reply #9 on: June 02, 2023, 10:31:23 am »
still unable to find PARSE, even if the code compiles (but not yet tested)

TRon

  • Hero Member
  • *****
  • Posts: 3623
Re: first generic attempt
« Reply #10 on: June 02, 2023, 10:52:45 am »
still unable to find PARSE, even if the code compiles (but not yet tested)
Honest question: what did you not understood from typehelpers located at sysutils ? It is fundamental to know about typehelpers in order to understand how it works.
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

Paolo

  • Hero Member
  • *****
  • Posts: 538
Re: first generic attempt
« Reply #11 on: June 02, 2023, 11:11:04 am »
Forget it, now I see where they are, thank you

 

TinyPortal © 2005-2018