Skip to content

Add unsafeHas#14

Merged
paf31 merged 1 commit into
purescript:masterfrom
natefaubion:unsafe-has
Sep 26, 2017
Merged

Add unsafeHas#14
paf31 merged 1 commit into
purescript:masterfrom
natefaubion:unsafe-has

Conversation

@natefaubion
Copy link
Copy Markdown
Contributor

No description provided.

@paulyoung
Copy link
Copy Markdown

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

Comment thread src/Data/Record/Unsafe.js
};

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

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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