zoukankan      html  css  js  c++  java
  • Scala curly braces & parenthesis

    今天上英文課, 因為我想記住這兩個單字curly braces & parenthesis

    你會看到一些代碼中curly braces & parenthesis是可以替換的?

    You may replace parenthesis with curly braces if, and only if, the method expects a single parameter.

    你可以把parenthesis替換成curly braces, 限制是只有一個參數的方法

    新詞 Infix Natation

    List(1,2,3) indexOf (2) //點(dot)在scala中是可以省略的
    List(1, 2, 3) indexOf 2 //有比較接近人類的語言嗎?

    Tuples

    ("key", 10)
    (("key", 10))
    "key" -> 10 //神奇的都一樣

    Function/Partial Function(偏函数,我還偏頭痛,這中文是怎取的阿)

    obj match {
        case pattern if guard => statements
        case pattern => statements
    }
    
    try {
    
    } catch {
        case pattern if guard => statements
        case pattern => statements
    } finally {
    
    }

    你不能把case statement拿出來寫在其他地方, 如果真要拿出來那就得聲明成Partial Function, 要注意到Function和Partial Function是完全不一樣的東西(參考我)

    curly braces可以包含很多statement, 而parenthesis只是一種expression

    //這是一個blocks of code
    {
    var x = 0 // declaration println("this is a statement") (x % 5) + 1 // expression (a % 2 == 0) // expression }

    但是你可以把curly braces放進parenthesis, Wow~

    ( { var x = 0; while (x < 2) { x += 1}; x } % 5) + 1 //答案是({2}% 5) + 1, 結果是3
    1 // literal
    (1) // expression
    {1} // block of code
    ({1}) // expression with a block of code
    {(1)} // block of code with an expression
    ({(1)}) // you get the drift(飄移?)...

    Where they( curly braces & parenthesis) are not interchangeable

     這只是一些現象而已, 未來想了解到交換意圖上的原因

  • 相关阅读:
    MyBatis学习(三)
    MyBatis学习(二)
    Linux(Ubuntu)下MySQL的安装与配置
    IO 流读取文件时候出现乱码 文件编码格式问题 怎么转换解决方法
    spring boot下MultipartHttpServletRequest如何提高上传文件大小的默认值
    Mybatis 批量插入时得到插入的id(mysql)
    对PDF的操作
    利用nginx进行集群部署
    Spring boot学习笔记之@SpringBootApplication注解
    git的使用命令
  • 原文地址:https://www.cnblogs.com/jHenry/p/3725956.html
Copyright © 2011-2022 走看看