Skip to content

Document where clauses. #22123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2015
Merged

Conversation

steveklabnik
Copy link
Member

Closes #21859.

@rust-highfive
Copy link
Contributor

r? @huonw

(rust_highfive has picked a reviewer for you, use r? to override)

@huonw
Copy link
Member

huonw commented Feb 9, 2015

It might be worth mentioning that where clauses add functionality not available elsewhere, e.g.

fn foo<T>(x: T) where i32: Trait<T> {
    1i32.method(x)
}

which is the main motivation for them, rather than just syntactic nicety.

use std::fmt::Debug;

fn bar<T, K>(x: T, y: K)
where T: Clone,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a set style for this? The most common I've seen is:

fn bar<T, K>(x: T, y: K)
where T: Clone, K: Clone + Debug {
     ..
}

and/or

fn bar<T, K>(x: T, y: K)
where T: Clone, 
      K: Clone + Debug {
     ..
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was also asked at http://internals.rust-lang.org/t/multiple-line-fn-and-impl-declarations/1106 but no one responded. His preference was 2 tab indentation for where.

fn bar<T, K>(x: T, y: K)
        where T: Clone, 
    K: Clone + Debug {
     ..
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we don't have one so I did what I'd do, and figured someone would mention it 😉

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen this pattern floating around a little also:

fn bar<T, K>(x: T, y: K)
    where
        T: Clone,
        K: Clone + Debug,
{
    ..
}

I find it nicely readable for situations where there are multiple types and many bounds, easy to refactor and plays nice with auto-indent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also recall seeing this somewhere in the std src:

fn bar<T, K>(x: T, y: K) where
    T: Clone,
    K: Clone + Debug {
    ..
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mitchmindtree The last one looks great visually, especially if one squints :). But is just begs for { to be on the next line for such multiline case. Also, what about the result:

fn bar<T, K>(x: T, y: K) 
    -> Result<bool, String> 
where
    T: Clone,
    K: Clone + Debug 
{
    ..
}

@steveklabnik
Copy link
Member Author

@huonw can you give a bigger example of that? I've never seen that before.

@huonw
Copy link
Member

huonw commented Feb 10, 2015

trait ConvertTo<Output> {
    fn convert(&self) -> Output;
}

impl ConvertTo<i64> for i32 {
    fn convert(&self) -> i64 { *self as i32 }
}

// can be called with T == i32
fn normal<T: ConvertTo<i64>>(x: &T) -> i64 {
    x.convert()
}

// can be called with T == i64
fn inverse<T>() -> T
        // this is using ConvertTo as if it were "ConvertFrom<i32>"
        where i32: ConvertTo<T> {
    1i32.convert()
}

@oli-obk
Copy link
Contributor

oli-obk commented Feb 10, 2015

another use-case and pretty explanation on stackoverflow: http://stackoverflow.com/questions/28405621/what-is-the-syntax-and-semantics-of-the-where-keyword

@steveklabnik
Copy link
Member Author

Added. The only thing left is the style question.


This flexibility can add clarity in complex situations.

`where` is also more powerful than the simpler syntax. For example:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this could be expanded to be specific about what is happening: where clauses allow bounds where the left-hand side is an arbitrary type, not just a plain type parameter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

@steveklabnik
Copy link
Member Author

@huonw r?

@huonw
Copy link
Member

huonw commented Feb 17, 2015

@bors r+ faf0 rollup

alexcrichton added a commit to alexcrichton/rust that referenced this pull request Feb 17, 2015
@huonw huonw merged commit faf0f5b into rust-lang:master Feb 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

document where keyword
8 participants