2014-02-08 49 views
1

我需要組合Enumeration的一些值。斯卡拉枚舉和值組

下面是代碼:

object PostType extends Enumeration { 
    // Documents 
    val BOOKMARK = Value("bookmark") 
    val FILE = Value("file") 
    val NOTE = Value("note") 
    val WIKIDOC = Value("wikidoc") 

    …  
} 

object PostTypes { 
    type PostTypes = List[PostType.Value] 
    val DOCUMENTS : PostTypes = List(PostType.BOOKMARK, PostType.FILE, PostType.NOTE, PostType.WIKIDOC) 
    val QUESTIONS : PostTypes = List(PostType.QUESTION, PostType.QUICKPOLL, PostType.SURVEY) 
    val EVENT : PostTypes = List(PostType.EVENT) 
    … 
    val ALL : PostTypes = PostType.values.toList 
} 

有沒有更好的辦法? 下面是我看到的缺點:PostType.ValuePostTypes.PostTypes在客戶端代碼!

[更新]提高代碼兩者的幫助下回答

object PostType extends Enumeration { 
    // Documents 
    val Bookmark = Value("bookmark") 
    val File = Value("file") 
    val Note = Value("note") 
    val Wikidoc = Value("wikidoc") 
    … 
} 

object PostTypes { 
    import PostType._ 
    implicit def toList(pt: Value) = List(pt) 

    type PostTypes = List[Value] 
    val Documents = List(Bookmark, File, Note, Wikidoc) 
    val Questions = List(Question, Quickpoll, Survey) 
    val All = values.toList 
} 

[更新2]的改進另一堆

object PostType extends Enumeration { 
    type PostType = Value 
    type PostTypes = List[Value] 

    implicit def toList(pt: Value) = List(pt) 

    // Documents 
    val Bookmark = Value(1, "bookmark") 
    val File = Value(2, "file") 
    val Note = Value(3, "note") 
    val Wikidoc = Value(12, "wikidoc") 

    // Declare after Val to avoid runtime error 
    val Documents = List(Bookmark, File, Note, Wikidoc) 
    val Questions = List(Question, Quickpoll, Survey) 
    val All = values.toList.sorted 
} 

回答

0

這是最後的代碼,在其他答案的幫助下。 只需要import PostType._在客戶端代碼。

object PostType extends Enumeration { 
    type PostType = Value 
    type PostTypes = List[Value] 

    implicit def toList(pt: Value) = List(pt) 

    // Documents 
    val Bookmark = Value(1, "bookmark") 
    val File = Value(2, "file") 
    val Note = Value(3, "note") 
    val Wikidoc = Value(12, "wikidoc") 

    // Declare after Val to avoid runtime error 
    val Documents = List(Bookmark, File, Note, Wikidoc) 
    val Questions = List(Question, Quickpoll, Survey) 
    val All = values.toList.sorted 
} 
2

我認爲這是盡善盡美,除非你想使用替代枚舉實現(請參閱我的回答here)或只使用一些常量。

我可以建議的唯一的事情是定義一個類型別名type PostType = Value並用它來代替Value。我相信你知道通過導入<package>.PostType._你不必在你的枚舉值前加上PostType

終於PostTypes看起來有點像矯枉過正,在閱讀時很容易將它與PostType混淆。但這些只是小事。我使用和你一樣的方法,而且我沒有意識到更好。

+0

而是通過導入PostType._來省略前綴(導入PostType.Value似乎沒有做這項工作)? –

+0

好點@YannMoisan –

3

下面是一種不使用枚舉的方法,但希望實現您的目標。該示例顯示如何從String實例實例化PostType。如果String不匹配,則會引發MatchError

package rando 

object PostType { 
    val all = Document.all ++ Question.all 
    def fromString(s: String): PostType = Document.fromString.orElse(Question.fromString)(s) 
} 
sealed trait PostType 

object Document { 
    val all = Set(Bookmark, File, Note, Wikidoc) 
    val fromString: PartialFunction[String, Document] = { 
    case "bookmark" => Bookmark 
    case "file" => File 
    case "note" => Note 
    case "wikidoc" => Wikidoc 
    } 
} 
sealed trait Document extends PostType 
case object Bookmark extends Document 
case object File extends Document 
case object Note extends Document 
case object Wikidoc extends Document 

object Question { 
    val all = Set(SlowPoll, QuickPoll, Survey) 
    val fromString: PartialFunction[String, Question] = { 
    case "slowpoll" => SlowPoll 
    case "quickpoll" => QuickPoll 
    case "survey" => Survey 
    } 
} 
sealed trait Question extends PostType 
case object SlowPoll extends Question 
case object QuickPoll extends Question 
case object Survey extends Question 

object Example extends App { 
    println(PostType.fromString("bookmark").getClass) 
} 
+0

我需要從'String'實例化PostType(數據來自文本文件) –

+0

我更新了答案以從'String'實例化PostType。 –