Jump to content
PiedSoftware

Type inference in assignment but not comparison??

Recommended Posts

I like the way Delphi is getting better at arrays, but there is still a gap in the compiler that let me down. 

I tried this code in 12.2 to highlight the issue.
 


const nostrings: tarray<string> = [];

procedure TForm1.FormShow(Sender: TObject);
begin
  var a: tarray<string>;
  a := [];
  if a = nostrings then
    say('nostrings');
  if a = [] then
    say('it''s blank');
end;

procedure TForm1.say(const s: string);
begin
  ListBox1.Items.Add(s);
end;

This refuses to compile at the line

 if a = [] then

with the error "Incompatible types". But it has no problem with the assignment, so it looks like the programmers have just put this in the "do eventually" basket.

Does anyone have any insights into this?

--- Mark 

  • Sad 1

Share this post


Link to post
4 hours ago, PiedSoftware said:

Does anyone have any insights into this?

 

Delphi doesn't know that you want [] to be an array rather than a set. Because the language has been designed iteratively and there is ambiguity. Delphi literals are a bit of a mess.

 

In any case are you sure that you want to use = with a reference type? That's reference identity and not value identity.

 

You will need to test for Length(a)=0 or not Assigned(a) or a = nil

  • Like 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

×