question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typing improvement for `letrec` combinator

See original GitHub issue

🚀 Feature Request

Improve typing of tie function in letrec combinator by expanding the type signature with the following, allowing it to read from the generic T.

tie: {
    <K extends keyof T>(key: K): Arbitrary<T[K]>;
    (key: string): Arbitrary<unknown>;
}

// so the full signature becomes:
export declare function letrec<T>(
  builder: (tie: {
    <K extends keyof T>(key: K): Arbitrary<T[K]>;
    (key: string): Arbitrary<unknown>;
  }) => { [K in keyof T]: Arbitrary<T[K]> },
): { [K in keyof T]: Arbitrary<T[K]> };

Motivation

The way how tie function of letrec combinator is typed means that we have to explicitly cast result of each call to that function. This way it is only needed to specify all return types of all arbitraries in T.

Example

For example, reusing letrec tree from docs.

import * as fc from 'fast-check';

const fst = fc.letrec((tie) => ({
  tree: fc.oneof(tie('node'), tie('leaf'), tie('leaf')),
  node: fc.record({
    left: tie('tree'),
    right: tie('tree'),
  }),
  leaf: fc.nat(),
}));

type Tree = number | { left: Tree; right: Tree };
const snd = fc.letrec<{
  tree: Tree;
  node: { left: Tree; right: Tree };
  leaf: number;
}>((tie) => ({
  tree: fc.oneof(tie('node'), tie('leaf'), tie('leaf')),
  node: fc.record({
    left: tie('tree'),
    right: tie('tree'),
  }),
  leaf: fc.nat(),
}));

In the first example, without explicit type specification, it all still defaults to unknown. However, in the second example with explicit type specification, everything becomes well typed.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dubzzzcommented, May 31, 2022

Released in v3! Once again thanks a lot for the help on those typings

1reaction
dubzzzcommented, May 19, 2022

See comment on #1572, but I may have found a way to make it real for v3 🤞

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypingAcademy: Learn touch typing with our free typing practice
Learn to type with 10 fingers and practice your typing skills online. We offer everything you need to master touch typing with ease....
Read more >
Learn To Type And Improve Typing Speed Free - Typing.com
Learn to touch type and improve your typing speed with free interactive typing lessons for all ages. Start your typing practice now!
Read more >
printf in ocaml
Unfortunately, printf poses a problem for a strongly, statically typed language ... Female -> "Female";; let rec get_sex() = let str = print_string...
Read more >
Typing.io: Typing Practice for Programmers - Hacker News
I have used this one before to practice typing on a split keyboard with chords. Never made it to proficiency but it worked...
Read more >
Typing Practice
Take a typing test, practice typing lessons, learn to type faster.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found