DEV Community

Discussion on: What's a useful programming language feature or concept that a lot of languages don't have?

Collapse
 
hoelzro profile image
Rob Hoelz

I don't know if I miss them all the time, but dependent types (found in Idris, among other languages) are very interesting. Learning Idris definitely had an influence on how I think about certain problems; for example, at work I needed to write some code to partition items in a user's cart into separate shipments. A post-condition of this code is that the total number of items across all of the returned shipments should be the same as the total number of items in the user's cart; in most languages, you would use rigorous testing to assert you'd done this, but in Idris, you can express this property in the type system itself!

Something I miss more often (also found in Idris, along with other ML-family languages) is type-directed search. If you don't know the name of a function, but you can describe what it does in a type, you can use that type to find the function you need. For example, let's say we don't know how to append vectors in Idris (vectors are like lists in other functional languages, only their length is part of their type). You can think of the type of append as being "take a vector of length N containing items of type A, and a vector of length M containing items of type A, and return a vector of length (N + M) containing items of type A". So we ask Idris to find such a function for us:

*Data/Vect> :search Vect n a -> Vect m a -> Vect (n + m) a

...and it gives us the answer!

*Data/Vect> :search Vect n a -> Vect m a -> Vect (n + m) a
= Data.Vect.(++) : Vect m elem -> Vect n elem -> Vect (m + n) elem
Append two vectors

> Data.Vect.merge : Ord elem => Vect n elem -> Vect m elem -> Vect (n + m) elem

Smalltalk has something similar in its method finder; we can ask it "which messages can I send to 'foo' that return 'FOO', and it'll tell us asUppercase will do this for us.

A random wacky feature in Scheme that I can't say I think about a lot (other than being just plain interesting and mind-bending) is continuations; they could merit their own post, but continuations are a way of encapsulating the "next step" a program would take. I recommend reading the Wikipedia article if that piqued your curiosity!