Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Sven Barth via fpc-pascal
On 23.08.2017 20:38, Marcos Douglas B. Santos wrote:
> On Wed, Aug 23, 2017 at 3:25 PM, Sven Barth via fpc-pascal
>  wrote:
>> On 23.08.2017 19:57, Marcos Douglas B. Santos wrote:
>>> Would we have any problem of memory leaks using Interface helper with
>>> COM interfaces (refcount)?
>>> I mean, the "type helper" has constructor/destructor to create/release
>>> something?
>>
>> It doesn't need to. A type helper is essentially syntactic sugar for a
>> class method with the extended parameter as first type.
> 
> So, is this work?
> 
> === code begin ===
> 
> type
>   IFoo = interface
> function Execute: IFoo;
>   end;
> 
>   TFooHelper = type helper for IFoo
> procedure Something;
>   end;
> 
>   TFoo = class(TInterfacedObject, IFoo)
>   public
> function Execute: IFoo;
>   end;
> 
> var
>   F: IFoo;
> begin
>   F := TFoo.Create;
>   F.Execute.Something;
> end;
> 
> === code end ===

Yes, this should work.

Regards,
Sven

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Marcos Douglas B. Santos
On Wed, Aug 23, 2017 at 3:25 PM, Sven Barth via fpc-pascal
 wrote:
> On 23.08.2017 19:57, Marcos Douglas B. Santos wrote:
>> Would we have any problem of memory leaks using Interface helper with
>> COM interfaces (refcount)?
>> I mean, the "type helper" has constructor/destructor to create/release
>> something?
>
> It doesn't need to. A type helper is essentially syntactic sugar for a
> class method with the extended parameter as first type.

So, is this work?

=== code begin ===

type
  IFoo = interface
function Execute: IFoo;
  end;

  TFooHelper = type helper for IFoo
procedure Something;
  end;

  TFoo = class(TInterfacedObject, IFoo)
  public
function Execute: IFoo;
  end;

var
  F: IFoo;
begin
  F := TFoo.Create;
  F.Execute.Something;
end;

=== code end ===
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Sven Barth via fpc-pascal
On 23.08.2017 19:57, Marcos Douglas B. Santos wrote:
> On Wed, Aug 23, 2017 at 2:45 PM, Sven Barth via fpc-pascal
>  wrote:
>> Am 23.08.2017 19:39 schrieb "Marcos Douglas B. Santos" :
>>>
>>> Wait a minute. Now I realize that procedure IShellLinkHelper.Save is a
>>> real implementation not by a class, but by a "interface helper".
>>> It's like default methods in Java(?) that we have code inside an
>>> interface...
>>
>> I don't know about Java, but the C# equivalent would be extension methods.
>> And yes, that's the point: the implementation is not part of the interface
>> implementer, but the user of the interface can add some implementation.
>> (Same for class, record and primitive types helpers)
> 
> Would we have any problem of memory leaks using Interface helper with
> COM interfaces (refcount)?
> I mean, the "type helper" has constructor/destructor to create/release
> something?

It doesn't need to. A type helper is essentially syntactic sugar for a
class method with the extended parameter as first type.

Take this for example:

=== code begin ===

type
  TInterfaceHelper = type helper for IInterface
procedure Something(aArg: String);
  end;

// is more or less equivalent to

  TInterfaceHelper = class
class procedure Something(aExtended: IInterface; aArg: String);
  end;

=== code end ===

Sidenote: for primitive types and records the hidden Self parameter of
the helper is a "var" parameter as the extended type can be changed.

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Sven Barth via fpc-pascal
Am 23.08.2017 16:01 schrieb "Anthony Walter" :
>
> As to what they are useful for, consider the following:
>
> type
>   // IShellLink is define by Microsoft
>   IShellLinkHelper = record helper for IShellLink
>   public
> procedure Save(const Target, Description, Link: string);
>   end;
>
> procedure IShellLinkHelper.Save(const Target, Description, Link: string);
> var
>   P: IPersistsFile;
> begin
>   SetPath(Target);
>   SetDescription(Description);
>   if Self is IPersistsFile then
>   begin
> P := Self as IPersistsFile;
> P.Save(Link, True);
>   end;
> end;
>

And I just noticed: it's "type helper" if you want to extend an interface,
not "record helper" ;)

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Sven Barth via fpc-pascal
Am 23.08.2017 19:40 schrieb "Anthony Walter" :
>
> Sven, multiple helpers would be greatly appreciated, especially
considering everyone wants to add their own helpers for types like string.

At least in FPC modes they can use inheritance to solve this (see below) or
by putting their units after the SysUtils unit (at least if they don't
want/need the helper provided in the SysUtils unit).

=== code begin ===

type
  TMyStringHelper = type helper(TStringHelper) for String
// whatever
  end;

=== code end ===

Note: Delphi supports inheritance only for class helpers, but FPC supports
it also for record and type helpers.

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Sven Barth via fpc-pascal
Am 23.08.2017 19:39 schrieb "Marcos Douglas B. Santos" :
>
> On Wed, Aug 23, 2017 at 11:00 AM, Anthony Walter  wrote:
> > Marco, it doesn't work that way.
> >
> > Type helpers simply allow you to extend an existing type with new
methods
> > and/or properties. When you declare a type helper you extend all
instances
> > of said type given that:
> >
> > A) You 'use' the unit declaring the type helper in some other unit.
> > B) No other unit you're using also defines a type helper for that same
type.
> > Only one type helper per type allowed.
> >
> > As to what they are useful for, consider the following:
> >
> > type
> >   // IShellLink is define by Microsoft
> >   IShellLinkHelper = record helper for IShellLink
> >   public
> > procedure Save(const Target, Description, Link: string);
> >   end;
> >
> > procedure IShellLinkHelper.Save(const Target, Description, Link:
string);
> > var
> >   P: IPersistsFile;
> > begin
> >   SetPath(Target);
> >   SetDescription(Description);
> >   if Self is IPersistsFile then
> >   begin
> > P := Self as IPersistsFile;
> > P.Save(Link, True);
> >   end;
> > end;
>
> Wait a minute. Now I realize that procedure IShellLinkHelper.Save is a
> real implementation not by a class, but by a "interface helper".
> It's like default methods in Java(?) that we have code inside an
interface...

I don't know about Java, but the C# equivalent would be extension methods.
And yes, that's the point: the implementation is not part of the interface
implementer, but the user of the interface can add some implementation.
(Same for class, record and primitive types helpers)

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Anthony Walter
Sven, multiple helpers would be greatly appreciated, especially considering
everyone wants to add their own helpers for types like string.

Thanks again!
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Marcos Douglas B. Santos
On Wed, Aug 23, 2017 at 11:00 AM, Anthony Walter  wrote:
> Marco, it doesn't work that way.
>
> Type helpers simply allow you to extend an existing type with new methods
> and/or properties. When you declare a type helper you extend all instances
> of said type given that:
>
> A) You 'use' the unit declaring the type helper in some other unit.
> B) No other unit you're using also defines a type helper for that same type.
> Only one type helper per type allowed.
>
> As to what they are useful for, consider the following:
>
> type
>   // IShellLink is define by Microsoft
>   IShellLinkHelper = record helper for IShellLink
>   public
> procedure Save(const Target, Description, Link: string);
>   end;
>
> procedure IShellLinkHelper.Save(const Target, Description, Link: string);
> var
>   P: IPersistsFile;
> begin
>   SetPath(Target);
>   SetDescription(Description);
>   if Self is IPersistsFile then
>   begin
> P := Self as IPersistsFile;
> P.Save(Link, True);
>   end;
> end;

Wait a minute. Now I realize that procedure IShellLinkHelper.Save is a
real implementation not by a class, but by a "interface helper".
It's like default methods in Java(?) that we have code inside an interface...


Marcos Douglas
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Marcos Douglas B. Santos
On Wed, Aug 23, 2017 at 12:13 PM, Sven Barth via fpc-pascal
 wrote:
> Am 23.08.2017 16:58 schrieb "Marcos Douglas B. Santos" :
>>
>> Is this compatible with Delphi?
>
> Interface helpers are only supported by FPC. But the other helpers (class,
> record, primitive types) are Delphi compatible.

Alright, thanks for the info.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Sven Barth via fpc-pascal
Am 23.08.2017 16:01 schrieb "Anthony Walter" :
>
> Marco, it doesn't work that way.
>
> Type helpers simply allow you to extend an existing type with new methods
and/or properties. When you declare a type helper you extend all instances
of said type given that:
>
> A) You 'use' the unit declaring the type helper in some other unit.
> B) No other unit you're using also defines a type helper for that same
type. Only one type helper per type allowed.
>

To be more precise: the last helper in scope simply wins. There is no error
if multiple ones are in scope. Also one can cheat a bit by having one's own
helper inherit from another helper of the same type ;)
Though I should mention that it's on my list to implement support for
multiple helpers. I just need to be sure about the resolution rules :)

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Sven Barth via fpc-pascal
Am 23.08.2017 16:59 schrieb "Marcos Douglas B. Santos" :
>
> On Wed, Aug 23, 2017 at 11:07 AM, Anthony Walter  wrote:
> > Here is another example:
> >
> > type
> >   TDay = (Monday = 0, Tuesday, Wednesday, Thursday, Friday, Saturday,
> > Sunday);
> >
> >   TDayHelper = record helper for TDay
> > function AsByte: Byte;
> > function ToString: string;
> >   end;
>
> I didn't know that helpers could be used in enums too. Great.

Helpers for primitive types are supported since 3.0.0.

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Sven Barth via fpc-pascal
Am 23.08.2017 16:58 schrieb "Marcos Douglas B. Santos" :
>
> On Wed, Aug 23, 2017 at 11:00 AM, Anthony Walter  wrote:
> > Marcos, it doesn't work that way.
> >
> > Type helpers simply allow you to extend an existing type with new
methods
> > and/or properties. When you declare a type helper you extend all
instances
> > of said type given that:
> >
> > A) You 'use' the unit declaring the type helper in some other unit.
> > B) No other unit you're using also defines a type helper for that same
type.
> > Only one type helper per type allowed.
> >
> > As to what they are useful for, consider the following:
> >
> > type
> >   // IShellLink is define by Microsoft
> >   IShellLinkHelper = record helper for IShellLink
> >   public
> > procedure Save(const Target, Description, Link: string);
> >   end;
>
> Anthony,
>
> I understood. In fact, this is a great feature for whose work with
> interfaces a lot.
> We can have small interfaces but add some methods in just some places,
> extending such interfaces.
>
> Is this compatible with Delphi?

Interface helpers are only supported by FPC. But the other helpers (class,
record, primitive types) are Delphi compatible.

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Marcos Douglas B. Santos
On Wed, Aug 23, 2017 at 11:07 AM, Anthony Walter  wrote:
> Here is another example:
>
> type
>   TDay = (Monday = 0, Tuesday, Wednesday, Thursday, Friday, Saturday,
> Sunday);
>
>   TDayHelper = record helper for TDay
> function AsByte: Byte;
> function ToString: string;
>   end;

I didn't know that helpers could be used in enums too. Great.

Best regards,
Marcos Douglas
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Marcos Douglas B. Santos
On Wed, Aug 23, 2017 at 11:00 AM, Anthony Walter  wrote:
> Marcos, it doesn't work that way.
>
> Type helpers simply allow you to extend an existing type with new methods
> and/or properties. When you declare a type helper you extend all instances
> of said type given that:
>
> A) You 'use' the unit declaring the type helper in some other unit.
> B) No other unit you're using also defines a type helper for that same type.
> Only one type helper per type allowed.
>
> As to what they are useful for, consider the following:
>
> type
>   // IShellLink is define by Microsoft
>   IShellLinkHelper = record helper for IShellLink
>   public
> procedure Save(const Target, Description, Link: string);
>   end;

Anthony,

I understood. In fact, this is a great feature for whose work with
interfaces a lot.
We can have small interfaces but add some methods in just some places,
extending such interfaces.

Is this compatible with Delphi?

Best regards,
Marcos Douglas
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Anthony Walter
Here is another example:

type
  TDay = (Monday = 0, Tuesday, Wednesday, Thursday, Friday, Saturday,
Sunday);

  TDayHelper = record helper for TDay
function AsByte: Byte;
function ToString: string;
  end;

