This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Aggregating import statements

7 replies
DaveScala
Joined: 2011-03-18,
User offline. Last seen 1 year 21 weeks ago.

Is there a way to aggregate import statements under an object
and import it in the main file? Of course if several import modules
have the same
import statements the import statement should be executed only once.
So it is a sort of include statement but for compiled packages
instead of source code.

So is there a way in Scala to change this:
a.scala
====================
package a
import javafx.application.Application
import javafx.stage.Stage
import javafx.scene.paint.{Color, CycleMethod}
import javafx.scene.effect.BlendMode
import javafx.scene.shape.StrokeType

object Main extends App {

}
=========================

into this:

b.scala
=====================
package b
import object myjavafxmainappimports {
import javafx.application.Application
import javafx.stage.Stage
import javafx.scene.paint.{Color, CycleMethod}
import javafx.scene.effect.BlendMode
import javafx.scene.shape.StrokeType
}

import object somejavafxuse {
import javafx.application.Application
import javafx.stage.Stage
import sys.runtime
import java.math.{MathContext,RoundingMode}
import java.lang.management.{ManagementFactory, RuntimeMXBean,
MemoryMXBean, MemoryUsage}
import scala.collection.JavaConversions._
import io.Source
import collection.parallel.ForkJoinTasks.defaultForkJoinPool
import scala.annotation.tailrec
}

trait x {
def ... etc
object A {
apply

}
}

class T(n: Int){

}

===================

a.scala
===================
package a
import b._

object Main extends App {

}
=================

Paulo Siqueira
Joined: 2011-04-13,
User offline. Last seen 42 years 45 weeks ago.
Re: Aggregating import statements
I'm not sure if it is possible, but would this really be a good idea? Generally, one could look at the import statements of a class and have a rough idea of its responsibilities. Too many imports could mean too much responsibility, right?

[]s,

2011/6/21 Dave <dave.mahabiersing@hotmail.com>
Is there a way to aggregate import statements under an object
and import it in the main file? Of course if several import modules
have the same
import statements the import statement should be executed only once.
So it is  a sort of include statement but for compiled packages
instead of source code.

So is there a way in Scala to change this:
a.scala
====================
package a
import javafx.application.Application
import javafx.stage.Stage
import javafx.scene.paint.{Color, CycleMethod}
import javafx.scene.effect.BlendMode
import javafx.scene.shape.StrokeType

object Main extends App {



}
=========================

into this:

b.scala
=====================
package b
import object myjavafxmainappimports {
  import javafx.application.Application
  import javafx.stage.Stage
  import javafx.scene.paint.{Color, CycleMethod}
  import javafx.scene.effect.BlendMode
  import javafx.scene.shape.StrokeType
}

import object somejavafxuse {
  import javafx.application.Application
  import javafx.stage.Stage
  import sys.runtime
  import java.math.{MathContext,RoundingMode}
  import java.lang.management.{ManagementFactory, RuntimeMXBean,
MemoryMXBean, MemoryUsage}
  import scala.collection.JavaConversions._
  import io.Source
  import collection.parallel.ForkJoinTasks.defaultForkJoinPool
  import scala.annotation.tailrec
}


trait x {
  def ... etc
  object A {
      apply

  }
}

class T(n: Int){

}

===================



a.scala
===================
package a
import b._

object Main extends App {



}
=================



--
Paulo "JCranky" Siqueira
Visit my blog: http://www.jcranky.com/
Russ P.
Joined: 2009-01-31,
User offline. Last seen 1 year 26 weeks ago.
Re: Aggregating import statements
What you are suggesting looks a lot like the "export" statement that I asked about recently, which I think is a good idea. Yes, it can be abused, but so can many other good ideas (e.g., implicit conversions).

--Russ P.


On Tue, Jun 21, 2011 at 3:26 PM, Dave <dave.mahabiersing@hotmail.com> wrote:
Is there a way to aggregate import statements under an object
and import it in the main file? Of course if several import modules
have the same
import statements the import statement should be executed only once.
So it is  a sort of include statement but for compiled packages
instead of source code.

So is there a way in Scala to change this:
a.scala
====================
package a
import javafx.application.Application
import javafx.stage.Stage
import javafx.scene.paint.{Color, CycleMethod}
import javafx.scene.effect.BlendMode
import javafx.scene.shape.StrokeType

object Main extends App {



}
=========================

into this:

b.scala
=====================
package b
import object myjavafxmainappimports {
  import javafx.application.Application
  import javafx.stage.Stage
  import javafx.scene.paint.{Color, CycleMethod}
  import javafx.scene.effect.BlendMode
  import javafx.scene.shape.StrokeType
}

import object somejavafxuse {
  import javafx.application.Application
  import javafx.stage.Stage
  import sys.runtime
  import java.math.{MathContext,RoundingMode}
  import java.lang.management.{ManagementFactory, RuntimeMXBean,
MemoryMXBean, MemoryUsage}
  import scala.collection.JavaConversions._
  import io.Source
  import collection.parallel.ForkJoinTasks.defaultForkJoinPool
  import scala.annotation.tailrec
}


trait x {
  def ... etc
  object A {
      apply

  }
}

class T(n: Int){

}

===================



a.scala
===================
package a
import b._

object Main extends App {



}
=================



--
http://RussP.us
Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Aggregating import statements
In your convenience object, writeval ValueName = imported.ValueName(or def instead of val) for every value (object, val, etc.) you want to export, andtype TypeName = imported.TypeName for every type (class, trait, type member) you want to export.
Then importing the object's contents will give you access to the aliases.


On Tue, Jun 21, 2011 at 6:26 PM, Dave <dave.mahabiersing@hotmail.com> wrote:
Is there a way to aggregate import statements under an object
and import it in the main file? Of course if several import modules
have the same
import statements the import statement should be executed only once.
So it is  a sort of include statement but for compiled packages
instead of source code.

So is there a way in Scala to change this:
a.scala
====================
package a
import javafx.application.Application
import javafx.stage.Stage
import javafx.scene.paint.{Color, CycleMethod}
import javafx.scene.effect.BlendMode
import javafx.scene.shape.StrokeType

object Main extends App {



}
=========================

into this:

b.scala
=====================
package b
import object myjavafxmainappimports {
  import javafx.application.Application
  import javafx.stage.Stage
  import javafx.scene.paint.{Color, CycleMethod}
  import javafx.scene.effect.BlendMode
  import javafx.scene.shape.StrokeType
}

import object somejavafxuse {
  import javafx.application.Application
  import javafx.stage.Stage
  import sys.runtime
  import java.math.{MathContext,RoundingMode}
  import java.lang.management.{ManagementFactory, RuntimeMXBean,
MemoryMXBean, MemoryUsage}
  import scala.collection.JavaConversions._
  import io.Source
  import collection.parallel.ForkJoinTasks.defaultForkJoinPool
  import scala.annotation.tailrec
}


trait x {
  def ... etc
  object A {
      apply

  }
}

class T(n: Int){

}

===================



a.scala
===================
package a
import b._

object Main extends App {



}
=================

DaveScala
Joined: 2011-03-18,
User offline. Last seen 1 year 21 weeks ago.
Re: Aggregating import statements

I get:
error: not found: value imported
Should I import something else to make this work? It doesn't recognize
'imported' keyword.

@Paulo
Of course in my example it is better to do an import of a specific
aggregate instead of the whole package:
import b.myjavafxmainappimports._
instead of b._
but that is a responsibility of the programmer

but it was an illustration of 'no double imports'
@Naftoli:
Is this concept checking for double imports at the main application
side or is the standard import functionality already checking for
doubles?

Russ P.
Joined: 2009-01-31,
User offline. Last seen 1 year 26 weeks ago.
Re: Aggregating import statements
It shouldn't be anywhere that difficult.

--Russ P.

On Tue, Jun 21, 2011 at 11:02 PM, Naftoli Gugenheim <naftoligug@gmail.com> wrote:
In your convenience object, writeval ValueName = imported.ValueName (or def instead of val) for every value (object, val, etc.) you want to export, andtype TypeName = imported.TypeName for every type (class, trait, type member) you want to export.
Then importing the object's contents will give you access to the aliases.


On Tue, Jun 21, 2011 at 6:26 PM, Dave <dave.mahabiersing@hotmail.com> wrote:
Is there a way to aggregate import statements under an object
and import it in the main file? Of course if several import modules
have the same
import statements the import statement should be executed only once.
So it is  a sort of include statement but for compiled packages
instead of source code.

So is there a way in Scala to change this:
a.scala
====================
package a
import javafx.application.Application
import javafx.stage.Stage
import javafx.scene.paint.{Color, CycleMethod}
import javafx.scene.effect.BlendMode
import javafx.scene.shape.StrokeType

object Main extends App {



}
=========================

into this:

b.scala
=====================
package b
import object myjavafxmainappimports {
  import javafx.application.Application
  import javafx.stage.Stage
  import javafx.scene.paint.{Color, CycleMethod}
  import javafx.scene.effect.BlendMode
  import javafx.scene.shape.StrokeType
}

import object somejavafxuse {
  import javafx.application.Application
  import javafx.stage.Stage
  import sys.runtime
  import java.math.{MathContext,RoundingMode}
  import java.lang.management.{ManagementFactory, RuntimeMXBean,
MemoryMXBean, MemoryUsage}
  import scala.collection.JavaConversions._
  import io.Source
  import collection.parallel.ForkJoinTasks.defaultForkJoinPool
  import scala.annotation.tailrec
}


trait x {
  def ... etc
  object A {
      apply

  }
}

class T(n: Int){

}

===================



a.scala
===================
package a
import b._

object Main extends App {



}
=================




--
http://RussP.us
Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Aggregating import statements
Sorry for not being clear. imported is not a keyword, it was just an example name of a package (other names used were also examples). There's no magic here. val x = y defines x as an alias for y (assuming why doesn't change, in which case you would write def x = y). That's for terms (values). For types you define an alias with the type keyword. So you're just putting regular members in your own object that really exist elsewhere but you're defining aliases. Once you do that, anyone can import them.

On Wed, Jun 22, 2011 at 4:19 AM, Dave <dave.mahabiersing@hotmail.com> wrote:
I get:
error: not found: value imported
Should I import something else to make this work? It doesn't recognize
'imported' keyword.

@Paulo
Of course in my example it is better to do an import of a specific
aggregate instead of the whole package:
import b.myjavafxmainappimports._
instead of b._
but that is a responsibility of the programmer

but it was an illustration of 'no double imports'
@Naftoli:
Is this concept checking for double imports at the main application
side or is the standard import functionality already checking for
doubles?



DaveScala
Joined: 2011-03-18,
User offline. Last seen 1 year 21 weeks ago.
Re: Aggregating import statements

Okay thanks Naftoli.
I have that solution too, and it works for the Oracle's getting
started colorfulcircles example, but for removing 6 (=7 -1) simple
looking import statements from the main file I had to create around 24
extra lines (excluding comments) in the dependent file that required
some testing and checking the javafx api. It should only be a simple
refactoring operation.

Probably 'export object' is a better name and then it is imported with
'import b.myjavafxmainappimports._ '

Compare this:

export object myjavafxmainappimports {
import javafx.application.Application
import javafx.stage.Stage
import javafx.scene.paint.Color
import javafx.scene.paint.CycleMethod
import javafx.scene.effect.BlendMode
import javafx.scene.shape.StrokeType
import javafx.scene.paint.Stop
}

with this:

type Application = javafx.application.Application
object Application {
//static void launch(java.lang.Class<? extends Application>
appClass, java.lang.String[] args)
def launch(appClass: java.lang.Class[_ <:
javafx.application.Application], args: Array[String]) =
javafx.application.Application.launch(appClass, args)
}
type Stage = javafx.stage.Stage
type Color = javafx.scene.paint.Color
object Color {
//static Color web(java.lang.String color)
def web(color: String) = javafx.scene.paint.Color.web(color)
//static Color web(java.lang.String colorRawName, double
opacity)
def web(colorRawName: String, opacity: Double) =
javafx.scene.paint.Color.web(colorRawName, opacity)
//static Color BLACK
val BLACK = javafx.scene.paint.Color.BLACK
}
type CycleMethod = javafx.scene.paint.CycleMethod // enumeration
object CycleMethod {
//NO_CYCLE
val NO_CYCLE = javafx.scene.paint.CycleMethod.NO_CYCLE
}
type BlendMode = javafx.scene.effect.BlendMode // enumeration
object BlendMode {
//OVERLAY
val OVERLAY = javafx.scene.effect.BlendMode.OVERLAY
}
type StrokeType = javafx.scene.shape.StrokeType // enumeration
object StrokeType {
//OUTSIDE
val OUTSIDE = javafx.scene.shape.StrokeType.OUTSIDE
}
type Stop = javafx.scene.paint.Stop

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland