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?

Comments

  1. I think the ^ notation only works in types.

    A

    ReplyDelete
  2. Andrea Raimondi Yes. But I don't see any reason why, hence my rant +  question.

    ReplyDelete
  3. Yes, as per Andrea.  Instead, try something like:
    type
      PInteger = ^Integer;
    function f(x: PInteger) : Integer;

    ReplyDelete
  4. Yes I know very well about this. My rant is about why we must define the separate pointer type in advance.

    For 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.

    ReplyDelete
  5. Why not use function (var x: Integer):Integer; ?

    ReplyDelete
  6. Lars Fosdal Well that doesn't allow you to (easily) pass nil.

    ReplyDelete
  7. function (var x: Variant): Integer;
    ;)

    ReplyDelete
  8. Lars Fosdal Heh. As mentioned this restriction is most annoying when using generics. I'd like to write function f(x: ^T): ...

    I 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.

    ReplyDelete
  9. And yes I know I could wrap my interface declaration in a record and do it that way but sheesh... talk about tedious!

    ReplyDelete
  10. TMyClass
      function 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;

    ReplyDelete
  11. On second thought, that is probably ambiguous...

    ReplyDelete
  12. Still, is it too much to ask for ^T to be valid whenever a type is expected?

    ReplyDelete
  13. you 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++.
    but there's some exceptions...like exceptions :) on e:Exception, and for open arrays: f(const a: array of string)

    ReplyDelete
  14. ...or should all types be nullable?

    ReplyDelete
  15. Paul TOTH So no good reason is your answer then?

    ReplyDelete
  16. Lars Fosdal I'd rather the basic syntax just work like one would naively expect...

    ReplyDelete
  17. Well, ^Type never worked for parameters, so that is as I'd expect ;)

    ReplyDelete
  18. I see. Must be my C++ days that have left me wanting more ;)

    ReplyDelete
  19. looking for troubles? AFAIK, PASCAL is a strongly typed language, so you need to declare before use.

    ReplyDelete
  20. Heinz 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.

    ReplyDelete
  21. I mean you need to declare a type before use it. So an Integer pointer must be declare before using it in a function header.

    ReplyDelete
  22. yes, no good reason, it's by design and it have never been changed. Anyway PInteger became a system type several release ago

    ReplyDelete
  23. Heinz Toskano var p: ^Integer; Why i dont need type declaration here?
    I think,its incoherence of pascal language

    ReplyDelete
  24. +Евгений СавиH, no var p: Integer without ^ !

    ReplyDelete
  25. Paul TOTH He meant variable declarations.

    ReplyDelete
  26. Евгений Савин seriously? you better check your compiler... delphi shows me a nice red line with an error.

    ReplyDelete
  27. As Asbjørn Heid said, I mean variable declaration
    procedure 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..

    ReplyDelete
  28. Евгений Савин Well I think it's a bit much to type instead of simply ^T, but yes :)

    ReplyDelete
  29. Евгений Савин Could/would operator overload help out with the TPointer.AType != Type.PInteger issue?

    ReplyDelete
  30. There is no way to overload operator for Type.PInteger and  TPointer.AType

    ReplyDelete

Post a Comment