Skip to content

Instantly share code, notes, and snippets.

@ClaireNeveu
Last active August 29, 2015 14:06
Show Gist options
  • Save ClaireNeveu/6b64e23085b84c1c155a to your computer and use it in GitHub Desktop.
Save ClaireNeveu/6b64e23085b84c1c155a to your computer and use it in GitHub Desktop.
Personal Scala Code Style

The standard indention level is 3 and indentation should be done with spaces.

if (true) {
   val foo = ...
   ...
}

Brackets should only be used when a block is required. If the block can be replaced by a single expression, the single expression is preferred.

The following forms for if statements are acceptable:

if (cond)
   expressionOne
else
   expressionTwo

if (cond) expressionOne else expressionTwo

The "has type" operator : is always preceded and followed by one space.

All type class definitions will take the following form:

In lower-dirs/module/TypeClass.scala:

package lower-modules.module

trait TypeClass[T] {
   def foo(t : T) : Int
   def bar : T
}

object Implicits {
   implicit class TypeClassOps[T : TypeClass](val t : T) extends AnyVal {
      def foo : Int = implicitly[TypeClass[T]].foo(t)
      def bar : T = implicitly[TypeClass[T]].bar
   }
}

In lower-dirs/package.scala:

package lower-modules

package object module {
   def foo[T : TypeClass](t : T) : Int = implicitly[TypeClass[T]].foo(t)
   def bar[T : TypeClass] : T = implicitly[TypeClass[T]].bar
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment