Hacker News new | past | comments | ask | show | jobs | submit login

It's a bit early in its lifecycle to get too excited for features which already exist in other languages. Carbon is exciting because of C++ interoperability, but I already get this behavior today in Ada.

  type Point is record
     x, y, z : Interfaces.Integer_64;
  end record;

  procedure Print(p : Point); 
Is `p` passed by reference or value? The compiler chooses what it thinks is best--all parameters are considered `const` unless they're `out` parameters. There's some rules for classes (tagged types), uncopyable (limited) objects, and `aliased` parameters which are always passed by reference.

I can't get a pointer type (access) out of the parameter to the function, since the accessibility rules prevent it:

  -- "constant" since we don't know if it is writable
  type Point_Access is access constant Point;

  Last_Printed : Point_Access := null;

  procedure Print(P : Point) is
  begin
     -- P'Access is sort of like C++ std::addressof(P) to get a "pointer"
     -- There's also P'Address to get the actual address, but then requires conversion to a pointer-like "access" type to be used.
     --
     -- Compiler Error: "non-local pointer cannot point to local object" since Point_Access type is declared at a higher level
     Last_Printed := P'Access;

     -- If we really, really, want to do it, "I'm smarter than the compiler", you can force it...
     -- Think of "Unrestricted" and "Unchecked" as grep-able warnings signs of "this is potentially very dangerous"
     Last_Access := P'Unrestricted_Access;

     -- ...
  end Last_Printed;
What about making and then trying to use a local pointer-like type? This doesn't work because you can only create pointer-like accesses to types which have been marked as `aliased`, since you don't know if there's a location you can point to which has the value.

  procedure Print (P : Point) is
     type Local_Access is access constant Point;

     -- Compiler Error: prefix of "Access" attribute must be aliased
     Ptr_Like : Local_Access := P'Access;

     -- Similar, "I am smarter than compiler" trick works here too...
     Ptr_Like : Local_Access := P'Unrestricted_Access;
You can allow passing any arbitrary pointer into a function by providing `access`, but you're not allowed to store it, since you don't know which flavor of the pointer type it could be, e.g. if it points to something on the stack, or on the heap:

  type Point_Access is access constant Point;
  Last_Printed : Point_Access := null;

  -- Allow printing any pointer-like (access) to a point.
  procedure Print (P : access constant Point) is
  begin
     -- Compile Error: implicit conversion of anonymous access parameter not allowed
     Last_Printed := P;

     -- If we really, really want to do this, we can force it with a cast...
     Last_Printed := Point_Access (P);
     -- ... 
  end Print;



Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: