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

GO2: proposal block check construction #28763

Closed
coquebg opened this issue Nov 13, 2018 · 1 comment
Closed

GO2: proposal block check construction #28763

coquebg opened this issue Nov 13, 2018 · 1 comment

Comments

@coquebg
Copy link

coquebg commented Nov 13, 2018

I would like to propose a new language construction for block level checks, which can be useful im many situations
One application can be error handling, but the construction is not limited to this only

The new keyword will be in the form
until [condition] { check expression } else { block code }
The condition will not be evaluated in declaration, but will be inserted after any expression with check keyword
The else clause is optional and will be executed if condition evaluate to false

Error handling can be expressed like this:

var err error
until err == nil {
   check err = someFunc()
} else {
   //handle error
}

This roughly is equivalent to

var err error
{
  check := func(){if !(err==nil) {goto ELSE} }
  err =someFunc; check();
  goto END_BLOCK
ELSE:
  // handle
END_BLOCK:
  // code after until block
}

The construction can be used in complex checks

var r result
var ok bool
until ok && r.Valid() {
   check v,ok := someMap[key] // check inserted here with both ok and r.Valid() evaluated; if r is nil - it should panic as in *if* construction
   check r.Update(someValue) // check evaluated here
   r.DoSomething() // no check evaluated
   check r.NoChage() // check inserted here even both *ok* and *r* are not changed - this is programmer's responsibility
} else {
  // handle situation
}

It can be used as loop as well

var a int
until a>1000 && a<2000 {
  for i:=10;i<20;i++{
     check a=i*i*i // this will break loop on i=13
  }
} 
@agnivade
Copy link
Contributor

The check syntax is also a proposal. Since your proposal builds up on that, I would request you to add a document in https://github.com/golang/go/wiki/Go2ErrorHandlingFeedback.

Thank you.

@golang golang locked and limited conversation to collaborators Nov 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants