🦀old:
let foo: Option<i8> = Some(6);
🐍bold:
foo: Optional[int] = 6a realization inspired by seeing Dhall in practice

Hey! I'm Lemma, and I'm a chubby queer robot VTuber who both makes and plays games on stream! I also occasionally write short stories and tinker with other projects, so keep an eye out! See you around~
Chubbyposting and IRL NSFW alt: @cuddlebot
name-color: #39B366
let foo: Option<i8> = Some(6);
foo: Optional[int] = 6a realization inspired by seeing Dhall in practice
there is no type conversion. int is a subtype of Optional[int], you don't need to wrap it in Option::Some, you can tell it's something from the fact that it's something. hence the title.
You're right... for whatever reason, it wasn't obvious to me that: for all int, Optional[int] exists.
Does this work for any pure?
What happens with foo: Optional[Optional[int]] = None
Optional[T] is an alias for T | None, so Optional[Optional[T]] would be T | None | None which is the same thing
Rust does cause me pain in the amount of wrapping and unwrapping every single thing has to undergo. Also the Result and Option types need to be dealt with differently causes me to suffer
Python’s Union and Optional types are really nice
there are a lot of things about typescript that make me sad but structural typing for optional values is not one of them
thing is in rust you could just write let foo = Some(6_i8);
it's extremely rare that you actually need to specify types of anything and even if you do you can generally throw _ and .into() everywhere
i mean yeah it's a simplified example. the Dhall snippet I linked to is a struct with like a dozen fields that are mostly Optional and so need to be wrapped in Some
i find optionals to be annoying most of the time in rust, but at the same time it feels like the more people try to avoid the issues of optionals while keeping the upsides, the closer it seems like we’re getting to trying to reinvent null. idk that’s just my very uneducated programmer experience tho
the problem with null isn't that it exists, it's that anything could be null and you always have to remember to check. if you have a real type system that lets you specify where things can or cannot be null, null is less of a problem
Yeah and the closer a type system gets to strongly specifying “this might be null” and “this can never be null” the more it feels to me like it’s trying to reinvent optional, type systems are a flat circle
Yeah, I do wish sometimes that rust allowed implicit conversion between a type and Some(type), but I do get that it wants things to be explicit.
Julia has arbitrary Union types and teh equivalent to Optional[T] would bee Union{T,Nothing}, andf it works teh same way, hehe
const Optional{T} = Union{T,Nothing}
function add_with_defaults(x::Optional{T}, y::Optional{T}) where T<:Number
a::T = x === nothing ? zero(T) : x
b::T = y === nothing ? zero(T) : y
return a + b
end
add_with_defaults(x::Nothing, y::Nothing) = 0
I vaguely rememberf them having nominal optional types at one pointf but I am pretty sure they just scraped them, meowf.
I was pleasantly surprised there is a blanket impl for T -> Option<T> in Rust, tho saddened by a lack of T/E -> Result<T, E>:
yeah I figured that probably existed, will definitely fn foo(x: impl Into<Option<i8>>) next time I need to. unfortunately there is no impl From<&str> for Option<String> so you can only skip one of the two types of annoying boilerplate in that case
null | number where either null or number is valid?i32 + implicit coercion from T to ?TOption<i32> + Some(T)unions like typescript are really nice until you make a generic type and realise that to be safe you need to use "null | {some: T}" in case the user passed in a nullable type, or just hope the user doesn't. but you can't enforce a constraint on the type argument to say it's not allowed to be nullable so like.
every time I write null | {some: T}, I get sad and wish I had real optionals
rust Some(T) is usually not nice but nice for generic types and nested optionals
zig is kind of cool until you do ???T and then have to do @as(??T, @as(?T, null)) or something to set the innermost optional to null.