Skip to content

Instantly share code, notes, and snippets.

@missingfaktor
Created October 29, 2011 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save missingfaktor/1324982 to your computer and use it in GitHub Desktop.
Save missingfaktor/1324982 to your computer and use it in GitHub Desktop.
// As Daniel said here[http://twitter.com/#!/djspiewak/status/129588554723639296], Scala's traits can be used as
// polymorphic modules. I was saying the same thing the other day.
// objects (both regular and the ones declared with 'object' keyword) serve as modules. If you want parametrized
// modules, trait + object combination should be used. A made up example below:
// This is our parametrized module.
trait PhysicsEngine {
// parameters
type P <: Planet
def coeffOfGravity: Double
// functions, values, classes, type classes, and all other sorts of stuff goes here.
}
// Some concretizations:
object MoonPhysicsEngine extends PhysicsEngine {
type P = Moon
def coeffOfGravity = 1.63
}
object EarthPhysicsEngine extends PhysicsEngine {
type P = Earth
def coeffOfGravity = 9.8
}
// At use site:
import MoonPhysicsEngine._
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment