[fpc-devel] XML node dump feature
Ben Grasset
operator97 at gmail.com
Tue Jun 25 02:15:50 CEST 2019
On Mon, Jun 24, 2019 at 4:11 PM J. Gareth Moreton <gareth at moreton-family.com>
wrote:
> "constexpr" is essentially to C++ as "pure" would be to FPC. Some of the
> methods that involve pointers and var parameters (which is probably the
> closest equivalent to the "&" parameters in C++) may be flagged as impure
> because they can't be dereferenced at compile time. Nevertheless, a
> function that simply returns a formatted string, rather than writing
> directly to an output stream, is certainly doable.
>
Well, just to be sure we're on the same page in general, the hypothetical
Pascal equivalent of that program would look something like this I guess:
*program *Example;
*{$mode ObjFPC}
{$modeswitch AdvancedRecords}
**uses *SysUtils;
*type
generic *TVec3<T> = *record
*X, Y, Z: T;
*class function *Create(*const *IX, IY, IZ: T): TVec3; static; pure;
*class operator *+(constref LHS, RHS: TVec3): TVec3; *vectorcall*; pure;
*class operator *-(constref LHS, RHS: TVec3): TVec3; *vectorcall*; pure;
*class operator **(constref LHS, RHS: TVec3): TVec3; *vectorcall*; pure;
*class operator */(constref LHS, RHS: TVec3): TVec3; *vectorcall*; pure;
*// This one doesn't really need to be "pure"... normal inlining
would be fine.
**procedure *Print; pure;
*end*;
*class function *TVec3.Create(*const *IX, IY, IZ: T): TVec3;
*begin
with *Result *do begin
*X := IX;
Y := IY;
Z := IZ;
*end*;
*end*;
*class operator *TVec3.+(constref LHS, RHS: TVec3): TVec3; *vectorcall*;
*begin
*Result := TVec3.Create(LHS.X + RHS.X, LHS.Y + RHS.Y, LHS.Z + RHS.Z);
*end*;
*class operator *TVec3.-(constref LHS, RHS: TVec3): TVec3; *vectorcall*;
*begin
*Result := TVec3.Create(LHS.X - RHS.X, LHS.Y - RHS.Y, LHS.Z - RHS.Z);
*end*;
*class operator *TVec3.*(constref LHS, RHS: TVec3): TVec3; *vectorcall*;
*begin
*Result := TVec3.Create(LHS.X * RHS.X, LHS.Y * RHS.Y, LHS.Z * RHS.Z);
*end*;
*class operator *TVec3./(constref LHS, RHS: TVec3): TVec3; *vectorcall*;
*begin
*Result := TVec3.Create(LHS.X / RHS.X, LHS.Y / RHS.Y, LHS.Z / RHS.Z);
*end*;
*procedure *TVec3.Print;
*begin
case *GetTypeKind(T) *of
*tkFloat:
WriteLn(Format('[%.4f, %.4f, %.4f]', [X, Y, Z]));
tkInteger, tkInt64, tkQWord:
WriteLn(Format('[%d, %d, %d]', [X, Y, Z]));
*otherwise
*WriteLn('[N/A, N/A, N/A]');
*end*;
*end*;
*type *TVec3F = *specialize *TVec3<Single>;
*const
*A: TVec3F = (X: 1.2; Y: 2.4; Z: 3.8);
B: TVec3F = (X: 2.1; Y: 4.2; Z: 8.3);
*// You can't do the next part currently, obviously
*C: TVec3F = A + B;
D: TVec3F = A - B;
E: TVec3F = A * B;
F: TVec3F = A / B;
*begin
*C.Print();
D.Print();
E.Print();
F.Print();*end*.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20190624/508bce8d/attachment-0001.html>
More information about the fpc-devel
mailing list