Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

rust just got 4x more productive [shill thread]

Name: Anonymous 2016-12-16 15:09

Rust designers developed an idiom for concisely propagating errors outwards with a macro called try!:
let file = try!(File::open("file.txt"));
More recently, Rust shipped an even more concise notation for error propagation, the postfix ? operator:
let file = File::open("file.txt")?;

Name: Anonymous 2016-12-31 11:25

>>16
Apart from null, it might very well be the biggest design mistake of the language.
And when I point out that null pointers are the billion dollar mistake that ruined C#, someone on here had the stupidity to disagree with me!

Name: Anonymous 2016-12-31 13:13

>>18
It all depends on your rhetoric. Your points may be valid but it is easily ignored when you use a retard's rhetoric.

Name: Anonymous 2016-12-31 14:24

>>19
People who see only tone and rhetoric are idiots anyway. I guess this just proves that I pay too much attention to dumbasses.

Name: Anonymous 2016-12-31 14:25

>>19
By the way, if you don't think null pointers ruined C#/C++/Java, then suck my dick you stupid bitch.

Name: Anonymous 2016-12-31 14:54

Stolen from Swift. Is there anything Apple doesn't invent first?

Name: Anonymous 2016-12-31 20:16

>>21
How would you have pointers work without NULL? What would malloc() return on failure? What would you use to indicate that the next pointer in the final node of a linked list doesn't point to anything?

Name: Anonymous 2016-12-31 20:33

>>23
If you read the article at the link or ever used Rust or ML or Haskell, you wouldn't ask these idiotic questions.

In short, pointers cannot work without NULL, but the type system at least can distinguish between pointers that may point to NULL and things that are definitely not pointing to NULL.

That prevents NULL infecting every fucking type and attacking you from every fucking corner at runtime which is the usual behavior of C/C++/C#/Java programs.

Name: Anonymous 2016-12-31 22:02

>>22
They even died of AIDS first. COURAGE!

Name: Anonymous 2016-12-31 23:12

>>24
A lot situations only work with nullable pointers, like linked list nodes for example (technically, you could implement a node struct with a non-nullable pointer and instead have a boolean nextNodeExists member to determine if the pointer can be legally dereferences, but that seems pretty wasteful IMHO). And you can already emulate non-nullable pointers in C++ with references.

Name: Anonymous 2017-01-01 9:58

>>26
A lot situations only work with nullable pointers
Wrong. Here is the next from Rust's iterators:

fn next(&mut self) -> Option<Self::Item>;

See how it returns an Option instead of just Item? It forces you to handle the end of list case, but it also prevents you from checking more than once:

match result {
Some(x) => ..., // In this branch you can be sure that x is an item and not some NULL bullshit.
None => ..., // In this branch you have to handle the end of list/none/NULL case, the compiler will complain if you don't.
}


And you can already emulate non-nullable pointers in C++ with references.
Orly? I'm not a C++ expert but it seems that Sepples references, while being genuine references unlike in Java/C#, are still pretty limited:

http://www.geeksforgeeks.org/references-in-c/
Due to the above limitations, references in C++ cannot be used for implementing data structures like Linked List, Tree, etc.

Name: Anonymous 2017-01-01 12:57

>>28
It forces you to handle the end of list case
A C compiler could also produce various NULL-related errors. I could give you that C doesn't have the types to cope with some situations and the compiler would probably have to emit a lot of useless errors.
it also prevents you from checking more than once
Really?

How is your code structurally different from, say, this C code:
if(result != NULL)
...;
else
...;

?
And 'forgetting' the NULL-check here is almost impossible when it's a linked list traversal (as was implied), since pretty much the first test run of such code would show absence of a NULL check. And when you don't test at all, Rust isn't gonna help you, either.

But let's go a little deeper here and imagine I wrote a list iterator for C. Would the issue still be there? Well, probably not because Rust did the exact same thing: they shifted the security check from the programmer to the library implementor. Forgetting to make it an Option in Rust is the exact same logical error as forgetting the NULL-check in C, except that the C style has to be done by the programmer, not the implementor.
And that leads us to a real C issue: lack of a high level library for things such as ADTs. But to be honest I'm not even sure that's really one of C's weaknesses: a lot of langs have come and gone and tried to fix this (probably starting with Java) and none of them replaced C. Not even C++, even though a lot of people claim that it's just a 'better' C.

Name: Anonymous 2017-01-01 15:14

>>29
could
would
You're sounding like a Lisp wizard. I've written C# and it was a fucking pain to keep track of what was and wasn't checked for null, with runtime errors constantly surprising me. The compiler can't help when the language's type system is compromised by the billion dollar mistake.

How is your code structurally different from, say, this C code:
In C you can write:
void foo(result) {
if(result != NULL) { // We can't omit the check here because there's no guarantee
...; // foo is always going to be called with non-NULL arguments.
}
}

if(result != NULL) // We check for NULL because we can't be sure foo will be doing that.
foo(result); // ...and that's a superfluous double check the type system didn't prevent.
else
...;


In those other more modern languages, on the other hand, you have to pattern match the Some/None on Option(Item) (ie. handle the case of the null painter) and you can't do that pattern match on Item. It's a tight correspondence which both gives correctness and prevents unnecessary performance degradation.

The point is, like I said, that there's a distinction on type level between nullable variables (Option(a)) and non-nullable values. That is a benefit of using sum types (you know, like C unions but done right), yet there are many more.

And 'forgetting' the NULL-check here is almost impossible when it's a linked list traversal
It's far from impossible in practice. Let's say the linked list is the result of a function called on the result of another function that handles various IO-related cases where one of many possible information sources may be used. In a complex code tree it's hard to keep track of where and in which code paths you remembered to check for null. It's either going to be amnesiac programming like in my example above, or null pointer exceptions biting you in the ass at runtime. The saddest thing is that it might seem you've gone full amnesiac but it still fucks up at runtime because there's still some obscure code path that you haven't covered in testing.

Null pointers are such a pain in the ass with such a clear solution that I don't see how anyone could argue they have benefits when even the creator of Java admitted them as a billion-dollar mistake.

they shifted the security check from the programmer to the library implementor
First of all that is a great thing because libraries are supposed to be heavily-verified, ready-to use pieces of code. Not reinventing the bicycle and all that. Secondly, you'd have to jump through some hoops to avoid Option when defining linked lists because linked lists are by nature sum types and the only way to deconstruct a sum type is through pattern matching, and that's when the compiler checks for exhaustiveless (ie. that all cases were handled). If you're trying to write the iterator as returning a non-nullable item, then what item are you going to return in the None branch?

fn next(&mut self) -> Self::Item {
match *self {
Cons(x, xs) => x,
Nil => ???,
}


You see, pattern matching is this beautiful concept because it balances creation of data structures with their destructuring. For every layer of wrapping you add, there must be a procedure of unwrapping that layer. The handling of failure cases (of which Option is just the simplest example) comes as a byproduct of that Yin-Yang balance.

Name: Anonymous 2017-01-01 17:52

NULLs are more efficient in practice(due overhead of encapsulating everything in Option/Error/Maybe structs)

Name: Anonymous 2017-01-01 19:16

>>31
Not really.

there is some compiler magic that optimises Option<ptr> to a single pointer (most of the time).

http://stackoverflow.com/questions/16504643/what-is-the-overhead-of-rusts-option-type

Name: Anonymous 2017-01-01 20:21

>>30
you have to pattern match the Some/None on Option(Item)
A C compiler could also produce errors/deny compilation for missing NULL-checks after function entry for every incoming pointer argument.

It's far from impossible in practice.
Again, ``good'' testing prevents this. If someone doesn't test their code it's not the lang's fault.
And since Rust code requires ``good'' testing, too, not much is gained. At least not in this case.

argue they [NULL pointers] have benefits
I'm not arguing they have benefits compared to things like Option. I'm arguing they are mostly the same. And when a lang compares similarly to the one it wants to replace, it should be disregarded.

the creator of Java admitted them as a billion-dollar mistake
That was Hoare, not Gosling.

Name: Anonymous 2017-01-01 21:39

>>33
A C compiler could also produce errors/deny compilation for missing NULL-checks after function entry for every incoming pointer argument.
It could sure, but that's really a bad idea, since it goes against the C mentality of avoiding unnecessary overhead. Sometimes the programmer can guarantee that a given pointer will never be NULL, so a null check would be wasteful. C is designed to avoid holding the programmer's hand, though implicit NULL checks might make sense in a higher-level language. C would benefit from a "dereference if not NULL" operator, since it is such a common pattern, though you can easily emulate that with a macro.

Name: Anonymous 2017-01-05 0:25

>>33
That was Hoare, not Gosling.

And as Hoare said about it:

I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement.

Even if Hoare had done the Right Thing in ALGOL W, it's pretty obvious that null references are such a convenient feature that someone else would've invented them for the same reasons. And then we'd be exactly where we are today: with all the most popular industry languages having very weak and unsafe type systems, because it's easier to bash out code that way.

Maybe programmers just can't be bothered with safety.

Name: Anonymous 2017-01-05 1:29

Most programmers produce disposibl garbage

Name: Anonymous 2017-01-05 3:31

>>35
Hoare ``invented'' the ``null reference'' not the null pointer.

Name: Anonymous 2017-01-05 6:32

Name: Anonymous 2017-01-05 17:51

>>38
In 2009 C.A.R. Hoare stated[7][8] that he invented the null reference in 1965 as part of the Algol W language, although NIL had existed in Lisp since 1959
More dumb goyim stealing from Lisp.

Name: Anonymous 2017-01-07 0:32

Invent my anus

Name: Anonymous 2017-01-19 14:52

>>35
Null references pretty clearly have enough uses to justify their existence. For example, with null references, a linked list node can look like:

typedef struct ll_node {
int val;
struct ll_node *next;
} ll_node;


On a 32-bit platform, that would be eight bytes per node. Without null references, it would become

typedef struct ll_node {
int val;
struct ll_node *next;
bool has_next;
} ll_node;


And since structs are typically aligned based on the alignment of their largest member, that single boolean field would add 4 bytes, or 50%, to the size of each node.

Name: Anonymous 2017-01-19 15:18

>>41
To add on to this, if C didn't have null pointers, programmers would probably resort to putting something like the following in the global scope:

char junk;
void * const null = &junk;


This would allow the programmer to simulate null pointers by following the convention that any pointer pointing to the variable 'junk' be treated as a pointer to invalid data. 'junk' could be used for something to avoid wasting memory, so long as no attempt is ever made to access it through a pointer.

Name: Anonymous 2017-01-19 15:23

>>28
I think you could use references to implement a circularly linked list though. And for regular linked lists, you could have a special "end of list" node, similar to the idea talked about in >>42; you'd create a special linked list node object, and consider any node with a reference to that object to be the end node of a list. Of course that wouldn't be efficient, and there's really no point to it, but it could be done if you really want to.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List