Skip to content

RFC: Consider supporting function return type deduction #10448

Closed
@erickt

Description

@erickt

I had a brief chat on IRC today about adding support for deducing the function return type. I realize we decided against doing this a couple years ago, but I think it's worth revisiting in light of iterators. One of the challenges with our iterators is that the types of complex iterators are unwieldy. For example, here's the type of chaining two vecs together:

type T<'self> = std::iter::Chain<
    std::vec::VecIterator<'self, int>,
    std::vec::VecIterator<'self, int>
>;

fn main() {
    let x = [1,2,3];
    let y = [4,5,6];
    let iter: T = x.iter().chain(y.iter());
}

While we can generally rely on type inference inside a function, things get really messy once we try to return an iterator from a function. One way to avoid this complicated type is to box of the iterators inside a ~Iterator<int>, but according to @thestinger, this is ~1000x slower than with a monomorphized iterator. Having return type deduction would make it much easier to pass around iterators.

This would also be really handy for macros and syntax extensions. For example, we could convert the serialize.rs over to iterators without needing generators (#9145).

This has been proposed for (C++14)[http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3638.html].

@cmr had an argument against this. He felt that adding return type deduction would make it that much easier to break ABIs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions