ponylang / rfcs

RFCs for changes to Pony
https://ponylang.io/
61 stars 48 forks source link

Request: Use primitives methods as lambda calls #164

Closed damon-kwok closed 4 years ago

damon-kwok commented 4 years ago

Primitives methods as lambda

If you call the primitive method without parentheses, the call is automatically wrapped as lambda

Example

primitive Str
   // Tweak whitespace
  fun trim (s: String): String =>
    ...

actor Main
  new create(env: Env) =>
    Seqs.map([" a "; " b "; " c "], Str.trim)   //Result:  ["a"; "b"; "c"]
SeanTAllen commented 4 years ago

Can you provide some motivation for why this would be desirable? Every RFC should contain some motivation as to why it is desirable. Even requests.

Please note in case you are aware. Opening an issue such as this is a request for another human (other than you) to write up the full RFC so the more details you provide them, the better.

SeanTAllen commented 4 years ago

I'm not sure of your motivation, but I suspect that you can already do it using partial application:

primitive Foo
  fun hello(x: String): String =>
    "hello " + x

actor Main
  new create(env: Env) =>
    print_with_y(env.out, Foo~hello())

  fun print_with_y(stream: OutStream, f: {(String): String} val) =>
    stream.print(f("there"))

Runnable at the playground:

https://playground.ponylang.io/?gist=7ac613b68b129c7588e828e904fcbd03

damon-kwok commented 4 years ago

Thanks @SeanTAllen This is exactly what I wanted. I am so sorry for this issue.