Live data from Hacker News

Carbon’s most exciting feature is its calling convention

foonathan.net

21–30 of 223 posts

Re: Carbon’s most exciting feature is its calling convention

#21

This is mostly a Google project? Unclear from the repo who is sponsoring it.

Microsoft. - don’t listen to me, it’s Google.

It's Google but yes, another instance of Embrace, Extend and Extinguish.

I hope this doesn't take off, with my sincere apologies to the ones who have been working hard on this. The last thing the C/C++ ecosystem needs is becoming de facto owned by a private company.

Re: Carbon’s most exciting feature is its calling convention

#22
Calling convention? That's the most exciting feature? It doesn't boost productivity, reduce compile times, offer a larger standard library or create a boost a reasonable language would provide?

No thanks google. I've been saying that for years now. You jumped the shark

Re: Carbon’s most exciting feature is its calling convention

#23
post #9

That's been done before. The original Modula 1 compiler had that. In a language where the default parameter mode is a read-only reference, it's an obvious optimization. The reverse is true. Anything passed by value can be treated as const reference by the compiler if the compiler knows enough about access and lifetime. The compiler must be able to determine that the parameter is neither deallocated nor modified while…

D and I think Fortran can also do this

Re: Carbon’s most exciting feature is its calling convention

#25
C++ also pays a price for insisting not only that objects have addresses, but those addresses are distinct.

If you've got a 1.6 billion empty tuples in variable A, 1.4 billion in variable B and 1.8 billion in variable C, C++ can't see a way to do that on a 32-bit operating system. It needs to give each empty tuple an address, so it must think of 4.8 billion integers between 0 and 2^32 and it can't do that, so your program won't work.

Carbon is still far from finished, but if objects needn't have addresses it can do the same as Rust here, and cheerfully handle A, B and C as merely counters, counting 1.6 billion, 1.4 billion and 1.8 billion respectively is fine. Empty tuples are indistinguishable, so I needn't worry about giving you back the "wrong" empty tuple when you remove one, I can just give you a fresh one each time and decrement the counter.

Re: Carbon’s most exciting feature is its calling convention

#26

Earlier quoted context omitted.

Microsoft. - don’t listen to me, it’s Google.

It's Google but yes, another instance of Embrace, Extend and Extinguish . I hope this doesn't take off, with my sincere apologies to the ones who have been working hard on this. The last thing the C/C++ ecosystem needs is becoming de facto owned by a private company.

Sorry, not in the ecosystem (clearly).

Why not? Just curious.

Re: Carbon’s most exciting feature is its calling convention

#27
post #7

Earlier quoted context omitted.

I hope they picked that name for the same reason Apple picked it: because all life is built on carbon.

Or because Carbon as an element is "C" and "C" is also the language the framework was built for.

Well, Apple was a little clever with Carbon for this reason, because it was a C based API. I'm not sure how you get Carbon from C++.

Re: Carbon’s most exciting feature is its calling convention

#28
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;

Re: Carbon’s most exciting feature is its calling convention

#29

C++ also pays a price for insisting not only that objects have addresses, but those addresses are distinct. If you've got a 1.6 billion empty tuples in variable A, 1.4 billion in variable B and 1.8 billion in variable C, C++ can't see a way to do that on a 32-bit operating system. It needs to give each empty tuple an address, so it must think of 4.8 billion integers between 0 and 2^32 and it can't do that, so your pr…

Maybe the right question to ask is, why you have 4.8 billion empty tuples? And why you're still on a 32-bit system?

Re: Carbon’s most exciting feature is its calling convention

#30

Maybe I'm missing something, but I don't see what's special about Carbon here. In C++ the compiler can also optimize pass-by-const-reference to pass-by-value, and they do. It just can't do it across an ABI boundary, but that should only be an issue with dynamic libraries, and Carbon has to follow the standard ABI there as well. Just make sure the compiler knows it doesn't have to follow the standard ABI for every sym…

I think the alias analysis “pro” is overblown, it’s UB to take the address of a parameter via any mechanism other than explicitly taking the address - which a compiler obviously sees, and because it’s UB the compiler optimizes is free to assume no one is taking the address. Then for any parameters that are passed by reference the compiler has to assume there are other references so there’s no gain.

Honestly the only thing that really stood out as nice is the default pass by word-sized value, in the context of templates - it’s a thing that is achievable in C++, but requires a bunch of obnoxious additional templates that aren’t even part of the standard library so everyone ends up reimplementing the same cruft. Happily I believe there’s a proposal to add this exact functionality.

I also loathe their desire to listen to the BNF maximalists insistence on not having any “ambiguity” from s. I’m sorry it’s clearly parseable, and s are the standard token for decades. Switching to []s doesn’t make it less conceptually ambiguous, if anything it makes it more ambiguous to a human reader. The only people who don’t want s are PLT academics obsessed with forcing their dragon book idea of what a grammar should be. You can’t argue you’re doing it because the ambiguity in a grammar or lexer is bad, because then you would also drop infix operators.

Then in carbon the more reasonable adoption of pascal’s : notation for typing a variable or parameter removes the most common case of the supposedly terrible ambiguity anyway.

Post reply on HN