Skip to content

Scala 3 book should describe aggregate bounds (e.g. for typeclasses) #3179

Open
@smheidrich

Description

@smheidrich

The Scala 3 book's section on writing methods that use type classes only shows how to specify a single type class as a parameter's bound.

How to specify multiple bounds is described in the Aggregate Context Bounds section of the reference:

  def showMax[X : {Ord as ordering, Show as show}](x: X, y: X): String =
    show.asString(ordering.max(x, y))

As far as I can tell, this syntax isn't described anywhere in the book, and it's not exactly intuitive.

So in my opinion, it would be nice if this syntax (or its alternatives for older Scala versions) was explained anywhere in the Scala 3 book. Maybe it doesn't have to be in the type class section because it is about bounds more generally, although it may also be worth pointing out how the as syntax specifically is useful for disambiguating between different type classes that have some identically named methods (cf. e.g. Rust's book which has an entire section on how to handle this case).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions