packages, are kinda use interface{} too often?
I take parser.ParseFile (in go/parser package) as an example, which was
recently used by myself. The function signature is:
func ParseFile(fset *token.FileSet, filename string, src interface{}, mode
Mode) (f *ast.File, err error)
The src argument is defined with type interface{}. From the document, its
type could be string, []byte, or io.Reader.
Here, the largest problem is that the compile cannot help anything with
assuring src is of one of the above three types. In case, io.Reader
changes, or some change in the caller side that makes the implementation
doesn't satifiy io.Reader, the compler will never complain. We have to find
the problem on runtime(testcases or online service).
I think we can make it much better.
One proposal is to introduce the union of types. The grammar is like this
func ParseFile(fset *token.FileSet, filename string, src
string|[]byte|io.Reader, mode Mode) (f *ast.File, err error)
The compiler will check the following things:
- In the caller side, the value given to src shoud be assignable to
either of the type.
- Inside the function, src can be only asserted as one of the types or a
type one of the delcared type can be converted to.
I'm open with details like
- The final grammar other than type | type | ...
- one can define a named type like this: type SourceType
string|[]byte|io.Reader
This can also be considered as a better alternative to function
overloading, which is not introduced in Go.
Any discussion is welcome!
Thanks!
--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.