`Unit`返回关闭类型

时间:2015-08-29 02:35:58

标签: scala closures

鉴于以下结束:

SELECT c.*, count(s.curso_id) as count, SUM(IF(s.status = "aprobado", 1, 0)) AS count_approved , SUM(IF(s.status = "cupolleno", 1, 0)) AS count_cupolleno
, SUM(IF(s.status = "cancelado", 1, 0)) AS count_cancelado, SUM(IF(s.status = "noacion", 1, 0)) AS count_noacion, SUM(IF(s.status = "ama_de_casa", 1, 0)) AS count_ama_de_casa
, SUM(IF(s.status = "cliente_externo", 1, 0)) AS count_cliente_externo
FROM cursos_modulos AS c
LEFT JOIN subscriptions AS s ON s.curso_id = c.id
LEFT JOIN users AS u ON u.userID = s.user_id

为什么返回类型为scala> def foo(x: Int) { | def bar(y: Int) = x + y | bar(55) | } foo: (x: Int)Unit ?我原以为Unit

Int

1 个答案:

答案 0 :(得分:7)

您正在使用过程语法。即,方法声明后没有=,因此该方法将返回Unit。你想要的是:

scala> def foo(x: Int) = { 
     |     def bar(y: Int) = x + y
     |     bar(55)
     | }
foo: (x: Int)Int

您的示例中的bar(55)的值被丢弃,您可以通过编译器标记捕获:

$ scala -Ywarn-value-discard

scala> def foo(x: Int) { 
     |     def bar(y: Int) = x + y
     |     bar(55)
     | }
<console>:9: warning: discarded non-Unit value
           bar(55)
              ^
foo: (x: Int)Unit