-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
When an interpreter type implementing an interface is used by the runtime, the runtime can extract its type and create new values using reflect, and call methods on it. The problem is that there will be no interpreted method counterpart in this case, which makes wrapper panic. Allow the String() method wrapper to always succeed and return an empty string if no interpreted method is present. This allows scripts to define custom flag.Value types on which the runtime internally instantiates values using reflect (see isZeroValue in Go src/flag/flag.go). This workaround could be generalized to all wrappers if necessary. At this moment, it is convenient to keep the default behavior of expecting instantiated interpreter methods, in order to catch interpreter bugs related to interfaces. Fixes #1276.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
) | ||
|
||
type customFlag struct{} | ||
|
||
func (cf customFlag) String() string { | ||
return "custom flag" | ||
} | ||
|
||
func (cf customFlag) Set(string) error { | ||
return nil | ||
} | ||
|
||
func main() { | ||
flag.Var(customFlag{}, "cf", "custom flag") | ||
flag.Parse() | ||
println("Hello, playground") | ||
} | ||
|
||
// Output: | ||
// Hello, playground |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.