Surprise when importing everything from an enum

Consider the following example

enum Foo:
  case Bar1, Bar2, Bar3, Bar4

import Foo.*
val one = valueOf[1]  //error: method valueOf in object Foo does not take type parameters

It’s very common to import an enumeration object namespace to get all its cases. However we also get the method like valueOf imported to the scope. I wonder if there is a good language solution here.
Two solutions I can think of:

  1. Limited wildcard import/export like import Foo.class, object, val to be explicit about what identifiers should be implemented, but still as a wildcard.
  2. Modifier to prevent import/export under wildcard imports like explicit def valueOf ..., so explicit identifiers can only be imported/exported when are explicitly referenced in the import/export statement.

Note:
Yes, I’m aware that in the example above it’s possible to exclude valueOf via import Foo.{valueOf => _, *}, but I wonder if this problem is more common and requires a better general solution.

3 Likes

Python lets you define an __all__ list to control what import foo.* imports. Something similar in Scala could be handy

1 Like

export wildcard omits synthetics, TIL.

enum E0:
  case A, B

object E:
  export E0.*

@main def test() = println:
  import E.*
  (A, valueOf[42])

There may be an “old” “feature request” to make export this.* somehow significant IIRC. The semantics could be, omit synthetic members from imports.

enum E:
  case A, B
  export this.*

currently

4 |  export this.*
  |              ^
  |              the ordinal method of enum class E0 can not be defined by the user

This currently works

case object X {
  object Y
  export this.*
}

@main def test() = println:
  import X.*
  productElementNames

Edit:
Export more stuff from this
Cannot create renamed copies of members of this using export