سؤال

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

هل كانت مفيدة؟

المحلول

-> 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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top