Jump to content
Lars Fosdal

Anon function with undefined result yields no warning

Recommended Posts

Is this a known issue?
If you assign an anonymous function to a property, there is no warning if the result of the function is not defined.

Normally, a function where Result is not set will yield a warning.

program anon_method_func_result;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils;

type
  TConvertFunc<T> = reference to function(const invalue: string; out outvalue: T): Boolean;

  TValue<T> = class
  private
    FConverter: TConvertFunc<T>;
  public
    property Converter: TConvertFunc<T> read FConverter write FConverter;
    constructor Create;
  end;

constructor TValue<T>.Create;
begin
  FConverter := function(const inValue: string; out outvalue: T): Boolean
    begin
      outvalue := Default(T); 
      // Would have expected a warning here
      // since Result is never set
    end;
end;

begin
  try
    try
      var V := TValue<Integer>.Create;
      V.Free;
    except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
    end;
  finally
  {$ifdef Debug}
    Write('Press Enter: ');
    Readln;
  {$endif}
  end;
end.

 

Share this post


Link to post
3 hours ago, Stefan Glienke said:

as old as generics

Can't expect 'em to implement a new feature in under 5 years now..

  • Haha 3

Share this post


Link to post
Guest

Ouch. Thx!!!, one thingy off my plate. I vas bitten too. Did not have the time to dig in to that at that moment. Delphipraxis shows its use case again!

Edited by Guest

Share this post


Link to post
On 8/27/2021 at 12:34 AM, FredS said:

Can't expect 'em to implement a new feature in under 5 years now..

And another 5 for it to actually work properly. 

  • Sad 1

Share this post


Link to post
On 8/26/2021 at 4:34 PM, FredS said:

Can't expect 'em to implement a new feature in under 5 years now..

This one was created on 11/Mar/15 🙂

Share this post


Link to post
Guest
3 hours ago, Vandrovnik said:

This one was created on 11/Mar/15 🙂

Please use ISO dates. I would have to do some research in order to know if you mean 2011-03-15 or 2015-03-11.

I would have to do research even to know if the "/" is information enough or if i need to know your "locale".

Share this post


Link to post
2 minutes ago, Dany Marmur said:

Please use ISO dates. I would have to do some research in order to know if you mean 2011-03-15 or 2015-03-11.

I would have to do research even to know if the "/" is information enough or if i need to know your "locale".

I just copies it from https://quality.embarcadero.com/browse/RSP-10506

I suppose it was created in 2015, but I would believe in 2011, too 🙂

Edited by Vandrovnik
  • Haha 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×