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

feature request - Haskell's $ operator #2415

Closed
vicuna opened this Issue Apr 5, 2004 · 8 comments

Comments

Projects
None yet
2 participants
@vicuna
Copy link
Collaborator

vicuna commented Apr 5, 2004

Original bug ID: 2415
Reporter: administrator
Assigned to: @lefessan
Status: closed (set by @xavierleroy on 2015-12-11T18:04:38Z)
Resolution: fixed
Priority: normal
Severity: feature
Fixed in version: 4.00.0+dev
Category: ~DO NOT USE (was: OCaml general)
Monitored by: mehdi @glondu @dbuenzli

Bug description

Full_Name: Charles Martin
Version:
OS:
Submission from: 65.57.245.11 (65.57.245.11)

FEATURE REQUEST

Would love to have Haskell's $ operator added to OCaml. Definining it myself as

let ($) f x = f x

doesn't work because of precedence and associativity.

Charles

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Feb 21, 2007

Comment author: ertai

Would love to have it too. Simple and useful.

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Feb 22, 2007

Comment author: @johnwhitington

Make use of the fact that & is deprecated for logical AND and write:

let ( & ) a b = a b

You can then write

f a b & g c & h 0

or whatever. Also, it can be read as "and" as in "reverse and flatten and fold" etc.

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Feb 22, 2007

Comment author: ertai

Cool, thanks for the solution...

Will using it from now and kill some parentheses :)

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Mar 10, 2007

Comment author: @dbuenzli

I really like it aswell, can make some code much clearer. Would be nice if the fun call could be optimized away.

Daniel

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Mar 10, 2007

Comment author: ertai

Here is a bit of Camlp4 (compile with the version from CVS) to do that:

$ cat pa_app.ml
open Camlp4.PreCast;
AstFilters.register_str_item_filter
(Ast.map_expr
(fun
[ <:expr@loc< & $e1$ $e2$ >> -> <:expr@loc< $e1$ $e2$ >>
| e -> e ]))#str_item;

$ ocamlc -pp camlp4rf -c -I +camlp4 pa_app.ml
$ camlp4o ./pa_app.cmo -str "f a b & g c & h 0"
f a b (g c (h 0))

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Mar 20, 2012

Comment author: @lefessan

This feature was implemented by Xavier Clerc in 3.12.1+dev and I backported it to trunk in commit r12070.

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented May 18, 2012

Comment author: jessicah

Can the feature also include defining the actual function/operator, not just the compiler primitive? From what I've been able to see, one hasn't been defined in pervasives.

And whilst (&) is deprecated, I'd recommend against using this, as it conflicts with jocaml.

Is there any reason ocaml can't add the ($) operator (and change the needed precedence/associativity rules)?

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented May 18, 2012

Comment author: @garrigue

There is a very good reason not to change the associativity for ($):
lots of people already use it in their code, for various applications.
It has the right associativity if you use it for left-to-right composition
for instance.
Moreover, $ is used by camlp4, so it cannot be reserved for something else.
If you are ready to use 2-character combinations, there are plenty of possibilities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.