by OskarS · 7y ago · 0 comments
Wait, what? Tuples aren't sum types, they're product types. Like, they're the "cartesian product" of some other types. A sum type would be like a tagged union (or Rust enum) where something can be either one type or the other. I haven't studied a lot of algebraic type theory, but Wikipedia agrees with me on that point [0]
Seems like a fairly elementary mistake in an article of this kind...
EDIT: also, this, a few paragraphs down:
> Moreover, in C++ the pass-by-reference has two flavors: pass the pointer (a common approach) and pass by what's actually called a reference, which is a syntax sugar over pointers that allows accessing the argument with non-pointer syntax (dot instead of arrow)
Pass-by-reference in C++ is most certainly not just syntactic sugar for passing in a pointer. A compiler may implement it that way (or it may not), but it's totally wrong to say that passing by reference is just syntactic sugar so you don't have to use arrow notation. The most obvious differences is that a reference can never be null, it can't be changed to point to some other object, you can't do pointer arithmetic to it and you always dereference it when use it. Also: in idiomatic C++, "passing by reference" is far more common than "passing by pointer".
I'm going to stop reading this article now. I don't have a lot of faith that this person really knows what they're talking about.
Thanks for the correction. As I wrote in the introduction, there will be some errors in these beta-version chapters that are published on the blog (as I, obviously, don't have correctors and reviewers), so I'm thankful to all who notice and point those.
> it's totally wrong to say that passing by reference is just syntactic sugar
OK, I'd say that it's still syntactic sugar with some benefits :) But I don't agree that it's totally wrong. Some of the things you mentioned are just side-effects (that you can't do pointer arithmetic or pass a null pointer. Actually, it seems that you can with some jiggling: https://stackoverflow.com/questions/8571078/pass-by-pointer-...). The point I wanted to make is that pass-by-reference is mostly the same thing. It's, actually, quite a confusing topic (as are many C++ solutions) that was not properly presented to me when I stduied it in school, and this book neither tries to present it, but I had to, at least, mention it here as I was talking about different passing styles.
type
Point = record
x, y : integer
end;
procedure ByPointer(p : ^Point);
begin
p^.x := 12
end;
procedure ByReference(var p : Point);
begin
p.x := 12;
p.y := 33
end;
procedure example
var
p : Point
begin
p.x := 23;
p.y := 28;
WriteLn('The point is ', p.x, ' ', p.y);
ByPointer(@p)
WriteLn('The point is ', p.x, ' ', p.y);
p.x := 23;
p.y := 28;
WriteLn('The point is ', p.x, ' ', p.y);
ByReference(p)
WriteLn('The point is ', p.x, ' ', p.y);
end;
Actually the concept is already exposed in Algol, a couple of years before C was even an idea.