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

Implicit return initial values #69

Closed
wants to merge 2 commits into from
Closed

Implicit return initial values #69

wants to merge 2 commits into from

Conversation

sebhochuli
Copy link

@sebhochuli sebhochuli commented Jul 1, 2019

@thecrypticace
Copy link

This would require a compilation step. Omitting the return in javascript means that the return is undefined. If you don't specify what you are returning then the Javascript engine doesn't know what you are trying to return and therefore returns nothing. These "hook" functions are just vanilla javascript.

In addition, I do not believe a compiler step enabling such a step would provide any meaningful benefit to one's workflow but it could hinder readability and understanding because javascript in general does not work this way.

@posva
Copy link
Member

posva commented Jul 1, 2019

This could be possible by making the functions (reactive, computed, etc) stateful but it causes problems:

  • They will not be generic usage functions that can be used outside of Vue anymore
  • They cannot be parallelized and couldn't be properly used in Web Workers in the future without using mutex or semaphores

This could be added to vue-loader but then the JS from the script section would not work without a compilation step, which is a gap we want to avoid

Finally, having the explicit return, also allows for switching different solutions easily while developing, by commenting out lines:

setup () {
  const { mouseX, mouseY } = useMouse()
  const { eyeX, eyeY } = useEyeTracking()
  
  // we can swap comments to switch what values are used
  return {
    x: mouseX,
    y: mouseY,
    // x: eyeX,
    // y: eyeY,
  }
}

@sebhochuli
Copy link
Author

sebhochuli commented Jul 1, 2019 via email

@bartlomiej-korpus
Copy link

Wouldn't it possibly hurt type inference in TypeScript in the future?

@posva
Copy link
Member

posva commented Jul 2, 2019

Wouldn't it possibly hurt type inference in TypeScript in the future?

Yes, an implicit return will break type inference as well

@phiter
Copy link

phiter commented Jul 8, 2019

I don't think that's a good idea, because you could have reactive value that you don't want to expose to the template when using in a component.
With return you specify what you want to use on your template, or what you want to return from a reactive function.

@posva
Copy link
Member

posva commented Jul 9, 2019

Since this goes against one of the main aspect of the API: typing, let's drop it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants