2 ms·
As implied by other commenters, the problem is that Rust cannot currently abstract out a concrete return type without indirection of some sort. More formally, i
by Sharlin 9y ago
As implied by other commenters, the problem is that Rust cannot currently abstract out a concrete return type without indirection of some sort. More formally, it’s not possible to existentially quantify over a trait bound; in other words, to say that whatever the actual type that is returned, all the caller needs to know is that it implements a certain trait or traits.
Note that it’s exactly the same in other major statically-typed languages like Java or C++: there are no existential types without indirection (which in the case of Java and similar managed languages is implicit and mandatory).
Rust is in the progress of adding existential trait bounds in the form of the `impl trait` feature. It’s already available in nightly.
- the_mitsuhiko 9y ago> Note that it’s exactly the same in other major statically-typed languages like Java or C++ It's not just statically typed languages. Python also returns loads of special types from iterator functions, they are just usually not documented as being types: >>> type(itertools.chain([1, 2], [3, 4])) <class 'itertools.chain'>
- Sharlin 9y agoYes, I just meant that in a dynamically typed language you don’t have the problem of types leaking to signatures because there are no type annotations in the first place. And when there are, everything has indirection anyway.