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

Private function parameters #52811

Open
5 tasks done
TomasHubelbauer opened this issue Feb 16, 2023 · 4 comments
Open
5 tasks done

Private function parameters #52811

TomasHubelbauer opened this issue Feb 16, 2023 · 4 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@TomasHubelbauer
Copy link

TomasHubelbauer commented Feb 16, 2023

Suggestion

πŸ” Search Terms

private function parameter

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Hi, I would like to propose adding private function parameters to TypeScript. Private function parameters can be only provided by the function itself when it calls itself recursively. Providing them when called in a different context is an error.

Private function parameters always come after public function parameters in the function signature.

πŸ“ƒ Motivating Example

https://twitter.com/TomasHubelbauer/status/1626321532187189248

function recursive(input: string; private state: State = { … }) {
  // …
  if (logic) {
    return recursive(input, { ...state, extra: true });
  }
  // …
}

// This is okay
recursive('test');

// This is an error
recursive('test', externalState);

// We have to do this now to achieve hiding `state` from the caller
function recursivePublic(input: string) {
  return recursive(input);
}

πŸ’» Use Cases

Enables writing recursive functions which need parameters to pass state between calls without having to private the "implementation" function which has the parameter for state and export a different function that has the same signature except for the state parameter(s) and is meant for outside callers.

There are two workarounds I am aware of:

  • Write another function which is public, make the original one private and export only the public one which doesn't have the state parameter in its sigature
    The shortcoming of the this workaround is that we need to introduce another function that is meant to be called by others which passes along the parameters to the implementation function which does the real processing but has the extra parameter(s) which cannot be exposed to non-self callers.

  • Use function within a function, call the local function when the state parameter is uninitialized and handover to it

    This is a special case of the above, we just move the implementation function into the wrapper function.

@whzx5byb
Copy link

whzx5byb commented Feb 16, 2023

Another workaround: using a function expression

const recursive: (input: string) => any = function _recursive(input: string, state: State = { … }) { 
// … 
if (logic) { 
  return _recursive(input, { ...state, extra: true }); 
} 
// … 
}

@TomasHubelbauer
Copy link
Author

@whzx5byb This works, but it has the same downside as the workarounds I mentioned: you nee to repeat the signature. I am hoping TypeScript can be extended with syntactic sugar that would make it unnecessary to repeat the signature. It would greatly help with readability of complicated function signatures, such as when the public signature of the function (the one without the state parameters) has a lot of parameters or even has overloads.

@RyanCavanaugh
Copy link
Member

It's sort of interesting that, AFAIK, no functional language has this capability, which you'd think would be in more use given how often the accumulator pattern appears in that style

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Feb 17, 2023
@Josh-Cena
Copy link
Contributor

Wellβ€”if your functions are auto-curried and/or there's proper HM style type inference, the ergonomics issue of duplicating signatures is largely moot...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants