News for hackers. Stuff that matters.
Re:Overhead of Returning Optional Values in Java and Rust by plmpsu on Saturday October 16, 2021 @01:18PM (#28889449)
I agree. Add null checking at compile time with something like checkerframework to get the best of both worlds.
Re:Overhead of Returning Optional Values in Java and Rust by brabel on Saturday October 16, 2021 @01:50PM (#28889813)
Exactly. I don't get why people complain about null-pointers in Java when all it takes to get rid of them is enabling static analysis that enforces checking for null at compile time... as that's exactly as safe as using Optional, and as Kotlin has shown, it's also possible to have a nicer interface (e.g. `something?.let { it.isNotNullHere() }`) with bare nulls... without incurring the cost of allocation or of declaring generic types everywhere.

Since I started using "safe" nulls with Ceylon, nearly a decade ago, later with Kotlin and TypeScript, I've been firmly in the camp of supporting nulls in the language instead of `Optional` or `Maybe`. Rust should've used them IMO... using `?` for error propagation instead is such a weird choice.

Re:Overhead of Returning Optional Values in Java and Rust by Zababa on Saturday October 16, 2021 @02:13PM (#28890016)
? is used for Result, and not for Option. For Option, you use .unwrap(). Supporting nulls in the language also means that everything else is going to be second-class, while sum types allows you to do pretty much everything you want.
Re:Overhead of Returning Optional Values in Java and Rust by brabel on Saturday October 16, 2021 @02:55PM (#28890399)
Looks like you have much to learn about Rust. Option can also use `?`

Example (from [1]):

    // Assume
    // fn halves_if_even(i: i32) -> Option<i32>

    fn do_the_thing(i: i32) -> Option<i32> {
        let i = halves_if_even(i)?;

        // use `i`
    }
Besides, the `?` is syntax sugar for the `try!` macro as explained in the `try!` macro docs [2], and has nothing to do with sum types, at least not directly.

Ceylon would be a closer case where nullable types are represented as sum types and not a special-case in the language (`String | Null` is the same as `String?`)... but Ceylon, as Rust, special-cased the `?` operator for "something"... in Ceylon, they are used for null-checks, and in Rust, for propagating errors via the `try!` macro, which is what my previous comment was about.

[1] https://stackoverflow.com/questions/42917566/what-is-this-qu...

[2] https://doc.rust-lang.org/std/macro.try.html

Re:Overhead of Returning Optional Values in Java and Rust by Zababa on Saturday October 16, 2021 @03:35PM (#28890750)
You're right, I'm not sure how I thought that it worked only with Result. Thanks for the correction.

> Ceylon would be a closer case where nullable types are represented as sum types and not a special-case in the language (`String | Null` is the same as `String?`).

Aren't those union types? My understanding is that they are different from sum types, as you usually have to declare the sum types beforehand, and sum types are collections of values, while union types are collections of types, and are often declared "on the spot". At least that's how they work in Typescript, I don't know much about Ceylon.

To go back to option types, people usually like them because there are well-known patterns to program with them (like map). Of course you can get the same safety with static flow analysis (Typescript does this), but I think part of it is ergonomics, influenced by functional programming. Though if there is an overhead like in Java, I'm not sure about their value.

Re:Overhead of Returning Optional Values in Java and Rust by brabel on Monday October 18, 2021 @07:35AM (#28904357)
I don't know if there's a meaningful difference between sum types and union types as both end up allowing a variable to be of one of a set of types.

Ceylon:

    alias StringOrInt = String | Integer;
    StringOrInt myValue = "foo";
    StringOrInt otherValue = 10;
Rust:

    enum StringOrInt {
        Str(String),
        Int(usize),
    }
    let my_value: StringOrInt = StringOrInt::Str("foo");
    let other_value: StringOrInt = StringOrInt::Int(10);
In Ceylon, you're right you don't need to name a "union", which IMO makes it much nicer, as I was able to use adhoc unions everywhere, e.g. inside streaming operations:

    [ for x in foos if (x.something()) "foo" else 10 ]
The above "automatically" has type `[String | Integer]`. In Rust (and Kotlin) I would need to name a type just for this.

Another disadvantage of Rust's approach is that the type variants are not types themselves. That's really annoying sometimes, e.g. you want to have a method that only handles one of the variants, but you can't (you need to pass the values the variant wraps separately, but that may be undesirable semantically as that would allow something that has no instance of the enum to also use the function).

Re:Overhead of Returning Optional Values in Java and Rust by Zababa on Tuesday October 19, 2021 @05:40AM (#28916391)
I think there is a difference. Sum types allows you to choose between values, union types allows you to choose between types. The values of the sum types can contain themselves types, but they don't have to. On the other hand, you have to declare sum types beforehand, and you don't have that with union types. The equivalent of sum types in Ceylon seems to be enumerated types, which from what I understand you also have to declare beforehand. The difference with Rust is that Ceylon also has union types (Scala is the same, Typescript has unions but lacks proper sum types).

As an interesting aside, Rust has what seem to be intersection types for trait bounds (fn toto<T: Trait1 + Trait2)(a: T)).

I'm still not sure about the tradeoffs between union/intersection types and sum/product types. I think the difference is that "set types" are structural, while algebraic types are nominal, which comes with the usual tradeoffs, but I'm not sure.

Re:Overhead of Returning Optional Values in Java and Rust by Measter on Sunday October 17, 2021 @08:29AM (#28895978)
The `?` operator doesn't desugar to the the `try!` macro, it uses the `Try` trait.

https://doc.rust-lang.org/std/ops/trait.Try.html

Re:Overhead of Returning Optional Values in Java and Rust by brabel on Monday October 18, 2021 @07:22AM (#28904294)
Ah, right... but as far as I know, it's the exact same behaviour, at least currently... reading the docs for the try macro, however, is what made me finally understand the behaviour of the `?` operator (e.g. how it can "magically" convert error types).
Re:Overhead of Returning Optional Values in Java and Rust by kangalioo on Saturday October 16, 2021 @03:05PM (#28890484)
(? works for both Option and Result, as does unwrap(). ? propagates to the caller, while unwrap() initiates a panic and unwinds the stack)
Re:Overhead of Returning Optional Values in Java and Rust by khuey on Saturday October 16, 2021 @02:54PM (#28890384)
? is used for Option. Just like Result, ? propagates the Err/None to your caller, and unwrap asserts that it's not present.
Re:Overhead of Returning Optional Values in Java and Rust by pjmlp on Sunday October 17, 2021 @02:22AM (#28894396)
Because otherwise how would they sell their favourite guest language?

It is like Google uses their pseudo Java dialect when selling Kotlin features, instead of comparing it with proper Java.

Re:Overhead of Returning Optional Values in Java and Rust by pkolaczk on Saturday October 16, 2021 @02:48PM (#28890339)
With Kotlin based approach to nulls you can't represent Option<Option<T>> values.
Re:Overhead of Returning Optional Values in Java and Rust by brabel on Saturday October 16, 2021 @03:00PM (#28890435)
And that's a good thing, no? I've never met a good use cases for double-wrapping values like this.
Re:Overhead of Returning Optional Values in Java and Rust by Zababa on Saturday October 16, 2021 @03:38PM (#28890764)
I'm not an expert on Kotlin but this reminds me of JS promises. The issue was that by not supporting Promise<Promise<T>>, promises were not monadic. It look like the same issue in Kotlin.
Re:Overhead of Returning Optional Values in Java and Rust by nemetroid on Saturday October 16, 2021 @04:13PM (#28891054)
I thought this was a good example: https://news.ycombinator.com/item?id=28746293