Jump to content
Stefan Glienke

Improving type safety

Recommended Posts

I think Marcos proposal to change the projects default settings would be a possible solution (workaround)

Quote

With {$WARN UNSAFE_CAST ON} the lines  ==> [dcc32 Warning] Unit47.pas(31): W1048 Unsafe typecast of 'TObject' to 'Pointer'

 

Share this post


Link to post

Look at this:

 

program Project20;
{$APPTYPE CONSOLE}
{$R *.res}
uses
  System.SysUtils;
type
  TVectorD3 = record
    x, y, z: Double;
  end;
  TDirection = type TVectorD3;
{$WARN UNSAFE_CAST ON}
procedure Test;
var
  V: TVectorD3;
  D: TDirection;
begin
  V := default (TVectorD3);
  D := TDirection(V);
end;
begin
  try
    Test;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Will also produce a Warning. So if you enable UNSAFE_CAST there will be a lot of Warnings.

And every place surround with a {$WARN UNSAFE_CAST OFF} is a bad Idea
But  I'm with Stefan here. A new Warning for Pointer Assignment would be fine.

 

 

  • Like 1

Share this post


Link to post

Search for UNSAFE_CAST in QP and you'll find some issues that report all the bogus this produces - maybe after fixing those it might be somewhat usable.

If that can avoid introducing yet a new warning type - I am all for that option.

Edited by Stefan Glienke
  • 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

×