Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

simple match function on rune #790

Closed
nythrox opened this issue Mar 23, 2023 · 6 comments
Closed

simple match function on rune #790

nythrox opened this issue Mar 23, 2023 · 6 comments
Assignees
Labels

Comments

@nythrox
Copy link
Contributor

nythrox commented Mar 23, 2023

If you have a param that's a or Rune<T | Uint8Array | undefined> or any other typescript union, and you'd like to treat each case separately, you'd have to do some combination of .unhandle(isUint8Array) and .rehandle(isUint8Array, $encoder.encode) for each separate type.

Would be nice to have a simple match that does the same thing, but with a more understandable control flow:

const value: Rune<T | undefined> =
  param.match(isUint8Array, $encoder.encode)

related to #624 , but for general conditional control flows based on any arbitrary value

@robocapi robocapi added this to Capi Mar 23, 2023
@tjjfvi
Copy link
Contributor

tjjfvi commented Mar 23, 2023

Yes, a .handle method is planned to do exactly that. Since Uint8Array is a class, you could simply write .handle(Uint8Array, $encoder.encode). We'll probably also add a simple not around the same time, to enable .handle(not(undefined), $encoder.encode)

@harrysolovay
Copy link
Contributor

Good point, especially since handle will accept guards too. Shall we close #624 in favor of this issue (and rename this issue to rune.handle)?

@harrysolovay
Copy link
Contributor

In addition to a not util, we might want a simple curried isTagged for easier creation of guards to handle tagged unions.

const isType = isTagged("type")

rune
  .handle(isType("Id"), () => {})
  .handle(isType("Address20"), () => {})

@tjjfvi
Copy link
Contributor

tjjfvi commented Mar 23, 2023

Shall we close #624 in favor of this issue

No, #624 is also important, because repeated calls to .handle could flow into each other.

@nythrox
Copy link
Contributor Author

nythrox commented Mar 23, 2023

I'm playing with this on my branch to see if it improves my control flow. Impl was surprisingly simple, but it'll be better typed when it's its own handle function

  match<T2 extends T, T3, U3>(
    fn: Guard<T, T2>,
    alt: (rune: ValueRune<T2, never>) => Rune<T3, U3>,
  ): ValueRune<T | T3, U | U3> {
    return this.unhandle(fn).rehandle(fn as Guard<T2 | U, T2>, alt)
  }

@tjjfvi
Copy link
Contributor

tjjfvi commented Mar 23, 2023

The issue with that implementation is that it may unintentionally rehandle values; part of the reason to have a .handle at all is to make that pattern less common.

@nythrox nythrox self-assigned this Mar 27, 2023
@nythrox nythrox closed this as completed Mar 28, 2023
@github-project-automation github-project-automation bot moved this to Done in Capi Mar 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
Status: Done
Development

No branches or pull requests

3 participants