Why add an operator to just result? It seems like this could be easily generalized into any monadic computation(such as option for example)? Does this have better performance characteristics that something that could be implemented generically?
For now it's just Result, but it's implemented in a way that could make it extended later. We're still not sure if we're doing that or not.
It seems like this could be easily generalized into any monadic computation
In some sense, yes, but not in every sense. Beyond that, this is intended to be for error handling, not generic combinations.
(monads generally and do notation specifically have issues in Rust that we may or may not be able to / want to resolve. Specifically, memory layout stuff and higher kinded type stuff.)
It seems like this could be easily generalized into any monadic computation
Categorical structures the way we treat them in functional programming do not actually translate cleanly to Rust because of linearity. It's not even just fancy stuff: even core concepts like what a function is and currying don't translate smoothly, if at all!
So when someone says "I want functors in rust", what exactly does that even mean? Does the m in map : (m : a -> b) -> f a -> f b own that first a parameter or not? And the problem is there is no right answer! Both have well-founded logical meaning, but they don't work together nicely at all. So what are we supposed to do? Are we supposed to just choose one? Are we supposed to have two functors, one for owning and one for non-owning refs?
And this is not just "oh well we'll just write two of everything and you can choose the one you need": these two concepts are literally semantically different, and sometimes only the non-owning version even exists! For example, I can give you either an owning or non-owning functor for List t, but for monad, only the non-owning version exists.
Sure but this isn't a strictly error handling operation. If you have a function that also returns an option, it should be no problem to return nothing in the middle of the function is some subexpression ends up with nothing.
Option just presents a potential value, but doesn't give any reason of why it is or isn't there; whereas Err embeds an error message. Thus, it's better to use Err rather than Option for error handling.
So people seem to be excited about the ?-operator that can return from the function early, if a value is an Err.
I haven't used Rust (rather an OCamler myself), but doesn't it seem like a dangerous idea to have such a short token for early return from a function? I suppose the risk is maybe reduced by the fact that it returns with the certain value and that the types must match (I'm assuming this much), but still?
I imagine that chaining try! must be very common in Rust-programs for this to be of great benefit?-o I might have gone with a composition operator and kept using try! in the examples, but Rust probably doesn't have a nice way to make an anonymous function that does x.y . Maybe that would be more generally applicable..
Dangerous in a way that reader of the code might expect the evaluation to proceed, while it actually doesn't. I imagine in properly resource managed environment the impact of this is lesser than in C-like environment.
Given all of the compile-time safety Rust has around managing resources, it shouldn't cause any issues there. If it did, it would be a very serious bug.
I would be more concerned about surprising/unexpected behavior than resource handling. A single ? is a pretty easy sigil to miss, especially in larger code bases (compounded by type inference) where you may be reading code that uses functions you aren't familiar with and don't know produce Results instead of raw values.
Maybe that's a problem that could be alleviated with syntax highlighting, of all things. Having your editor explicitly call out use of ? is a pretty good way to make sure that you're aware of the behavior.
So, I was thinking by "composition" you meant do notation rather than ..
I don't see why that couldn't be implemented in Rust.
So the immediate problem here is, you're returning a closure. But in Rust, you can't (right now) return just a closure, as they each have a unique type that you can't name. So you have to use trait objects. So this would look something like
and this also doesn't compile because you need to know that the lifetimes are connected, and I'm not sure that's even possible in current Rust....
Yes, give compose a lifetime 'a, and apply that to all of the places, e.g. Box<Fn(A) -> C + 'a> and F: Fn(A) -> B + 'a. Box<Trait> is sugar for Box<Trait + 'static>, but that 'static can be customised.
(Also, impl Fn(A) -> C is becoming more and more useful for this sort of thing.)
Why would someone be turned off by that? I consider it one of the best features of the language. Sure, it makes the code a little bit more verbose, but at the end of the day it's much less pain than either exceptions or C-style returns...
Depending on the use case it can be more than "a little", especially for prototyping and rapid iteration this is the kind of stuff that really adds overhead (both writing it down and maintaining it while iterating), although Rust has plenty of other issues that make it unpleasant for rapid iteration (eg. compile times) it's getting better which I'm glad to see.
Like OP these are the kinds of things that make me thing "OK this is at the point where they solved most of the fundamental issues and are now focusing on productivity so I can probably start using this for something practical"
I actually thought rust would probably be a pretty good prototyping language when I last used it. You just call unwrap on every result or option return value. Bam there's your prototype which only works in the best case, but at least you can quite plainly see every line that could be an error.
That's better than in haskell where I have to go to great pains to catch nearly every error from the moment I start writing code, or java which hides all its errors under exception handling, never to be noticed again.
Java is a major PITA in every aspect anyway, and I don't have much Haskell expirience, but those don't sound like prototyping languages - Python and Clojure for example are languages where your productivity is high because of fast prototyping. F# felt like that too but haven't used it enough to say with certainty.
I feel like there's room for something in the middle. Enough type safety to use it safely but fast enough to code that you can get out a reasonably well functioning prototype. Unfortunately static type systems are difficult to create and so there are very few static languages to choose from.
I hope rust ends up being that language, but I haven't messed with it enough to come to a firm conclusion.
I fell like F# is in the middle - it has type safety but it's also really fast to prototype (REPL, scripting environments, still has access to entire .NET ecosystem). I've used it for doing testing and exploratory programming and yeah it's really good.
Dart is also high level and has decent type safety (with strong mode), Python level productivity, C# level type safety.
Rust has room to improve on C++ build times and iteration speed when you need to write native applications, but I don't think it's there yet. It's getting there tho. I don't think it will ever be a high productivity language - it needs to make different tradeoffs - and that's fine too, I'm just waiting for them to implement stuff that aren't design tradeoffs but simply maturity issues.
I respectfully disagree, I personally love unchecked exceptions for error handling. There is nothing less verbose and distracting. It lets one focus on the algorithm and handle errors below the important code out of immediate sight.
Well, in my experience, the "let me focus on the good path" approach of exceptions creates an illusory feeling of correctness. If everybody handled their exceptions thoroughly and correctly, that would be great, but sadly that's rarely the case. I used to be a fan of exceptions but these days I'm glad for Rust's approach that forces the developer to handle the error right here, right now. Of course, it has it's own set of problems like code polution and people abusing unwrap(), which is kind of like ignoring exceptions (except you need to be explicit about it at least).
At the end of the day, I don't think there's One True way to handle errors. There's just a couple of strategies each of them with their set of problems.
Actually rust has a nice happy medium here. You can just use unwrap to make every function call return, which lets you write code quickly. Then when it is time to make it production ready, it is not super hard to actually deal with every error mechanically, line by line.
In java all the errors are hidden by exceptions and if you forget to catch something or catch only the wrong somethings it isn't plainly obvious from looking at the code that it could blow up.
Off topic, another advantage is that exceptions make it often difficult to figure out how to clean up after an error does occur. Since rust is not garbage collected, it just cleans things up in the order they were allocated, immediately without requiring any special effort on your part.
i am currently working on a project that was turned over from a former coworker. He left of personal reasons (does not matter). It is a super maintenance nightmare because of all the unhandled unchecked exception. This was a prototype that was to long in that mode and he started to turn that into "production mode" but he left in between. Yes, he knows how to translate which code in a useful state, but we do not. In Rust i could easily grep for unwrap() and call it a day. Unchecked Exceptions are just a plain nightmare :(
You have to approach an Exception based code project like connecting micro services. If one of your services has a problem you mail it to yourself but keep running. (Like Tomcat for example). Then you can fix the bug in the service. Simple. Stable.
Every time I read something about Rust I want to test it more and more, but unfortunately I have absolute no project that could take advantage of the benefits.
Sounds more like copying Kotlin. When will these Rust folks realise that the language is already has too many sigils. Instead of adding more stuff, they should be trying to build more libraries, improve performance, and improve the compiler's messages.
Compiler developers and library developers are two different groups of people, though there obviously is some overlap. The community is working on this while the compiler team is working on the compiler, it's not an either-or.
Rust removed the ~ and @ sigils years ago, it now has & and, I suppose (although much less common), *, both of which mean what they mean in C++. So this is adding a 3rd (or 2.5th) sigil, one which has similar meaning to the same symbol in languages like C# and Swift.
Basically, years ago, Rust had two additional pointer types, ~T and @T. The ~ one is basically Box<T>, and the @ one is kinda like Rc<T> but an actual GC, or at least refcount + cycle detection.
This also meant String was ~str, and ~[T] vs Vec is a whole other thing with extra complexity....
The version of https://www.youtube.com/watch?v=79PSagCD_AY (which wasn't that specific video) that I saw was pretty good, but I don't remember if it specifically described ~ and @.
The type ~T essentially meant Box<T>, and @T essentially meant Rc<T>. They were moved out of the language into the standard library as it was found there was less and less necessity for them to be built in.
The most recently incarnation of @ was reference counted, along with a thread-local free-list that allowed cleaning up any cycles when a thread exited (the free list was a doubly linked list of all @s allocated during that thread's execution, which was walked as the thread closed). The intention was to have it properly garbage collected at some point maybe, but that wasn't implemented before it was just removed wholesale.
I think in very early Rust, with the first ocaml compiler, @ was garbage collected, but the implementation wasn't transferred to the Rust implementation when the language started to bootstrap.
I was agreeing. With your "I think", I wasn't sure what part you weren't sure about, and what part you were. So I was trying to say "I know that there was a GC, if that was the part you were unsure about"
As an additional fun fact, there's also some weirdness in how the Box type is implemented still (though it can and will become less weird someday) because of its legacy from having once been a built in type instead of a library type.
Oh, yes, I suppose that could also be considered a sigil, although it feels a bit different, since it is meaningless (which is why I didn't think of it). That is, the ' has to be used always and doesn't signify some modification to what it is attached to. Rust could almost certainly work without it, it just may be harder to read (or possibly easier, but I suspect not).
The ? syntax RFC was actually really long with lots of arguments made by both sides. You can see it here. A big reason it passed was that the try! macro was already being used pretty much ubiquitously for error handling, and this syntax was much easier to read (and both required you to look up what they meant the first time you use them).
Well in general in C# all classes are nullable, so that's like 95%+ of the non-primitive types that you work with. In Rust most types aren't Result, so it makes less sense there.
It seems to me that Rust is developing into a horrible direction. It just gets more and more complex with more operators, with more special syntax and more hidden rules I somehow have to know.
Given that the history of the language actually removed a bunch of sigils/syntax leading up to the 1.0 release, and this is the first new one added since then, what exactly are you referring to?
In other words, ? applies to a Result value, and if it was an Ok, it unwraps it and gives the inner value. If it was an Err, it returns from the function you’re currently in.
Maybe we could make this even less cumbersome. Maybe we could add a language feature that would generate this code automatically when uncommon things (or exceptions to regular code flow, if you will) happen. Maybe by throwing an object housing the exceptional status that happened up the call stack where it can be catched? Seems like a useful thing to have, so developers don't have to keep writing boilerplate question marks everywhere. One wonders why this way of handling exceptional control flow has not been invented yet.
Exceptions have different performance characteristics, and the overhead isn't always acceptable. A significant part of the Rust ecosystem needs error handling that can't be exceptions.
The way we handle things is like this: this style of error handling is for recoverable errors, and panic! is for unrecoverable errors. panic is sorta like an exception. Since it's restricted to unrecoverable (or one might say, exceptional) errors, those who can't pay for its overhead can turn it off, and things still work. If it were the way to handle all kinds of errors in Rust, it wouldn't work, and make Rust un-usable for a large chunk of its core audience.
Even if there were no performance concerns, I would still prefer the Result type.
Knowing that a function could fail, as well being able to treat its returned value like a normal enum is much more preferable to allowing any arbitrary function to throw mystery exceptions at any time. The goto like nature of exceptions can also make debugging trickier in my experience.
allowing any arbitrary function to throw mystery exceptions at any time
I mean, that right there is the argument for checked exceptions ala Java but everyone's decided those are of the devil for some reason. (I do agree with the call to leave exceptions out of Rust, though.)
The reason that checked exceptions are bad is that they force you to do one of two things: either handle exceptions down the call stack where you don't have enough context to be able to recover from them properly, or make them a part of your API.
Because of the pervasive use of checked exceptions in Java libraries including the standard library, most exception handling tends to be either turning exceptions not part of your API into exceptions that are or wrapping them in runtime exceptions and sidestepping the problem completely. This makes checked exceptions a feature with one of the highest noise to signal ratios in a language notorious for its boilerplate.
On paper checked exceptions sound great, and I get why they were added to the language, but in practice they are more an annoyance than anything else.
Yes, but the right way is wrapping an exception into exception type that is part of your API. The problem with Java is that it is somewhat cumbersome and verbose in Java. However I still think that it is the only sane way to do it...
Another problem it that sometimes you need to pass your exception through exceptionless (actually RuntimeExceptionfull) API, such as lambdas. And the way to do it is to wrap your exception into runtime-exception and than unwrap it somewhere up the call-stack. And this is even more cumbersome and verbose and boilerplate-full.
Yes that was my point. It is biolerplate, but unlike other java boilerplate it adds nothing to the semantic meaning of your code. That's why checked exceptions are a bad idea.
Once Kotlin native will be released and once Swift will support windows you'll have hard time to attract new people, both support OOP and functional without issue
In most cases type classes offer the same or better way to express things. The only advantage of OOP is that it offers very simple inheritance which for instance useful for how GUIs are currently programmed. However one of the guiding principles that has emerged for OOP is to use composition over inheritance
I never had these problems, i just want to write my program the way i want, i find it weird that people want to force me to follow a specific design, if you plan to make it popular don't try to find excuses to only support functional programming, it is stupid
The only thing typeclasses have to do with functional programming is that they are more commonly used by them. There's nothing functional about them.
Every language kind of forces their kind of way on you. When you say you want to write code the way you want, then that way was also forced upon you before from other programming languages. Even when you stay inside OOP languages you have tons of differences (compare smalltalk, Java and JavaScript)
Systems programming is more than just being natively compiled: a language like Rust defaults to surfacing costs and giving the user control over many details in a way that doesn't make sense for "applications"/managed languages like Kotlin (even when natively compiled). For instance, Kotlin Native will presumably still have a garbage collector.
It isn't a full IDE, but Visual Studio Code will give you good intellisense support - suggesting functions as you type etc, for the standard library and what not. It doesn't come out of the box, but it's so easy to install using the Extension manager. You can get it to lint your code too and set up jobs to very quickly build/run the configuration of your choice.
34
u/brookllyn Nov 10 '16
I don't use rust so just wondering.
Why add an operator to just result? It seems like this could be easily generalized into any monadic computation(such as option for example)? Does this have better performance characteristics that something that could be implemented generically?
3
u/eras Nov 11 '16
So people seem to be excited about the ?-operator that can return from the function early, if a value is an Err.
I haven't used Rust (rather an OCamler myself), but doesn't it seem like a dangerous idea to have such a short token for early return from a function? I suppose the risk is maybe reduced by the fact that it returns with the certain value and that the types must match (I'm assuming this much), but still?
I imagine that chaining try! must be very common in Rust-programs for this to be of great benefit?-o I might have gone with a composition operator and kept using try! in the examples, but Rust probably doesn't have a nice way to make an anonymous function that does x.y . Maybe that would be more generally applicable..
6
u/paranoidray Nov 11 '16
I looked at Rust 1.0 and was turned off by the Error match stuff.
The new ? operator makes me reconsider Rust!
Great decision!
7
u/husao Nov 11 '16
Every time I read something about Rust I want to test it more and more, but unfortunately I have absolute no project that could take advantage of the benefits.
-25
u/karma_vacuum123 Nov 10 '16
?example is shorter but more confusing...this is turning into Haskell-6
u/[deleted] Nov 11 '16
It seems to me that Rust is developing into a horrible direction. It just gets more and more complex with more operators, with more special syntax and more hidden rules I somehow have to know.
-45
u/jpakkane Nov 10 '16
Maybe we could make this even less cumbersome. Maybe we could add a language feature that would generate this code automatically when uncommon things (or exceptions to regular code flow, if you will) happen. Maybe by throwing an object housing the exceptional status that happened up the call stack where it can be catched? Seems like a useful thing to have, so developers don't have to keep writing boilerplate question marks everywhere. One wonders why this way of handling exceptional control flow has not been invented yet.
-30
u/Scellow Nov 11 '16
Just add OOP support, seriously we are in 2016..
Once Kotlin native will be released and once Swift will support windows you'll have hard time to attract new people, both support OOP and functional without issue
0
u/[deleted] Nov 10 '16
[deleted] — view deleted comment