On 30/08/12 13:49, leledumbo wrote:
straight, easy to write code to load a file contents into a string
variable easily. complicated techniques required)

What's so difficult about:

var
  sl: TMyNewStringList;
  fn: IString;
begin
  fn := s('c:\myfile.txt');
  sl := TMyNewStringList.Create;
  sl.LoadFromFile(fn, encUTF8);  // for a UTF-8 encoded file
// ...or...
  sl.LoadFromFile(fn, encUTF16); // for a UTF-16 encoded file


Internally TMyNewStringList will use the TString type. The second parameter in LoadFromFile() is the encoding the file is in. TMyNewStringList will load the data and do the encoding conversion (if required) to store the data in the encoding used by TString.

Just because Java requires 3 classes to work together to read a text file [1], doesn't mean Object Pascal must too.


Anyway Graeme, if you like the expression style, overloaded operators
would be more intuitive. Or a(nother) proper naming: .Append and
.Replace should NOT create new instance,

The same confusion could arise as before, and that is what immutable objects try to avoid. For example:

var
  lUserJohn: IString;
  lFirstName: IString;
begin
  lUserJohn := s('john');
  lUserJohn.Replace('sussie');
  { now lUserJohn doesn't store the name one would expect }

// ...or...

  lFirstName := s('John');
  lFirstName.Append(' Doe');
  { now lFirstName doesn't store what the variable name suggests }

...but this is much more clear...

  lFullName := lFirstName.Append(' Doe');

lFirstName still contains what it says, and so does lFullName.


Then also throw multi-threading into the mix. With immutable strings or objects you don't need to worry about Mutexes or Critical Sections etc... This simplifies multi-threaded programming a lot.



[1] http://www.mkyong.com/java/how-to-read-utf-8-encoded-data-from-a-file-java/


Regards,
  - Graeme -



--
_______________________________________________
Lazarus mailing list
[email protected]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply via email to