* [Caml-list] using infix operators/functions in modules when not using "open"
@ 2012-04-26 22:36 ocamllist.robertwork
2012-04-26 23:01 ` Haoyang Wang
0 siblings, 1 reply; 5+ messages in thread
From: ocamllist.robertwork @ 2012-04-26 22:36 UTC (permalink / raw)
To: caml-list
[-- Attachment #1: Type: text/plain, Size: 447 bytes --]
I'm a OCaml newbie. I don't understand how to use infix functions that are
in modules when I don't want to use "open".
I did discover http://xahlee.org/ocaml/functions.html which explains that
an infix operator can be used as a prefix. So, this format:
((Module.(op)) "foo" "bar")
does seem to work. But, intuitively, it seems like:
"foo" Module.(op) "bar"
would work -- it doesn't.
Is there a way to keep the infix notation?
Thanks.
[-- Attachment #2: Type: text/html, Size: 754 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] using infix operators/functions in modules when not using "open"
2012-04-26 22:36 [Caml-list] using infix operators/functions in modules when not using "open" ocamllist.robertwork
@ 2012-04-26 23:01 ` Haoyang Wang
2012-04-27 0:28 ` Anthony Tavener
2012-04-27 6:57 ` Gabriel Scherer
0 siblings, 2 replies; 5+ messages in thread
From: Haoyang Wang @ 2012-04-26 23:01 UTC (permalink / raw)
To: caml-list
On Apr 26, 2012, at 3:36 PM, ocamllist.robertwork@spamgourmet.com wrote:
> I'm a OCaml newbie. I don't understand how to use infix functions that are in modules when I don't want to use "open".
>
> I did discover http://xahlee.org/ocaml/functions.html which explains that an infix operator can be used as a prefix. So, this format:
>
> ((Module.(op)) "foo" "bar")
>
> does seem to work. But, intuitively, it seems like:
>
> "foo" Module.(op) "bar"
>
> would work -- it doesn't.
>
> Is there a way to keep the infix notation?
>
> Thanks.
Module.("foo" (op) "bar")
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] using infix operators/functions in modules when not using "open"
2012-04-26 23:01 ` Haoyang Wang
@ 2012-04-27 0:28 ` Anthony Tavener
2012-04-27 6:57 ` Gabriel Scherer
1 sibling, 0 replies; 5+ messages in thread
From: Anthony Tavener @ 2012-04-27 0:28 UTC (permalink / raw)
To: Haoyang Wang; +Cc: caml-list
[-- Attachment #1: Type: text/plain, Size: 1814 bytes --]
Haoyang Wang provided the answer you're looking for, I think!
In case you are writing your own modules with infix operators, I've found
the following style to be useful:
--- Inside Vector.ml ---
(* definition of some basic functions here... *)
(* now some infix defs using the basic functions, in "Ops" submodule *)
module Ops = struct
let ( +| ) u v = add u v
let ( -| ) u v = sub u v
let ( *| ) a v = scale a v
end
open Ops (* so that they are usable within Vector itself *)
---
--- Some other module without opening all of Vector ---
open Vector.Ops
let answer = a +| b
let same_answer = Vector.add a b
---
In some cases I'll also expose types in a Type submodule. This is to
minimize name-collision with record fields, but have direct access to the
fields in situations which it's appropriate (by doing "open Vector.Type",
for example).
Hope that's of some use!
-Tony
On Thu, Apr 26, 2012 at 5:01 PM, Haoyang Wang <hywang@pobox.com> wrote:
>
> On Apr 26, 2012, at 3:36 PM, ocamllist.robertwork@spamgourmet.com wrote:
>
> > I'm a OCaml newbie. I don't understand how to use infix functions that
> are in modules when I don't want to use "open".
> >
> > I did discover http://xahlee.org/ocaml/functions.html which explains
> that an infix operator can be used as a prefix. So, this format:
> >
> > ((Module.(op)) "foo" "bar")
> >
> > does seem to work. But, intuitively, it seems like:
> >
> > "foo" Module.(op) "bar"
> >
> > would work -- it doesn't.
> >
> > Is there a way to keep the infix notation?
> >
> > Thanks.
>
>
> Module.("foo" (op) "bar")
>
> --
> Caml-list mailing list. Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
[-- Attachment #2: Type: text/html, Size: 3405 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] using infix operators/functions in modules when not using "open"
2012-04-26 23:01 ` Haoyang Wang
2012-04-27 0:28 ` Anthony Tavener
@ 2012-04-27 6:57 ` Gabriel Scherer
2012-04-27 8:30 ` Goswin von Brederlow
1 sibling, 1 reply; 5+ messages in thread
From: Gabriel Scherer @ 2012-04-27 6:57 UTC (permalink / raw)
To: Haoyang Wang; +Cc: caml-list
> Module.("foo" (op) "bar")
This is a local open (a more explicit notation is "let open Module in
foo op bar"). I think this is a fine solution: I don't like open, but
local mitigates its flaws.
Another solution is to rebind your operator locally:
let (op) = Module.blah in
"foo" op "bar"
(of course Module.blah doesn't need to be an infix itself)
If you are going to use an infix operator in a wide scope, I think
that such a rebinding solution is nice because it's more explicit,
which is good when it helps users understand the code.
On Fri, Apr 27, 2012 at 1:01 AM, Haoyang Wang <hywang@pobox.com> wrote:
>
> On Apr 26, 2012, at 3:36 PM, ocamllist.robertwork@spamgourmet.com wrote:
>
>> I'm a OCaml newbie. I don't understand how to use infix functions that are in modules when I don't want to use "open".
>>
>> I did discover http://xahlee.org/ocaml/functions.html which explains that an infix operator can be used as a prefix. So, this format:
>>
>> ((Module.(op)) "foo" "bar")
>>
>> does seem to work. But, intuitively, it seems like:
>>
>> "foo" Module.(op) "bar"
>>
>> would work -- it doesn't.
>>
>> Is there a way to keep the infix notation?
>>
>> Thanks.
>
>
> Module.("foo" (op) "bar")
>
> --
> Caml-list mailing list. Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] using infix operators/functions in modules when not using "open"
2012-04-27 6:57 ` Gabriel Scherer
@ 2012-04-27 8:30 ` Goswin von Brederlow
0 siblings, 0 replies; 5+ messages in thread
From: Goswin von Brederlow @ 2012-04-27 8:30 UTC (permalink / raw)
To: Gabriel Scherer; +Cc: Haoyang Wang, caml-list
Gabriel Scherer <gabriel.scherer@gmail.com> writes:
>> Module.("foo" (op) "bar")
>
> This is a local open (a more explicit notation is "let open Module in
> foo op bar"). I think this is a fine solution: I don't like open, but
> local mitigates its flaws.
>
> Another solution is to rebind your operator locally:
>
> let (op) = Module.blah in
> "foo" op "bar"
>
> (of course Module.blah doesn't need to be an infix itself)
>
> If you are going to use an infix operator in a wide scope, I think
> that such a rebinding solution is nice because it's more explicit,
> which is good when it helps users understand the code.
And that also works more globally as in
let (op) = Module.blah
let foo x y = x (op) y
MfG
Goswin
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2012-04-27 8:30 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-26 22:36 [Caml-list] using infix operators/functions in modules when not using "open" ocamllist.robertwork
2012-04-26 23:01 ` Haoyang Wang
2012-04-27 0:28 ` Anthony Tavener
2012-04-27 6:57 ` Gabriel Scherer
2012-04-27 8:30 ` Goswin von Brederlow
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox