Re:A JavaScript parser and interpreter written in Go
by jessedhillon on Thursday November 28, 2013 @04:53AM
(#6813851)
The alternative is a data structure which has N-1 empty, yet allocated, slots (N is the number of possible types.) instead you can have one field indicating the type of the variable and then your code can switch which field it accedes based off that indication. Total size is one int plus size of largest type represented.
[ Parent
| on HN ]
Re:A JavaScript parser and interpreter written in Go
by 1631- on Thursday November 28, 2013 @06:58AM
(#6814178)
You could potentially also have an interface that defined methods for all the basic types `interface { AsString() (string, error), etc. }` and then each basic type implements this interface and returns an error if it can't/shouldn't be represented as the requested type. A type-switch could give you the same info you get from the tag and you don't pay for type-assertions