带花括号的 Apply 方法 [英] Apply method with curly braces

查看:44
本文介绍了带花括号的 Apply 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么会这样:

  List(
   "string", "string2"
  )

但这不是吗?

  List{
   "string", "string2"
  }

编译错误:;'预期但找到,".

但是对于我自己的带有 apply 方法的对象:

But for my own object with apply method there:

object Dictionary {    
  ...    
  private[dictionary] def apply(words: List[Word]) = {
    ...
  }
}

Dictionary { // curly braces works fine
  List ( // but here, for List - I can Not use curly braces
    "hello", "hello2" 
  )
}

推荐答案

关于大括号的规范答案补充.

官方词汇表 有用地将块称为副作用的封装和结果值.

The official glossary helpfully calls a block an encapsulation of side effects and a result value.

最近对 ML 表达了很少有意见关于大括号及其非尖头表兄弟.

A few opinions were recently expressed on the ML about braces and their non-pointy cousins.

认为括号和大括号在某些情况下可以互换的想法可能没有帮助.

Perhaps it is unhelpful to think of parens and braces as somehow interchangeable in some contexts.

从表达式和块的角度考虑更容易,在这种情况下,函数参数可以是逗号分隔的表达式或块.

It is easier to think in terms of exprs and blocks, and in this case, function args can be either comma-separated exprs or a block.

这篇关于带花括号的 Apply 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