Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming convention in Play Framework

Here is a part of Play Framework's source code:

package play.api.mvc
trait Results extends java.lang.Object {
  def $init$() : scala.Unit = { /* compiled code */ }

  def Async(promise : scala.concurrent.Future[play.api.mvc.Result]) : play.api.mvc.AsyncResult = { /* compiled code */ }
  val Ok : Results.this.Status = { /* compiled code */ }
  val Created : Results.this.Status = { /* compiled code */ }
  val Accepted : Results.this.Status = { /* compiled code */ }
//.......................

I wonder, why do they name val and def starting with the capital letters?

Furthermore, some classes and objects start with the small letters:

package views.html.play20

object book extends BaseScalaTemplate[play.api.templates.Html, play.templates.Format[play.api.templates.Html]] with play.api.templates.Template1[scala.Seq[scala.Predef.String], play.api.templates.Html] {
  def apply(pages : scala.Seq[scala.Predef.String]) : play.api.templates.Html = { /* compiled code */ }
  //.............
}

package views.html.play20

object manual extends play.templates.BaseScalaTemplate[play.api.templates.Html, play.templates.Format[play.api.templates.Html]] with play.api.templates.Template3[scala.Predef.String, scala.Option[scala.Predef.String], scala.Option[scala.Predef.String], play.api.templates.Html] {
  def apply(title : scala.Predef.String, main : scala.Option[scala.Predef.String], sidebar : scala.Option[scala.Predef.String]) : play.api.templates.Html = { /* compiled code */ }
 //............
}

package views
package object xml extends java.lang.Object {
}

1 Answers

As a disclaimer i'm not writing in Java nor using Play in projects.

As for objects. From OOP point of view first class object in Scala is just an object instantiated with a singleton pattern in Java (i'm not using Java, son correct me if i'm mistaken). So i can guess that Play team using lowercase singleton objects name because of Java compatibility and in Java variables are written with a first lowercase letter:

Tyme varName = new Type()

As defs/vals which starts with an upper case letter. For example Async, again Async with an upper case letter looks like a constructor of some data and you can do this through creating some case class Async, singleton object Async with an apply method or just make a function which takes an argument. In case of Async, you just need to note that this computation should be done asynchronously, you don't need an actual data type for this, just it's result: AsyncResult which is done as a case class.

Also this trick can be used in functional way of scala when you model everything with trait. Trait as itself cannot be instantiated, you can just write new Sometrait {}, which is not beautiful, but you can add a method for this:

trait Welcome {
  def welcome: String
}

def Welcome(name: String) = new Welcome {
  def welcome = s"Welcome! $name"
}

which is a more traditional way of making an object.

As for real reasons for this convention, i guess only someone from the Play team can answer.

like image 163
4lex1v Avatar answered May 09 '26 02:05

4lex1v



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!