Re: [fpc-devel] Abs(Variant) unexpected result
On Sun, Feb 25, 2018 at 5:34 PM, Michael Van Canneytwrote: >> So, would it be possible to have an overloaded Abs(V: Variant): >> Variant; function in the variants unit? > > > I advise against it. > > S : String; > > begin > S:='My very nice string'; > S:=Abs(S); > end; > > Will then compile and give a run-time error, as opposed to a compile-time > error now. Suggestion from the forum: function Abs(var V: Variant): Variant; overload; begin; If (VarIsNumeric(V)) then begin if V< 0 then Result := -V else Result := V end else Raise EVariantBadVarTypeError.Create(SomeMessage); end; While this will give hints if you did not initialize V, it will not let you compile Abs(AString). Bart ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] MACRO - correct syntax?
On 25/02/18 18:15, Giuliano Colla wrote: Il 25/02/2018 18:34, Florian Klämpfl ha scritto: http://www.gnu-pascal.de/gpc/Preprocessor.html, nobody prevents people to run a preprocessor before> FPC gets the code. That's a constructive suggestion. Easily put in the makefile [DUCKS] :-) -- Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues] ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] MACRO - correct syntax?
Il 25/02/2018 18:34, Florian Klämpfl ha scritto: http://www.gnu-pascal.de/gpc/Preprocessor.html, nobody prevents people to run a preprocessor before FPC gets the code. That's a constructive suggestion. If fpc doesn't implement an actual preprocessor, but macro expansion is part of the source parsing, then I must agree with you that the effort required wouldn't be justified by the benefits obtained. Thank you for your time. Giuliano ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Abs(Variant) unexpected result
On 25/02/18 17:59, Michael Van Canneyt wrote: On Sun, 25 Feb 2018, Bart wrote: On Sun, Feb 25, 2018 at 5:34 PM, Michael Van Canneytwrote: So, all we can do is let the compiler pick the float version for Abs(Variant)? It seems so. Better yet, don't use variants. They violate what Pascal stands for: type safety. If you really _must_ use them, do any conversions explicitly. Additionally, A warning when using variant with Abs() would be useful then. var X:Variant; begin X:=5.5; writeln('X = ',Abs(X));// <-- gives 6 Rik ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] MACRO - correct syntax?
Am 25.02.2018 um 18:21 schrieb Giuliano Colla: > Il 25/02/2018 13:55, Florian Klämpfl ha scritto: > >>> To limit their use. > > Well, just for sake of argument, it appears to me a rather drastic approach. > > You may write other similar pages on stackoverflow, telling why Dereferencing > is evil, Typecasting > is evil, or why "Absolute" or the "with" construct are evil. All of them, if > misused, can lead to > unexpected results, with code hard to read, easy to misinterpret, and hard to > debug. Nonetheless > they're part of the language, and, properly used, are useful programmer's > tools. http://www.gnu-pascal.de/gpc/Preprocessor.html, nobody prevents people to run a preprocessor before FPC gets the code. > > Most of the drawbacks of macro arguments can be easily overcome if > a) The listing of preprocessor output is generated. ... which would be a huge effort in FPC as at the point macro are replaced, everything is already tokenized, comments etc. are already stripped. > b) The preprocessor output is provided to the debugger as the actual program > source > None of the two should require a titanic effort! So why is it still a problem for C programmers? > > IMHO macro arguments, if properly used, may be a valid additional programmer > tool. This can be said for a lot of things we do not implement in FPC. > > E.g. Converting a C program to Pascal I had: > C code: > > #define EXCH_INITIALIZER(nam) \ > {NULL,NULL,NULL,NULL,NULL,PTHREAD_MUTEX_INITIALIZER,nam} > > EXCHANGE_DESCRIPTOR cp_ready_ex = EXCH_INITIALIZER("CP_RDY"); > > > Lacking macro arguments, the equivalent declaration in fpc became: > > cp_ready_ex: exchange_descriptor = ( > message_head : Nil; > message_tail : Nil; > task_head : Nil; > task_tail : Nil; > exchange_link : Nil; > mutex : (__m_reserved: 0; __m_count: 0; __m_owner: Nil;__m_kind: 0;__m_lock: > (__status: 0;__spinlock:0)); > name: ' CP_RDY';); > > (and I had quite a number of them!) > A lot of error prone typing, just to provide the only useful information, > i.e. the readable name of > the thing. For every language feature some rare use case can be found. ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Abs(Variant) unexpected result
On Sun, Feb 25, 2018 at 5:59 PM, Michael Van Canneytwrote: >> So, all we can do is let the compiler pick the float version for >> Abs(Variant)? > > > It seems so. OK, for D compatibilty (untill they change that). > Better yet, don't use variants. They violate what Pascal stands > for: type safety. > > If you really _must_ use them, do any conversions explicitly. Point taken. Bart ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] MACRO - correct syntax?
Il 25/02/2018 13:55, Florian Klämpfl ha scritto: To limit their use. Well, just for sake of argument, it appears to me a rather drastic approach. You may write other similar pages on stackoverflow, telling why Dereferencing is evil, Typecasting is evil, or why "Absolute" or the "with" construct are evil. All of them, if misused, can lead to unexpected results, with code hard to read, easy to misinterpret, and hard to debug. Nonetheless they're part of the language, and, properly used, are useful programmer's tools. Most of the drawbacks of macro arguments can be easily overcome if a) The listing of preprocessor output is generated. b) The preprocessor output is provided to the debugger as the actual program source None of the two should require a titanic effort! IMHO macro arguments, if properly used, may be a valid additional programmer tool. E.g. Converting a C program to Pascal I had: C code: #define EXCH_INITIALIZER(nam) \ {NULL,NULL,NULL,NULL,NULL,PTHREAD_MUTEX_INITIALIZER,nam} EXCHANGE_DESCRIPTOR cp_ready_ex = EXCH_INITIALIZER("CP_RDY"); Lacking macro arguments, the equivalent declaration in fpc became: cp_ready_ex: exchange_descriptor = ( message_head : Nil; message_tail : Nil; task_head : Nil; task_tail : Nil; exchange_link : Nil; mutex : (__m_reserved: 0; __m_count: 0; __m_owner: Nil;__m_kind: 0;__m_lock: (__status: 0;__spinlock:0)); name: ' CP_RDY';); (and I had quite a number of them!) A lot of error prone typing, just to provide the only useful information, i.e. the readable name of the thing. Giuliano ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Abs(Variant) unexpected result
On Sun, 25 Feb 2018, Bart wrote: On Sun, Feb 25, 2018 at 5:34 PM, Michael Van Canneytwrote: So, would it be possible to have an overloaded Abs(V: Variant): Variant; function in the variants unit? I advise against it. S : String; begin S:='My very nice string'; S:=Abs(S); end; Will then compile and give a run-time error, as opposed to a compile-time error now. Did not think about that. Yeah, that's really bad. So, all we can do is let the compiler pick the float version for Abs(Variant)? It seems so. Better yet, don't use variants. They violate what Pascal stands for: type safety. If you really _must_ use them, do any conversions explicitly. Michael. ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Abs(Variant) unexpected result
On Sun, Feb 25, 2018 at 5:34 PM, Michael Van Canneytwrote: >> So, would it be possible to have an overloaded Abs(V: Variant): >> Variant; function in the variants unit? > > > I advise against it. > > S : String; > > begin > S:='My very nice string'; > S:=Abs(S); > end; > > Will then compile and give a run-time error, as opposed to a compile-time > error now. Did not think about that. Yeah, that's really bad. So, all we can do is let the compiler pick the float version for Abs(Variant)? Bart ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Abs(Variant) unexpected result
On Sun, 25 Feb 2018, Bart wrote: On Sun, Feb 25, 2018 at 5:01 PM, Jonas Maebewrote: As Michael said, overloads are selected at compile time. This is true for both FPC and Delphi. We even have over a 100 unit tests that we ran under Delphi to reverse engineer their selection priorities in case of variants: https://svn.freepascal.org/svn/fpc/trunk/tests/test/cg/variants/ Abs(), however, gets a forced conversion to float in Delphi: https://bugs.freepascal.org/view.php?id=20551 Hmm, did not find that one (I searched bugtracker before posting here). Seems you are right: Delphi Tokyo 10.2 X = -1 VarIsFloat : FALSE VarIsNumeric: TRUE VarIsOrdinal: TRUE After Abs() X = 1 VarIsFloat : TRUE VarIsNumeric: TRUE VarIsOrdinal: FALSE So, would it be possible to have an overloaded Abs(V: Variant): Variant; function in the variants unit? I advise against it. S : String; begin S:='My very nice string'; S:=Abs(S); end; Will then compile and give a run-time error, as opposed to a compile-time error now. Michael. ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Abs(Variant) unexpected result
On Sun, Feb 25, 2018 at 5:01 PM, Jonas Maebewrote: > As Michael said, overloads are selected at compile time. This is true for > both FPC and Delphi. We even have over a 100 unit tests that we ran under > Delphi to reverse engineer their selection priorities in case of variants: > https://svn.freepascal.org/svn/fpc/trunk/tests/test/cg/variants/ > > Abs(), however, gets a forced conversion to float in Delphi: > https://bugs.freepascal.org/view.php?id=20551 Hmm, did not find that one (I searched bugtracker before posting here). Seems you are right: Delphi Tokyo 10.2 X = -1 VarIsFloat : FALSE VarIsNumeric: TRUE VarIsOrdinal: TRUE After Abs() X = 1 VarIsFloat : TRUE VarIsNumeric: TRUE VarIsOrdinal: FALSE So, would it be possible to have an overloaded Abs(V: Variant): Variant; function in the variants unit? To be clear. Personally I don't need it (at least not ATM), I'm just curious. Thanks for explaining. Bart Bart ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Abs(Variant) unexpected result
On 25/02/18 16:41, Bart wrote: Delphi 10.2 Tokyo: X = -1,5 VarIsFloat : TRUE VarIsNumeric: TRUE VarIsOrdinal: FALSE After Abs() X = 1,5 VarIsFloat : TRUE VarIsNumeric: TRUE VarIsOrdinal: FALSE I asked to test with "X := -1" to see if Delphi always chooses the float overload. As Michael said, overloads are selected at compile time. This is true for both FPC and Delphi. We even have over a 100 unit tests that we ran under Delphi to reverse engineer their selection priorities in case of variants: https://svn.freepascal.org/svn/fpc/trunk/tests/test/cg/variants/ Abs(), however, gets a forced conversion to float in Delphi: https://bugs.freepascal.org/view.php?id=20551 Jonas ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Abs(Variant) unexpected result
Delphi 10.2 Tokyo: X = -1,5 VarIsFloat : TRUE VarIsNumeric: TRUE VarIsOrdinal: FALSE After Abs() X = 1,5 VarIsFloat : TRUE VarIsNumeric: TRUE VarIsOrdinal: FALSE I asked to test with "X := -1" to see if Delphi always chooses the float overload. Bart ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Abs(Variant) unexpected result
On 25/02/18 15:59, Bart wrote: I don't know how Delphi behaves, but the official Delphi docs state that Abs() only has overloads for floats, integer and int64. If their compiler behaves as the docs say, Abs(Variant) would be a syntax error. No, because variant can be implicitly converted to all of those types and there are different (undocumented by Embarcadero) overload selection priorities for each of those types. Jonas ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Abs(Variant) unexpected result
On Sun, Feb 25, 2018 at 3:40 PM, Michael Van Canneytwrote: > Only if we add an > Abs(Variant) : Variant; which will then make the choice will this work. If the compiler accepts Abs(Variant), it should IMHO have a correct overload for this. (Maybe in the variants unit?) I don't know how Delphi behaves, but the official Delphi docs state that Abs() only has overloads for floats, integer and int64. If their compiler behaves as the docs say, Abs(Variant) would be a syntax error. I asked on Dutch Delphi forum if someone could test. Bart ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Abs(Variant) unexpected result
On Sun, 25 Feb 2018, Bart wrote: On Sun, Feb 25, 2018 at 1:01 PM, Michael Van Canneytwrote: The compiler does not know at compile time what type the variant is, how can you expect it to choose the "right" overloaded version ? I would have expected that it will choose the right one @runtime . Choices between overloads are always made at compile time. Only if we add an Abs(Variant) : Variant; which will then make the choice will this work. But I'm not sure whether this would interfere with the normal working of abs() overloads. Michael. ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Abs(Variant) unexpected result
On Sun, Feb 25, 2018 at 1:01 PM, Michael Van Canneytwrote: > The compiler does not know at compile time what type the variant is, how can > you expect it to choose the "right" overloaded version ? I would have expected that it will choose the right one @runtime . Bart ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] MACRO - correct syntax?
Thank you Sven! I saw all of the threads on this topic in 2011, but, figured I would ask here if it had changed, and I was just doing it wrong. We are in the process of porting Minix 3 source to Free Pascal (Andrew T. tells me it can’t be done, and I know FPC can handle it). I just wanted to keep the C macros close so he can see I did an automated conversion not a logic conversion. Thanks to everyone else on this topic, but “not supported” is fine with me. Ozz From: fpc-devel [mailto:fpc-devel-boun...@lists.freepascal.org] On Behalf Of Sven Barth via fpc-devel Sent: Sunday, February 25, 2018 3:17 AM To: FPC developers' listCc: Sven Barth Subject: Re: [fpc-devel] MACRO - correct syntax? Am 25.02.2018 03:01 schrieb "Ozz Nixon" >: {$MACRO ON} {$DEFINE _CTASSERT(X,Y,Z):=assert(x=y,z);} Begin _CTASSERT(1,1,'Constant failed.'); end. Macros with arguments are not supported. Regards, Sven ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] MACRO - correct syntax?
Am 25.02.2018 um 13:31 schrieb Florian Klämpfl: > Am 25.02.2018 um 13:28 schrieb Giuliano Colla: >> Il 25/02/2018 13:15, Sven Barth via fpc-devel ha scritto: >> >>> Yes, we definitely feel good about this, because we don't *want* parameter >>> support in FPC's macro >>> handling. >> >> Maybe I'm obtuse, but I fail to grasp the reason why. >> Can you elaborate a bit more? > > To limit their use. Well, probably usage is the better word, meaning being less used. ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] MACRO - correct syntax?
Am 25.02.2018 um 13:28 schrieb Giuliano Colla: > Il 25/02/2018 13:15, Sven Barth via fpc-devel ha scritto: > >> Yes, we definitely feel good about this, because we don't *want* parameter >> support in FPC's macro >> handling. > > Maybe I'm obtuse, but I fail to grasp the reason why. > Can you elaborate a bit more? To limit their use. More can be read here: https://stackoverflow.com/questions/14041453/why-are-preprocessor-macros-evil-and-what-are-the-alternatives The same would would apply to pascal as well. ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] MACRO - correct syntax?
Il 25/02/2018 13:15, Sven Barth via fpc-devel ha scritto: Yes, we definitely feel good about this, because we don't *want* parameter support in FPC's macro handling. Maybe I'm obtuse, but I fail to grasp the reason why. Can you elaborate a bit more? Thanks Giuliano ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] MACRO - correct syntax?
Am 25.02.2018 12:14 schrieb "Mark Morgan Lloyd" < markmll.fpc-de...@telemetry.co.uk>: On 25/02/18 10:30, Florian Klämpfl wrote: > Am 25.02.2018 um 11:12 schrieb Mark Morgan Lloyd:> On 25/02/18 02:15, Ozz > Nixon wrote:>> {$MACRO ON}>> {$DEFINE _CTASSERT(X,Y,Z):=assert(x=y,z);}>> > Begin _CTASSERT(1,1,'Constant failed.');end.> > I don't believe > parameters are supported :-(> > Why so sad? They are not supported on purpose :) > Because it messes up what the OP was trying to do, which is not necessarily something to feel good about. Yes, we definitely feel good about this, because we don't *want* parameter support in FPC's macro handling. What the author wants to achieve here can also be done using a procedure marked as inline. As long as the unit containing the procedure is rebuild when assertions are enabled or disabled everything will work as expected and even the whole call will disappear if it isn't needed. Much cleaner than a preprocessor. Regards, Sven ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] Abs(Variant) unexpected result
On Sun, 25 Feb 2018, Bart wrote: Hi, See: http://forum.lazarus.freepascal.org/index.php/topic,40223.msg277657/ This seems rather unexpected. Not really. The abs() function is overloaded for different types. The compiler does not know at compile time what type the variant is, how can you expect it to choose the "right" overloaded version ? It probably takes the first fit for possible values in a Variant, which is most likely an integer version. Don't use variants if you want type safety :) Michael. ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
[fpc-devel] Abs(Variant) unexpected result
Hi, See: http://forum.lazarus.freepascal.org/index.php/topic,40223.msg277657/ This seems rather unexpected. Is it a bug? program v; {$ifdef windows} {$apptype console} {$endif} uses variants; var X: Variant; B: Boolean; begin X := -1.5; writeln('X = ',X); B := VarIsFloat(X); writeln('VarIsFloat : ',B); B := VarIsNumeric(X); writeln('VarIsNumeric: ',B); B := VarIsOrdinal(X); writeln('VarIsOrdinal: ',B); X := Abs(X); writeln('After Abs()'); writeln('X = ',X); B := VarIsFloat(X); writeln('VarIsFloat : ',B); B := VarIsNumeric(X); writeln('VarIsNumeric: ',B); B := VarIsOrdinal(X); writeln('VarIsOrdinal: ',B); end. Outputs: C:\Users\Bart\LazarusProjecten\bugs\Console\variants>v X = -1,5 VarIsFloat : TRUE VarIsNumeric: TRUE VarIsOrdinal: FALSE After Abs() X = 2 VarIsFloat : FALSE VarIsNumeric: TRUE VarIsOrdinal: TRUE Bart ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] MACRO - correct syntax?
On 25/02/18 10:30, Florian Klämpfl wrote: Am 25.02.2018 um 11:12 schrieb Mark Morgan Lloyd:> On 25/02/18 02:15, Ozz Nixon wrote:>> {$MACRO ON}>> {$DEFINE _CTASSERT(X,Y,Z):=assert(x=y,z);}>> Begin _CTASSERT(1,1,'Constant failed.');end.> > I don't believe parameters are supported :-(> Why so sad? They are not supported on purpose :) Because it messes up what the OP was trying to do, which is not necessarily something to feel good about. And as far as "doing it on purpose" is concerned, you might or might not be acquainted with what happened when the illustrious Brigadier Gerard tried his hand at cricket... https://www.arthur-conan-doyle.com/index.php/The_Brigadier_in_England :-) -- Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues] ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] MACRO - correct syntax?
Am 25.02.2018 um 11:12 schrieb Mark Morgan Lloyd: > On 25/02/18 02:15, Ozz Nixon wrote: >> {$MACRO ON} >> {$DEFINE _CTASSERT(X,Y,Z):=assert(x=y,z);} >> Begin _CTASSERT(1,1,'Constant failed.');end. > > I don't believe parameters are supported :-( > Why so sad? They are not supported on purpose :) ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] MACRO - correct syntax?
On 25/02/18 02:15, Ozz Nixon wrote: {$MACRO ON} {$DEFINE _CTASSERT(X,Y,Z):=assert(x=y,z);} Begin _CTASSERT(1,1,'Constant failed.');end. I don't believe parameters are supported :-( -- Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues] ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Re: [fpc-devel] MACRO - correct syntax?
Am 25.02.2018 03:01 schrieb "Ozz Nixon": {$MACRO ON} {$DEFINE _CTASSERT(X,Y,Z):=assert(x=y,z);} Begin _CTASSERT(1,1,'Constant failed.'); end. Macros with arguments are not supported. Regards, Sven ___ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel