Type() Complaints
Posted: 20 May 2014, 22:14
I experimented with Type() some today, with the following results:
Most of this is what I expected.
I consider some of it wrong:
* Type() returns "Object" rather than the class for instances of user defined classes, even though the interpreter stored the type information in the "__Class" field.
* Type() returns "Object" and doesn't store anything in the "__Class" field for a exception.
Some of it is inconsistent:
* "Func" objects do not have a "Object" suffix like other objects.
Some of it is merely unfortunate:
* boolean values are still type-punned as integers
* arrays and dictionaries are still considered the same type
I hope v2 will be taken as a opportunity to clean up the type system.
I may have missed some built-in types (and therefore problems with Type()), since they aren't listed anywhere I can find in the documentation.
If you need a example of a situation where getting this right matters, imagine writing code to traverse a tree data structure stored as a array of arrays, with values of interest inside (as Lisp does with lists). It's vital to be able to tell whether a element is a "atom" or a "list" to descend into. I can't currently use Type() to do that, since it might mistake dictionaries, objects, or exceptions for arrays.
In general, though, it's important to be able to know the type of the data you're working with.
I hope these problems with Type(), and any I missed, will be fixed. It would be nice to have Type() backported to v1, assuming it works well.
Code: Select all
Type("") => String
Type("test") => String
Type(0) => Integer
Type(0.5) => Float
Type(1.0) => Float
Type(True) => Integer
Type([1, 2]) => Object
Type({foo: "5", bar: "8"}) => Object
Type(UserDefinedInstance) => Object
UserDefinedInstance.__Class => UserDefinedClass
Type(RegExMatchInstance) => RegExMatchObject
Type(FuncInstance) => Func
Type(FileInstance) => FileObject
Type(ComInstance) => ComObject
Type(ExceptionInstance) => Object
ExceptionInstance.__Class => ""
I consider some of it wrong:
* Type() returns "Object" rather than the class for instances of user defined classes, even though the interpreter stored the type information in the "__Class" field.
* Type() returns "Object" and doesn't store anything in the "__Class" field for a exception.
Some of it is inconsistent:
* "Func" objects do not have a "Object" suffix like other objects.
Some of it is merely unfortunate:
* boolean values are still type-punned as integers
* arrays and dictionaries are still considered the same type
I hope v2 will be taken as a opportunity to clean up the type system.
I may have missed some built-in types (and therefore problems with Type()), since they aren't listed anywhere I can find in the documentation.
If you need a example of a situation where getting this right matters, imagine writing code to traverse a tree data structure stored as a array of arrays, with values of interest inside (as Lisp does with lists). It's vital to be able to tell whether a element is a "atom" or a "list" to descend into. I can't currently use Type() to do that, since it might mistake dictionaries, objects, or exceptions for arrays.
In general, though, it's important to be able to know the type of the data you're working with.
I hope these problems with Type(), and any I missed, will be fixed. It would be nice to have Type() backported to v1, assuming it works well.