Comments (12)
Similar, except for introduction of new keyword
from go.
Well the main difference is that this doesn't call a function but injects a switch block in predictable locations.
Also this is intended to be purely at compile time only.
from go.
This makes the question if a variable has a trap or not decidable at compile time.
What about assignments through pointers, and additionally in other functions?
p := &err
if ... { *p = ... }
maybeAssignToArg(p)
from go.
See also #56258.
from go.
Assignments in other functions would not have the trap appended. Assignments through a pointer would but only if the variable named in the trap is a pointer. The trap block is injected only if there is a = or := statement. So @randall77 in your examples no trap block is injected unless if there also is a select block for p. The assignments are though the p variable not through the err variable. Like this this proposal remains a pure compile time text expansion that is easy to understand.
The difference with the other proposal is that the trap is installed explicitly and statically on a named variable, not on the _ variable.
from go.
So
var err error
select err { ... } // set up a trap
err = ... // this traps
*(&err) = ... // this doesn't trap
?
from go.
That does trap because err is mentioned explicitly. That is the point of this proposal, everything is explicit! :) Of course doing *(&err) is a bit obtuse.
Edit:
And in case err is a struct err.Field = blah does inject the trap since err is mentioned.
from go.
That does trap because err is mentioned explicitly.
err
is mentioned, but there is never an explicit assignment to it. Perhaps this example is better:
p := &err
*p = ...
For extra fun , add arbitrary other complications between the two statements, like
p := &err
if ... { p = ... something else ... }
*p = ...
The general point is that at any pointer assignment, how do you tell whether it is assigning to a trapped variable or not?
I think the only possible answers are "we have to check it at runtime somehow" or "assignments through pointers don't trap".
from go.
Then it is "assignments through pointers don't trap". The idea is that the trap is strictly compile time and nominal only, which makes it a lot simpler to implement than, say, a defer statement for which Go has to keep a runtime list.
As I see it it will still be very useful even with these restrictions.
Edit: this is also how it this proposal is different from simply using a defer() statement for error handling as we can already do with the help of generics e.g. this comment: #57645 (comment)
from go.
Is the panic here "trapped" or "untrapped"?
var err error
f := func() { err = io.EOF }
select err {
case err != nil:
panic("trapped")
}
g := func() { err = io.EOF; panic("untrapped") }
f()
g()
Is the panic here 1 or 2? What gets printed before the panic?
var err error
select err {
case err != nil:
println(1)
panic(1)
}
select err {
case err != nil:
println(2)
panic(2)
}
err = io.EOF
from go.
The anonymous func is a separate scope so untrapped. The second trap block overwrites the first one so for the second example it is 2 only.
from go.
package main
import (
"reflect"
"runtime"
"runtime/debug"
)
func main() {
c := &Caller{}
c.call()
}
type Caller struct{}
func (c *Caller) call() {
Error("this is an error", &c)
// do not run here
println(1111)
println(1111)
println(1111)
println(1111)
}
func Error(err any, ptr any) {
if err == nil {
return
}
debug.PrintStack()
pc, _, _, _ := runtime.Caller(1)
fn := runtime.FuncForPC(pc)
funcName := fn.Name()
//Is it possible for reflection to dynamically insert a return statement at the next line of code where the parent function calls the sub-function?
//Also need to assign a value to the return value, not sure of the type
reflect.Indirect(reflect.ValueOf(ptr)).MethodByName(funcName).Close()
}
from go.
Related Issues (20)
- proposal: mime/multipart: add method Reader.SetRejectContentTransferEncoding(bool) to support net/http rejecting multipart form parts with "Content-Transfer-Encoding" header which is explicitly deprecated by RFC 7578 Section 4.7 HOT 2
- slices: add examples HOT 1
- x/crypto/ssh: SSH Client fails handshake when using publickey method HOT 4
- go/ast,go/format: newline missing between modified comment and function HOT 3
- text/template: A bug in the 'parseFiles' function of' helper. go ' HOT 1
- net/http: dial should not be canceled if an idle connection is reused HOT 4
- x/tools/go/packages: _test packages cannot be loaded directly HOT 3
- x/tools/(goimports): unexpected package is being imported
- cmd/compile: use mapaccess1_fast64 for small keys HOT 6
- net/mail: address parsing fails when name contains '@' HOT 2
- x/website/_content/doc: wrong branch names in install from source docs HOT 4
- x/tools/gopls: recovered panic in RenderPackageDoc HOT 2
- proposal: runtime: implement a DIT/DOIT mode HOT 7
- x/playground: func Test doesn't work in module mode: function main is undeclared HOT 2
- x/pkgsite: search for math/rand should answer with math/rand/v2 too HOT 1
- go/doc: New trashes the AST, even in PreserveAST mode HOT 2
- net/http: Host Header must not be deleted from http.Request.Header HOT 2
- proposal: errors: provide a way to iterate over nested errors HOT 18
- proposal: cmd/cgo support response files to avoid "argument too long" issues with CGO_LDFLAGS
- x/pkgsite: right aligned ΒΆ HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go.