Optional is useful in an obvious way in a statically typed language, but I'm wondering if it's also useful in dynamic languages. Usually there's no compiler telling you, "hey, you used this Optional<String> as a String", so you still have to discover your mistakes at runtime. I have a static typing background and from my perspective I can't see the benefit of an Optional type in a dynamic language.

  • One situation that made me wish it was there: what should first on an array return? If you get nil/null back, you don't know if that means there was a non-empty array starting with nil/null, or if it was an empty array.
    – Alexander
    Apr 20, 2020 at 15:50

3 Answers 3


That weakness is really no different than for all types in dynamic languages. You have to run it to find out what's in it. Options aren't really different in that regard.

Options are more than just a static annotation that a value may or may not be present. They have operations associated with them as well. Chains of mapped operations on options are one highly useful feature, that keeps you from having to check for failure after every step in the chain.

Even if you only immediately retrieved the value, it still would prod the programmer to check for Nothing the first time it ran and failed. Without an option, that prodding would only take place if it happened to contain null the first time you ran it. Many a null pointer dereference have stubbornly waited until the product was in the field.


I can talk for Objective-C.

Since dynamic typed languages bind late, checking for an "optional" (a non-value is represented with nil) is inherent: If you check for the type at runtime, you check for nil implicitly. Of course this is chainable. This is done inherently, too.

For static typed languages this extra concept is a benefit, because they have to take the type decision at compile time – a situation dynamic typed languages never have.


I think not. First issue is that dynamic type languages have different primitive then optional, it is null (JS, PHP), undefined (JS), none (Python). These primitives are values, so we can assign them like

const x = null

Second is that the whole standard library, structures given by the language use this primitive. Lets say we want to find item in an array in JS. We have arr.find(x => x === 'smth') and we can get or value or undefined. So if we use Optional, we would need to wrap every using of primitive null or undefined into Optional. Next thing are values in JSON, which is the standard format or receiving and sending the data in JS, we would need to convert nulls into Nones. In other words the whole language and libraries will constantly fight with us.

Thirdly - how you can know you have Optional. You have the same issue like with null, you never know what you have because its dynamic typed language, and nobody protects you from just using optional string as just string, and if you do lets say str.concat(str2), and str was optional you will get undefined is not a function, so just great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.