-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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: error handling by template functions #57822
Comments
Can you clarify what you mean by "share variables"? Thanks. |
It seems confusing that What happens if an |
Inline is not a function, it's a code that will be inserted to function. With this behavior By |
#32473 and #35093 are very different approaches to my. It's more about return leveling that is very interesting, but not situable for error handling. |
This sounds like #32620. |
Yeah, it's very close, I didn't see it. The only thing to think about - compilation scripting language is harder to implement and its syntax is a very hard question. |
This may work but when it comes to dealing with interface as a parameter how will |
I didn't really understand what the problem with interfaces is?
should be "translated" to
or
to
|
if the
Or
I think if there’s no keyword |
Returning from inline is case i thinked a long time. My conclusion was that u can't The only case in which require Type checking is a good idea but
should works because Go
The last is parsing question and it seems like the simpliest because of Go code rules: if you can't have |
This can be a problem because most of error handling complains always focused on
so not allowing
the compiler is smart enough to inline it but as it comes to a more sophisticated error handling like with switch statements inlining can’t be possible (last I recall), this can be useful in solving the |
It's a good question, i didn't think about it because i personally like named returns and U also can write something like this i think
as
The only limitation that in this case u can't write universal While trying to solve this problem, it seems to me that we will again come to programmable compilation.
It's still easier than classical try catch finally with optional guard and others, but i thing this will be hard to implement in compiler. It's like a new quality stage. At least in my experimental preprocessor, which I'm going to publish soon to test this concept, it seems very difficult to implement. |
I proposed before that we could add hygienic macros to Go , but this was rejected. #32620 How is this proposal different? |
@beoran Some syntax difference and my example preprocessor is on the way |
//Hand Error
func main() {
f, err := os.Open("/test.txt") :e {
fmt.Println( e )
}
//or
f, err := os.Open("/test.txt") :e HandEorr( e )
//or
f, err := os.Open("/test.txt") e: {
fmt.Println(e)
}
f, err := os.Open("/test.txt") e:
} |
@xiaokentrl that's just #37243 #41908 #56895 with a different token |
Why not create an extension to go then support all these features that are typed related like go generate, having a file extension like
if a developer wants to return two or more then
the
there’re also some rules that I applied above about inline keyword:
with this is reduces the load of verbose and complex type system while giving features when it’s needed by the developer, |
@Patrickmitech I'm in process of preprocessor creation. Seem's like it will be available in two or three days. |
This comment was marked as spam.
This comment was marked as spam.
Little update: my example preprocessor is in the process of writing, I need a little more time for correct |
These template functions are very unlike anything else we have in Go. They introduce a new macro concept. It's an idea one could pursue, but it's complex, and would have to be fully explored independently of any connection to error handling. We would have to understand the name scoping, blocks as arguments, and other ideas that do not exist in Go today. This is not a direction we're going to take with Go. Therefore this is a likely decline. Leaving open for four weeks for final comments. |
No further comments. |
Author
Consider myself as intermediate go developer.
Experience: 2 years in production with go.
Other language experience: the most experience is in Js, tried many interesting things from LLVM IR to Nim and Odin.
Related proposals
Has this been proposed before? Variations have been proposed, this is discussed in the proposal.
Affect error handling: yes
Would this change make Go easier or harder to learn, and why?
This will make go harder by one middle-sized manual page. This will not be mandatory-to-use.
Proposal
Updated by comments at 19.01.23
Add a new "function" type
template
, specialexpression
type for code blocks andinline
function forexpression
insertion totemplate
. As exampleshould work as
The most important difference between
func
andtemplate
is thatfunc
share variables withtemplate
, andtemplate
is only a template of actions to insert tofunc
.It seems like universal
try
should be added to builtin libraryWhy not use special keywords like try, catch and others?
The first reason is my personal reason - I wrote this sentence after monthly reading of other proposals on this topic which make me unhappy.
I selected Go as my primary language because of it's simplicity and human readability and i think new keywords will make it Java.
The second reason is that error handling problem is not error handling problem at all, it's a problem of template code which enrage developers. Now it's about errors, next we'll find some old-new places needs in template code problem solving.
Backward compatibility:
Full because no any go program in world use
template
keyword.Example code:
should work as
should work as
Cost of this proposal:
Tools to rework: compiler, vet, gopls
Compile time cost: grows with the size of the project, seems like generics cost
Runtime cost: no cost
Possible implementation:
The most stupid is read code for template and insert it on the next read, but it's not a go way.
I tried to write prototype as preprocessor but didn't finish it.
The text was updated successfully, but these errors were encountered: