# Expanding/changing \`Selectable\` based on upcoming Named Tuples feature

**URL:** <https://contributors.scala-lang.org/t/expanding-changing-selectable-based-on-upcoming-named-tuples-feature/6395>\
**Category:** Language Design\
**Created:** [November 27, 2023, 4:12am UTC](https://contributors.scala-lang.org/t/expanding-changing-selectable-based-on-upcoming-named-tuples-feature/6395 "2023-11-27T04:12:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [November 27, 2023, 4:12am UTC](https://contributors.scala-lang.org/t/expanding-changing-selectable-based-on-upcoming-named-tuples-feature/6395/1 "2023-11-27T04:12:48Z")

</div>

Now that [Named Tuples](https://github.com/lampepfl/dotty/pull/19075) may become a reality, I want to bring up a pain point that this feature can help solve.

# The problem with structural types

Currently [structural types](https://dotty.epfl.ch/docs/reference/changed-features/structural-types.html) are very limiting. The only way to get a refinement is through casting and requires every function that transforms the refined object to keep the refinement along the way.

For example, say I have a class `Foo[T]`. I need to have specific fields available (at compile-time) for instances of `Foo` according to the type `T`. Structural types provide no such functionality.

# Solving the problem via Selectable and Implicit Conversions

The only way to achieve this is by extending `Selectable` and creating an ugly implicit conversion and a helper macro like so:

```scala
final class Foo[T] extends Selectable:
  def selectDynamic(name: String): Any = /* runtime field selection */
given [T](using r: Refiner[T]): Conversion[Foo[T], r.Out] = _.asInstanceOf[r.Out] //just forcing the compiler to cooperate 
trait Refiner[T]{ type Out <: Foo[T] }
object Refiner:
  transparent inline given [T]: Refiner[T] = ${ refineMacro[T] }
  def refineMacro[T](using Quotes, Type[T]): Expr[Refiner[T]] =
    import quotes.reflect.*
    val tTpe = TypeRepr.of[T]
    val fooTpe = TypeRepr.of[Foo[T]]
    val fields: List[(String, TypeRepr)] = /*your field logic here*/
    val refined = fields.foldLeft(fooTpe) { case (r, (n, t)) => Refinement(r, n, t) }
    val refinedType = refined.asTypeOf[Foo[T]]
    '{ new Refiner[T]{ type Out = refinedType.Underlying }}
  end refineMacro
end Refiner

```

This is clearly ugly, relies on a clunky implicit conversion mechanism (like implicit classes did) and also fails to support field auto-completion by the IDE because the IDE cannot know what fields are actually available.

# Proposal: Expanding `Selectable` with a Named Tuple `Fields` type member

We add to `Selectable` a Tuple type member `Fields`.

```scala
trait Selectable:
  type Fields <: Tuple

```

**Rules** :

- If `Fields` is unbounded further by the user (the upper bound remains `Tuple`), then the current behavior of Selectable extended objects remains the same. (If the user just bounds with `NonEmptyTuple` we treat it the same also).
- If `Fields` is an `EmptyTuple`, there are no selectable fields available and we should get a compiler error when trying to select a field that does not already exist.
- If `Fields` is some kind of non-zero Tuple arity, then the available fields and their types will be defined by the tuple type. This should work with both named and unnamed tuples just the same, but of course generally more useful with named tuples.

---

<div class="post-metadata">

**Author:** ![alvae](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/alvae/32/2810_2.png) [@alvae](https://contributors.scala-lang.org/u/alvae)\
**Post date:** [November 30, 2023, 7:44am UTC](https://contributors.scala-lang.org/t/expanding-changing-selectable-based-on-upcoming-named-tuples-feature/6395/2 "2023-11-30T07:44:45Z")

</div>

The proposed feature reminds me of the way we can “forward” selection in Swift so I thought it might be useful to quickly present the way things work over there, in case there are some parts of the design we can copy. Otherwise, my apologies for the noise.

In Swift, we can customize the runtime selection of a type’s member using a feature called “[dynamic member lookup](https://github.com/apple/swift-evolution/blob/main/proposals/0195-dynamic-member-lookup.md)”. Here’s a simple example:

```plaintext
typealias Version = (major: Int, minor: Int)

@dynamicMemberLookup
struct Configuration {
  var version: Version
  var user: [String: Int]
  subscript(dynamicMember k: String) -> Int? { user[k] }
}

var s = Configuration(version: (major: 2, minor: 1), user: ["revision": 2])
print(s.version.minor) // compile-time selection
print(s.revision) // run-time selection

```

I believe annotating a Swift type with `@dynamicMemberLookup` is similar to extending `Selectable` in Scala. Please correct me if I’m wrong.

Swift has a notion of “[key paths](https://github.com/apple/swift-evolution/blob/main/proposals/0161-key-paths.md)”, which are values denoting a how to access a specific field from a root type. They can be seen as lenses whose semantics is understood by the compiler. Here’s an example:

```plaintext
var s = Configuration(version: (major: 2, minor: 1), user: ["revision": 2])
var k = \Configuration.version.minor
print(s[keyPath: k]) // compile-time selection

```

We can [combine the two features](https://github.com/apple/swift-evolution/blob/main/proposals/0252-keypath-dynamic-member-lookup.md) to “forward” a member lookup at compile-time. For example:

```plaintext
@dynamicMemberLookup
struct Configuration {
  /* same as before */
  subscript<U>(dynamicMember k: KeyPath<Version, U>) -> U { version[keyPath: k] }
}

var s = Configuration(version: (major: 2, minor: 1), user: ["revision": 2])
print(s.major) // compile-time selection
print(s.version.minor) // compile-time selection
print(s.revision) // run-time selection

```

Of course the root of a key path may be generic as well. So IIUC, here’s how to implement OP’s example in Swift:

```plaintext
@dynamicMemberLookup
struct Foo<T> {
  var m: T
  subscript<U>(dynamicMember k: KeyPath<T, U>) -> U { m[keyPath: k] }
}

var s = Configuration(version: (major: 2, minor: 1), user: ["revision": 2])
var t = Foo(m: s)
print(t.major) // compile-time selection

```

What I like about this feature is that it uniformly applies to every compile-time “selectable” thing, or in Swift terms any kind of compile-time member lookup. That’s because we can create key paths for every property that appears in the API of a type, no matter whether the type is structural or not, no matter whether the property is computed or not. Indeed, notice that `t.major` requires two key path applications, but that is transparent to the author of `Foo`.

---

<div class="post-metadata">

**Author:** ![odersky](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/odersky/32/42_2.png) [@odersky](https://contributors.scala-lang.org/u/odersky)\
**Post date:** [December 23, 2023, 1:05pm UTC](https://contributors.scala-lang.org/t/expanding-changing-selectable-based-on-upcoming-named-tuples-feature/6395/3 "2023-12-23T13:05:22Z")

</div>

I am not yet convinced that structural types would not be enough to do this.

What would be the difference between

```scala
Selectable { type Fields = (name: String, age: Int) }

```

and

```scala
Selectable { val name: String, age: Int }

```

? For me, the point of structural types is that they are facades for (subsets of) the APIs of class types, whereas the point of named tuples is that they are stand-alone value types: tuples with a more readable access mechanism.

It would be good to see concrete usage examples, expressed with the best possible idiom for structural types and then expressed using the proposed feature.

---

<div class="post-metadata">

**Author:** ![Katrix](https://contributors.scala-lang.org/letter_avatar_proxy/v4/letter/k/c6cbf5/32.png) [@Katrix](https://contributors.scala-lang.org/u/Katrix)\
**Post date:** [December 24, 2023, 8:34am UTC](https://contributors.scala-lang.org/t/expanding-changing-selectable-based-on-upcoming-named-tuples-feature/6395/4 "2023-12-24T08:34:26Z")

</div>

The difference as I see it between them is that the first one a match type can generate on its own given sufficient input, while you need a macro to generate a dynamic version of the second one.

---

<div class="post-metadata">

**Author:** ![odersky](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/odersky/32/42_2.png) [@odersky](https://contributors.scala-lang.org/u/odersky)\
**Post date:** [February 4, 2024, 8:21pm UTC](https://contributors.scala-lang.org/t/expanding-changing-selectable-based-on-upcoming-named-tuples-feature/6395/5 "2024-02-04T20:21:43Z")

</div>

Yes, I have come to appreciate that now as well. It gives us a way to compute selectable fields, which is powerful, but also looks well contained, compared to (say) implicit conversions. I am warming up to this!

---

<div class="post-metadata">

**Author:** ![odersky](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/odersky/32/42_2.png) [@odersky](https://contributors.scala-lang.org/u/odersky)\
**Post date:** [February 5, 2024, 7:07pm UTC](https://contributors.scala-lang.org/t/expanding-changing-selectable-based-on-upcoming-named-tuples-feature/6395/6 "2024-02-05T19:07:11Z")

</div>

This is now implemented and documented as part of the named tuples implementation [#19174](https://github.com/lampepfl/dotty/pull/19174).

---

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [February 5, 2024, 7:29pm UTC](https://contributors.scala-lang.org/t/expanding-changing-selectable-based-on-upcoming-named-tuples-feature/6395/7 "2024-02-05T19:29:20Z")

</div>

> [@odersky](#):
>
> This is now implemented and documented as part of the named tuples implementation [#19174](https://github.com/lampepfl/dotty/pull/19174).

That’s awesome!  
I see that the change is only in the typer. What is required to change so the field list will be available in completions for editors like Metals?

---

<div class="post-metadata">

**Author:** ![odersky](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/odersky/32/42_2.png) [@odersky](https://contributors.scala-lang.org/u/odersky)\
**Post date:** [February 5, 2024, 8:27pm UTC](https://contributors.scala-lang.org/t/expanding-changing-selectable-based-on-upcoming-named-tuples-feature/6395/8 "2024-02-05T20:27:30Z")

</div>

I believe one would need to work on the presentation compiler plugin in dotc. There are methods that provide the possible completions. Since the changes in the Typer are quite small, I don’t think this would be a big problem either.
