Recent

Author Topic: Helper Types - Why was I not in the memo y'all??  (Read 1820 times)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1111
  • Professional amateur ;-P
Helper Types - Why was I not in the memo y'all??
« on: March 24, 2021, 12:28:30 am »
Hey all,

This has been popping in my sights and I'm quite curious as when did it became available and why was I not included in the memo!!
Code: Pascal  [Select][+][-]
  1. var
  2.   iVariable: Inetger;
  3.   sVariable: String;
  4. begin
  5.   sVariable:= iVariable.ToString;
  6. end;

This is revolutionary and eliminates the ye'old IntoToStr() all together!!!
Are integers now considered as internal Classes ?!?
Are the other basic data structures this advanced now!?!?!
So many questions, so limited time!!!

So what other nice sugar syntax have I missed!?!?!?

Cheers,
Gus

[Fixed thread title to be meaningful]
« Last Edit: March 24, 2021, 05:06:13 am by trev »
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

korba812

  • Sr. Member
  • ****
  • Posts: 391
Re: Why was I not in the memo y'all??
« Reply #1 on: March 24, 2021, 12:33:12 am »
So what other nice sugar syntax have I missed!?!?!?
this is called "type helpers"
https://wiki.freepascal.org/Helper_types

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Why was I not in the memo y'all??
« Reply #2 on: March 24, 2021, 01:24:24 am »
And it pops up when you use SysUtils unit. 😃

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1111
  • Professional amateur ;-P
Re: Why was I not in the memo y'all??
« Reply #3 on: March 24, 2021, 01:27:51 am »
Hi korba812,

this is called "type helpers"
https://wiki.freepascal.org/Helper_types

Holy moly, that's awesome!!!
Thank you so much for the clarification and the link.

Ever since I saw this type of syntax sugar on the DotNet languages, I've envied it so badly!!
The fact that you can do your own and inherit from already made stuff is like pure gold!!
I love FPC so MUCH!!!!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1111
  • Professional amateur ;-P
Re: Why was I not in the memo y'all??
« Reply #4 on: March 24, 2021, 01:34:10 am »
Hey engkin,

And it pops up when you use SysUtils unit. 😃

That is triple awesome since it's usually included by default, right?
Code: Pascal  [Select][+][-]
  1. unit SomeUnit;
  2. {...}
  3. uses
  4.   Classes, SysUtils; // <-- this is the standard for any auto generated unit

Just one doubt: Do I need to add this every time I want to enjoy Type Helpers?:
Code: Pascal  [Select][+][-]
  1. {$modeswitch TypeHelpers}

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1111
  • Professional amateur ;-P
Re: Why was I not in the memo y'all??
« Reply #5 on: March 24, 2021, 01:38:26 am »
Hey Jamie,

Its been around for some time now..

Yeah, the link that @korba812 provided said 3.0.
I'm so thankful for 3.0 due to, now, 2 things:
  • Namespaces
  • Type Helpers

I love you guys, the whole FPC/Lazarus team!!!

So with Operator overloads and Type helpers you can make the compiler work hard and you much less.

I'm all in for that, Jamie, I'm all in for that!!!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Why was I not in the memo y'all??
« Reply #6 on: March 24, 2021, 04:56:49 am »
Just one doubt: Do I need to add this every time I want to enjoy Type Helpers?:
Code: Pascal  [Select][+][-]
  1. {$modeswitch TypeHelpers}

Only for modes which don't enable it by default (i,e, for all but the "delphi" ones) *and* if you are programming your own helpers.

Since it's (IIRC) a local directive, you can use helpers from other units (e.g. sysutils) without adding a mode switch to the current one.

It's all quite well explained in the Language Reference
« Last Edit: March 24, 2021, 04:59:57 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Why was I not in the memo y'all??
« Reply #7 on: March 24, 2021, 05:06:23 am »
Just one doubt: Do I need to add this every time I want to enjoy Type Helpers?:
Code: Pascal  [Select][+][-]
  1. {$modeswitch TypeHelpers}

No, use this mode switch only if you intend to 1create a new helper, AND 2you use ObjFpc mode.

If you use Delphi mode in the unit where you define a new TypeHelper, you do *not* need {$modeswitch TypeHelpers}

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1111
  • Professional amateur ;-P
Re: Helper Types - Why was I not in the memo y'all??
« Reply #8 on: March 24, 2021, 05:42:29 am »
Hi Lukamar, engkin,

Thank you both for the clarifications!!

That suits me quite well!!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

ojz0r

  • Jr. Member
  • **
  • Posts: 58
Re: Helper Types - Why was I not in the memo y'all??
« Reply #9 on: March 24, 2021, 08:29:27 am »
Does the .ToString work on any (simple) data type?
Just trying to learn.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Why was I not in the memo y'all??
« Reply #10 on: March 24, 2021, 09:12:56 am »
Since it's (IIRC) a local directive, you can use helpers from other units (e.g. sysutils) without adding a mode switch to the current one.

Please don't confuse the terms here: it's not a local directive. A local directive is one that you can use anywhere inside a unit. $Modeswitch is always a global directive and can only be used before the uses-section (or the first declaration if there is no such section or the begin if even those are missing). Also all directives are specific to the unit or program/library/package file. It's just that some directives make no sense in every context (e.g. the global directive $AppType only makes sense inside the main program file).

Does the .ToString work on any (simple) data type?

It will only work if the data type has a type helper declared, that type helper is in scope and that type helper has a method or property named ToString.

 

TinyPortal © 2005-2018