[fpc-pascal] with in classes/records

Ryan Joseph ryan at thealchemistguild.com
Tue Sep 4 11:27:02 CEST 2018



> On Sep 4, 2018, at 2:06 PM, Ryan Joseph <ryan at thealchemistguild.com> wrote:
> 
> Sorry I didn’t think enough before I sent this.
> 
> We *must* allow this assignment to make operator overloads work. +=  operators are also basically assigning TWrapper to TWrapper, right? I guess we need to break the default property behavior is instances that the same type is being assigned to itself but correct me if I’m wrong.
> 
> var
> 	wrapper: TWrapper;
> 
> wrapper += 10;

Some questions about operator overloads.

1) rec := 1; should resolve to rec.num := 1 obviously.

2) rec += 10; should call the function TWrapper.+ right? It could operate directly on the field “num” but then the actual function wouldn’t be called.

3) should writeln(rec); resolve to writeln(rec.num); or be a syntax error? If it resolves to rec.num then passing around the record would technically just pass the num field and not the record. That doesn’t sound right to me. Without thinking about it much it feels like “rec” in isolation should be treated as the base type, ie. TWrapper.

4) I guess := operator overloads for records with a default property should be disabled, right? Otherwise they present a conflict that needs to be resolved.


Example code:


type
	TWrapper = record
		num: integer;
		property _default: integer read num write num; default;
		class operator + (left: TWrapper; right: integer): TWrapper;
	end;

class operator TWrapper.+ (left: TWrapper; right: integer): TWrapper;
begin
	left.num += right;
	result := left;
end;

var
	rec: TWrapper;
begin
	rec := 1;	// rec.num := 1
	rec += 10;	// as-is, TWrapper.+ is called
	writeln(rec);	// syntax error, can’t write TWrapper?

Regards,
	Ryan Joseph




More information about the fpc-pascal mailing list