-
Notifications
You must be signed in to change notification settings - Fork 463
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
Bug: JSX 4 doesn't work with prop having var with the same name as default value #6374
Comments
Good catch! We can see why by looking at the code that JSX PPX converted. module DoesntWork = {
let a = "foo" // 1
type props<'a> = {a?: 'a}
let make = ({?a, _}: props<_>) => { // 2
let a = switch a {
| Some(a) => a
| None => a // <- this should refer to the 1, not 2
}
React.string(a)
}
...
} |
Maybe we can fix this using alias the props name: module DoesntWork = {
let a = "foo"
type props<'a> = {a?: 'a}
let make = ({a: ?a_, _}: props<_>) => { // alias a as a_
let a = switch a_ { // refer a_ here
| Some(a) => a
| None => a
}
React.string(a)
}
...
} |
What do you think? @cristianoc |
I find this more reliable: module Works = {
let a = "foo"
type props<'a> = {a?: 'a}
let make = (props: props<_>) => {
let a = switch props {
| {a} => a
| _ => a
}
React.string(a)
}
} It won't break if there's a prop with |
|
If we have other props besides a, it is probably better off destructuring like it was used to. |
How about: module Works = {
let a = "foo"
type props<'a, 'anotherProp1, 'anotherProp2> = {a?: 'a, anotherProp1: 'anotherProp1, anotherProp2: 'anotherProp2}
let make = (props: props<_, _, _>) => {
let a = switch props {
| {a} => a
| _ => a
}
let {anotherProp1, anotherProp2} = props
React.string(a)
}
} |
I still prefer this: #6374 (comment) |
What I like in this case #6374 (comment) that it also allows to use the |
Actually, there is no |
There is no module DoesntWork = {
let a = "foo"
@react.component
let make = (~a=a) => React.string(a)
} |
I think |
I don't think this is a topic of whether the code is clean or not. What do you think about variables called props being exposed in the function body that user didn't express? |
I find it fine and that the behaviour can be expected. The same as the |
Well, I've changed my mind :) |
Adding |
I've remembered a bunch of React code with |
Well, a good solution would be an ability to set the default value during distructuring, but it's not supported in ReScript: module Works2 = {
let a = "foo"
type props<'a, 'anotherProp1, 'anotherProp2> = {
a?: 'a,
anotherProp1: 'anotherProp1,
anotherProp2: 'anotherProp2,
}
let make = ({a=a, anotherProp1, anotherProp2, _}: props<_>) => {
Js.log(anotherProp1)
Js.log(anotherProp2)
React.string(a)
}
} |
And probably it won't be possible to add support for this until there's the |
Here's another suggestion. Although, I can't say that I like it more
|
This seems fine. In general, I would keep changes minimal at this stage and make sure not to introduce possible breaking changes, e.g. to the location shown in error messages, or error messages themselves. As from past experience it's quite easy to introduce issues inadvertently. |
Agree, but I'd vote for underscore in front of the var name instead of at the end. |
Definitely underscore before. |
https://rescript-lang.org/try?version=v11.0.0-beta.4&code=ALBWGcA8GEHsDsBmBLA5gCgN4DcCmAncZBALgAIAWAGjIFtYATXcgIgEMBXAF1lra+QBjFgF8AlACgJ9BhwA2uMgHVY+ANbgyAXjKYJZMgq5kARtrItEsWC31lg+XG0FcAdIN4AHBLnhc7RnRsaoo66AB+bFomYtoAfGQASk4uruBc+MjwGGySIlIy8ooAIrC44H4q6uZ6BoFs5pbWtgYOKW4etN7wvv51uMZ8IeYRUbnxSe1pGVk5eRJAA
The text was updated successfully, but these errors were encountered: