How can I express in Go a type that is "a list (strings or other such lists)"? Basically, a good "ol" tree is represented as infinitely nested list lists and something like a value (lines in this example) "
I'm looking for the simplest possible representation of an S-expression (which would itself be the simplest of AST), which in Python would look like this:
sexp1 = ["+", "x", "y", ["*", "10", "myVal"]] sexp2 = ["foo" "bar" "baz"] sexp3 = [ [ [["gooo"], "moo"] ], "too", ["yoo", "2"] ]
What type will all these expressions have in Go? Obviously, the [][]string does not work, since it does not work:
func makeSexp(parserName string, values ...[][]string) [][]string { return append([]string{parserName}, values...) }
(Compilation errors: 1. cannot use values (type [][][]string) as type []string in append , 2. cannot use append([]string literal, values...) (type []string) as type [][]string in return argument .)
... while the fully untyped version is working ( but I do not want to completely abandon type safety! ):
func makeSexp(parserName string, values ...interface{}) interface{} { return append([]interface{}{parserName}, values...) }
types algebraic-data-types go abstract-syntax-tree
Neuronq
source share