Skip to content

Conversation

@kazupon
Copy link
Member

@kazupon kazupon commented Sep 2, 2017

What kind of change does this PR introduce? (check at least one)

  • Bugfix
  • Feature
  • Code style update
  • Refactor
  • Build-related changes
  • Other, please describe:

Does this PR introduce a breaking change? (check one)

  • Yes
  • No

If yes, please describe the impact and migration path for existing applications:

The PR fulfills these requirements:

If adding a new feature, the PR's description includes:

  • A convincing reason for adding this feature (to avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it)

Other information:

const cache: Object = Object.create(null)
return (function cachedFn (str: string) {
const hit = cache[str]
const hit: any = cache[str]
Copy link
Member

Choose a reason for hiding this comment

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

Does this affect anything?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think there is any problem. 🤔
In fact it can return various types of values.

case1: Regex

const buildRegex = cached(delimiters => {

case2: Object

export const parseStyleText = cached(function (cssText) {

@yyx990803
Copy link
Member

Hmm, I personally don't like annotating variable declarations when it is obvious enough. Flow is also smart enough to infer these cases I believe. It's more valuable to annotate function arguments because those are not immediately obvious when the definition of the function is separate from the call site.

@HerringtonDarkholme
Copy link
Member

Hi @kazupon. As far as I know, most languages prefer inference over annotation, at declaration site.

The reasoning for inference:

  1. type is already obvious and explicit from initializer, e.g. parseFloat, isObject is very clear.
  2. additional annotation is duplication. If we later want to change initializer, we have to also change annotation.
  3. annotating everywhere can make reader confused. Readers might think universal annotation is code authors' quirk so no more info brought by annotation. But some annotations do have some useful information. Consider code let i: Context = {}; i.field = fieldInfo; and let i: number = 42. The former annotation does express additional information.

There is also good advice for when to add annotation, though in scala (we can skip scala specific rule like implicit).
https://github.com/databricks/scala-style-guide#type-inference

@kazupon
Copy link
Member Author

kazupon commented Sep 5, 2017

@HerringtonDarkholme
Thank you for great explanations!
Indeed.

I seemed to have been lack of knowledge about type annotation names yet.
I close this PR.
Sorry for confusing you. 🙇

@kazupon kazupon closed this Sep 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants