Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Why does it arbitrarily choose length 5?

https://doc.rust-lang.org/core/array/fn.from_fn.html



In that example N is inferred from the assertion; observing the output has affected it.


Wow that is stupid. That is way too much assumption.


https://en.wikipedia.org/wiki/Hindley%E2%80%93Milner_type_sy...

It's a dream once you understand it. You don't need to understand the math, just the general concept of type unification, and that type inference can flow in "both directions", as opposed to in e.g. C++ where `auto` inference only works in one direction.

Rust is not exactly HM type inference, but it's something close to it. http://smallcultfollowing.com/babysteps/blog/2014/07/09/an-e...


No, it’s just the right amount, and makes perfect sense. This is the whole point of type inference: where there are multiple possibilities, determine types based on how you use them, and if you still have ambiguity after that, complain.

If you tried comparing it to something that wasn’t an array, or if you tried comparing it to multiple different-sized arrays, it would complain.


Sure, it's a lot of assumption, but the type system would prevent an invalid assumption from compiling (arrays are typed [T; N] where T is the type they hold and N is the number of elements), and you can always override it with a turbofish.


There is no assumption here, just type inference. If you want it to be more explicit, you could just decorate the declaration with a type.


Exactly 0 assumptions are made - how is that too much?


Is likely based on what you compare it to. For example, this passes as well

    let array = core::array::from_fn(|i| i);
    assert_eq!(array, [0, 1, 2, 3, 4, 5]);
The magic of type inference




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: