Modules?

279 views
Skip to first unread message

BobLondon

unread,
Nov 3, 2011, 6:54:16 AM11/3/11
to scala-debate
I've been taking a look at Scala and liking some but not all of what I
see. However, I was surprised to see Scala as a "scalable language"
but without any obvious support for modules in the language. Am I
missing something? Why would you create a new language without support
for modules?

Bob

Tony Morris

unread,
Nov 3, 2011, 6:55:22 AM11/3/11
to scala-...@googlegroups.com
Scala has modules. See the object keyword.

--
Tony Morris
http://tmorris.net/


Miles Sabin

unread,
Nov 3, 2011, 7:06:45 AM11/3/11
to BobLondon, scala-debate

Scala objects and path dependent types encode ML-style modules,

trait Signature {
type T
def succ(t : T) : T
def zero : T
}

object IntStructure extends Signature {
type T = Int
def succ(t : T) = t+1
def zero = 0
}

object StringStructure extends Signature {
type T = String
def succ(t : T) = t+"*"
def zero = ""
}

Sample REPL session,

scala> import StringStructure._
import StringStructure._

scala> val z = zero
z: java.lang.String = ""

scala> val one = succ(z)
one: java.lang.String = *

Cheers,


Miles

--
Miles Sabin
tel: +44 7813 944 528
gtalk: mi...@milessabin.com
skype: milessabin
http://www.chuusai.com/
http://twitter.com/milessabin

Paul Brauner

unread,
Nov 3, 2011, 7:08:21 AM11/3/11
to tmo...@tmorris.net, scala-...@googlegroups.com
Also see http://www.mpi-sws.org/~rossberg/ for a comparison between
scala's objects and ML modules.

Paul Brauner

unread,
Nov 3, 2011, 7:13:32 AM11/3/11
to Miles Sabin, BobLondon, scala-debate
To be precise, Scala goes beyond ML modules by providing first class
ML modules (as in Moscow ML and OCaml). It also has (to some extent)
first class functors.

Paul

Bob London

unread,
Nov 3, 2011, 7:28:22 AM11/3/11
to scala-debate
> ----- Original Message -----
> From: Miles Sabin
> On Thu, Nov 3, 2011 at 10:54 AM, BobLondon <bobl...@mail.com> wrote:
> Scala objects and path dependent types encode ML-style modules,

Seems like a different kind of module to what I'm thinking of. This looks like just a way to structure code.

I'm thinking of versioned deployable artifacts. Plus mechanisms like Ruby gems or I guess Maven/OSGi/Jigsaw in Java. A mechanism to declare the dependencies of code.

Bob

Miles Sabin

unread,
Nov 3, 2011, 7:33:24 AM11/3/11
to Paul Brauner, tmo...@tmorris.net, scala-...@googlegroups.com
On Thu, Nov 3, 2011 at 11:08 AM, Paul Brauner <polu...@gmail.com> wrote:
> Also see http://www.mpi-sws.org/~rossberg/ for a comparison between
> scala's objects and ML modules.

Thanks for the pointer. Do you mean the discussion in the "Related
Work" section of the "Mixin' up the ML Module System" paper?

Marius Danciu

unread,
Nov 3, 2011, 7:38:22 AM11/3/11
to Bob London, scala-debate
That's really beyond a language scope. These are really libraries more or less "standard". I believe one can use OSGi in Scala ... although OSGi sucks big time. Any .jar can impersonate a (hot)-deploy-able module with a bit of classloaders help. So what is your expectation from Scala language really ?

Marius

Guillaume Yziquel

unread,
Nov 3, 2011, 7:39:13 AM11/3/11
to Bob London, scala-debate

Well, in OCaml, "versioned deployable artifacts" and "mechanisms to
declare the dependencies of code" can be done by using first-class
modules (not only a way to structure code, but also a runtime value
analog to a C struct). You'd dynamically load a module satisfying a
given module signature, and you'd pass the module around in your code as
a first-class runtime value. Some kind of "plugin".

This is not really a problem with ML-style modules, except that it
indeed was intended more as a way to structure code as opposed to any
kind of OSGi replacement.

Now, I do not see why you wouldn't be able to use the OSGi Java
machinery from Scala itself.

--
Guillaume Yziquel

Paul Butcher

unread,
Nov 3, 2011, 7:41:52 AM11/3/11
to Bob London, scala-debate


Most Scala libraries use Maven or Ivy (which interoperate pretty well). Sbt provides particularly good support for Ivy. Is there anything that Maven/Ivy don't give you that you think would justify a Scala-specific solution?

--
paul.butcher->msgCount++

Snetterton, Castle Combe, Cadwell Park...
Who says I have a one track mind?

http://www.paulbutcher.com/
LinkedIn: http://www.linkedin.com/in/paulbutcher
MSN: pa...@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher

Paul Brauner

unread,
Nov 3, 2011, 7:44:40 AM11/3/11
to Miles Sabin, tmo...@tmorris.net, scala-...@googlegroups.com
On Thu, Nov 3, 2011 at 12:33, Miles Sabin <mi...@milessabin.com> wrote:
> On Thu, Nov 3, 2011 at 11:08 AM, Paul Brauner <polu...@gmail.com> wrote:
>> Also see http://www.mpi-sws.org/~rossberg/ for a comparison between
>> scala's objects and ML modules.
>
> Thanks for the pointer. Do you mean the discussion in the "Related
> Work" section of the "Mixin' up the ML Module System" paper?

Yes. It's not much but I think it's up to date (the paper is very
recent and I know from Andreas that he updated it to take Scala's last
developments into account).

Paul

Kevin Wright

unread,
Nov 3, 2011, 8:32:38 AM11/3/11
to Paul Brauner, Miles Sabin, tmo...@tmorris.net, scala-...@googlegroups.com
For build-time management of "modules", Maven artefacts are the de-facto Java standard.  You don't have to use Maven as a build tool though, Ant, SBY, Ivy, and others are all capable of working with Maven repositories.

If you want want to manage, load, unload, etc. versioned and encapsulated units of functionality *at run time* then it's really a platform problem, not a language one.  Scala isn't responsible for isolating classloaders and loading bytecode, the Java platform is.

Having said that, Java does have a solution - in the form of OSGi.  We also have a rather nice wrapper/DSL around OSGi provided via the scalamodules project [1]

Somewhere in the middle is the Jigsaw project.  Jigsaw is much less intrusive that OSGi, and is intended to work at both build and run time.  You can expect to see a 1-to-1 correspondance between Jigsaw modules and OS-native equivalents (such as deb packages in Ubuntu).  You'll also see the Java standard library being carved up by Jigsaw, and offering a much smaller initial footprint.

If you need something now, and it absolutely has to be at runtime with dynamic loading/unloading, then use OSGi+scalamodules.  If this was just asking for your own curiosity, then Scala will offer full support for Jigsaw once it's released with Java 8.  Even if we don't get a nice Scala wrapper in the core library, you can be quite sure that *someone* will be keen to do this as a 3rd party offering.

Bob London

unread,
Nov 3, 2011, 9:39:49 AM11/3/11
to Marius Danciu, scala-debate
> ----- Original Message -----
> From: Marius Danciu
> That's really beyond a language scope. These are really libraries more or
> less "standard". I believe one can use OSGi in Scala ... although OSGi
> sucks big time. Any .jar can impersonate a (hot)-deploy-able module with a
> bit of classloaders help. So what is your expectation from Scala language
> really ?

Its just a sense that classes/objects are too low level a unit to be thinking in terms of. And Maven/OSGi type solutions feel like tacking the extra data on after the fact rather than building it in the base unit you produce day-to-day. Why should packaging and deployment be something separate to development? Why not compile modules rather than classes?

All, thanks for the other links, which are useful.

Bob

Naftoli Gugenheim

unread,
Nov 3, 2011, 8:48:32 PM11/3/11
to Kevin Wright, Paul Brauner, Miles Sabin, tmo...@tmorris.net, scala-...@googlegroups.com
Also there's https://github.com/marcusatbang/Hooks --- anyone have opinions on it?

Naftoli Gugenheim

unread,
Nov 3, 2011, 8:50:08 PM11/3/11
to Bob London, Marius Danciu, scala-debate
It's not separate from development, but that doesn't mean it's part of the language per se. I believe the Typesafe Stack includes sbt. I really think sbt is what you want.
Also, there's a project somewhere that lets you put sbt dependencies directly in a scala script.

Eugen Labun

unread,
Nov 4, 2011, 6:54:06 AM11/4/11
to scala-...@googlegroups.com
On 2011-11-03 11:55, Tony Morris wrote:
> Scala has modules. See the object keyword.

There is some info in the Chapter 29 "Modular Programming Using Objects" in "Programming in Scala",
2nd ed. (for the 1st ed. also available online
http://www.artima.com/pins1ed/modular-programming-using-objects.html).

After reading I thought, that one can use a Scala object as container for an own library. But it
seems that nobody does it in reality. Hmmm...

It would be great to see some real-world usages of objects as modules in Scala.
Perhaps in std. library? In other projects?

Thanks!

--
Eugen

Simon Ochsenreither

unread,
Nov 4, 2011, 1:17:41 PM11/4/11
to scala-...@googlegroups.com, Paul Brauner, Miles Sabin, tmo...@tmorris.net


If this was just asking for your own curiosity, then Scala will offer full support for Jigsaw once it's released with Java 8.

How do you know that?

Kevin Wright

unread,
Nov 4, 2011, 1:23:39 PM11/4/11
to scala-...@googlegroups.com, Paul Brauner, Miles Sabin, tmo...@tmorris.net


On 4 November 2011 17:17, Simon Ochsenreither <simon.och...@googlemail.com> wrote:


If this was just asking for your own curiosity, then Scala will offer full support for Jigsaw once it's released with Java 8.

How do you know that?


Because it'll be a Java API, and I'm not aware of any Java feature that current versions of Scala can't use.  There are some features that Scala can't implement, but that's not the same thing at all.

Paul Brauner

unread,
Nov 4, 2011, 1:26:06 PM11/4/11
to Eugen Labun, scala-...@googlegroups.com
> After reading I thought, that one can use a Scala object as container for an own library.

That's the idea of modules. I do it all the time.

Paul

Simon Ochsenreither

unread,
Nov 4, 2011, 3:13:59 PM11/4/11
to scala-...@googlegroups.com, Paul Brauner, Miles Sabin, tmo...@tmorris.net
Maybe I'm wrong, but the last proposal I saw was closely bundled with Java-the-language....

Naftoli Gugenheim

unread,
Nov 6, 2011, 8:35:08 PM11/6/11
to Eugen Labun, scala-...@googlegroups.com
On Fri, Nov 4, 2011 at 6:54 AM, Eugen Labun <la...@gmx.net> wrote:
After reading I thought, that one can use a Scala object as container for an own library. But it
seems that nobody does it in reality. Hmmm...

 
Well, you use packages, which are similar to objects in Scala.

Reply all
Reply to author
Forward
0 new messages