Recent

Author Topic: String Type Helper Join  (Read 3443 times)

avk

  • Hero Member
  • *****
  • Posts: 752
Re: String Type Helper Join
« Reply #15 on: May 03, 2022, 02:54:39 pm »
To be honest, I would be more worried about the details of its implementation(meaning AnsiString.Join()).
It turns out that the function can raise an exception, which the documentation is modestly silent about.
But is an exception really necessary? Especially considering that, moreover, not all unwanted cases are covered? For example, System.Copy() just returns an empty string in such cases.
And it's sad to see such ugliness in a function from RTL as allocating memory inside a loop.

WayneSherman

  • Full Member
  • ***
  • Posts: 243
Re: String Type Helper Join
« Reply #16 on: May 03, 2022, 04:30:23 pm »
For clarity and to avoid confusion, a convention is now needed such that the Join helper should never be called on a string instance, but always on the type directly:
Code: Pascal  [Select][+][-]
  1. MyString := String.Join(...);  //Always use Join on the type directly like this
  2. MyString := SomeString.Join(...);   //never write code like this
  3. MyString := 'a string literal'.Join(...);  //never write code like this

For type helpers, if it is bad practice to call a class function on an instance of the type, would it be better to:
  - Enforce good form by generating a compile error (or at least a warning)?
  - For Lazarus code completion, do not show class functions on a instance of the type?
« Last Edit: May 03, 2022, 04:57:31 pm by WayneSherman »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: String Type Helper Join
« Reply #17 on: May 04, 2022, 09:29:27 am »
For clarity and to avoid confusion, a convention is now needed such that the Join helper should never be called on a string instance, but always on the type directly:
Code: Pascal  [Select][+][-]
  1. MyString := String.Join(...);  //Always use Join on the type directly like this
  2. MyString := SomeString.Join(...);   //never write code like this
  3. MyString := 'a string literal'.Join(...);  //never write code like this

For type helpers, if it is bad practice to call a class function on an instance of the type, would it be better to:
  - Enforce good form by generating a compile error (or at least a warning)?
  - For Lazarus code completion, do not show class functions on a instance of the type?

Again: it is valid code. It makes no sense enforce a compile error or even a warning for valid code. Also it makes no sense for Lazarus to hide the methods because it is valid code. What Lazarus should do instead (and which would help in other situations as well) is to highlight that these are class methods, because Lazarus currently doesn't do that.

avk

  • Hero Member
  • *****
  • Posts: 752
Re: String Type Helper Join
« Reply #18 on: May 05, 2022, 01:34:32 pm »
I've added some version of Join() to my TAnsiStrHelper(Join2), which does not raise exceptions and seems to be about three times faster than the RTL version.
Does it make sense to offer it as a replacement for the current RTL version, or let it stay in TAnsiStrHelper?
 

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: String Type Helper Join
« Reply #19 on: May 05, 2022, 05:40:54 pm »
I personally consider this far more readable, simple and easy to spot mistakes -

Code: Pascal  [Select][+][-]
  1. AString := PathDelim + 'system32' + PathDelim + 'drivers' + PathDelim + 'etc' + PathDelim + 'hosts';

It could be improved by using macro to define a shorter name for PathDelim but you get used to it as it is.

Davo
I've created a TPath class that allows the following:
Code: Pascal  [Select][+][-]
  1. AString := TPath('C:') / 'system32' / 'drivers' / 'etc' / 'hosts';
But currently the normalize function is still quite buggy and I hadn't had time to fix it (basically resolving '..' at the start of the path doesn't work so 'foo/../../bar' should be normalized to '../bar' but this doesn't work right now).

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: String Type Helper Join
« Reply #20 on: May 06, 2022, 03:50:02 pm »
I've added some version of Join() to my TAnsiStrHelper(Join2), which does not raise exceptions and seems to be about three times faster than the RTL version.
Does it make sense to offer it as a replacement for the current RTL version, or let it stay in TAnsiStrHelper?

If Delphi raises exceptions as well, then no, it won't be a suitable replacement. Otherwise: simply report it. If suitable someone (probably MvC) will pick it up or reject it.

avk

  • Hero Member
  • *****
  • Posts: 752
Re: String Type Helper Join
« Reply #21 on: May 06, 2022, 06:11:10 pm »
Thank you. Fortunately, a very similar proposal has already been presented there.

 

TinyPortal © 2005-2018