PDA

View Full Version : create c# like classes in delphi



noeska
21-12-2006, 06:10 PM
In C# you have special classes like String that work like this



String MyString = " This is an test string ";
Console.WriteLine(MyString.Trim());


It class like the String class is created like this:
http://www.codeproject.com/useritems/Csharp_implicit_operator.asp

Is it possible to use something like this in Delphi ((win32) 2005/turbo)? I now something like this is possible for arrays. E.g. MyObject[i] instead of MyObject.Lines[i].

Thanks for your answers in advance.

JSoftware
21-12-2006, 06:58 PM
code for Turbo Delphi win32

type
MyString = record
data: string;
function Trim: MyString;
class operator implicit(a: string): MyString;
class operator implicit(a: MyString): String;
end;

{ MyString }

class operator MyString.implicit(a: string): MyString;
begin
result.data := a;
end;

class operator MyString.implicit(a: MyString): String;
begin
result := a.data;
end;

function MyString.Trim: MyString;
begin
//trim stuff here
result := self;
end;

begin
a := ' This is an test string ';
writeln(string(a.Trim()));
end;

noeska
21-12-2006, 08:26 PM
Where can i find a tutorial on this class operator constructions? I get lost in the delphi help.

PS: i seem to have posted this in the wrong forum. It should have been posted in the delphi forum. Sorry for that.

WILL
21-12-2006, 08:29 PM
Easily moved. ;)

JSoftware
21-12-2006, 09:06 PM
I haven't found any valuable online resources. I learnt everything through the help file. Atm it doesn't work but searching for operator overloading with filter set to language: delphi you should find what you need.

if you need a list of operators available to overload then heres a shortened list which is contained in my Tvector4f record:

class operator add(a,b: tvector4f): tvector4f;
class operator add(a: tvector4f; b: single): tvector4f;
class operator add(a: single; b: tvector4f): tvector4f;
class operator Subtract(a,b: tvector4f): tvector4f;
class operator Subtract(a: tvector4f; b: single): tvector4f;
class operator Subtract(a: single; b: tvector4f): tvector4f;
class operator Multiply(a,b: tvector4f): tvector4f;
class operator Multiply(a: tvector4f; b: single): tvector4f;
class operator Multiply(a: single; b: tvector4f): tvector4f;
class operator Divide(a,b: tvector4f): tvector4f;
class operator Divide(a: tvector4f; b: single): tvector4f;
class operator Divide(a: single; b: tvector4f): tvector4f;
class operator Equal(a,b: tvector4f): boolean;
class operator NotEqual(a,b: tvector4f): boolean;
class operator Implicit(a: tvector4f): string;

If you use operator overloading for classes then you'll have to create the result yourself

cronodragon
30-12-2006, 09:17 PM
That new feature of operator overloading in Delphi is very interesting.

I would really like to have arithmetic assigners, like +=, *= in Java. But how would them be in Pascal? Something like +:= or :*=... hehe, looks like a rabbit :lol:

JSoftware
30-12-2006, 09:38 PM
fpc supports prefix operators(?) like c like +=, -=, *=,...

noeska
31-12-2006, 07:49 AM
can you also do operator overloading in fpc?

savage
31-12-2006, 08:42 AM
can you also do operator overloading in fpc?

Yes it's been part of FPC way before Delphi got it.

JSoftware
31-12-2006, 10:52 AM
operator overloading in fpc is done outside type definitions like this

interface

type
tvector = ...
end;

operator /(a: tvector4f; b: single)result: tvector4f;
operator *(a: tvector4f; b: single)salmon: tvector4f;

implementation

operator /(a: tvector4f; b: single)result: tvector4f;
begin
result := vector4f(a.x/b,...);
end;

operator *(a: tvector4f; b: single)salmon: tvector4f;
begin
salmon := vector4f(a.x*b,...);
end;

cronodragon
31-12-2006, 12:23 PM
That syntax,


operator /(a: tvector4f; b: single)result: tvector4f;

with the name of result variable looks very nice.

JSoftware
31-12-2006, 12:28 PM
I think the syntax for fpc's way of overloading looks weird but I'm able to make it easily maintainable this way:


{$IFDEF FPC}
operator -(a: single; b: tvector4f) result: tvector4f;
{$ELSE}
class operator tvector4f.Subtract(a: single; b: tvector4f): tvector4f;
{$ENDIF}
begin
result := sub(vector4f(a,a,a,a), b);
end;

dmantione
31-12-2006, 01:11 PM
When we implemented operator overloading years ago, we decided not to diverge the language more than necessary and took the syntax from GNU-Pascal. Unofrtunately, Borland decided that copying C# is more important than Pascal compiler compatibility :evil: