The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,862 other subscribers

Delphi analog to C# ?? null-coalescing operator and Light Table like debugger evaluation

Posted by jpluimers on 2017/09/05

Interesting stuff:

via:

In more detail:

Source: 2016-10-15 – Zenryokuwawa

And:

type
  TObjectHelper = class helper for TObject
  public
    class function &&op_LogicalOr(A, B: T): T; static;
  end;

class function TObjectHelper.&&op_LogicalOr(A, B: T): T;
begin
  if A <> nil then
    Result := A
  else
    Result := B;
end;

procedure Test;
var
  sl1, sl2, sl3: TStringList;
begin
  sl1 := nil;
  sl2 := TStringList.Create;
  sl3 := sl1 or sl2; // -> sl3 = sl2
end;

–jeroen


type
TObjectHelper = class helper for TObject
public
class function &&op_LogicalOr<T: class>(A, B: T): T; static;
end;
class function TObjectHelper.&&op_LogicalOr<T>(A, B: T): T;
begin
if A <> nil then
Result := A
else
Result := B;
end;
procedure Test;
var
sl1, sl2, sl3: TStringList;
begin
sl1 := nil;
sl2 := TStringList.Create;
sl3 := sl1 or sl2; // -> sl3 = sl2
end;

https://vimeo.com/36579366

3 Responses to “Delphi analog to C# ?? null-coalescing operator and Light Table like debugger evaluation”

  1. rvelthuis's avatar

    rvelthuis said

    The TObjectHelper thing is amazing. It also works on classes, and I already managed to do addition, or to write a helper that gives TStrings an in operator. It is simply a cool find.

    • jpluimers's avatar

      jpluimers said

      It’s indeed one of the language additions I like a lot.

      • sglienke's avatar

        sglienke said

        It’s a feature by accident – I hope they will add it as an official feature. Some operators are harmless to introduce for classes also on non ARC. Also introducing operator overloads per helper is a good thing and should be supported officially rather than having to use some compiler glitch.

Leave a reply to rvelthuis Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.