简体   繁体   中英

How to use ToString() implicitly in Delphi objects or strings?

Delphi is like an England Queen Guard. It does not like ambiguity and may even kill to protect the hard code. But Java is almost a street corner woman. When I use this is java:

 Button button = new Button();
 String a = "This is a " + button;

I get This is a button

But if I do that in Delphi:

 ShowMessage('This is a ' + Button1);

I get an error, because Delphi has toString() method (now) but it does not implicitly calls it. Because literal strings are not objects in OP. The correct use is:

 ShowMessage('This is a ' + Button1.toString());

Is there any way to override this behave so it works like Java?

For reference: How an object will call toString method implicitly?

There's no way to enforce an implicit cast or method call on an object instance.

If this was a record that you controlled then you could implement an Implicit class operator that would perform a cast to string .

The issue discussed link that you refer to is simply an implementation detail of PrintStream.println() . That relies on the ability of String.valueOf() to come up with a string representation for any object which in turn relies on Object.toString() . In fact the discussion there concerning println() is unrelated to the way the + operator in Java works which is the pertinent issue in your question.

Delphi's TObject has a virtual ToString method that could be used to perform the same purpose. So it would be easy enough to use the exact same technique as PrintStream.println() in Delphi code.

If you had a special function that worked like Format , you could get that behavior. I wrote a function like that several years ago, back when the built-in function didn't support Unicode. I've now updated it to support calling ToString on an object when it's passed as the argument for the %s format string. You'd call it like this:

ShowMessage(WideFormat('This is a %s', [Button1]));

I'm sorry the code hasn't really been touched in several years, so it might not work as-is in newer Delphi versions. You'll have to decide whether it's worth the effort to update that code, although it was included in Project Jedi long enough that it did get a few nominal updates to support UnicodeString and 64-bit compilation.

Too bad that it works only on ARM compiler and Delphi.NET Great article by Nick Hodges in http://edn.embarcadero.com/article/34324

TMyClass = class
    class operator Add(a, b: TMyClass): TMyClass; // Addition of two operands of type TMyClass
    class operator Subtract(a, b: TMyClass): TMyclass; // Subtraction of type TMyClass
    class operator Implicit(a: Integer): TMyClass; // Implicit conversion of an Integer to type TMyClass
    class operator Implicit(a: TMyClass): Integer; // Implicit conversion of TMyClass to Integer
    class operator Explicit(a: Double): TMyClass; // Explicit conversion of a Double to TMyClass
end;

// Example implementation of Add class operator 
TMyClass.Add(a, b: TMyClass): TMyClass;
begin
  ...
end;

var
x, y: TMyClass
begin
  x := 12; // Implicit conversion from an Integer 
  y := x + x; // Calls TMyClass.Add(a, b: TMyClass): TMyClass 
  b := b + 100; // Calls TMyClass.Add(b, TMyClass.Implicit(100)) 
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM