Reputation: 9104
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?
Upvotes: 4
Views: 8974
Reputation: 9104
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;
Upvotes: 2
Reputation: 163287
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.
Upvotes: 4
Reputation: 612993
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.
Upvotes: 6