Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2012-10

New Graphics/Animation Library - Language Feature Requests

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Anthony Walter

My request is a bit long and includes videos, images, and code. As such I've located it here:

http://www.codebot.org/cross/?doc=9589

The basic gist of my request is to advance some features in Free Pascal by asking the FPC community leaders to take interest. Also, much thanks to Sven Barth and the rest of the FPC team for your work on implementing so much with new Delphi language features and bringing generic support up many levels.

Reply by email

Sven Barth
to Anthony Walter
Anthony Walter wrote 10 lines
My request is a bit long and includes videos, images, and code. As such I've located it here: http://www.codebot.org/cross/?doc=9589 The basic gist of my request is to advance some features in Free Pascal by asking the FPC community leaders to take interest. Also, much thanks to Sven Barth and the rest of the FPC team for your work on implementing so much with new Delphi language features and bringing generic support up many levels.

If you have language requests, you should request them on fpc-devel, not Lazarus (though I'll take a look at your document, but some FPC devs are not subscribed to lazarus).

And regarding Delphi features: this is only the beginning :D

Regards,
Sven

Reply by email

Sven Barth
to Anthony Walter: My request is a bit long and includes videos, images, and code. As such I've located it…
Anthony Walter wrote 10 lines
My request is a bit long and includes videos, images, and code. As such I've located it here: http://www.codebot.org/cross/?doc=9589 The basic gist of my request is to advance some features in Free Pascal by asking the FPC community leaders to take interest. Also, much thanks to Sven Barth and the rest of the FPC team for your work on implementing so much with new Delphi language features and bringing generic support up many levels.

I have had a look at your requests. Interface helpers (and object helpers by the way) are something that I wanted to implement since I implemented helper support. First I'll add XE3's primitive type helper support though ;)

Regarding the operator overloads you can achive the same by using global operator overloads, so I don't know whether I'll add support for operators to record helpers...

Regards,
Sven

Reply by email

Anthony Walter
to Sven Barth

Sven, thank you for that information. I did some googling on free pascal and delphi global operator overloading and only found references describing operator overloading like I am already doing.

Could you provide me with an example of a global operator doing either an implicit or explicit overload, maybe continuing with with my example:

type
  TPointI = TPoint<Integer>;
  TPointF = TPoint<Float>;
// define a global operator to implicitly convert from TPointI to
TPointF, or explicit from TPointF to TPointI

Reply by email

Anthony Walter
to Sven Barth: I have had a look at your requests. Interface helpers (and object helpers by the way) are…

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;

Reply by email

Vincent Snijders
to Anthony Walter
Show quoted text 5 lines
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).

Even then loss of data can occur, because a 32 bit float can not represent 31 binary digits.

Vincent

Reply by email

Sven Barth
to Anthony Walter: Okay, I looked into global operators a bit more and I think what you are referring to is…
Anthony Walter wrote 5 lines
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.

They do, see below.

Show quoted text 19 lines
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).

If you don't know whether something works, you should just try it:

=== source begin ===

program asgnoptest;
{$mode objfpc}
{$modeswitch advancedrecords}
type
   generic TPoint<T> = record
     X, Y: T;
   end;
   TPointF = specialize TPoint<Single>;
   TPointI = specialize TPoint<LongInt>;
operator := (const Value: TPointI): TPointF;
begin
   Writeln('Implicit');
   Result.X := Value.X;
   Result.Y := Value.Y;
end;
operator Explicit(const Value: TPointF): TPointI;
begin
   Writeln('Explicit');
   Result.X := Round(Value.X);
   Result.Y := Round(Value.Y);
end;
var
   FloatPoint: TPointF;
   IntPoint: TPointI;
begin
   IntPoint.X := 21;
   IntPoint.Y := 42;
   FloatPoint := IntPoint;
   // No implicit from TPointF -> TPointI
   //IntPoint := FloatPoint;
   FloatPoint.X := 42.5;
   FloatPoint.Y := 21.25;
   IntPoint := TPointI(FloatPoint);
end.

=== source end ===

This compiles using 2.6.0. The output is (as expected):

=== output begin ===

Implicit
Explicit

=== output end ===

The only difference to operators in helpers would be that they aren't "grouped" in one structure.

That said: I'm not opposing your idea to allow operators in helpers (and classes) per se, but I just suggest you how you can acomplish your target with FPC's current features as I don't know when I'd get around to implement this.

Regards,
Sven

Reply by email