Discussion:
Feature request: default properties for non-indexed properties.
(too old to reply)
Skybuck Flying
2011-07-11 04:47:33 UTC
Permalink
Hello,

Currently I have a class somewhat like so:

TSomeObject<GenericType> = class
protected
mStorage : GenericType
public
// not possible :)
// property Value : GenericType read mStorage; default;

property Value : GenericType read mStorage;

// other stuff
end;

I would like to be able to write code as follows:
var
A : TSomeObject<integer>;
B : TSomeObject<integer>;
C : integer;
begin
C := A; // not possible.

// must write:
C := A.Value;

end;

Having to write .Value everytime is kinda annoying and looks bad.

I wish it was possible to use a non-indexed property as the default.

Then I hope I could also use it for:

SetLength( something, A );

Bye,
Skybuck.
Skybuck Flying
2011-07-11 04:52:01 UTC
Permalink
Perhaps it's possible to add a implicit typecast operator for generic types
to the class, so I will try this first.

I think I have done this in the past already so it might work...

I am gonna try this now ;)

Bye,
Skybuck.
Skybuck Flying
2011-07-11 04:53:47 UTC
Permalink
Ok,

That was a short try, as far as I know "class" does not have operator
overloading so this is impossible with current delphi version.

Also since records do not have destructors I cannot use records as well.

A situation which many Delphi programmers have probably faced already. I
hope this gets solved soon, because that would be nice.

Especially destructors for records would really come in handy right about
now.

Bye,
Skybuck.
Skybuck Flying
2011-07-11 04:57:00 UTC
Permalink
I read a little rumor that "class" operator overloading is now possible for
classes too, so perhaps it will work... I am skeptical but going to give it
a try anyway to see if it works out ;)

Bye,
Skybuck.
Skybuck Flying
2011-07-11 05:03:46 UTC
Permalink
Nope,

Class operator overloading not possible, my first try did look believable ;)

// *** Begin of Test Program ***

program TestProgram;

{$APPTYPE CONSOLE}

{

Test class operator overloading

version 0.01 created on 11 july 2011 by Skybuck Flying

Nope this won't work in Delphi XE.

}

uses
SysUtils;

type
TSomeObject = class
private

public
// looks believable lol.
// class function Implicit( ParaSomeObject : TSomeObject ) : integer;

class operator Implicit( ParaSomeObject : TSomeObject ) : integer;
end;

{
class function TSomeObject.Implicit( ParaSomeObject : TSomeObject ) :
integer;
begin
result := 666;
end;
}

class operator TSomeObject.Implicit( ParaSomeObject : TSomeObject ) :
integer;
begin
result := 666;
end;


procedure Main;
var
vSomeObject : TSomeObject;
vA : integer;
begin
writeln('program started');

vSomeObject := TSomeObject.Create;

vA := vSomeObject;

writeln( vA );


vSomeObject.Free;

writeln('program finished');
end;

begin
try
Main;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
ReadLn;
end.

// *** End of Test Program ***

Bye,
Skybuck.

Loading...