-
Notifications
You must be signed in to change notification settings - Fork 470
Hint about coercion in error msg #7505
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
[1;31mWe've found a bug for you![0m | ||
[36m/.../fixtures/subtype_record.res[0m:[2m23:23[0m | ||
|
||
21 [2m│[0m } | ||
22 [2m│[0m | ||
[1;31m23[0m [2m│[0m let x = takesSomeType([1;31mv[0m) | ||
24 [2m│[0m | ||
|
||
This has type: [1;31msomeOtherType[0m | ||
But this function argument is expecting: [1;33mSomeModule.someType[0m | ||
|
||
Possible solutions: | ||
- These types are compatible at runtime. You can use the coercion operator [1;33m:>[0m to convert to the expected type [1;33mSomeModule.someType[0m. | ||
If you want to use coercion, rewrite the highlighted code to: [1;33m(v :> SomeModule.someType) | ||
[0m |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,16 @@ | ||||||||
|
||||||||
[1;31mWe've found a bug for you![0m | ||||||||
[36m/.../fixtures/subtype_string.res[0m:[2m7:18-20[0m | ||||||||
|
||||||||
5 [2m│[0m } | ||||||||
6 [2m│[0m | ||||||||
[1;31m7[0m [2m│[0m let x = takesStr([1;31mOne[0m) | ||||||||
8 [2m│[0m | ||||||||
|
||||||||
This has type: [1;31mvariant[0m | ||||||||
But it's expected to have type: [1;33mstring[0m | ||||||||
|
||||||||
Possible solutions: | ||||||||
- These types are compatible at runtime. You can use the coercion operator [1;33m:>[0m to convert to the expected type [1;33mstring[0m. | ||||||||
If you want to use coercion, rewrite the highlighted code to: [1;33m(One :> string) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is great! But I'd simplify the wording a bit, it feels a bit verbose:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tsnobip how would you simplify the wording? Can't see anything along those lines in the diff above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see now. |
||||||||
[0m |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module SomeModule = { | ||
type someType = { | ||
one: bool, | ||
two: int, | ||
} | ||
} | ||
|
||
type someOtherType = { | ||
...SomeModule.someType, | ||
three: string, | ||
} | ||
|
||
let v = { | ||
one: true, | ||
two: 1, | ||
three: "hello", | ||
} | ||
|
||
let takesSomeType = (s: SomeModule.someType) => { | ||
s.one | ||
} | ||
|
||
let x = takesSomeType(v) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
type variant = One | Two | ||
|
||
let takesStr = (s: string) => { | ||
s ++ "hello" | ||
} | ||
|
||
let x = takesStr(One) |
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.
Is this really a good suggestion for literals?
Wouldn't I want to write
2. + 2.
rather than2. + (2 :> float)
to fix the error?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.
Yeah we could ignore these cases, agreed. The int/float/string suggestions need a refactor as well now that we have new capabilities, but we can save that for later.
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.
Oh wow, the penny didn't yet drop for me that you can write:
Really cool!
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.
@nojaf yeah coercion (especially in v12) opens up a ton of cool possibilities actually. One example is that you can (together with variant type spreads) easily create fully type driven decoders:
Can easily just add new cases to
someType
and have them automatically accounted for. Works because of the unboxed variant version ofsomeType
that has theOtherString(string)
case, which covers any string that isn't already covered by the rest of the constructors.Playground: https://rescript-lang.org/try?version=v12.0.0-alpha.13&module=esmodule&code=AIVwdgRg9gHgpgEwFABcCeAHOACAzlAWzgBVMcBebAeTBwB9tiB3KbB4gCwCc57sAxKCC5sBASwBucJElCRYiVGTyESZAMooRlBgDp9+IqSyiqKDnC6auYsAHMAFLi227AShkAbOChVGy-FyE1q7YlE4AXHgu9m5hAHxI2HhMYigAxhzYTtgR8X5qWNZxAN5Jovq6hoU4AIa4BcYU+eqqTqpNHskMZhZWMY4A+nHk+QByULTlAL5IQA