Jump to content
Stefan Glienke

Delphi WAT of the day

Recommended Posts

function (const arr: TArray<T>): RecWithTwoFields (Pointer, Integer)

 

image.png.7fa5d277e5b02ba2c8ab1b239858a6ab.png

 

Bonus: inlining that function looks like this - go go eax, you got it eventually!

 

image.png.cf783e9a855a5d6064bf61996ef2d46e.png

Edited by Stefan Glienke
  • Like 1
  • Thanks 1
  • Haha 1
  • Confused 2
  • Sad 2

Share this post


Link to post
19 hours ago, Stefan Glienke said:

Bonus: inlining that function looks like this - go go eax, you got it eventually!

That is useful if you get paid by the LOC compiler generates-

  • Haha 3

Share this post


Link to post
27 minutes ago, Stefan Glienke said:

Inspired by that other thread:

 


procedure TypeSafeMyAss(i: Cardinal);
begin
end;

var
  i: Integer;
begin
  i := -1;
  {$RANGECHECKS ON}
  TypeSafeMyAss(i);
end.

 

I don't know what you mean. For sure it gives a range check error.

Share this post


Link to post
16 minutes ago, Leif Uneus said:

I don't know what you mean. For sure it gives a range check error.

Exactly - at runtime. Warning would be nice, don't you think?

Edit: to be clear not because I put the -1 there which will for sure blow up but because Integer->Cardinal will blow up for 50% of the value range.

 

We have W1023 and W1024 when comparing or combining, but assignment/parameter passing blows up at runtime... eventually, some day.

Edited by Stefan Glienke
  • Like 2

Share this post


Link to post
9 minutes ago, Stefan Glienke said:

Exactly - at runtime. Warning would be nice, don't you think?

Ok, yes a warning would be expected!

Share this post


Link to post

Inspired by RSP-28311:

 

{$APPTYPE CONSOLE}
uses 
  TypInfo;

type
  generic = record
    class procedure test<T>(const value : T); static;
  end;

class procedure generic.test<T>(const value: T);
begin
  Writeln(GetTypeName(TypeInfo(T)));
end;

procedure test(a: Int32); overload;
begin
  Writeln('Int32');
end;

procedure test(a: Int64); overload;
begin
  Writeln('Int64');
end;

procedure test(a, b: Int32); overload;
begin
  Writeln('Int32');
end;

procedure test(a, b: Int64); overload;
begin
  Writeln('Int64');
end;

const
  one = Int64(1);
  two = Int64(MaxInt);
  three = Int64(MaxInt)+1;
  four: Int64 = 1;
begin
  generic.test(one);    // Int64
  generic.test(two);    // Int64
  generic.test(three);  // Int64
  generic.test(four);   // Int64

  test(one);            // Int64
  test(two);            // Int64
  test(three);          // Int64
  test(four);           // Int64

  test(one, 1);         // Int32 ?!
  test(two, 1);         // Int32
  test(three, 1);       // Int64
  test(four, 1);        // Int64
  test(one, Int64(1));  // Int64
  test(Int64(1), 1);    // Int32 ?!
end.

 

  • Sad 1

Share this post


Link to post

restart, mom

 

if I add 

procedure test(a: Int64; b: Int32); overload;
begin
  Writeln('Int64/Int32');
end;

everything is ok again. Why should the compiler decide for Int64 if it fits into Int32?

We know that the type cast in the const declaration is just some placeholder.

Edited by Attila Kovacs

Share this post


Link to post
9 minutes ago, Attila Kovacs said:

We know that the type cast in the const declaration is just some placeholder.

Which is not what the documentation says:

Quote

const MyNumber = Int64(17);

declares a constant called MyNumber, of type Int64, that returns the integer 17.

 

Edited by Stefan Glienke
  • Thanks 1

Share this post


Link to post
11 minutes ago, Stefan Glienke said:

Which is not what the documentation says:

 

Ahh, I see.

 

@Stefan Glienke

So you mean, it should not compile if the integer size and signedness does not match to any of the method declarations?

Or should it bring warnings?

I'd love to see those messages, could be interesting!

 

Edited by Attila Kovacs

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

×