4 ms·
good point, so, here, it is exactly syntax sugar as, unlike in C++, nothing else except syntax is different from the pointer-based version. Not to mention that,
by vseloved 7y ago
good point, so, here, it is exactly syntax sugar as, unlike in C++, nothing else except syntax is different from the pointer-based version. Not to mention that, in the article, I was mentioning the general concept that has one (and single) variant, in C and Java, and 2 in Pascal or C++
- pjmlp 7y agoActually it is more than syntax sugar. I should have provided a better example, this is impossible to do languages like Java and in C requires explicit double indirection to fake how reference types work. type Point = record x, y : integer end; procedure ByPointer(p : ^Point) begin New(p); p.x := 12 end; procedure ByReference(var p : ^Point) begin if p <> nil then Dispose(p) end; New(p); p^.x := 12; p^.y := 33 end; procedure example var p : ^Point begin New(p); p^.x := 23; p^.y := 28; WriteLn('The point is ', p^.x, ' ', p^.y, ' at ', p); ByPointer(p) WriteLn('The point is ', p^.x, ' ', p^.y, ' at ', p); p^.x := 23; p^.y := 28; WriteLn('The point is ', p^.x, ' ', p^.y, ' at ', p); ByReference(p); WriteLn('The point is ', p^.x, ' ', p^.y, ' at ', p); (* here p will point to the one allocated in ByReference *) Dispose(p); end;
- jstimpfle 7y ago> Actually it is more than syntax sugar. Where? I see only syntax sugar. The catchy phrase from the Google C++ style guide is stuck in my head: references have value syntax but pointer semantics. And that's it (and that's why I think references are mostly a bad idea). > C requires explicit double indirection Your 2nd example is no different, it's still just "indirection" (value syntax but pointer semantics!). If what you are indirecting is itself a pointer type (as in your 2nd example), then yes, there technically is a "double indirection", but that's besides the point. > impossible to do in languages like Java actually, "in every memory safe language there is". You can do this in Pascal because Pascal is really just C-style systems programming. With a few features like references and ARC that pretend otherwise but quickly fall flat on their head.
- pjmlp 7y agoOk I might have to give in with the syntax sugar part, but safety? Here is my example in C#. So now C# is unsafe? class Point { public int x, y; } static class CSharpIsUnsafe { static void ByPointer(Point p) { p = new Point(); p.x = 12; } static void ByReference(ref Point p) { p = new Point(); p.x = 12; p.y = 33; } public static void Main(string[] args) { p = new Point(); p.x = 23; p.y = 28; Console.WriteLn($"The point is {p.x} {p.y}"); ByPointer(p) Console.WriteLn($"The point is {p.x} {p.y}"); p.x = 23; p.y = 28; Console.WriteLn($"The point is {p.x} {p.y}"); ByReference(ref p); Console.WriteLn($"The point is {p.x} {p.y}"); } }
- jstimpfle 7y agoI guess it might very well be safe. I don't know C# very well, but I'd assume that ref is sort of a proxy that 1) under the hood stores an (object) reference to the lowest structural ancestor that is GC'ed, as well as an offset from that object to the ref'ed property. 2) is itself GC'ed Escape analysis might in some cases optimize out the GC'ing of the ref, but that of course doesn't work if the ref does escape. Something like that, but in any case it's something very different than the Pascal/C++ examples we've talked about.
- pjmlp 7y agoThe generated code is similar as how a C++ compiler would do it. Easy to check with VS Assembly visualizer or WinDbg alongside SOS.dll. D, Ada, CLU, Mesa, Mesa/Cedar, Algol, PL/I, PL/S, PL.8, Delphi, Dylan, .NET, Swift, Oberon, Oberon-2, Component Pascal, Active Oberon, QuickBasic, Turbo Basic are just a couple of examples of languages allowing for pass by value and pass by reference, some of them also quite safe. And then there is also call by name introduced alongside Algol.