Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2014-11

Thanks to FPC team: Record helper for primitive types!

17 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

silvioprog

Hello,

Today I compiled my FPC from trunk and saw the following possibility:

{ TIntegerHelper }

  TIntegerHelper = record helper for Integer
    function ToString: string;
  end;

...

{ TIntegerHelper }

function TIntegerHelper.ToString: string;
begin
  Result := IntToStr(Self);
end;

...

var
  I: Integer = 123;
begin
  WriteLn(I.ToString); // Prints 123
end.

Or:

{ TStringHelper }

  TStringHelper = record helper for String
    function ToUpperCase: string;
  end;

...

uses
  LazUTF8;

{ TStringHelper }

function TStringHelper.ToUpperCase: string;
begin
  Result := LazUTF8.UTF8UpperCase(Self);
end;
...
var
  S: UTF8String = 'aten??o';
begin
  WriteLn(S.ToUpperCase); // Prints ATEN??O
  // or
  WriteLn('aten??o'.ToUpperCase); // Prints ATEN??O
end.

It's evolution, baby! =)

Thanks alot for this great implementation, FPC team! (and special thanks to Sven!) =D

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20141129/d7657a11/attachment.html>

Reply by email

Bart
to silvioprog
silvioprog wrote 14 lines
{ TIntegerHelper } function TIntegerHelper.ToString: string; begin Result := IntToStr(Self); end; ... var I: Integer = 123; begin WriteLn(I.ToString); // Prints 123 end.

Looks like Java.

Bart

Reply by email

Sven Barth
to silvioprog: Today I compiled my FPC from trunk and saw the following possibility: { TIntegerHelper }…
silvioprog wrote
Hello, Today I compiled my FPC from trunk and saw the following possibility:

This feature is available since February 2013 already, you just had to use {$modeswitch typehelpers} (I even announced that on fpc-devel, fpc-pascal and fpc-announce back then). What I committed yesterday was simply to allow them by default in mode Delphi as that mode is simply a collection of features supported by the current Delphi version (in so far they are already implemented in FPC). You still have to use the modeswitch in other modes and in all modes except the Delphi ones it's "type helper" instead of "record helper".

Regards,
Sven

Reply by email

Michael Van Canneyt
to Sven Barth: Just that it's easier to extend and one can decide oneself which methods are available.…
Sven Barth wrote 9 lines
On 29.11.2014 18:00, Graeme Geldenhuys wrote: > On 2014-11-29 15:54, silvioprog wrote: >> It's evolution, baby! =) > > No, its like Java from years ago! ;-) Just that it's easier to extend and one can decide oneself which methods are available. :) (and yes, I plan to work on the "one helper per type active" problem)

The REALLY nice thing is that - as soon as the Lazarus IDE is extended to support this, you can do

Var
   I : Integer;
   S : String;
begin
   S:=i.<hit ctrl-spacebar>
end;

and get a list of appropriate methods.

No more searching for IntToStr(), StrToDate, dateToStr... and so on: the IDE will tell you what you can use.

For beginners, this really is an invaluable aid which cannot be underestimated. Morfik Appsbuilder had this already almost 10 years ago.

For this reason, Sven, I would really enable this also by default in mode objfpc.

Michael.

Reply by email

Mark Morgan Lloyd
to Bart: Looks like Java. Bart
Bart wrote 21 lines
On 11/29/14, silvioprog wrote: > { TIntegerHelper } > > function TIntegerHelper.ToString: string; > begin > Result := IntToStr(Self); > end; > > ... > > var > I: Integer = 123; > begin > WriteLn(I.ToString); // Prints 123 > end. Looks like Java. Bart

Are you going to be the one to hack FPC's macro facility to define { and } as begin and end? :-)

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

silvioprog
to Sven Barth: This feature is available since February 2013 already, you just had to use {$modeswitch…
Sven Barth wrote 18 lines
On 29.11.2014 16:54, silvioprog wrote: > Hello, > > Today I compiled my FPC from trunk and saw the following possibility: > This feature is available since February 2013 already, you just had to use {$modeswitch typehelpers} (I even announced that on fpc-devel, fpc-pascal and fpc-announce back then). What I committed yesterday was simply to allow them by default in mode Delphi as that mode is simply a collection of features supported by the current Delphi version (in so far they are already implemented in FPC). You still have to use the modeswitch in other modes and in all modes except the Delphi ones it's "type helper" instead of "record helper". Regards, Sven

Awesome. I'll use type helper. =)

Plans to implement the currently XE helpers? (TIntegerHelper, TStringHelper etc.)

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20141129/47ed72da/attachment.html>

Reply by email

Michael Van Canneyt
to silvioprog
silvioprog wrote 21 lines
On Sat, Nov 29, 2014 at 2:04 PM, Sven Barth wrote: On 29.11.2014 16:54, silvioprog wrote: Hello, Today I compiled my FPC from trunk and saw the following possibility: This feature is available since February 2013 already, you just had to use {$modeswitch typehelpers} (I even announced that on fpc-devel, fpc-pascal and fpc-announce back then). What I committed yesterday was simply to allow them by default in mode Delphi as that mode is simply a collection of features supported by the current Delphi version (in so far they are already implemented in FPC). You still have to use the modeswitch in other modes and in all modes except the Delphi ones it's "type helper" instead of "record helper". Regards, Sven Awesome. I'll use type helper. =) Plans to implement the currently XE helpers? (TIntegerHelper, TStringHelper etc.)

Definitely, as soon as I find time.
But initial patches welcome.

Michael.

Reply by email

silvioprog
to Michael Van Canneyt

On Sat, Nov 29, 2014 at 4:54 PM, Michael Van Canneyt <

Show quoted text 39 lines
wrote: On Sat, 29 Nov 2014, silvioprog wrote: > On Sat, Nov 29, 2014 at 2:04 PM, Sven Barth > wrote: > On 29.11.2014 16:54, silvioprog wrote: > Hello, > > Today I compiled my FPC from trunk and saw the following > possibility: > > > This feature is available since February 2013 already, you just had > to use {$modeswitch typehelpers} > (I even announced that on fpc-devel, fpc-pascal and fpc-announce > back then). What I committed > yesterday was simply to allow them by default in mode Delphi as > that mode is simply a collection of > features supported by the current Delphi version (in so far they > are already implemented in FPC). You > still have to use the modeswitch in other modes and in all modes > except the Delphi ones it's "type > helper" instead of "record helper". > > Regards, > Sven > > > Awesome. I'll use type helper. =) > > Plans to implement the currently XE helpers? (TIntegerHelper, > TStringHelper etc.) > Definitely, as soon as I find time. But initial patches welcome. Michael.

Nice. To implement it we can need to enable the {$modeswitch typehelpers}. Can you enable it in SysUtils?

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20141129/1ecceba3/attachment.html>

Reply by email

Bart
to Mark Morgan Lloyd: Are you going to be the one to hack FPC's macro facility to define { and } as begin and…
Mark Morgan Lloyd wrote 6 lines
> Looks like Java. > > Bart Are you going to be the one to hack FPC's macro facility to define { and } as begin and end? :-)

No, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, no, nooooooooooooooooooooooooooh!

I hate Java.
(And I dislike (or am too stupid) for any C dialect).

I'm more likely to do it the other way around.
#define begin {
#define end }
(untested syntax, I already stated I might simply be too stupid)

Bart

Reply by email

silvioprog
to silvioprog: On Sat, Nov 29, 2014 at 4:54 PM, Michael Van Canneyt < Nice. To implement it we can need…
silvioprog wrote 45 lines
On Sat, Nov 29, 2014 at 4:54 PM, Michael Van Canneyt < > wrote: > > On Sat, 29 Nov 2014, silvioprog wrote: > >> On Sat, Nov 29, 2014 at 2:04 PM, Sven Barth >> wrote: >> On 29.11.2014 16:54, silvioprog wrote: >> Hello, >> >> Today I compiled my FPC from trunk and saw the following >> possibility: >> >> >> This feature is available since February 2013 already, you just >> had to use {$modeswitch typehelpers} >> (I even announced that on fpc-devel, fpc-pascal and fpc-announce >> back then). What I committed >> yesterday was simply to allow them by default in mode Delphi as >> that mode is simply a collection of >> features supported by the current Delphi version (in so far they >> are already implemented in FPC). You >> still have to use the modeswitch in other modes and in all modes >> except the Delphi ones it's "type >> helper" instead of "record helper". >> >> Regards, >> Sven >> >> >> Awesome. I'll use type helper. =) >> >> Plans to implement the currently XE helpers? (TIntegerHelper, >> TStringHelper etc.) >> > > Definitely, as soon as I find time. > But initial patches welcome. > > Michael. Nice. To implement it we can need to enable the {$modeswitch typehelpers}. Can you enable it in SysUtils?

Just testing:

unit sysutils;
interface
{$MODE objfpc}
{$MODESWITCH OUT}
{$MODESWITCH TYPEHELPERS}
...

sysutilh.inc
...

  function GetModuleName(Module: HMODULE): string;
type
  TGuidHelper = type helper for TGuid
//    class function Create(const B: TBytes): TGUID; overload; static;
//    class function Create(const S: string): TGUID; overload; static;
//    class function Create(A: Integer; B: SmallInt; C: SmallInt; const D:
TBytes): TGUID; overload; static;
//    class function Create(A: Integer; B: SmallInt; C: SmallInt; D, E, F,
G, H, I, J, K: Byte): TGUID; overload; static;
//    class function Create(A: Cardinal; B: Word; C: Word; D, E, F, G, H,
I, J, K: Byte): TGUID; overload; static;
//    class function NewGuid: TGUID; static;
//    function ToByteArray: TBytes;
//    function ToString: string;
  end;
...
sysutilh.inc(330,3) Error: Type "TGuid" cannot be extended by a type helper

I'll leave this task to you. =D

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20141129/d1730a7f/attachment.html>

Reply by email

Dmitry Boyarintsev
to Michael Van Canneyt: The REALLY nice thing is that - as soon as the Lazarus IDE is extended to support this,…

On Sat, Nov 29, 2014 at 12:32 PM, Michael Van Canneyt <

Show quoted text 8 lines
wrote: For beginners, this really is an invaluable aid which cannot be underestimated. Morfik Appsbuilder had this already almost 10 years ago. For this reason, Sven, I would really enable this also by default in mode objfpc.

I thought there's a support for that in ObjC categories.

But anyways, beware of trolls!

Troll: Hey! this isn't your daddy's pascal! it's modernized, and everything
is an object here, so make sure you create them and free them like this:
var
  i: Integer
begin
  i := Integer.Create;
  i:=10;
  writeln(i.toString());
  i.Free;
end.

and now with helpers having Create and Free on integer type is possible :)

thanks,
Dmitry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20141129/4c3acec9/attachment.html>

Reply by email

Marco van de Voort
to Michael Van Canneyt: The REALLY nice thing is that - as soon as the Lazarus IDE is extended to support this,…
Michael Van Canneyt wrote 7 lines
and get a list of appropriate methods. No more searching for IntToStr(), StrToDate, dateToStr... and so on: the IDE will tell you what you can use. For beginners, this really is an invaluable aid which cannot be underestimated. Morfik Appsbuilder had this already almost 10 years ago.

(the first time I read this message, I thought you were being sarcastic :-)

Reply by email

Michael Van Canneyt
to Marco van de Voort
Marco van de Voort wrote 10 lines
On Sat, Nov 29, 2014 at 06:32:13PM +0100, Michael Van Canneyt wrote: > and get a list of appropriate methods. > > No more searching for IntToStr(), StrToDate, dateToStr... and so on: > the IDE will tell you what you can use. > > For beginners, this really is an invaluable aid which cannot be underestimated. > Morfik Appsbuilder had this already almost 10 years ago. (the first time I read this message, I thought you were being sarcastic :-)

That is known to happen :-)

But no, it is in fact serious. I myself was incredulous until I have seen it in action...

Michael.

Reply by email