-
Notifications
You must be signed in to change notification settings - Fork 29
Object.is documentation #100
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 7 commits
7da0d96
da6197a
8f1d753
5db75f0
2741d73
8f6db7c
2d0fb2c
3bca678
5dec67f
08db9a8
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 | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,37 @@ | ||||||
@obj external empty: unit => {..} = "" | ||||||
|
||||||
@val external is: ('a, 'b) => bool = "Object.is" | ||||||
/** | ||||||
`is` determines if two objects are identical in all contexts. Objects, arrays, records, and other non-primitives are only identical if they reference the **exact** same object in memory. Primitives like ints, floats, and strings are identical if they have the same value. `+0` and `-0` are distinct. NaN is equal to itself. See [Object.is on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) | ||||||
|
||||||
In most scenarios use `==` or `===` or the custom `equals` function (if provided) for the type. | ||||||
|
||||||
The `==` operator [is different in ReScript than Javascript](https://rescript-lang.org/docs/manual/latest/overview#boolean). Arrays, records and other non-primitives are equal if they have the same contents (deep equality). | ||||||
|
||||||
In ReScript, the `===` operator performs a strict equality check, like Javascript, and is similar but not identical to `is` | ||||||
|
||||||
## Examples | ||||||
|
||||||
```rescript | ||||||
Object.is(25, 13) // false | ||||||
Object.is("abc", "abc") // true | ||||||
Object.is(undefined, undefined) // true | ||||||
Object.is(undefined, null) // false | ||||||
Object.is(-0.0, 0.0) // false | ||||||
Object.is(list{1, 2}, list{1, 2}) // false | ||||||
|
||||||
Object.is([1, 2, 3], [1, 2, 3]) // false | ||||||
[1, 2, 3] == [1, 2, 3] // true | ||||||
[1, 2, 3] === [1, 2, 3] // false | ||||||
|
||||||
let fruit = {"name": "Apple" } | ||||||
Object.is(fruit, fruit) // true | ||||||
Object.is(fruit, {"name": "Apple" }) // false | ||||||
fruit == {"name": "Apple" } // true | ||||||
fruit === {"name": "Apple" } // false | ||||||
``` | ||||||
*/ | ||||||
@val | ||||||
external is: ('a, 'b) => bool = "Object.is" | ||||||
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. I'm not sure this type signature makes sense. By definition you'd think that objects of different types wouldn't be considered identical, and you might as well weed that out at compile time. There are of course exceptions, such as JSON-encoded primitives, but better to require explicitness than cater to convenience for weird edge cases I think. I would therefore suggest this instead:
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. Ok I'll change it. |
||||||
|
||||||
@val external create: {..} => {..} = "Object.create" | ||||||
@val external createWithProperties: ({..}, {..}) => {..} = "Object.create" | ||||||
|
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.
Describing other functions and operators seem outside the scope of reference documentation. It will quickly become hard to maintain the documentation if it gets scattered across other definitions.
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.
Ok thanks for the feedback.