Created
October 29, 2011 19:45
-
-
Save missingfaktor/1324982 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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