roma
roma

Reputation: 1580

Is it possible to split object definion in several files in scala?

I'm writing a facade to use CreateJS lib in scala, like this

object CreateJS
{
    sealed trait EventListener extends js.Object

    type EventHandler = js.Function

    class DisplayObject extends js.Object
    {
    }

    class Container extends DisplayObject
    {
        def addChild(item:DisplayObject):Unit = ???
        def update():Unit = ???
    }

    @JSName("createjs.Stage")
    class Stage(canvasId:String) extends Container with EventListener

    // TODO: Is enum done this way ???
    object MovieClip
    {
        @JSName("createjs.MovieClip")
        var INDEPENDENT:String = _
        @JSName("createjs.SINGLE_FRAME")
        var SINGLE_FRAME:String = _
        @JSName("createjs.SYNCHED")
        var SYNCHED:String = _
    }

    @JSName("createjs.MovieClip")
    class MovieClip(mode:js.String=MovieClip.INDEPENDENT,startPosition:js.Number=0, loop:js.Boolean=true, labels:js.Object = null) extends DisplayObject
    {

        def addEventListener(eventName:String, target:EventHandler):Unit = ???
        var timeline:TimeLine = _
        def gotoAndPlay(where:String):Unit = ???
    }

    class TimeLine extends js.Object
    {
        def addTween(tween:Tween):Unit = ???
    }

    @JSName("createjs.Shape")
    class Shape(graphics:Graphics = null) extends DisplayObject

    @JSName("createjs.Graphics")
    class Graphics extends js.Object
    {
        def beginFill(color:String):Graphics = ???
        def drawCircle(x:js.Number, y:js.Number, r:js.Number):Graphics = ???
    }

    @JSName("createjs.Ticker")
    object Ticker extends js.Object
    {
        def addEventListener(eventName:String, handler:EventHandler):Unit = ???
        def addEventListener(eventName:String, stage:Stage):Unit = ???
    }

    class Tween extends js.Object
    {
        def get(item:DisplayObject):Tween = ???
        def to(state:js.Dynamic, time:js.Number = 0):Tween = ???
    }

    @JSName("createjs.Tween")
    object Tween extends Tween


/*   Load queue */
    class Plugin extends js.Object

    @JSName("createjs.Sound")
    object Sound extends Plugin {
        def play(soundId:String):Unit = ???
    }

    @JSName("createjs.LoadQueue")
    class LoadQueue(useXHR:Boolean = true, basePath:String="", crossOrigin:String="") extends js.Object
    {
        def installPlugin(plugin:Plugin):Unit = ???

        def on(event:String, handler:EventHandler):Unit = ???

        def addEventListener(event:String, handler:EventHandler):Unit = ???

        def loadFile(params:js.Dynamic):Unit = ???

        def loadManifest(paramList:js.Array[js.Dynamic]):Unit = ???
    }


}

This file becomes just a large pile. In real createJS these are split in 4 files - easel, tween, sound and preload, each extending base createjs object. Is it possible I do this file separation in scala while keeping everything in CreateJS namespace?

(Maybe such port already exist?)

Upvotes: 2

Views: 935

Answers (1)

cchantep
cchantep

Reputation: 9168

You can split code in different trait, each one responsible for a coherent aspect (and gathering related functions), each trait in a separate file, then have the public singleton facade object exposing all.

For example with trait A implementing function foo and bar in file A.scala, and trait B with functions oof and rab in file B.scala, then facade object can be declared like object Facade extends A with B.

In this way, I would suggest to declare trait as package private, as they are there only to organise code: private[mypackage] trait A { ... }.

Upvotes: 4

Related Questions