2017-02-23 46 views
0

我知道這個問題很簡單,但因爲我是斯卡拉的新來者,我無法弄清楚。下面是我的示例代碼:錯誤的停止語句聲明後定義對象

package datastructure 

sealed trait List[+A] 
case object Nil extends List[Nothing] 
case class Cons[+A](head: A, tails: List[A]) extends List[A] 

object List { 
    def sum(ints: List[Int]): Int = ints match { 
    case Nil => 0 
    case Cons(x, xs) => x + sum(xs) 
    } 

    def product(ds: List[Double]): Double = ds match { 
    case Nil => 1.0 
    case Cons(x, xs) => x * product(xs) 
    } 
} 

var a: Int = 3 

我不知道,我總是在最後一行滿足follwing錯誤:

錯stop語句聲明

這裏是一個演示說明: enter image description here

+1

你不能在對象或類外聲明一個var。 – puhlen

+0

我正在研究scala工作表。所以斯卡拉仍然阻止我這樣做? –

回答

2

您試圖在類或對象之外聲明變量。

如果您正在使用Scala工作表,請刪除軟件包聲明。

package datastructure

相關問題