Add a kind of function that takes a value

This fixes issue #1477 by checking the type before trying to cast. Also added a TypeError test.