This proposal is not very much different from a one-liner

if err != nil { <single_statement> }

It's just 10 more characters. Granted, gofmt won't leave the one-liner
formatted that way, but it's not much more typing and comes with the
benefit of not needing a new keyword.

One variation on this proposal that is a little weird is that if could in
the case of checking for an error omit the requirement that its argument be
a boolean, giving

if err { <single_statement> }

Which is only 3 characters longer than on and again avoids a new keyword. I
believe changing the behavior of if this way should be backwards compatible.

On Wed, Jul 3, 2019 at 6:26 AM Michael Ellis <michael.f.el...@gmail.com>
wrote:

> I like this. A lot.
>
> It's clean and explicit.  The reader only needs to understand that 'on' is
> a test for a nil value (vs 'if' which tests for boolean true).
>
>
>
> On Tuesday, July 2, 2019 at 3:57:24 PM UTC-4, Liam wrote:
>>
>> This proposal has attracted modest attention from the Go team...
>> https://github.com/golang/go/issues/32611
>>
>> It suggests:
>>
>> err := f()
>> on err, <single_statement>
>>
>> on err, return err            // any type can be tested for non-zero
>> on err, return fmt.Errorf(...)
>>
>> on err, fmt.Println(err)      // doesn't stop the function
>> on err, continue              // retry in a loop
>>
>> on err, goto label            // labeled handler invocation
>> on err, hname                 // named handler invocation
>>
>>
>>
>> And offers these possible extensions:
>>
>> on err, os.IsNotExist(err):  <stmt>
>> on err, err == io.EOF:       <stmt>
>> on err, err.(*os.PathError): <stmt>    // doesn't panic if not a match
>>
>> on err, <condition>: <stmt>
>> on err: <stmt>              // this pair provides if/else in 2 lines
>>
>> on err := f(), <stmt> // for assignment with single lvalue
>>
>>
>>
>> Other punctuation is possible, e.g. on (err) <stmt>
>>
>> Now if we could just convince the Go gods to prototype this along with
>> try() in 1.14 :-)
>>
> --
> 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/decc63a5-9e65-4e96-929f-76d44cf19e14%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/decc63a5-9e65-4e96-929f-76d44cf19e14%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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/CANfvvbVy1iP8Un8zudCwZhkTg5dQfGCrBkN-0VmFbbK-FgD8dA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to