Instead of this:
numberString := // string that we know contains only digits
number, err := strconv.Atoi(numberString)
if err != nil {
panic(err)
}
fmt.Printf("Your number times two is %d\n", 2*number)
I would like to write this:
numberString := // string that we know contains only digits
fmt.Printf("Your number times two is %d\n", 2*must(strconv.Atoi(numberString)))
The semantics of must() are: evaluate the single expression inside, which returns N values (N>=1); if the Nth value is non-nil, pass the Nth value to panic(); otherwise, return the first N-1 values.
must() is especially useful in unit tests, where I find a 25% of my code is error handling for errors that should really never happen, and if they did, panic() is fine.
must() is a built-in function like "append"; it can be re-declared in user code. Thus, introducing this function would not create backward compatibility issues.
must() is inspired by regexp.MustCompile.
Thanks,
-Ken
Kenneth Duda
What prevents you from writing this function in your code?
Why does it need to be part of built-in functions?
See also https://github.com/golang/go/issues/20803#issuecomment-312302972.
What prevents you from writing this function in your code?
Why does it need to be part of built-in functions?
Because, we can not write this function only with golang compiler. How to define that xxx.Must() function? we can not do that right now.
We can write another compiler that compile that code with must() and output golang source code, or we can just change golang compiler.
Wouldn't it be possible to write a must function exactly as described if Go gained generics?
@tmthrgd
I have used the c#/java with some generics support.
I found that generics is not enough for this kind of problem.
generics can solve some problem
How do I write a function , and make it callable from another machine? How do I embed a png content in the finial binary memory, and serve it as a http response. How do I write a function that can works like golang builtin make? How do I write a function that can direct call some windows apis?
I think we need same simple way to program read and write golang code. May be some way to generate code on the fly and compile into the finial binary.
@bronze1man You could create a must function using generics something like this and it wouldn't require a language built-in:
func must<T>(arg T, err error) T {
if err != nil {
panic(err)
}
return arg
}
func must<T0, T1>(arg0 T0, arg1 T1, err error) (T0, T1) {
if err != nil {
panic(err)
}
return arg0, arg1
}
func must<T0, T1, T2>(arg0 T0, arg1 T1, arg2 T2, err error) (T0, T1, T2) {
if err != nil {
panic(err)
}
return arg0, arg1, arg2
}
// ... and so on ...
numberString := "0" // string that we know contains only digits
fmt.Printf("Your number times two is %d\n", 2*must(strconv.Atoi(numberString)))
@tmthrgd
This version of generics/override implement you said only solve little problems and make the language very complex. (For example, writing a tool that can read/write the language).I think this is the reason the golang writer do not want it.
I do not want it too. Because I need to change my tools (that can read/write the language) to accept the generics/override feature.
A lot of problem that generics need solved in java/c# have already been solved by go built stuff like map, reflect, unsafe.Pointer. And looks like those problems have been solved better(run faster).
But there are a lot problems that generics can not solve(like write a function , and make it callable from another machine). And tools that read/write the language can solve them.
So if golang support this version of generics.I may not upgrade to that version of golang in a long time.
I think I can write a function that can do compute at compile time, and output the right code base on input type, should be better solution to this kind of problem.
@bronze1man
make it callable from another machine
What do you mean by that and how it relates to this proposal?
Closing in favor of #15292 (generics) and #21161 (error handling).