News for hackers. Stuff that matters.
Re:Announcing Rust 1.13 by namelezz on Thursday November 10, 2016 @03:53PM (#12924245)
I find the Go version to be easier to debug though.
Re:Announcing Rust 1.13 by stouset on Thursday November 10, 2016 @06:05PM (#12925435)
Really? Really?

    foo()?.bar()?.baz()?
This is somehow less clear, and somehow "more debuggable" than

    a, err := foo()
    if err != nil {
        nil, err 
    }
    b, err := a.bar()
    if err != nil {
        nil, err 
    }
    c, err := b.baz()
    if err != nil {
        nil, err 
    }
    return c, nil
One of these obviously has no bugs. There is literally nothing to debug. The other has no obvious bugs.

Oh, except look again, I accidentally forgot to return from those error-handling-statements and now I have what should have been a completely avoidable bug.

Ask me how many times I've been bitten by this.

Re:Announcing Rust 1.13 by iand on Thursday November 10, 2016 @06:36PM (#12925744)
OK, how many times have you been bitten by it?

It doesn't even compile so I doubt you have run into it in real code.

Re:Announcing Rust 1.13 by andrewchambers on Thursday November 10, 2016 @07:57PM (#12926386)
that problem does compile in Go, and i have done it before (maybe with single err and not a comma). iirc there are go vet checks to catch that.
Re:Announcing Rust 1.13 by iand on Friday November 11, 2016 @04:21PM (#12934463)
See my other comment for runnable example. https://news.ycombinator.com/item?id=12925765
Re:Announcing Rust 1.13 by masklinn on Thursday November 10, 2016 @04:43PM (#12924689)
Nothing prevents you from writing

    let a = foo()?;
    let b = a.bar()?;
    let c = b.baz()?;
if that's what you want, not sure how the Go version would be easier to debug beyond that, and you can easily perform that transformation when you actually need to debug that code if the "pointfree" version hinders that.
Re:Announcing Rust 1.13 by Matthias247 on Thursday November 10, 2016 @04:55PM (#12924799)
If you want to set a breakpoint on the error then I guess that's not possible in your form. I don't know what ? really does but I guess it somehow short-circuits the current block in the error form and returns the error.

I also sometimes include log statements in specific error branches for debugging and delete them afterwards. That's trivially possible in the Go version but requires more effort in the shorter variants (e.g. also if the funcitons throw exceptions instead of returning errors in other languages).

Re:Announcing Rust 1.13 by stouset on Thursday November 10, 2016 @06:08PM (#12925464)
> I don't know what ? really does

Because you would rather pay the ongoing cost of writing (and worse, having to read) thousands of pointless `if err != nil` blocks than take three minutes to see exactly what try! does, which is (almost) exactly the Rust equivalent of just that.

> That's trivially possible in the Go version

It's also trivially possible in the Rust version, except you don't pay a ludicrous cost in readability of the logic flow in the thousands of other places you have to return an error.

Re:Announcing Rust 1.13 by junke on Thursday November 10, 2016 @05:00PM (#12924862)
That's trivially possible in the Go version because you paid the cost of adding explicit error handling upfront.

It takes more effort to wrap a handler for a specific exception because there was no need originally to write a handler in that place.

Re:Announcing Rust 1.13 by Retra on Thursday November 10, 2016 @07:10PM (#12926042)
>I also sometimes include log statements in specific error branches for debugging and delete them afterwards. That's trivially possible in the Go version but requires more effort in the shorter variants

You can just map your logging in between:

foo().map_err(|e| {log(e); e}).bar()

Re:Announcing Rust 1.13 by masklinn on Thursday November 10, 2016 @05:01PM (#12924867)
> If you want to set a breakpoint on the error then I guess that's not possible in your form.

You can just macroexpand ? and do that (although I don't know that there are tools doing macroexpansion currently).

> I don't know what ? really does

It's really nothing special, `a?` simply expands to:

    match a {
        Ok(val) => val,
        Err(err) => {
            return Err(From::from(err))
        }
    }
> also if the funcitons throw exceptions instead of returning errors in other languages

?

Re:Announcing Rust 1.13 by Matthias247 on Thursday November 10, 2016 @05:09PM (#12924953)
> It's really nothing special, `a?` simply expands to:

Ok, that's what I expected

> ?

I meant if I have foo().bar() in C# or Java and foo() throws it's equally hard to change the code to add error handling in between since I would need to add a try/catch clause and rethrow the error.

Re:Announcing Rust 1.13 by pcwalton on Thursday November 10, 2016 @04:00PM (#12924299)
Why? Can you explicitly name a type of mistake that might happen and explain how Go solves it in a way that Rust does not?
Re:Announcing Rust 1.13 by namelezz on Thursday November 10, 2016 @05:02PM (#12924876)
It's not really a mistake. Just coding preference. For example: do_A()?.do_B()?.do_C()?. With the one line statement like that, when weird things show up, I don't really know if it's caused by bugs in do_A, do_B, or do_C. The linear layout of Go code lets me go through each do_X call line by line and setting break points is also easier. The problem here is when I need to investigate the chain do_A()?.do_B()?.do_C()?., I may have to rewrite that statement.
Re:Announcing Rust 1.13 by pcwalton on Thursday November 10, 2016 @05:16PM (#12925013)
I agree that we should have good debugging tools for this (in particular, break on error return). But note that you can break on function calls and use "finish" to effectively go call by call to see where the error is today.
Re:Announcing Rust 1.13 by namelezz on Thursday November 10, 2016 @06:34PM (#12925726)
> use "finish" to effectively go call by call

Great tip. Thank you for the hint.

Re:Announcing Rust 1.13 by junke on Thursday November 10, 2016 @03:56PM (#12924265)
I hope so, because it is more subject to accidental copy-paste bugs.
Re:Announcing Rust 1.13 by lomnakkus on Thursday November 10, 2016 @04:41PM (#12924672)
And follow-up fixing n copies when that bugs gets copied, but only gets discovered later.
Re:Announcing Rust 1.13 by junke on Thursday November 10, 2016 @04:55PM (#12924804)
My favorite: those two snippets are 98% similar, is there a bug, two bugs or two distinct cases?
Re:Announcing Rust 1.13 by nercury on Thursday November 10, 2016 @04:13PM (#12924406)
But consider that the Rust does not have null, so every case where you need to step over the code and see if it does the right thing when value is null does not need debugging!