Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I did something very similar in our migration from Delphi 2007 and also discovered that you can't use Format() to pass records to, and after reading the comments it makes perfect sense. An explicit cast (ideally to string) will tell the compiler what to do; the "explicit" methods are not required, however. As for suggestions to use "AsAnsiString": I personally don't like this idea because 1) extra function to write whereas an explicit cast can do the job 2) if readability is important, then so should be consistency, i.e. you do TShortStringRec.AsAnsiString, but do you also need to add an explicit method to set data, like SetAsAnsiString (or just do AsAnsiString as a property)? To me this defeats the point of implicit class operators. I recommend to stick to explicit casts, let the compiler determine which call is correct.</p> <p>We use a lot of string[] types, so I auto-generated all my records. I thought it would be better to specify a default property to get AnsiChars out of ShortString types rather than have them get converted to UnicodeString then getting the char through [ ], for example:</p> <pre><code>type _ShortString3 = string[3]: ShortString3 = record private FData: _ShortString3; function GetAnsiChar(Index: Integer): AnsiChar; procedure PutAnsiChar(Index: Integer; const Value: AnsiChar); public class operator Implicit(const A: string): ShortString3; class operator Implicit(const A: ShortString3): string; class operator Equal(const A: ShortString3; B: AnsiChar): Boolean; class operator NotEqual(const A: ShortString3; B: AnsiChar): Boolean; class operator Equal(const A: ShortString3; B: ShortString3): Boolean; class operator NotEqual(const A: ShortString3; B: ShortString3): Boolean; class operator Add(const A: ShortString3; B: ShortString3): string; class operator Add(const A: ShortString3; B: AnsiChar): string; class operator Add(const A: ShortString3; B: string): string; property AnsiChars[Index: Integer]: AnsiChar read GetAnsiChar write PutAnsiChar; default; end; </code></pre> <p>FWIW here's the implementation:</p> <p>{ ShortString3 }</p> <pre><code>function ShortString3.GetAnsiChar(Index: Integer): AnsiChar; begin Result := FData[Index]; end; procedure ShortString3.PutAnsiChar(Index: Integer; const Value: AnsiChar); begin FData[Index] := Value; end; class operator ShortString3.Implicit(const A: string): ShortString3; begin Result.FData := _ShortString3(A); end; class operator ShortString3.Implicit(const A: ShortString3): string; begin Result := string(A.FData); end; class operator ShortString3.Equal(const A: ShortString3; B: AnsiChar): Boolean; begin Result := A.FData = B; end; class operator ShortString3.NotEqual(const A: ShortString3; B: AnsiChar): Boolean; begin Result := A.FData &lt;&gt; B; end; class operator ShortString3.Equal(const A: ShortString3; B: ShortString3): Boolean; begin Result := A.FData = B.FData; end; class operator ShortString3.NotEqual(const A: ShortString3; B: ShortString3): Boolean; begin Result := A.FData &lt;&gt; B.FData; end; class operator ShortString3.Add(const A: ShortString3; B: ShortString3): string; begin Result := string(A.FData + B.FData); end; class operator ShortString3.Add(const A: ShortString3; B: AnsiChar): string; begin Result := string(A.FData + B); end; class operator ShortString3.Add(const A: ShortString3; B: string): string; begin Result := string(A.FData) + B; end; </code></pre> <p>This has been turned out to be overall a good trick because we didn't manually fiddle with hundreds of files, instead, just wrote 1 file with all our custom ShortString records with implicit class operators. (There was an intermediate step that automatically changed all ShortString types to our own and added the unit StringTypes to the uses, but it was safe.) Thousands of ShortString related warnings disappeared.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload