Skip to content

Opaque types don't play nicely with anonymous functions #5481

Closed
@adamgfraser

Description

@adamgfraser

Normally I could write an anonymous function and the argument type could be inferred based on the expected type. For example, if I defined type Set[A] = A => Boolean then I could write def singleton[A](a: A): Set[A] = _ == a and the type of _ would be inferred as A. However, if I define Set as opaque and try to do this in the type companion it doesn't work. I have to specify the type of the argument explicitly, which seems unnecessary and inconsistent given that within the type companion the opaque type and its definition are supposed to be the same thing.

object TypeAlias {

  type Set[A] = A => Boolean

  object Set {
    def singleton[A](a: A): Set[A] = _ == a       // Works
  }
}

object OpaqueType {

  opaque type Set[A] = A => Boolean

  object Set {
    //def singleton[A](a: A): Set[A] = _ == a     // Does not compile
    def singleton0[A](a: A): Set[A] = (_: A) == a // Works
  }
}

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