Re: [fpc-pascal] Legitimate use of for and break
El 16/06/2023 a las 16:09, Mattias Gaertner via fpc-pascal escribió: On Fri, 16 Jun 2023 20:51:42 +0700 Hairy Pixels via fpc-pascal wrote: On Jun 16, 2023, at 6:23 PM, Thomas Kurz via fpc-pascal wrote: Whether it's elegant is a different question. In my opinion YES because it often gives better readable code than nested "if" statements inside the loop. But I've also read that using "break" is discouraged because it shows a bad choice of the loop range. This is highly suspect. Doing an early break in loops is the essence of how to do linear searching. No idea who thinks that's a bad idea. 20 years ago there were some programmers, claiming a loop condition must only be at start or end, but not in the middle. I mostly agree with that programmers. That's called structured programming. "Break" and "continue" are in fact, a subset of GOTO When you see a structure, a block, you know at the beginning (or end of the block) the exit conditions. So you can skip the block and you know the conditions after the block. It is very useful when you are skimming the code or debugging. You don't have the investigate the inner loop to see if there are hidden GOTOs. But, as any other golden rule, you must know when it makes sense to ignore it. I use the break, but only at the beginning of the loop. (or exit in function/procedure) And sometimes with deep nested loops, but, when I commit such crime, I highlight it with neon lights in the comments. I really hate having the read the full code to guess whats happening. Structured programming is your friend. Gladfully, most programmers came to their senses. Mattias ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal -- Saludos Santi ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] converting to UTF8
El 23/03/2022 a las 10:51, Mattias Gaertner via fpc-pascal escribió: FPC does not yet understand comments, What do you mean with "understand comments"? so maybe it does not know this literal is cp1252. Add {$codepage cp1252} somewhere at the start of the unit. If this is part of a Lazarus application, then String is by default UTF-8, so your "original" is already converted to UTF-8. So, as far as Ive understood, without {$codepage cp1252}, when I read the string from a file, it supposes it is utf8, even when it is not a valid utf8 string. And ansiToUtf8 doesn't suppose that the input string ansi, but utf8? I can't understand what is ansiToUtf8 for. If you want to load a string encoded in CP1252, then you can use from unit lconvencoding: s:=CP1252ToUTF8(StringFromFile); OK. that is the solution I've chosen. Thanks Mattias ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal -- Saludos Santi ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Bookmark, TBookmarkStr, TBytes and BytesOf
El 26/08/2021 a las 13:05, Michael Van Canneyt via fpc-pascal escribió: On Thu, 26 Aug 2021, Santi via fpc-pascal wrote: El 26/08/2021 a las 11:58, Michael Van Canneyt via fpc-pascal escribió: On Thu, 26 Aug 2021, Santiago A. via fpc-pascal wrote: Hello: Freepascal 3.2.0. Windows 7-32bits TMyObject=class private fDset:TDataset; function GetBookmark:TBookmarkStr; ... end; function TMyObject.GetBookmark:TBookmarkStr; Begin result:=FDset.Bookmark; end; I get this compiling error: Incompatible types: got "TBytes" expected "AnsiString" Nevertheless, /TbookmarkStr/ documentation example assigns bookmark property to a TBookmarkStr variable https://www.freepascal.org/docs-html/fcl/db/tdataset.bookmark.html The type used for bookmarks changed over time. You really must use TBookmark. I have corrected the documentation, it seems the 2 types were switched when I corrected the documentation when the type actually changed. Sorry about that :/ By the way, BytesOf documentation is also misleading In what sense ? https://www.freepascal.org/docs-html/rtl/sysutils/bytesof.html The abstract is "Return the bytes in a string". Probably you can blame my English, but I think it is ambiguous whether it must be read as Return (the bytes (in a string)) or return (the bytes) -> (in a string) before reading the function declaration, I thought it was the second case. better something like "Return the bytes OF a string in an array of bytes TBytes" Michael. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal -- Saludos Santi ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Bookmark, TBookmarkStr, TBytes and BytesOf
El 26/08/2021 a las 11:58, Michael Van Canneyt via fpc-pascal escribió: On Thu, 26 Aug 2021, Santiago A. via fpc-pascal wrote: Hello: Freepascal 3.2.0. Windows 7-32bits TMyObject=class private fDset:TDataset; function GetBookmark:TBookmarkStr; ... end; function TMyObject.GetBookmark:TBookmarkStr; Begin result:=FDset.Bookmark; end; I get this compiling error: Incompatible types: got "TBytes" expected "AnsiString" Nevertheless, /TbookmarkStr/ documentation example assigns bookmark property to a TBookmarkStr variable https://www.freepascal.org/docs-html/fcl/db/tdataset.bookmark.html The type used for bookmarks changed over time. You really must use TBookmark. I have corrected the documentation, it seems the 2 types were switched when I corrected the documentation when the type actually changed. Sorry about that :/ By the way, BytesOf documentation is also misleading Michael. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal -- Saludos Santi ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Bookmark, TBookmarkStr, TBytes and BytesOf
El 26/08/2021 a las 11:58, Michael Van Canneyt via fpc-pascal escribió: On Thu, 26 Aug 2021, Santiago A. via fpc-pascal wrote: Hello: Freepascal 3.2.0. Windows 7-32bits TMyObject=class private fDset:TDataset; function GetBookmark:TBookmarkStr; ... end; function TMyObject.GetBookmark:TBookmarkStr; Begin result:=FDset.Bookmark; end; I get this compiling error: Incompatible types: got "TBytes" expected "AnsiString" Nevertheless, /TbookmarkStr/ documentation example assigns bookmark property to a TBookmarkStr variable https://www.freepascal.org/docs-html/fcl/db/tdataset.bookmark.html The type used for bookmarks changed over time. You really must use TBookmark. I have corrected the documentation, it seems the 2 types were switched when I corrected the documentation when the type actually changed. Sorry about that :/ So, TbookmarkSr is deprecated and I only have to use TBookmark. Michael. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal -- Saludos Santi ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] function const default argument
El 26/08/2021 a las 10:00, Ondrej Pokorny via fpc-pascal escribió: On 26.08.2021 09:40, Santiago A. via fpc-pascal wrote: const DefaultDelta:extended = 1E-8; function F(const x:extended; const delta:extended=DefaultDelta):extended; function G(const x:extended; const delta:extended=DefaultDelta):extended; But I get "Illegal expression" in the header function declaration. Const parameters don't accept const expressions? You need: const DefaultDelta = 1E-8; Thanks, it works fine. But I can't see why specifying the type doesn't work; function F(const x:extended; const delta:extended=DefaultDelta):extended; Ondrej ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal -- Saludos Santi ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal