On Saturday, February 8, 2020 at 2:14:31 AM UTC-7, Brian Candler wrote:

> On Saturday, 8 February 2020 07:02:34 UTC, addi t0t08 wrote:
>>
>> I think the keyword we are looking for is `pass`. Similarly, `pass` only 
>> needs to return if err != nil.
>>
>> func writeSomething() error {
>>    f, err := os.Open("file.dat")
>>    pass err
>>    defer f.Close()
>>
>>    f.WriteString("...")
>> }
>> ---
>> passing a wrapped error(using the Wrap function above which returns nil 
>> if err is nil (not a special function) ):
>>
>>
>> func writeSomething() error {
>>    f, err := os.Open("file.dat")
>>    pass errors.Wrap(err, "couldn't open file")
>>    defer f.Close()
>>
>>    f.WriteString("...")
>> }
>>
>>
> In the second example, how does it know that "err" is the value to check 
> against nil?  If I wrote
>
> pass foo.Foo(bar, baz, qux)
>
> which of bar, baz or qux is checked against nil?  Is it looking for an 
> actual variable called "err" ?
>
> Secondly: it doesn't capture the common case of returning a new (and not 
> wrapped) error.
>
> if err != nil {
>     return DatabaseConnectionError
> }
>
> Wrapping errors isn't always a good idea: it exposes your implementation 
> details.  The API of the services you consume becomes part of the public 
> API that you expose to your clients, which clients will then depend on.  
> You can therefore not change your implementation to consume a different set 
> of services.
>
> Also, I don't see how your proposal handles functions which return more 
> values than just an error:
>
> if err != nil {
>     return nil, nil, DatabaseConnectionError
> }
>
> "pass should work with 100% of the cases within its limited scope." - in 
> other words, not all cases.  It belongs to Eric's category 1: "Identify a 
> solution that captures the 80-90% case of if err != nil { return ... }".  I 
> prefer the explicit if err != nil to this.
>



No, 'pass' accepts an error type. in this case Foo function must return an 
error type otherwise that would be a compile error.

You can return a new error without wrapping. Just create a function. Let's 
call it Foo  (you can call it anything). Also, the implementation of Foo 
doesn't matter if it returns an error then pass will return the error 
value, if nil it will be ignored.

func Foo(err error, text string) error {
       if err == nil {
              return nil
       }

       return errors.New(text)
}


to use it with pass:

pass Foo(someErr, "an error message")


if someErr is nil then Foo will return  nil and pass won't do anything.

I already said, multiple return values are supported (the original proposal 
also handles this) otherwise what's the point?. You can do this:


func doSomeTask() (int, bool, error) {
        f, err := os.Open(filename)
        pass err
        defer f.Close()
        
        ....
        return 10, true, nil
        
}

this isn't a complete proposal so it doesn't explain everything in detail 
but it should be pretty straightforward. 
 
 

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/c333c8e1-e242-41e6-9abe-4a2d682510b9%40googlegroups.com.

Reply via email to