Skip to content
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

Allow equality comparer in the Set/Map constructor #50

Open
nmunday opened this issue Apr 5, 2021 · 3 comments
Open

Allow equality comparer in the Set/Map constructor #50

nmunday opened this issue Apr 5, 2021 · 3 comments

Comments

@nmunday
Copy link

nmunday commented Apr 5, 2021

There maybe a way already to allow this. I would like to be able to use interfaces for the elements. At the moment I believe that would mean having equals and hashCode on the interface which would then mean creating an instance of the interface would require setting those each time.

If the collection implementations could take an EqualityComparer which has those 2 methods then I believe I could use interfaces.

Another advantage is that I could then have different Equality for the same interface. My main Interface is:

interface Fact { subject: string, property: string , value: string }

Depending on the circumstance any of the properties could be the key or any combination also.

Thanks.

@emmanueltouzery
Copy link
Owner

emmanueltouzery commented Jul 12, 2021

It's true that prelude-ts is currently more geared towards objects. Maybe we could add such a feature, depending on how much change would have to be done to the implementation. I'm guessing it wouldn't be too bad, but I don't plan on investing time on this soon.

Maybe you could leverage javascript's prototype nature.. https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes
I didn't think about it too much though.

Sorry for the delay to answer...

@ornamentist
Copy link

I think this means that Sets that require custom equality or hash code generation can only be used with classes and methods and not simple objects (for example number[])?

@emmanueltouzery
Copy link
Owner

Yes, you shouldn't put things like plain arrays. This is covered in the docs:
https://github.com/emmanueltouzery/prelude-ts/wiki/Prelude%E2%88%92ts-user-guide#equality

It wouldn't compile in typescript and should throw in JavaScript.

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

No branches or pull requests

3 participants