question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal for a new language construct that combines `object`, `package` and `package object`

See original GitHub issue

Proposal for a new language construct that combines object, package and package object

Proposal outline

Current state

Packages

Currently, both Scala and the upcoming Dotty treat packages as second class citizens: They are intangible, cannot be passed around and provides no more functionality than allowing code to be structured to fit the JVM model.

Objects

Scala objects allow the same structural organization as packages but lack the privilege of defining a package path.

Package objects

While packages cannot contain top level definitions, objects can. Package objects provide a place in which top-level definitions can exist and be allowed to import together with other package artifacts. I feel they add extra complexity by crudely mashing the two concepts together.

Proposed state

This proposal aims at condensing object, package and package object into a single module concept.

The module

A module consists of two things:

  • A path or module-path, consisting of path segments separated by ‘.’ (just like a package path)
  • An instance, from this point on simply refered to as ‘module’

A module definition creates one or more paths and one or more modules. An empty module — that is, a module that doesn’t contain any top level definitions — will not be reified during compilation.

For example, module com.org defines:

  • the module ‘com’ with path ‘_root_’
  • the module ‘org’ with path ‘_root_.com’

Interoperability with current things

For a normal class definition

package com.org

class Foo

becomes

module com.org

class Foo

A package sequence such as

package com.org
package communication

class HelloService(response: String)

becomes

module com.org
module communication

class HelloService(response: String)

A package object definition such as

package com.org

package object communication {
  def standardHello(response: String): HelloService = new HelloService(response)
}

becomes

module com.org

module communication {
  def standardHello(response: String): HelloService = new HelloService(response)
}

or even

module com.org.communication {
  def standardHello(response: String): HelloService = new HelloService(response)
}

Implied syntactic pitfalls

Scala currently allows package members to be defined in blocks, like

package foo {
  class Bar
}

which would directly translate to

module foo {
  class Bar
}

which looks ambigous as to whether this declares a “path module” or a “package object module”.

As this module contains no top level definitions it could be ignored during compilation and not generate bytecode corresponding to today’s package objects, allowing other module blocks to be declared without creating a collision between module instances.

However, in my experience this practice is not widely regarded as idiomatic or used in the wild. For this reason, I feel the better approach would be to disallow the package block syntax, or more specifically, always generate a module instance when a block is attached to the definition.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:10
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
smartercommented, Jan 11, 2018

Closing since this proposal isn’t concrete enough to be actionable, more discussion could go in http://contributors.scala-lang.org/ or https://github.com/scala/scala-dev/issues/441

0reactions
som-snyttcommented, Jan 4, 2020

Yoda would say there are no packages, only packagings.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Proposal to add top-level definitions (and replace package ...
This is a request for comments for the proposal to add top-level definitions in Scala 3 as a simpler replacement for package objects....
Read more >
Object Constraint Language
This specification defines the Object Constraint Language (OCL), version 2.4 . OCL version 2.4 is the latest version of. OCL that is aligned...
Read more >
Spread syntax (...) - JavaScript - MDN Web Docs - Mozilla
In an object literal, the spread syntax enumerates the properties of an object and adds the key-value pairs to the object being created....
Read more >
Scala | Package Objects - GeeksforGeeks
Main objective of a package is to keep files modularized and easy to be maintained. So we keep project files in several different...
Read more >
A tour of the Dart language
A Dart string ( String object) holds a sequence of UTF-16 code units. You can use either single or double quotes to create...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found