effect icon indicating copy to clipboard operation
effect copied to clipboard

From Discord: Pattern matching with typeguard - different array sizes

Open effect-bot opened this issue 8 months ago • 0 comments

Summary

Summary

  • User nemmind1 is experimenting with the Predicate.isTupleOf function from the Effect-TS library to achieve pattern matching on different sizes of elements, similar to Elm/Haskell.
  • They encountered an issue where the type inference for the matched elements is not working as expected. Specifically, the variable a is inferred as unknown instead of number.
  • User datner_ initially was not familiar with Predicate.isTupleOf and pointed out the difference between a Predicate and a Refinement.
  • User nemmind1 clarified that Predicate.isTupleOf is indeed a type guard (Refinement) and provided a link to the relevant documentation.
  • User datner_ acknowledged the feature and noted that the generic type T is not being properly inferred in tacit usage, providing a link to a playground example.
  • User datner_ expressed uncertainty about whether this issue can be resolved.

Key Takeaways

  1. Type Inference Issue: When using Predicate.isTupleOf in the Match.when function, the type of the matched element is inferred as unknown instead of the expected type (e.g., number).
  2. Refinement vs. Predicate: It's important to distinguish between a Predicate and a Refinement in TypeScript. A Refinement has type narrowing capabilities.
  3. Documentation and Features: The Predicate.isTupleOf function is a new feature in Effect-TS, and its usage and behavior are documented on the Effect-TS website.
  4. Unresolved Issue: The problem with type inference might be a limitation or bug in the current implementation, and it is unclear if or how it can be resolved.

Next Steps

  • Investigate further into the type inference mechanism of Predicate.isTupleOf to understand why the type is not being narrowed correctly.
  • Consider reaching out to the maintainers of the Effect-TS library for clarification or potential fixes.
  • Explore alternative approaches or workarounds for achieving the desired pattern matching behavior.

Discord thread

https://discord.com/channels/795981131316985866/1250722166882959381

effect-bot avatar Jun 13 '24 09:06 effect-bot