Recent

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

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 511
  • 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 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 20.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.0(stable) Ubuntu 20.10 64b Dark Theme
http://github.com/gcarreno

korba812

  • Full Member
  • ***
  • Posts: 167
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: 2705
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. 😃

jamie

  • Hero Member
  • *****
  • Posts: 4457
Re: Why was I not in the memo y'all??
« Reply #3 on: March 24, 2021, 01:27:13 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

Its been around for some time now..

Many languages have  structures like these, they are a little different here and there.

But what you see there basically started in Delphi for the pascal language as far as I know.

And yes, it is a nice feature but it simply generates the code that you would otherwise do but just makes it look nicer.

So with Operator overloads and Type helpers you can make the compiler work hard and you much less.
The only true wisdom is knowing you know nothing

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 511
  • Professional amateur ;-P
Re: Why was I not in the memo y'all??
« Reply #4 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 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 20.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.0(stable) Ubuntu 20.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 511
  • Professional amateur ;-P
Re: Why was I not in the memo y'all??
« Reply #5 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 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 20.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.0(stable) Ubuntu 20.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 511
  • Professional amateur ;-P
Re: Why was I not in the memo y'all??
« Reply #6 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 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 20.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.0(stable) Ubuntu 20.10 64b Dark Theme
http://github.com/gcarreno

lucamar

  • Hero Member
  • *****
  • Posts: 3898
Re: Why was I not in the memo y'all??
« Reply #7 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: 2705
Re: Why was I not in the memo y'all??
« Reply #8 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: 511
  • Professional amateur ;-P
Re: Helper Types - Why was I not in the memo y'all??
« Reply #9 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 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 20.10 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.0(stable) Ubuntu 20.10 64b Dark Theme
http://github.com/gcarreno

ojz0r

  • New Member
  • *
  • Posts: 22
Re: Helper Types - Why was I not in the memo y'all??
« Reply #10 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: 2981
  • Compiler Developer
Re: Why was I not in the memo y'all??
« Reply #11 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