-
Notifications
You must be signed in to change notification settings - Fork 531
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
Make FieldType and @@ abstract #807
Conversation
Sadly I think this is not allowed on Scala 2.10 so this has to wait for #655 |
|
||
import scala.reflect.macros.whitebox | ||
|
||
object labelled { | ||
/** | ||
* The type of fields with keys of singleton type `K` and value type `V`. | ||
*/ | ||
type FieldType[K, +V] = V with KeyTag[K, V] | ||
type FieldType[K, +V] <: V with KeyTag[K, V] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the purpose of V
in KeyTag
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about calling FieldType[K, V]
K ->> V
?
f1a4c77
to
602ce99
Compare
Codecov Report
@@ Coverage Diff @@
## master #807 +/- ##
==========================================
- Coverage 89.86% 87.85% -2.02%
==========================================
Files 34 65 +31
Lines 1470 1498 +28
Branches 1 3 +2
==========================================
- Hits 1321 1316 -5
- Misses 149 182 +33
Continue to review full report at Codecov.
|
b2a53cd
to
a42e14f
Compare
|
56469d0
to
59edbc8
Compare
c8d15d9
to
e7c72d6
Compare
def apply[U] = new Tagger[U] | ||
|
||
private[shapeless] trait Tag { | ||
type @@[+T, U] <: T with Tagged[U] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have been giving this encoding a spin, for encoding/decoding tagged types to/from json:
- with the
@@
part of the privateTag
class, I run into a problem when using play-json's format macro:
No instance of play.api.libs.json.Format is available for xxx.Tag.$at$at[java.lang.String, TaggingTest.FooTag] in the implicit scope (Hint: if declared in the same file, make sure it's declared before)
-
If I move the
@@
type to thetag
object (just a few lines below), it does work. Check-out this scastie: https://scastie.scala-lang.org/kwark/rnWsgsTQQQ2Fmsh97NMRdQ -
circe-generic seems to work with both variants
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I introduced the trait only because Scala 2.10 doesn't support abstract types in objects / classes, but somehow it supports them when inherited.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also it looks like type @@[+T, U] <: T with Tagged[U]
now boxes tagged primitives, while type @@[+T, U] = T with Tagged[U]
does not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's true unfortunately.
c58af22
to
5eaf2fa
Compare
5eaf2fa
to
eb7f006
Compare
eb7f006
to
4944f42
Compare
aka. "You shall not dealias" Fixes a bunch of problems caused by: 1. the compiler normalizing types behind your back; 2. the compiler failing to unify the normalized types.
4944f42
to
7517d9a
Compare
Continuing the discussion from scala/community-build#1047 (comment). I think it's worth investigating which approach most smoothly transfers to Dotty. At least some uses of tagged types will be replaced by opaque types, but probably not all. It'd be good to see what works best for the cases which aren't handled well by opaque types. |
Progress! The boxing issue is solved by scala/scala#8720 so we can have our cake and eat it too. Dotty has better type inference and some simple cases that I tried work better than in Scala 2 with the current encoding of tagged types. I still need to come up with a better comparison (and also check how they are erased in dotty). I agree that perhaps tagged types will not be completely replaced (because they are subtypes) but as a native solution I expect opaque types to become much more widely used, perhaps even as much as to make tagged types on shapeless 3 unwarranted. |
Changed milestone to 2.5.0 which will be 2.13 only. |
Codecov Report
@@ Coverage Diff @@
## master #807 +/- ##
==========================================
- Coverage 89.86% 87.85% -2.02%
==========================================
Files 34 65 +31
Lines 1470 1498 +28
Branches 1 3 +2
==========================================
- Hits 1321 1316 -5
- Misses 149 182 +33
Continue to review full report at Codecov.
|
In the end it was not merged. In Scala 3 we have opaque types. |
aka. "You shall not dealias"
Fixes a bunch of problems caused by:
Also has the additional benefit of printing more nicely in the REPL.
This will always box however. Would that be a problem?
TODO: Check which bugs are fixed, reference them here and add regression tests: