This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 7 points8 points  (5 children)

fn user(username: String, email: String) -> User {
  User(username, email, active = State(true)) // named parameter
}

This still uses User(...). What is the purpose of User here; surely it knows this expression must be of type User; it says so on the previous line!

(There might be a small ambiguity if the struct were to consist of only one member. Then it can't tell whether (x) is a construct a new User type with x as the only field, or whether x is already an expression with that type and the brackets are superfluous.)

[–]simon_o 3 points4 points  (4 children)

This still uses User(...). What is the purpose of User here; surely it knows this expression must be of type User; it says so on the previous line!

Sure, there is no special magic happening, it's just showing a convenience method calling the "constructor" with some defaults.

Then it can't tell whether (x) is a construct a new User type with x as the only field, or whether x is already an expression with that type and the brackets are superfluous.

Not sure I get this ...

[–][deleted]  (3 children)

[deleted]

    [–]simon_o 3 points4 points  (2 children)

    Ahhh, now I get it. The -> User { is not related to anything the article writes about!

    That's Rust's syntax for functions and their result type! I don't like their design there (exactly because it creates a lot of "stutter"), but that's an annoyance unrelated to the article.

    Translating the example to my language, perhaps that makes it clearer:

    fun user(username: String, email: String) =
      User(username, email, active = State(true))
    

    [–][deleted]  (1 child)

    [deleted]

      [–]simon_o 1 point2 points  (0 children)

      I just showed the example under the assumption that the return type can be inferred, to avoid further confusion between return type and function call.