Does MACRO work in this case?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Does MACRO work in this case?

silvioprog
Hello,

Looking an alternative to solve the problem I've talked at this topic, I've tried to solve it declaring a macro named "TIStringComparer.Ordinal":

=== begin code ===

{$IFDEF FPC}

function EqualityComparison(constref ALeft, ARight: string): Boolean;
begin
  Result := CompareText(ALeft, ARight) = 0;
end;

function ExtendedHasher(constref AValue: string): UInt32;
var
  S: string;
begin
  S := LowerCase(AValue);
  Result := TDefaultHashFactory.GetHashCode(Pointer(S), Length(S) * SizeOf(Char), 0);
end;

{$MACRO ON}
{$DEFINE TIStringComparer.Ordinal := TEqualityComparer<string>.Construct(EqualityComparison, ExtendedHasher) }

{$ENDIF}

=== end code ===

The macro is just for retro-compatibility with Delphi, allowing to use this syntax:

GVariables := TDictionary<string, string>.Create(TIStringComparer.Ordinal); // the macro should replace it, but ...

However, it seems the macro can't replace the original "TIStringComparer.Ordinal" to my "TEqualityComparer<string>.Construct(EqualityComparison, ExtendedHasher)" definition.

So, is macro indicated to solve this kind of problem?

--
Silvio Clécio

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Does MACRO work in this case?

David Emerson
On 09/08/2016 10:08 AM, silvioprog wrote:
> Hello,
>
> Looking an alternative to solve the problem I've talked at this topic
> <http://lists.freepascal.org/pipermail/fpc-pascal/2016-August/048635.html>,
> I've tried to solve it declaring a macro named "TIStringComparer.Ordinal":

Macros can only replace identifiers, the dot breaks it.

I would make something like,

_TIStringComparer_dot_Ordinal

and then you can use the definition you proposed.

I'm in the habit of naming macros with a leading underscore, as I don't
name anything else that way and it makes it easy to identify them.

~David.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Does MACRO work in this case?

silvioprog
On Thu, Sep 8, 2016 at 6:27 PM, David Emerson <[hidden email]> wrote:
On 09/08/2016 10:08 AM, silvioprog wrote:
Hello,

Looking an alternative to solve the problem I've talked at this topic
<http://lists.freepascal.org/pipermail/fpc-pascal/2016-August/048635.html>,
I've tried to solve it declaring a macro named "TIStringComparer.Ordinal":

Macros can only replace identifiers, the dot breaks it.

I would make something like,

_TIStringComparer_dot_Ordinal

and then you can use the definition you proposed.

I'm in the habit of naming macros with a leading underscore, as I don't name anything else that way and it makes it easy to identify them.

Thanks for sharing this info regarding macros. :-)

I've declared a macro trying to keep the Delphi syntax, so unfortunately I can't declare it as _TIStringComparer_dot_Ordinal instead of TIStringComparer.Ordinal, hence I've temporally declared it as:

class constructor TFoo.Bar;
begin
...
  GVariables := TDictionary<string, string>.Create(
{$IFDEF FPC}
    TEqualityComparer<string>.Construct(EqualityComparison, ExtendedHasher)
{$ELSE}
    TIStringComparer.Ordinal
{$ENDIF});
end;

but I will remove the implementation above as soon as rtl-generics allow to use TIStringComparer.Ordinal without workarounds.

--
Silvio Clécio

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal