List boilerplate

79 views
Skip to first unread message

Rich Oliver

unread,
Oct 19, 2012, 9:36:29 AM10/19/12
to scala-...@googlegroups.com
One common pattern I find myself using a lot is compile time lists built through inheritance. At the moment I'm using this trick from a Stack Overflow answer:

trait Base  {
      protected def contribute : List[String] = List("Beginning")
      val list = contribute.reverse
 }
 trait TrA extends Base
 { override protected def contribute = "A" :: super.contribute }
trait TrB extends TrA
{ override protected def contribute = "B" :: super.contribute } 
 trait TrC extends TrB
 { override protected def contribute = "C" :: super.contribute }

 val x = new TrC{}                        
 x.list             //> res0: List[String] = List(Beginning, A, B, C)

Do others use this and is it worthy of language support? Now obviously I can write my own type abbreviations, but I think its much better to use standardised abbreviations, than us all writing our own for the same thing. Also it seems odd that we keep having to type out "List[String]". Should we have more abbreviations. Common types that I'm always using are:

type ListF[T] =  scala.collection.immutable.List[Function0[T]] //I particularly use these as compile time inheritance lists
type ListS = scala.collection.immutable.List[String]
type ListFS = ListF[String]

Just on a side note, during development I often want a place holder function that takes no parameters, returns nothing and does nothing. Am I right that () => {} is the shortest way of doing this.
Rich
Reply all
Reply to author
Forward
0 new messages