rsw0x <anonym...@anonymous.com> wrote: > On Saturday, 20 February 2016 at 09:40:40 UTC, Tobias Müller > wrote: >> Yuxuan Shui <yshu...@gmail.com> wrote: >>> [...] >> >> In Rust that would be: >> >> let var : Option<i32> = ...; >> if let Some(var) = var { >> // You can use var here >> } >> >> It works for every enum (= tagged union), not just Option<T> >> >> Swift also has "if let". >> >> It's not much more verbose but more explicit. >> Changing the type of a variable based on static analysis is >> just advanced >> obfuscation. It hurts readability and the gain is questionable. >> At least it >> only works for nullable types. >> >> Tobi > > D has this too, but only for nullable types afaik. > > if(byte* ptr = someFunc()){ > //... > } > >
That's not quite the same as there are no non-nullable pointers in D. There's no guarantee from the type system that the byte* is not null and there are no compiler checks involved. It's a simple runtime check. OTOH in the examples in Kotlin/Rust the variable 'var' changes its type from 'int?' to plain 'int'. In Kotlin this is done with static analysis, in Rust with rebinding of the name. Tobi