Skip to content

EitherIdOpsBinCompat0 causes scala-newtype tagged type to not compile #2491

@tom91136

Description

@tom91136

Quite an interesting issue here. Basically, I was using scala-newtype together with cats 1.4.0 which I upgraded from 1.2.0 just today and my project will not longer compile. I isolated the issue to this:
I had a @newtype case class Foo(value : String) which, at some point, compiles to:

	type Foo = Foo.Type
	object Foo {
		type Repr = String
		type Base = Any {type Foo$newtype}
		trait Tag extends Any
		type Type <: Base with Tag

		def apply(x: String): Foo = x.asInstanceOf[Foo]

		implicit final class Ops$newtype(val $this$: Type) extends AnyVal {
			def value: String = $this$.asInstanceOf[String]
		}
	}

	def a() = {
		import cats.implicits._
		val foo = Foo("")
		val value: String = foo.value // error
	}

And the compiler says:

[error]  found   : test.Foo
[error]     (which expands to)  test.Foo.Type
[error]  required: String
[error]                 val value: String = foo.value
[error]                                         ^

I've manually pasted the code generated from scala-newtype and removed the dependency and this still happens.
If I change Foo's value to some other name, say def abc: String = $this$.asInstanceOf[String] then it works.

It seems that 380f721 added the syntax final class EitherIdOpsBinCompat0[A](val value: A) extends AnyVal{...} which clashes with my value defined in my tagged type. To verify this, I also used the name a and that also causes the same issue but this time with final class ValidatedIdOpsBinCompat0[A](val a: A) extends AnyVal{...}. IntelliJ's click to open implementation when used on the foo.value symbol actually takes me to EitherIdOpsBinCompat0[A](val value: A) which confirms my suspicion.

The obvious workaround is to not have newtypes with a or value as fields but I imagine these are popular names for this kind of use.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions