Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shouldn't KeyTag be an abstract type rather than a trait? #1285

Open
DmytroMitin opened this issue Nov 12, 2022 · 4 comments
Open

Shouldn't KeyTag be an abstract type rather than a trait? #1285

DmytroMitin opened this issue Nov 12, 2022 · 4 comments

Comments

@DmytroMitin
Copy link
Contributor

DmytroMitin commented Nov 12, 2022

type FieldType[K, +V] = V with KeyTag[K, V]
trait KeyTag[K, +V] 

trait Tagged[U]
type @@[+T, U] = T with Tagged[U]

None.asInstanceOf[FieldType[Symbol @@ "field1", Any]]
// ClassCastException: scala.None$ cannot be cast to KeyTag

vs.

type FieldType[K, +V] = V with KeyTag[K, V]
type KeyTag[K, +V] 

trait Tagged[U]
type @@[+T, U] = T with Tagged[U]

None.asInstanceOf[FieldType[Symbol @@ "field1", Any]] // no exception

Normally the trait works but not with Any or AnyRef.

field[Symbol @@ "field1"](None: Any) doesn't throw but val f = field[Symbol @@ "field1"](None: Any) does.

Also

def foo(x: Any): FieldType[Symbol @@ "field1", Any] = field[Symbol @@ "field1"](x)
foo(None)

throws.

Scala 2.13.10.

Discovered in https://stackoverflow.com/questions/74384745/encoding-decoding-a-field-with-any-datatype-assigned-with-value-none-in-scala

@DmytroMitin
Copy link
Contributor Author

Or is it too late because of the binary compatibility?

@joroKr21
Copy link
Collaborator

I tried to make some of these types abstract a while ago - see #807 - one of the issues was it caused boxing which would be unacceptable performance regression. I don't think I tried to make the tag itself abstract though. Can you check if this would cause boxing? You can use :javap in the REPL to view the generated bytecode.

@joroKr21
Copy link
Collaborator

Or is it too late because of the binary compatibility?

That might be the case too. If MiMa complains then probably we can't do it.

@DmytroMitin
Copy link
Contributor Author

#44 (noticed at https://stackoverflow.com/questions/75012811/should-tagged-types-work-for-value-classes )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants