Posts tagged with types

Some experiments with natural numbers in Scala without type class wizardry

November 14th, 2011

Scala doesn’t have dependent types but I wanted to see if I could wrassle up something that would approximate it for non-negative natural numbers. I’ve posted my results on GitHub.

Types in Python

September 22nd, 2010

There are many great things going for Python but I find its type system to be rather wonky. Most people will direct you to use the type() function to determine types:

>>> type([])
<type 'list'>
>>> type([]) == type(list)
False
>>> type([]) == type(list())
True
>>> type([]).__name__ == 'list'
True

However, in everyday use isinstance() is much more useful:

>>> isinstance([], list)
True
>>> isinstance({}, dict)
True
>>> isinstance('', str)
True
>>> isinstance(u'', unicode)
True
>>> isinstance(True, bool)
True
>>> isinstance(0, int)
True
>>> isinstance(0.0, float)
True

And yes, str and unicode are different types. Some have proposed various complicated approaches to deal with duck-typing and the like, but I’m happy with isinstance(). If you need more information, I suggest checking out the types section of the Python documentation or using the types modules. Finally, Shalabh Chaturvedi has written a nice little guide called Python Types and Objects.

Javascript Type Gotchas

May 1st, 2010
typeof null // -> object
null instanceof null // -> TypeError: Can't use instanceof on a non-object.
true instanceof Boolean // -> false
true == new Boolean(true) // -> true
true === new Boolean(true) // -> false

Needless to say, this is very annoying if you’re trying to be be careful with your types and just goes to show that not everything is an object in Javascript. Come back soon for my attempt at a solution.