1

I am studying scala. For an practice, I am building a login page. To do this, I have been searched examples on web.

I found some interesting code like below.

  val loginForm = Form(
    tuple(
        "username"->nonEmptyText, 
        "password"->nonEmptyText
    )
  ) 

I think after '->', it has to be an reserved words, right? I wonder how many reserved word can be in this place.

Especially some type like password. Any reference or example will be greatly welcomed :D

2
  • It's informative to read over Predef.scala - github.com/scala/scala/blob/master/src/library/scala/… - where the -> method and a lot of other interesting stuff is defined. Commented Feb 24, 2014 at 2:43
  • There is no "tuple" keyword in Scala. There are only the TupleN defined names, where N is, prior to Scala 2.11, a small (-ish) integer. Commented Feb 24, 2014 at 2:45

1 Answer 1

4

-> is actually just an operator to make creating a Tuple2 (or Pair) object easier. See the hairy details, for "how" such an operator can work across different types.

The normal/predef meaning of the -> operator is such that x -> y is equivalent to Tuple2(x, y), where y is just an expression. The code can then use any expression (reserved words or not) that is valid in the context.

In Play, note that nonEmptyText is itself just a value and does not involve any reserved words. Likewise, tuple is just a method for Play; see Handling form submission.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I thought 'tuple' is spring keyword. but now I clearly understand :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.