Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: Go 2: simplify error handling and simplify conditions processing and "?" pipe operation symbol #39148

Closed
enddeadroyal opened this issue May 19, 2020 · 8 comments
Labels
error-handling Language & library change proposals that are about error handling. FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal Proposal-FinalCommentPeriod v2 An incompatible library change
Milestone

Comments

@enddeadroyal
Copy link

1.Simplify error handling:
for example (reference to rust)
old(go 1.12):

dat,err := ioutil.readFull("Go Programming.pdf")
if err != nil {
   log.error(err.Message())
   return err
}

OR

if dat,err := ioutil.readFull("Go Programming.pdf");err != nil {
   log.error(err.Message())
   return err
}

expect:

dat := ioutil.readFull("Go Programming.pdf") ? err =>  log.error(err.Message())

OR

dat := ioutil.readFull("Go Programming.pdf") ? err => {
     log.error(err.Message())
     return err
}

when err is nil, don't do err expression

2 Simplify Conditions Process
old(go 1.12):

dat,err := ioutil.readFull("Go Programming.pdf")
_,ok := err.(os.Err)
if ok {
 //do something
}
_,ok := err.(file.NotFindErr)
if ok {
 //do something
}

expect:

switch err => {
    os.Err, io.Err: =>    //do something
    file.NotFindErr:=> {
            //do something
     }
}

when 1 and 2 use at the same time

dat := ioutil.readFull("Go Programming.pdf") ? err => {
   switch err => {
     os.Err, io.Err:=>    //do something
     file.NotFindErr:=> {
            //do something
      }
    }
}

OR

dat := ioutil.readFull("Go Programming.pdf") ? switch err => {
    os.Err, io.Err:=>    //do something
    file.NotFindErr:=> {
            //do something
    }
}

"?" operation symbol is was used as a pipe which concat expressions
like as linux "|" operation symbol

netstat -an | grep 8080
@gopherbot gopherbot added this to the Proposal milestone May 19, 2020
@enddeadroyal enddeadroyal changed the title proposal: Go 2: Simplify error handling and Simplify Conditions Process proposal: Go 2: Simplify error handling and Simplify Conditions Process and "?" pipe operation symbol May 19, 2020
@ianlancetaylor ianlancetaylor changed the title proposal: Go 2: Simplify error handling and Simplify Conditions Process and "?" pipe operation symbol proposal: Go 2: simplify error handling and simplify conditions processing and "?" pipe operation symbol May 20, 2020
@ianlancetaylor ianlancetaylor added error-handling Language & library change proposals that are about error handling. v2 An incompatible library change LanguageChange Suggested changes to the Go language labels May 20, 2020
@ianlancetaylor
Copy link
Contributor

Has similarities to #33177, #32946, and likely others.

@ianlancetaylor
Copy link
Contributor

ianlancetaylor commented May 20, 2020

The switch err => syntax seems quite similar to a type switch. I don't see the advantage to the new syntax.

The other syntax seems to change

if r, err := F(); err != nil {
    ...
}

to

r := F() ? err => {
    ...
}

If I'm counting right this replaces if, a comma, a semicolon, and != nil with ? and =>. This seems to save 6 characters, not counting whitespace. That doesn't seem like a huge benefit to me.

@enddeadroyal
Copy link
Author

Thank you for answer

if r, err := F(); err != nil {
    ...
}

to

r := F() ? err => {
    ...
}

The scope of these two r is different
The first will make the nesting very serious

if r, err := F(); err != nil {
    ...
}else {
   if r, err := F(); err != nil {
    ...
   }else{
      if r, err := F(); err != nil {
         ...
      }else {
         ...
     }
   }
}

to

r := F() ? err => {
    ...
}
r := F() ? err => {
    ...
}
r := F() ? err => {
    ...
}

@enddeadroyal
Copy link
Author

no need for “=>”

if r, err := F(); err != nil {
    ...
}

to

r := F() ? err {
    ....
}

@ianlancetaylor
Copy link
Contributor

Thanks for the correction on scoping. I don't think it affects the token count, though.

Eliminating => does save another two characters, so now overall it saves 8 characters.

@gocs
Copy link

gocs commented May 25, 2020

This looks like something like a ternary operator.

@ianlancetaylor
Copy link
Contributor

This proposal introduces new syntax unlike anything else in Go. As discussed above, it doesn't appear to bring a significant savings in character count. It has similarities to other error handling proposals that have been declined.

For these reasons, this is a likely decline. Leaving open for four weeks for final comments.

-- for @golang/proposal-review

@ianlancetaylor
Copy link
Contributor

No further comments.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
error-handling Language & library change proposals that are about error handling. FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal Proposal-FinalCommentPeriod v2 An incompatible library change
Projects
None yet
Development

No branches or pull requests

4 participants