Well, some answers (INMHO) in order:
@Martin_fr: Yes, you're right with the nil. But it's a compiler option, it could be nil as default and no side effect. A class var not created could start by default with nil, so no boilerplate coding with that. Same as being zeroed when calling the constructor (why there and not in declaration?). Or using other definition, if the pointer is invalid(if have a internal valid/assigned field than can be checked), then use the constructor.
The idea behind was not to use (in my way of thinking...) a counterintuitive sintax like
MyClass:=TMyClass.Create (<args>);
And use more simple MyClass.Create and leave the use of MyClass.Create as a method only if MyClass is not a valid reference.
It enforces that a class is constructed by their own type as default and forces the sentences like MyClass:=TDifferentClass.Create as being explicitally typed and not using the "sugar" declaration.
Other option is that If one declares a Class var like
MyClass:TMyClass;
The declaration of TMyClass Automatically does the getmen (init instance) unless explicitally declaring not doing it. So the .create will be only a init method. Why not this aproach?. This avoids the duality of a constructor method called as a newinstance and init if called as TMyclass.create and as a method if called as myclass.create method.
Option 3 sees what you say, so includes in var declaration a specific getmen.
And even, i'm sure some would appreciate some like a HEAP object. (what is what i try to mimic in class) and not having all the new and deference/reference boiler plating (^) and no TObject reference
type
TTest = HEAP object //not stack one, managed object like Ansistring
private
{ private declarations }
public
{ public declarations }
end;
Var
TestObject:TTest; {in heap, zeroed and with valid reference/pointed}
On the other hand.. The FreeAndNil...
Well, it's a tough question
. I think the option to nil the pointer should be up to the programmer and FreeAndNil masks it, but i think too that all pointers (at declaration time) should start as nil. And the validity/invalidity/assigned of the pointer being a different field and starting as unassigned.
Aplying this to classes, i think all class declaration (like pointers) should be starting as nil, but pascal does let it to the programmer. I do not know what is gained when i type
And myPointer is not nil by declaration. I must declare this.
Var
myPointer:pointer=nil;
But with managed types, nil is the default. (like AnsiString);
If i asked what i should choose for pointer types (i take class as pointer types)
and choose between these options;
Option 1: nil is not default
Var
NilPointer:pointer=nil;
NonilPointer:pointer;
Option 2: nil is default
Var
NilPointer:pointer;
NoNilPointer:pointer=TakeStupidCurrentMemInvalidPossiblyNonNilValue;
I choose 2. I never used and invalid (and possibly) non nil pointer for anything...
So for coherence, i will do all the things the same. All starting pointers to nil...? Ok, all to nil.
Not to nil? well, none to nil (so ansistring starting with anyvalue to point);
We should difference between nil/nor nil valid/not valid?, well, a nil pointer and a valid field in the pointer internals. But i think we have different strategies into the basis of the compiler with nil pointers/classes.
I would prefer same behaviour (all nil or non nil, field of valid or not field..)
And my personal option is that a pointer defaults to a nil and unasigned by declaration, and then the programmer's choice to assign subsecuents nils and/or validity..