twop / ts-union

ADT sum type in typescript
MIT License
70 stars 2 forks source link

Ability to specify discriminator name #6

Open tanfonto opened 5 years ago

tanfonto commented 5 years ago
tanfonto commented 5 years ago

Great job otherwise, btw :bow:

twop commented 5 years ago

Could you provide a little bit more context what you would like to do and why?

One of the explicit decisions I made is to make data structures for values opaque to the user. The reason was to experiment with different representations (and I already changed it twice).

But again, would love to learn more about your usecase

tanfonto commented 5 years ago

Yeah, sorry for the delay. Put simply, I'd like to be able to tell Union to use field name different than k. Type is something that comes to mind mostly because Redux relies on it. Might be there's something I'm missing, in which case I'm more than open for explanation. Cheers.

twop commented 5 years ago

No worries!

Got it.

I made a design decision that the shape of the union values is opaque to developer (partially because that how it works in rust/swift etc).

Fun history story:

const U = Union({a: of(null), b: of<string>()})
const val = U.b('aha!');

This union value was originally represented as this:

['b', 'aha!']

then

['b', ['aha!']]

now

// objects are faster because of shape optimizations
{k:'b', p:['aha!']}

And I'm benchmarking

{k:'b', p0:'aha!', p1: undefined, p2: undefined, l:1}

This might be faster because internally I'm using (...args) => {/*...*/} which might stop some optimizations (args shape is not stable, thus hard to jit)

So going back to the issue: I didn't know at the time what would be the fastest internal implementation of the library api. The worst part is that this state is very dynamic and might change at any given moment. So I don't want to expose internal api because it will stop any perf explorations.

Hope that makes sense and answers your question.

MiracleBlue commented 4 years ago

Sorry for my somewhat off-topic comment @twop but I just had to say that I've been reading your issue replies on this repo and you've given me yet another reason to love the open source community. I'm really looking forward to using this, and seeing if it fits well with what I need, but either way, love your work! I hope you keep doing what you're doing!

twop commented 4 years ago

@MiracleBlue Thank you so much for such kind words <3 Curious to know more about your use case, please give feedback if you will have time and energy

MiracleBlue commented 4 years ago

My pleasure!

I have a few possibilities. I've always been a fan of modelling data with data types ever since my first encounter with them in purescript. I also get very annoyed at how limiting typescript feels since PS was actually how I first really embraced a type system. Right now I'm working on a sort of css in js library, and I'm working on it's parsing system, as well as caching. I've actually started the caching stuff, playing around with different approaches to dealing with stateful yet serializeable information in a nicely FP way. It was actually a search I did on github for IORef that eventually led me here (indirectly). Since posting my comment I actually have started seeing so many little opportunities to model things the way I really want to using ts-union and I especially love the work you've done on making the pattern matching a really nice experience, which I expect I'm going to rely on quite a bit in the parser. This might even lead me to completely rethink the approach I took with the object literal walking utility (the styles are generated from object literals)

On Tue, 24 Dec. 2019, 6:41 pm Simon, notifications@github.com wrote:

@MiracleBlue https://github.com/MiracleBlue Thank you so much for such kind words <3 Curious to know more about your use case, please give feedback if you will have time and energy

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/twop/ts-union/issues/6?email_source=notifications&email_token=AAAFWSFKLEXULLKYPRC4BJTQ2G4LBA5CNFSM4IYI4EL2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEHSWSYI#issuecomment-568682849, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAFWSDN25CSMMCIMQFIS3DQ2G4LBANCNFSM4IYI4ELQ .

twop commented 4 years ago

@MiracleBlue Nice! I need a good css solution so badly. Offtopic: Here is what I have in mind: https://codesandbox.io/s/sketch-of-typed-css-5hlyn (both index.ts and dynStyles.ts)

But for that probably plugin compiler API is a prerequirement: https://github.com/microsoft/TypeScript/issues/16607#issuecomment-552748039

Do you have a sketch API? for that?

P.S. probably it makes more sense to move conversation to somewhere else. my twitter handle @twopSK if that is a convenient medium for you.