KSP-KOS / KSLib

The standard library and examples for the Kerboscript language
MIT License
120 stars 40 forks source link

Add lib_get_type variable type inference #94

Closed gisikw closed 8 years ago

gisikw commented 9 years ago

Another fun library, variable type inference!

get_type(5) => "NUMBER"
get_type("5") => "STRING"
get_type(list()) => "LIST"
get_type(vessel("targetVessel")) => "SHIP"
... etc

Useful for type-checking in other functions in order to fail gently when passed invalid arguments, or to allow functions to operate on both collections and single items.

TDW89 commented 9 years ago

This is really cool thanks. Just a couple of issues before i can pull it. Vectors and Directions are V and R respectively after splitting, as it stands both of these types throw errors when you get_type() on them. Only other thing, which might not be easily accomplished, would be to have some protection against missing or new types. (No idea if that could be done, if not don’t worry about it).

Last thing is there a reason for the ")". ? of is it just left over from previous edits of the script?

gisikw commented 9 years ago

Thanks, I'll take a look at vectors and directions shortly. As far as detecting new types, unfortunately I can't really think of a way to make that work. If I can come up with something, I'll gladly PR it later though.

The ")". is a workaround for a current KOS bug, which I've reported here: https://github.com/KSP-KOS/KOS/issues/1285

gisikw commented 9 years ago

Oooh, actually, thinking about detecting new types, it revealed another bug: get_type("LIST") -> "LIST". Thinking on it more, I might be able to compare the lex value dumped with the input, to see if there's a difference, which would fix that, but also allow detection of non-string, non-recognized types.

gisikw commented 8 years ago

It's likely that this is going to be handled shortly in newer version of kOS once primitive types support suffixes, so I'm going to go ahead and close this PR for the time being.