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

Allow application in combination with dynamic (?) operator #543

Closed
5 of 6 tasks
alfonsogarciacaro opened this issue Feb 23, 2017 · 4 comments
Closed
5 of 6 tasks

Comments

@alfonsogarciacaro
Copy link

alfonsogarciacaro commented Feb 23, 2017

I propose we improve dynamic programming with F#. In Fable, we're almost there with the use of three operators:

  • ? dynamic operator, defined in standard F# but implemented by Fable
  • !! dynamic casting, defined by Fable and equivalent to unbox<'t>
  • $ operator to apply arguments directly to an obj type.

With these operators you can do something like this:

let result: string = myObject?foo$(4, 5, 6)

// JS: var result = myObject.foo(4, 5 6)

Note that Fable deconstructs the tuple literal into multiple arguments

However, the $ operator has problems. Besides not being very nice, the precedence rules don't let you do something like myObject?foo?bar$(4, 5, 6).

Fable is solving this at the moment by implementing ? with the following signature: `obj->string->(obj->obj). This means the user can now do something like this:

let result: string = !!myObject?foo?bar(4, 5, 6)

This is very close to a dynamic experience and makes it very easy to temporarily cancel type checking when the user needs to interact with untyped code. However this hack is still no ideal, there are two main problems.

  • When doing let x = myObject?foo, users expect x to be of type obj, not obj->obj.
  • The F# compiler creates unnecessary closures that Fable needs to track and eliminate.

It would be very useful if the F# compiler allowed directly the application of any number of arguments in combination with the ? operator without the Fable hack. This would be similar to the currently available combination of ? and assignment.

let (?<-) (o: obj) (v: obj): unit = ...

myObj?foo <- 5

Probably a similar syntax should be used, but I'm not sure how it should be:

let (?()) (o: obj) ([<ParamArray>] args: arg[]): unit = ...

myObj?foo(1, 2)
  • Pros: It'd be much easier to cancel type checking in Fable if necessary
  • Cons: Dynamic programming is always risky, but libraries will still have to implement the ? on their own, so the risk is mitigated for standard code
  • Estimated cost: M-L

Affadavit (must be submitted)

  • This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I would be willing to help implement and/or test this
  • I or my company would be willing to help crowdfund F# Software Foundation members to work on this
@Rickasaurus
Copy link

I wouldn't mind having something like this kicking around, but I'd rather it wasn't in my namespace by default and would require you to open a module first.

@alfonsogarciacaro
Copy link
Author

alfonsogarciacaro commented Feb 23, 2017

@Rickasaurus Absolutely! Maybe I was not clear in the description, but this is not proposing that ? should be included in the FSharp.Core. ? will remain unimplemented as it's now, but (if the suggestion is accepted) it will be possible for libraries to implement ? + application (the same way it's possible to implement ? + assignment right now).

Right now the ? operator for Fable is defined in the Fable.Core.JsInterop module and you need to open it explicitly in order to access it.

@dsyme
Copy link
Collaborator

dsyme commented Jun 16, 2022

@alfonsogarciacaro Is there still any need for this from the Fable side?

@alfonsogarciacaro
Copy link
Author

@dsyme The current workaround seems to be working well and I didn't get any feedback from users on this regard for years, so if it's not needed for anything else, we can close the suggestion. Thanks a lot for reviewing!

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

No branches or pull requests

3 participants