Do I need a trailing semicolon to disambiguate this code?

前端 未结 3 1971
攒了一身酷
攒了一身酷 2021-01-20 09:52

If I omit the semicolon, this code doesn\'t compile.

def checkRadioButton(xml: DslBuilder): String => XmlTree = {
    val inputs = top(xml).\\\\*(hasLocal         


        
相关标签:
3条回答
  • 2021-01-20 10:20

    I’d write it like this instead:

    def checkRadioButton(xml: DslBuilder): String => XmlTree = {
        val inputs = top(xml).\\*(hasLocalNameX("input"));
        (buttonValue: String) => { // <-- changed position of {
          // code omitted
        }
    }
    
    0 讨论(0)
  • 2021-01-20 10:44

    Just add a second newline, which apparently is equivalent to the semicolon.

    Still, I'm not entirely happy with this, as it seems fragile.

    0 讨论(0)
  • 2021-01-20 10:47

    Here is a simplification, explanation, and beautification.

    Simplified,

    scala> def f: String => String = {
         | val i = 7
         | { (x: String) =>
         |   "y"
         | }
         | }
    <console>:9: error: Int(7) does not take parameters
           { (x: String) =>
           ^
    <console>:12: error: type mismatch;
     found   : Unit
     required: String => String
           }
           ^
    

    This fails because the newline after the 7 is not taken as a semicolon, for the reason that it might be a function application; you might want a DSL where the brace is on the next line. Here is the little nl in the syntax of an arg with braces.

    Newline handling is described in 1.2 of the spec; a few spots like this one, where a single nl is accepted, are mentioned at the end of the section.

    (Two newlines doesn't work, which is why that also fixes your problem.)

    Notice that a nl is not accepted in front of a paren, so the following works (though with only parens, you get only one expression for your function literal):

    scala> def g: String => String = {
         | val i = 7
         | ( (x: String) =>
         |   "y"
         | )
         | }
    g: String => String
    

    In fact, the best edit for the problem code is not more braces but fewer:

    scala> def f: String => String = {
         | val i = 7
         | x: String =>
         | "y"
         | }
    f: String => String
    

    The reason for this nice syntax is that your method body is already a block expression, and when the result expression of a block is a function literal, you can simplify.

    The type of x is also redundant.

    scala> def f: String => String = {
         | val i = 7
         | x =>
         | val a = "a"
         | val b = "b"
         | a + i + x + b
         | }
    f: String => String
    

    And not surprisingly:

    scala> def f: (String, Int) => String = {
         | val i = 7
         | (x, j) =>
         | x + (i + j)
         | }
    f: (String, Int) => String
    
    scala> f("bob",70)
    res0: String = bob77
    
    0 讨论(0)
提交回复
热议问题