Skip to content

Add function statics and object callable properties #55

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

Merged
merged 8 commits into from
Jun 5, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,36 @@ function add(x, y) {
}
```

## Function statics and object callable properties

### Flow

You can use objects with callable properties as function with statics. This is useful when annotating memoized functions.

```js
type MemoizedFactorialType = {
cache: {
[number]: number,
},
[[call]](number): number, // callable property

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think [[call]]: (number) => number is a more commonly used syntax. Flow also supports {(number) => number} Maybe add examples for all syntaxes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose you are referring to the $call property syntax?

type MemoizedFactorialType = {
  cache: {
    [number]: number,
  },
  (number) => number, // is a syntax error
}

Try Flow

The $call property syntax is deprecated and removed therefore I think we should leave that out.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose you are referring to the $call property syntax?

type MemoizedFactorialType = {
  cache: {
    [number]: number,
  },
  (number) => number, // is a syntax error
}

Try Flow

The $call property syntax is deprecated and removed therefore I think we should leave that out.

Sorry, I should've been more clear. What I meant was include all the "syntaxes":

  1. Your original example: { [[call]](number): number } (explicit call property)
  2. The more common format of the above: { [[call]]: (number) => number } (explicit call property)
  3. Implicit call property: { (number): number } (implicit call property).

The reason why I suggested the second example is because I think it's more common in libdefs (maybe I am wrong), too. Also, I think it might be the easiest to read/notice from the three, but maybe that's just my bias.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Sorry about misreading the last comment. Will work on integrating them.

}

const factorial: MemoizedFactorialType = n => {
if (!factorial.cache) {
factorial.cache = {}
}
if (factorial.cache[n] !== undefined) {
return factorial.cache[n]
}
factorial.cache[n] = n === 0 ? 1 : n * factorial(n - 1)
return factorial.cache[n]
}
```

### TypeScript

Not supported now, [proposal to tc39](https://github.com/microsoft/TypeScript/issues/25628).

## Read-only Types

### Flow
Expand Down