[fpc-pascal] High() and Low() for empty dynamic arrays
Some time ago there was a discussion about the data type given back by the "length" function. Several reasons were given for the type beeing a *signed* integer. But what about High() and Low()? Shouldn't they give back signed integers too for the same reasons? Especially, when I have a dynamic array like var MyArray : array of ... ... for i := Low(MyArray) to High(MyArray) do MyArray[i] := ... I now have to prepend this with a if Length(MyArray)<>0 then.. because otherwise I would get an error in case the length is 0 because Low() and High() both give back 0. The result of both functions is the same as if the array had exact one element. I find this quite illogical and would prefer if High() gives back a value less than Low() in this case so that the above for-loop would not be entered at all without the need to prepend it with an if-condition. For example, setting High() to -1 while Low() to 0 for empty dynamic arrays would avoid this special case. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] High() and Low() for empty dynamic arrays
Am 08.02.2014 16:56, schrieb Jürgen Hestermann: because otherwise I would get an error in case the length is 0 because Low() and High() both give back 0. The result of both functions is the same as if the array had exact one element. Here: var MyArray : array of longint; begin writeln(low(MyArray)); writeln(high(MyArray)); end. prints 0 -1 Unfortunatly you posted not a complete example which shows the behaviour but only uncompilable code snippts. Post always complete examples when discussing strange behaviour. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] High() and Low() for empty dynamic arrays
> Here: > > var MyArray : array of longint; > > begin >writeln(low(MyArray)); >writeln(high(MyArray)); > end. > > prints > > 0 > -1 Is this (High() on empty dynamic arrays return -1) documented somewhere? -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/High-and-Low-for-empty-dynamic-arrays-tp5718263p5718265.html Sent from the Free Pascal - General mailing list archive at Nabble.com. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] High() and Low() for empty dynamic arrays
Am 2014-02-08 17:05, schrieb Florian Klaempfl: > Here: > var MyArray : array of longint; > begin > writeln(low(MyArray)); > writeln(high(MyArray)); > end. > prints > 0 > -1 Hmm. I was under the impression that both give back zero. But you are right. A closer look showed that the error I got for arrays of zero length came from the fact that I used a Cardinal type variable for the for-loop: --- var i : Cardinal; for i := Low(MyArray) to High(MyArray) do ... --- which raises an exception when the array is empty but works ok in all other cases. Thanks for pointing me to my fault. > Unfortunatly you posted not a complete example which shows the behaviour but only uncompilable code snippts. > Post always complete examples when discussing strange behaviour. You mean I should post thousands of code lines? ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] High() and Low() for empty dynamic arrays
On Sat, 8 Feb 2014, leledumbo wrote: Here: var MyArray : array of longint; begin writeln(low(MyArray)); writeln(high(MyArray)); end. prints 0 -1 Is this (High() on empty dynamic arrays return -1) documented somewhere? It is now, in the system unit documentation for the High() function. Michael. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] High() and Low() for empty dynamic arrays
Am 08.02.2014 17:35, schrieb Jürgen Hestermann: > Unfortunatly you posted not a complete example which shows the behaviour but only uncompilable code snippts. > Post always complete examples when discussing strange behaviour. You mean I should post thousands of code lines? Of course not, just a small example as I did. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] High() and Low() for empty dynamic arrays
Am 2014-02-08 17:40, schrieb Florian Klaempfl: You mean I should post thousands of code lines? Of course not, just a small example as I did. But for what reason? It just generates work without benefit. If I had doubts about my assumption regarding High and Low I would not have asked my question at all but instead tested it more thoroughly. But I had no doubts so I only asked why High and Low give back 0. Why should I write code for my question? ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] High() and Low() for empty dynamic arrays
Am 08.02.2014 18:06 schrieb "Jürgen Hestermann" : > > > Am 2014-02-08 17:40, schrieb Florian Klaempfl: > >> >>> You mean I should post thousands of code lines? >> >> >> Of course not, just a small example as I did. >> > > But for what reason? > It just generates work without benefit. > > If I had doubts about my assumption regarding High and Low > I would not have asked my question at all > but instead tested it more thoroughly. > > But I had no doubts so I only asked why High and Low give back 0. > Why should I write code for my question? This way we could try to reproduce your problem easier. Also it often happens that the problem goes away when one simplifies it, because it was an error in ones own code. This would likely have been true here as well. Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] High() and Low() for empty dynamic arrays
Am 08.02.2014 18:06, schrieb Jürgen Hestermann: > > Am 2014-02-08 17:40, schrieb Florian Klaempfl: >> >>> You mean I should post thousands of code lines? >> >> Of course not, just a small example as I did. >> > > But for what reason? > It just generates work without benefit. > > If I had doubts about my assumption regarding High and Low > I would not have asked my question at all > but instead tested it more thoroughly. > > But I had no doubts so I only asked why High and Low give back 0. > Why should I write code for my question? You didn't have only a question but you made also a proposal for a language change. And when proposing a change it is very usefull to have an example which demonstrates the reason for the change. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
[fpc-pascal] Some questions regarding language changes in trunk
> http://wiki.freepascal.org/User_Changes_Trunk#Comparative_operators_can_have_any_result_type Is this a preparation for LINQ-like functionality? > http://wiki.freepascal.org/User_Changes_Trunk#True_and_False_are_not_keywords_anymore With false and true being integer, how could it be used in an boolean-expression-expected context? e.g. if, while and until condition? Anyway, AFAIK classically Boolean is declared as enum: type Boolean = (False,True); -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Some-questions-regarding-language-changes-in-trunk-tp5718272.html Sent from the Free Pascal - General mailing list archive at Nabble.com. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal