kitten
kitten copied to clipboard
Handling multiple return types and runtime casting
One last thing and I'll stop bothering you for a while ;-) I was playing around with Kitten and was wondering about how multiple return types can/will be handled. Looks like right now only a single type can be returned from a function?
The reason I ask is because I was working on a YAML DOM API for another static language and found it problematic in that an arbitrary YAML node can be either a Scalar, Sequence or Mapping. Unless you have pre-knowledge of the YAML you can't know which it will be. So one function can return any one of these. But worse, the end-developer ends up having to do a lot of casting to make use of the API, which sucks. IMO something is wrong when I have to tell a language something is an "Array" or "Hash" (for example) when the data it is translating already makes that clear.
If I weren’t trying to be clever, I would probably parse into a tree structure like in Haskell’s aeson
library:
type Value:
case object (HashMap<List<Char>, Owned<Value>>)
case array (List<Owned<Value>>)
case string (List<Char>)
case number (Float64)
case boolean (Bool)
case null
You could pattern-match on this to extract values, do some validation, and possibly convert it to a less dynamic structure:
match (some_json parse)
case object -> hash:
// value was an object, unpack & use hash
…
case array -> list:
// etc.
…
else:
…
Of course, the common vocab doesn’t actually have HashMap
or Owned
yet.