• 0 Posts
  • 7 Comments
Joined 1 year ago
cake
Cake day: June 27th, 2023

help-circle

  • The example even used unwrap_or_else where they should use unwrap_or. Then it uses std::i64::MIN as fallback value where they could use something like 0 that would be a better example and honestly make more sense there.

    let parsed_numbers = ["1", "not a number", "3"]
        .iter()
        .map(|n| n.parse().unwrap_or(0))
        .collect();
    
    // prints "[1, 0, 3]"
    println!("{:?}", parsed_numbers);
    

    Even without trimming this to something less convoluted, the same functionality (with different fallback value) could be written in more readable form.

    Obviously in the context of the page something like this would make way more sense:

    maybe_number.unwrap_or(0)
    

    Or perhaps more idiomatic version of the above:

    maybe_number.unwrap_or_default()
    


  • I don’t have much experience with TS, but in other strongly typed language it goes even further than string vs number.

    For example you can have two numbers Distance and TimeInSeconds and even though they are both numbers, the type system can make sure that you won’t do distance+time.

    It can also let you do distance/time and return Speed type.

    It will prevent many logical errors even though everything is technically a number.


  • Yeah it’s pretty amazing system all things considered. It’s kind of as if 8-bit home computer systems continued to evolve, but keep the same principles of being really closely tied to the HW and with very blurry line between kernel and user space. It radiates strong user ownership of the system. If you look at modern systems where you sometimes don’t even get superuser privileges (for better of worse) it’s quite a contrast.

    Which is why it reminds me of Emacs so much. You can mess with most of the internals, there’s no major separation between “Emacs-space” and userspace. There are these jokes about Emacs being OS, but it really does remind me of those early days of home computing where you could tinker with low level stuff and there were no guardrails or locks stopping you.