Skip to content

Add unsafeHas #14

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

Merged
merged 1 commit into from
Sep 26, 2017
Merged

Add unsafeHas #14

merged 1 commit into from
Sep 26, 2017

Conversation

natefaubion
Copy link
Contributor

No description provided.

@paulyoung
Copy link

@natefaubion could you help me understand what makes this "unsafe"?

@@ -24,3 +24,7 @@ exports.unsafeDeleteFn = function(label, rec) {
}
return copy;
};

exports.unsafeHasFn = function(label, rec) {
return {}.hasOwnProperty.call(rec, label);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using Object.prototype.hasOwnProperty.call(rec, label) so that a new object isn't created?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's using the same code the compiler generates in record updates. Engines will optimize this out.

@natefaubion
Copy link
Contributor Author

It's unsafe because it violates referential transparency. The type signature says it works forall r. { | r }, but you are able to observe more than that.

@natefaubion
Copy link
Contributor Author

Maybe referential transparency isn’t the right word? There isn’t a use for this function in safe code since the whole point of records is that you know the fields statically, making this kind of check unnecessary. The only use case for it is if you are making a dynamic assertion about records, which I would consider inherently unsafe. Maybe “disingenuousHas” is a better name? :D

@paf31 paf31 merged commit 058cdde into purescript:master Sep 26, 2017
@paf31
Copy link
Contributor

paf31 commented Sep 26, 2017

Thanks!

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

Successfully merging this pull request may close these issues.

3 participants