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

Should we have this? #68

Closed
Mouvedia opened this issue Jan 28, 2016 · 19 comments · Fixed by #137
Closed

Should we have this? #68

Mouvedia opened this issue Jan 28, 2016 · 19 comments · Fixed by #137

Comments

@Mouvedia
Copy link
Collaborator

Mouvedia commented Jan 28, 2016

proposal comment

related discussion: microsoft/TypeScript#3694

@Mouvedia Mouvedia changed the title Should we have this? Should we have *this*? Jan 28, 2016
@Mouvedia Mouvedia changed the title Should we have *this*? Should we have this? Jan 28, 2016
@ericelliott
Copy link
Owner

Cool. Looking over a few of their examples, they make a good case (.bind(), .apply() w/mixins). I'm open to it.

Want to write up a few example use cases?

@Mouvedia
Copy link
Collaborator Author

It's not a rhetorical question. This one is out of my league.
I suppose it's missing, that's it.

@maiermic
Copy link

Yes, we should. this is like regular parameters an argument that is passed to a function call. We should be able to type it.

Things that should be considered:

Ps: I just quickly scaned the TypeScript discussion yet.

@ericelliott
Copy link
Owner

@maiermic In most cases, this should not be typed by the function using this, but instead, define some typed subset of this. Because this is determined by the call-site usage, it's impossible for the user of this to accurately determine its full exact shape.

@maiermic
Copy link

maiermic commented Feb 1, 2016

it's impossible for the user of this to accurately determine its full exact shape.

@ericelliott Who is the user of this? The author of the function?

@Mouvedia
Copy link
Collaborator Author

Mouvedia commented Feb 1, 2016

What he's saying is that, unless you have a way to declare the executing context, this can be (re-)set to anything. So using this would require to have its pendant: the context himself declared in the documentation and tightly coupled somehow to the function signature.

That's why it's only useful—without any prior context setting—when the function doesn't expect something in particular.

@amcdnl
Copy link

amcdnl commented Mar 10, 2016

In a lot of languages this is considered redundant.

@Mouvedia
Copy link
Collaborator Author

Mouvedia commented May 1, 2016

I am closing this for now and moving it to the backlog: it's not essential for the MVP.

@Mouvedia Mouvedia closed this as completed May 1, 2016
@Mouvedia Mouvedia added this to the backlog milestone May 1, 2016
@Mouvedia Mouvedia added the icebox label May 1, 2016
@Mouvedia Mouvedia removed this from the backlog milestone May 1, 2016
@maiermic
Copy link

maiermic commented May 4, 2016

@amcdnl

In a lot of languages this is considered redundant.

Any example of a language with type annotations where this is not bound to the method? In statically typed languages like Java and C# this is of the enclosing class or interface type.

This was referenced Mar 29, 2017
@Mouvedia
Copy link
Collaborator Author

Mouvedia commented Apr 1, 2017

In light of #120 and the discussion that ensued on #110, I am reopening this.

@Mouvedia
Copy link
Collaborator Author

Mouvedia commented Apr 1, 2017

I am gonna re-introduce the problem at hand for the ones dropping by.

The main use case that we need to cover is—as explained by @ericelliott before—being able to restrict the type of "the call-site".

Now here's my take on this (pun intended), I think it would make sense to place it just before the function signature. e.g. <here> signatureName(paramName: Type) => ReturnType

If we really want to make it obvious we could reuse something recognizable by any JS dev:

// assumes Example has been defined

// either
Example.signatureName(paramName: Type) => ReturnType
// or
Example[signatureName(paramName: Type) => ReturnType]

I don't particularly support this proposal. The priority for me is the position/order.
Which means that the introduction of a new operator would do just as well:

Example :: signatureName(paramName: Type) => ReturnType

@ericelliott
Copy link
Owner

ericelliott commented Apr 1, 2017

The :: syntax seems promising for this, mirroring the ECMAScript This-Binding Syntax proposal:

// Iterable::first() => a

const first = function () {
  const [a] = this;
  return a;
};

first.call([1,2,3]); // 1

@Mouvedia
Copy link
Collaborator Author

Mouvedia commented Apr 2, 2017

I forgot about that proposal but now that you remind me I did participate in tc39/proposal-bind-operator#24

I like your example, it's simple and it's reusing a reserved type that has already been defined, much clearer than mine.

The problem will be the error case. The way it will be handled is tool-dependent hence off-topic in the rtype specification.

@Mouvedia
Copy link
Collaborator Author

Mouvedia commented Apr 7, 2017

@ericelliott

  1. Which syntax(es) should be supported?
// A
Iterable::first() => a

// B
::Iterable.first() => a

I am leaning toward A. Both is also a valid option.

  1. Are spaces allowed around ::?

If so, shouldn't it be recommended?

@ericelliott
Copy link
Owner

ericelliott commented Apr 8, 2017

I think you're misunderstanding the second syntax. In the TC39 proposal, the ::map() syntax means that the return value from the previous expression will be bound to this inside map().

I'm not sure how we'd use that in Rtype.

@Mouvedia
Copy link
Collaborator Author

Mouvedia commented Apr 8, 2017

obj::func(val)
// is equivalent to:
func.call(obj, val)

::obj.func(val)
// is equivalent to:
func.call(obj, val)

Are you telling me that https://babeljs.io/docs/plugins/transform-function-bind/#detail is erroneous?

@ericelliott
Copy link
Owner

OK, but not sure how that makes sense.

::obj.func(val)
// is equivalent to:
func.call(obj, val)
// is equivalent to:
obj.func(val)

@Mouvedia
Copy link
Collaborator Author

Mouvedia commented Apr 22, 2018

You were right it was a typo:

::obj.func(val)
// is equivalent to:
obj.func.call(obj, val)

@Mouvedia
Copy link
Collaborator Author

@ericelliott #137

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

Successfully merging a pull request may close this issue.

4 participants