func myFunc() error {
var err error
if err = otherFunc(); err != nil {
return err
}
x, err = anotherFunc()
if err != nil {
return err
}
yetAnotherFunc(&err)
if err != nil {
return err
}
return nil
}
func myFunc() error {
var err error
when err != nil {
return err
}
otherFunc()
x, err = anotherFunc()
yetAnotherFunc(&err)
return nil
}
--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Without taking your example at face value, this is a form of data binding. Being able to do such on any arbitrary type would have nonobvious side effects. The &err example func you have highlights this. In the body of yet another func, does it automatically return after setting *err non nil?
Does it need to bind as well?
If it's your first time reading someone else yetAnotherFunc(*error) would you feel comfortable knowing what parts of the func body would and wouldn't run simply based on an unknown binding?
What about any *type argument?
I think there are pros & cons to evaluate here:
- [+/-] it would make less effective stack traces because would not be clear where exactly happened a failure (same problem as C++, not Python). The stack trace must include the point where the check happened.
- [+/-] coverage would be misleading, unless tools would be adapted cutting and pasting the body of the where clause where they have been placed.
- [con] cose less fluent, the same can be already said for defer.
Rather than a when keyword, something pre or preemt might work.
Preempt (func () { if err != nil { log.Print (err) } })()
This would then be called prior to any call instruction.
I still don't like it and prefer how Go is currently but it would be a more palatable alternative to your proposal for me.
As stated this is almost the opposite of a defer. It makes the code more concise but less clear.Rather than a when keyword, something pre or preemt might work.
Preempt (func () { if err != nil { log.Print (err) } })()
This would then be called prior to any call instruction.
It also doesn't work for the "x, err = ..." case.
preempt {
if err != nil {
return err
}
}
var err error {
if err != nil { return err }
}
otherFunc()
...
--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
package main
func readFromRedis() (data, error) {}
func readFromService() (data, error) {}
func readFromDB() (data, error) {}
func readData() (data, error) { data, err := readFromRedis() if err == nil { return data, nil }
if err.(RedisError) != redis.NotFoundError { return nil, err }
data, err = readDataFromService() if err == nil { return data, nill }
if er, ok := err.(ServiceError); ok && er != service.NotAvailableError && err.(ServiceError) != service.NotFoundError { return nil, err }
return readFromDB()}
func main() { readData()}
func readData() (data X, err error) {
when err == nil {
return data, nil
}
data, err = readFromRedis()
if err.(RedisError) != redis.NotFoundError {
return nil, err
}
data, err = readDataFromService()
if er, ok := err.(ServiceError); ok && er != service.NotAvailableError &&
func myFunc() error {
var err errorerr = otherFunc()
x, err = anotherFunc()// What will happen to the functions which are placed before `when`?// Binding or not binding, it's a question.// The point is the scope of the keywords `when`.// It may take too much attention to decide how and where to use.
On 03/08/2016 09:09 PM, ben.davies...@gmail.com wrote:
See, to me that just looks misleading; if I read that quickly it looks like the success path would result in the return readFromDB() line.
All but the the first line you mean? ;-)
func readData() (data X, err error) {
when err == nil {
return data, nil
}
data, err = readFromRedis()
if err.(RedisError) != redis.NotFoundError {
return nil, err
}
data, err = readDataFromService()
if er, ok := err.(ServiceError); ok && er != service.NotAvailableError &&
err.(ServiceError) != service.NotFoundError {
return nil, err
}
data, err := readFromDB()
return nil, err
}
func readData() (data X, err error) {
when err == nil {
return data, nil
} else if !isNotFound(err) {
return nil, err
}
data, err = readFromRedis()
data, err =
readDataFromService()
data, err = readFromDB()
return nil, err
}
func readData() (data X, err error) {
data, err = readFromRedis()
if err == nil {
return
data, nil
} else if !isNotFound(err) {
return nil,
err
}
data, err =
readDataFromService()
if err == nil {
return
data, nil
} else if !isNotFound(err) {
return nil,
err
}
data, err = readFromDB()
if err == nil {
return
data, nil
} else if !isNotFound(err) {
return nil,
err
}
return nil, err
}
func readData() (data X, err error)
{
for
readFunc in range []func() (data, error){
readFromRedis,
readDataFromService,
readFromDB,
} {
data, err =
readFunc()
if
err == nil {
return data,
nil
} else if !isNotFound(err) {
return nil, err
}
}
return nil, err
}