[Lazarus] New Graphics/Animation Library - Language Feature Requests

Anthony Walter sysrpl at gmail.com
Thu Oct 11 22:58:39 CEST 2012


Okay, I looked into global operators a bit more and I think what you
are referring to is objpas mode operator functions. If this is the
case, then there is a problem because I don't believe objpas mode
supports explicit conversions.

For clarity, I believe the correct intended use of explicit
conversions is when a loss of data may occur while converting a type
(rounding a float for example), and implicit conversions are correct
when no loss of data can occur (assigning a 32bit integer to a 32bit
float).

type
  Float = Single;
  TPointI = TPoint<Integer>;
  TPointF = TPoint<Float>;

FloatPoint := IntPoint; // correct, no loss of data
IntPoint := FloatPoint; // incorrect, data might be lost
IntPoint := TPointI(FloatPoint); // okay, data might be lost but
explicit conversion was used

I don't think the above is currently possible given that you cannot
define Implicit or Explicit operators on TPointI or TPointF since they
are specialized from a generic record type (no inheritance).

I believe the fix would be to allow for class operators on class,
record, and interface helper types.

type
  TPointFHelper = record helper for TPointF
    class operator Implicit(const Value: TPointI): TPointF;
    class operator Explicit(const Value: TPointF): TPointI;
  end;

{ TPointFHelper }

class operator TPointFHelper.Implicit(const Value: TPointI): TPointF;
begin
  Result.X := Value.X;
  Result.Y := Value.Y;
end;

class operator TPointFHelper.Explicit(const Value: TPointF): TPointI;
begin
  Result.X := Round(Value.X);
  Result.Y := Round(Value.Y);
end;




More information about the Lazarus mailing list