So, how weird am I for wanting to write
So, how weird am I for wanting to write
function f(x: ^integer): integer;
?
Any reason I'm missing for why that isn't accepted?
function f(x: ^integer): integer;
?
Any reason I'm missing for why that isn't accepted?
I think the ^ notation only works in types.
ReplyDeleteA
Andrea Raimondi Yes. But I don't see any reason why, hence my rant + question.
ReplyDeleteYes, as per Andrea. Instead, try something like:
ReplyDeletetype
PInteger = ^Integer;
function f(x: PInteger) : Integer;
Yes I know very well about this. My rant is about why we must define the separate pointer type in advance.
ReplyDeleteFor example in C/C++ there is no such need and I can write
int f(int* x) { ... }
Why does this matter? Well when you're working with a generics you can't always define the "pointer-to-T" type.
Why not use function (var x: Integer):Integer; ?
ReplyDeleteLars Fosdal Well that doesn't allow you to (easily) pass nil.
ReplyDeletefunction (var x: Variant): Integer;
ReplyDelete;)
Lars Fosdal Heh. As mentioned this restriction is most annoying when using generics. I'd like to write function f(x: ^T): ...
ReplyDeleteI know you can use local type declarations, but only in some cases (for example, not in an interface). Besides it's tedious and adds only noise.
And yes I know I could wrap my interface declaration in a record and do it that way but sheesh... talk about tedious!
ReplyDeleteTMyClass
ReplyDeletefunction f(var x: T; const IsNil: Boolean = False): T; overload;
function f(const x): T; overload;
end;
function TMyClassf(const x):T;
begin
Result := f(Default, True);
end;
On second thought, that is probably ambiguous...
ReplyDeleteStill, is it too much to ask for ^T to be valid whenever a type is expected?
ReplyDeleteyou can't not use a type definition in a parameter type. like you can't write "for integer i := ..." while you can do it in C/C++.
ReplyDeletebut there's some exceptions...like exceptions :) on e:Exception, and for open arrays: f(const a: array of string)
...or should all types be nullable?
ReplyDeletePaul TOTH So no good reason is your answer then?
ReplyDeleteLars Fosdal I'd rather the basic syntax just work like one would naively expect...
ReplyDeleteWell, ^Type never worked for parameters, so that is as I'd expect ;)
ReplyDeleteI see. Must be my C++ days that have left me wanting more ;)
ReplyDeletelooking for troubles? AFAIK, PASCAL is a strongly typed language, so you need to declare before use.
ReplyDeleteHeinz Toskano This has nothing to do with strong typing. You can declare P1 = ^integer; P2 = ^integer; and assign a variable of type P1 to P2 without issue.
ReplyDeleteI mean you need to declare a type before use it. So an Integer pointer must be declare before using it in a function header.
ReplyDeleteyes, no good reason, it's by design and it have never been changed. Anyway PInteger became a system type several release ago
ReplyDeleteHeinz Toskano var p: ^Integer; Why i dont need type declaration here?
ReplyDeleteI think,its incoherence of pascal language
+Евгений СавиH, no var p: Integer without ^ !
ReplyDeletePaul TOTH He meant variable declarations.
ReplyDeleteЕвгений Савин seriously? you better check your compiler... delphi shows me a nice red line with an error.
ReplyDeleteAs Asbjørn Heid said, I mean variable declaration
ReplyDeleteprocedure TForm15.FormCreate(Sender: TObject);
var
p: ^Integer; // dont need declare type
begin
end;
----
BTW, there is simple way to declare generic pointer:
type
TPointer = record
public type
AType = ^T;
end;
type
TMyClass = class
public
procedure DoWork(P: TPointer.AType);
end;
But TPointer.AType is not compatible with Type.PInteger..
Евгений Савин Well I think it's a bit much to type instead of simply ^T, but yes :)
ReplyDeleteЕвгений Савин Could/would operator overload help out with the TPointer.AType != Type.PInteger issue?
ReplyDeleteThere is no way to overload operator for Type.PInteger and TPointer.AType
ReplyDelete