function TDayHelper.AsByte: Byte;
begin
  Result := Ord(Self);
end;

function TDayHelper.ToString: string;
begin
  case Self of
Monday: Result := 'Monday';
Tuesday: Result := 'Tuesday';
Wednesday: Result := 'Wednesday';
Thursday: Result := 'Thursday';
Friday: Result := 'Friday';
Saturday: Result := 'Saturday';
Sunday: Result := 'Sunday';
  end;
end;

And you can have code like this:

var
  Day: TDay;
  S: string;
begin
  Day := TDay.Monday;
  S := Day.ToString.ToLower;
  WriteLn(S);
end;
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Anthony Walter
Marco, it doesn't work that way.

Type helpers simply allow you to extend an existing type with new methods
and/or properties. When you declare a type helper you extend all instances
of said type given that:

A) You 'use' the unit declaring the type helper in some other unit.
B) No other unit you're using also defines a type helper for that same
type. Only one type helper per type allowed.

As to what they are useful for, consider the following:

type
  // IShellLink is define by Microsoft
  IShellLinkHelper = record helper for IShellLink
  public
procedure Save(const Target, Description, Link: string);
  end;

procedure IShellLinkHelper.Save(const Target, Description, Link: string);
var
  P: IPersistsFile;
begin
  SetPath(Target);
  SetDescription(Description);
  if Self is IPersistsFile then
  begin
P := Self as IPersistsFile;
P.Save(Link, True);
  end;
end;

...

CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IShellLink,
Link);

Link.Save('C:\Program Files\Google\chrome.exe', 'Web Browser',
C:\Users\me\Desktop\The Internet.lnk')
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Marcos Douglas B. Santos
On Wed, Aug 23, 2017 at 6:18 AM, Sven Barth via fpc-pascal
 wrote:
> Am 23.08.2017 09:42 schrieb "Michael Van Canneyt" :
>>
>>
>> Any other examples ?
>
> It's the same as for the other kinds of helpers: being syntactic sugar.
> E.g. if I have an interface that has two methods that both return a string,
> but more often than not I need a combination of both results. The previous
> solution would have been a global function, now I can also do a helper with
> a method.

If I understood right, I could have a Interface with 1 method and
maybe 3 classes that implements this method. But one of theses classes
could use an interface helper to add 1 or more methods. All instances
(variables) will be interface-typed but in that last case, the
instance will have more that 1 original method.

Am I right?
(please say yes)

Best regards,
Marcos Douglas
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Sven Barth via fpc-pascal
Am 23.08.2017 09:42 schrieb "Michael Van Canneyt" :
>
>
>
> On Tue, 22 Aug 2017, Anthony Walter wrote:
>
>> I just wanted to point out that revision 37023 Sven added type helper
>> support for interfaces.
>>
>> Good job and thank you Sven!
>>
>
> I'm having trouble understanding why this could be useful (apart from
completeness).
> The only thing I can come up with is adding properties:
>
>   Property SomeProp : TSomeType Read GetSomeProp Write SetSomeProp;
>
> where GetSomeProp and SetSomeProp are part of the interface.
>
> Any other examples ?

It's the same as for the other kinds of helpers: being syntactic sugar.
E.g. if I have an interface that has two methods that both return a string,
but more often than not I need a combination of both results. The previous
solution would have been a global function, now I can also do a helper with
a method.

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Michael Van Canneyt



On Tue, 22 Aug 2017, Anthony Walter wrote:


I just wanted to point out that revision 37023 Sven added type helper
support for interfaces.

Good job and thank you Sven!



I'm having trouble understanding why this could be useful (apart from 
completeness).
The only thing I can come up with is adding properties:

  Property SomeProp : TSomeType Read GetSomeProp Write SetSomeProp;

where GetSomeProp and SetSomeProp are part of the interface.

Any other examples ?

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Check In Interface Type Helpers

2017-08-23 Thread Sven Barth via fpc-pascal
Am 23.08.2017 02:04 schrieb "Anthony Walter" :
>
> I just wanted to point out that revision 37023 Sven added type helper
support for interfaces.
>
> Good job and thank you Sven!

You're welcome. I wanted to add them for some time already :)

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal