2012-04-27 46 views
0

我目前放在一起下面的代碼,它識別元素,據稱其添加到組,但是當我打印出來的設置集充滿了功能Scala中的對象上訪問一組

class PropositionOrderer extends Identity{ 
    var Names = SortedSet[Name] _ 
    override def variable = { 
     _ match { 
     case name => 
      Names+(name) 
      Variable(name) 
     } 
    } 
} 

我想然後調用它的一個命題,並獲得名字的排序列表中的命題

type Names = SortedSet[Name] 
    val Names = SortedSet[Name] _ 
    def vars: Proposition => Names = 
    { 
    case p => 
     val prop = new PropositionOrderer 
     prop.visit(p) 
     println(prop.Names) 
     //this just to fit the return definition 
     Names("Dan","Web") 
    } 

如果我回到prop.Names它說,我返回了錯誤類型的對象。有任何想法嗎?

回答

3

這裏有幾個問題。我會列舉一些。糾正這些應該讓你走上正軌。

首先,你在兩種不同的方式,這是不行的定義Names。它看起來像你意味着它是一個類型,因此與堅持。

type Names = SortedSet[Name] // this defines a new type called `Names` 
val Names = ...    // this defines a variable called `Names` 

接下來,如果要定義一個新的空SortedSet,語法如下。 (請注意,變量名應該總是小寫。大寫是留給類型名稱。)

val names = SortedSet[Name]() // `names` is a new `SortedSet` 
val Names = SortedSet[Name] _ // `Names` is a function that takes multiple `Name` arguments and constructs a `SortedSet` 

第三,如果你想要的東西添加到Set,你必須使用+=,否則現有的一組不更改。

var names = SortedSet[String]() 
names + "test" // `names` is unchanged 
names += "test" // `names` is updated