* [Caml-list] baffled by semicolon
@ 2004-08-26 3:22 briand
2004-08-26 3:30 ` David Brown
` (2 more replies)
0 siblings, 3 replies; 10+ messages in thread
From: briand @ 2004-08-26 3:22 UTC (permalink / raw)
To: caml-list
This might really by a lablgtk2 question, but I thought it's probably
more of a ocaml syntax question :
The following works :
let window = GWindow.window ~width:400 ~height:400()
;;
let area = GMisc.drawing_area ~packing:window#add ()
;;
let w = area#misc#realize ()
;
area#misc#window;;
let drawing = new GDraw.drawable w
;;
However making the simple change
let w = area#misc#realize ()
;;
^^ notice the double semicolon gives an error:
This expression has type unit but is here used with type
[> `drawable ] Gobject.obj
For the simple reason that w has somehow, mysteriously, become = ()
instead of Gdk.window which is the value it would acquire if it was
only a single semi-colon.
I went back through the manual and really couldn't find anything which
explained the difference between ; and ;; - so it's not at all obvious
to me what's going on here. I would think that w would take on the value of evaluating
area#misc#realize ()
in either case.
???
Brian
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] baffled by semicolon
2004-08-26 3:22 [Caml-list] baffled by semicolon briand
@ 2004-08-26 3:30 ` David Brown
2004-08-26 4:18 ` Alex Valdez
2004-08-30 7:12 ` Baffeld by manual (Was: [Caml-list] baffled by semicolon) Florian Hars
2 siblings, 0 replies; 10+ messages in thread
From: David Brown @ 2004-08-26 3:30 UTC (permalink / raw)
To: briand; +Cc: caml-list
On Wed, Aug 25, 2004 at 08:22:33PM -0700, briand@aracnet.com wrote:
> let area = GMisc.drawing_area ~packing:window#add ()
> ;;
>
> let w = area#misc#realize ()
> ;
Without looking at the library you are using, I would guess that the
'realize' method doesn't take an argument. As such, the () is extraneous,
although I'm not sure how it is parsing.
The 'drawing_area" is a function and not a method, and is required to have
at least one required argument.
Dave
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 10+ messages in thread
* RE: [Caml-list] baffled by semicolon
2004-08-26 3:22 [Caml-list] baffled by semicolon briand
2004-08-26 3:30 ` David Brown
@ 2004-08-26 4:18 ` Alex Valdez
2004-08-26 5:05 ` briand
2004-08-30 7:12 ` Baffeld by manual (Was: [Caml-list] baffled by semicolon) Florian Hars
2 siblings, 1 reply; 10+ messages in thread
From: Alex Valdez @ 2004-08-26 4:18 UTC (permalink / raw)
To: briand, caml-list
The semicolon separates a series of expressions. The value of a series of
expressions separated by semicolons is the value of the last expression.
Every expression except the last must evaluate to unit.
The following code snippet
let w = area#misc#realize ()
;
area#misc#window;;
is the same as
let w = area#misc#realize ( () ; area#misc#window );;
which is the same as
let w = area#misc#realize area#misc#window;;
-----Original Message-----
From: owner-caml-list@pauillac.inria.fr
[mailto:owner-caml-list@pauillac.inria.fr]On Behalf Of
briand@aracnet.com
Sent: Wednesday, August 25, 2004 10:23 PM
To: caml-list@pauillac.inria.fr
Subject: [Caml-list] baffled by semicolon
This might really by a lablgtk2 question, but I thought it's probably
more of a ocaml syntax question :
The following works :
let window = GWindow.window ~width:400 ~height:400()
;;
let area = GMisc.drawing_area ~packing:window#add ()
;;
let w = area#misc#realize ()
;
area#misc#window;;
let drawing = new GDraw.drawable w
;;
However making the simple change
let w = area#misc#realize ()
;;
^^ notice the double semicolon gives an error:
This expression has type unit but is here used with type
[> `drawable ] Gobject.obj
For the simple reason that w has somehow, mysteriously, become = ()
instead of Gdk.window which is the value it would acquire if it was
only a single semi-colon.
I went back through the manual and really couldn't find anything which
explained the difference between ; and ;; - so it's not at all obvious
to me what's going on here. I would think that w would take on the value of
evaluating
area#misc#realize ()
in either case.
???
Brian
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives:
http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ:
http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 10+ messages in thread
* RE: [Caml-list] baffled by semicolon
2004-08-26 4:18 ` Alex Valdez
@ 2004-08-26 5:05 ` briand
2004-08-29 4:45 ` William Lovas
0 siblings, 1 reply; 10+ messages in thread
From: briand @ 2004-08-26 5:05 UTC (permalink / raw)
To: Alex Valdez; +Cc: caml-list
>>>>> "Alex" == Alex Valdez <alexandervaldez@sbcglobal.net> writes:
Alex> The following code snippet
Alex> let w = area#misc#realize () ;
Alex> area#misc#window;;
Alex> is the same as
Alex> let w = area#misc#realize ( () ; area#misc#window );;
interesting. Somehow I managed to edit it poorly, in the file I
copied it from, the original statement is actuallly :
let w = area#misc#realize () ; area#misc#window
and the next statement is a let, so the ;; is not really needed, I
just like to put them in for clarity.
Alex> which is the same as
Alex> let w = area#misc#realize area#misc#window;;
Neither of the above works properly :
This expression has type Gdk.window = [ `drawable | `gdkwindow ] Gobject.obj
but is here used with type unit
However your response woke up some neurons - why wouldn't it be :
let w = (area#misc#realize ()); area#misc#window ;;
Which also makes sense, the first statement being called for a
side-effect, and the second is the value for w.
ding-ding-ding we have a winner. The above _does_ work.
Brian
Alex> -----Original Message----- From:
Alex> owner-caml-list@pauillac.inria.fr
Alex> [mailto:owner-caml-list@pauillac.inria.fr]On Behalf Of
Alex> briand@aracnet.com Sent: Wednesday, August 25, 2004 10:23 PM
Alex> To: caml-list@pauillac.inria.fr Subject: [Caml-list] baffled
Alex> by semicolon
Alex> This might really by a lablgtk2 question, but I thought it's
Alex> probably more of a ocaml syntax question :
Alex> The following works :
Alex> let window = GWindow.window ~width:400 ~height:400() ;;
Alex> let area = GMisc.drawing_area ~packing:window#add () ;;
Alex> let w = area#misc#realize () ;
Alex> area#misc#window;;
Alex> let drawing = new GDraw.drawable w ;;
Alex> However making the simple change
Alex> let w = area#misc#realize () ;;
Alex> ^^ notice the double semicolon gives an error: This expression
Alex> has type unit but is here used with type [> `drawable ]
Alex> Gobject.obj
Alex> For the simple reason that w has somehow, mysteriously, become
Alex> = () instead of Gdk.window which is the value it would acquire
Alex> if it was only a single semi-colon.
Alex> I went back through the manual and really couldn't find
Alex> anything which explained the difference between ; and ;; - so
Alex> it's not at all obvious to me what's going on here. I would
Alex> think that w would take on the value of evaluating
Alex> area#misc#realize ()
Alex> in either case.
Alex> ???
Alex> Brian
Alex> ------------------- To unsubscribe, mail
Alex> caml-list-request@inria.fr Archives: http://caml.inria.fr Bug
Alex> reports: http://caml.inria.fr/bin/caml-bugs FAQ:
Alex> http://caml.inria.fr/FAQ/ Beginner's list:
Alex> http://groups.yahoo.com/group/ocaml_beginners
Alex> ------------------- To unsubscribe, mail
Alex> caml-list-request@inria.fr Archives: http://caml.inria.fr Bug
Alex> reports: http://caml.inria.fr/bin/caml-bugs FAQ:
Alex> http://caml.inria.fr/FAQ/ Beginner's list:
Alex> http://groups.yahoo.com/group/ocaml_beginners
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [Caml-list] baffled by semicolon
2004-08-26 5:05 ` briand
@ 2004-08-29 4:45 ` William Lovas
0 siblings, 0 replies; 10+ messages in thread
From: William Lovas @ 2004-08-29 4:45 UTC (permalink / raw)
To: caml-list
On Wed, Aug 25, 2004 at 10:05:49PM -0700, briand@aracnet.com wrote:
> [...]
> However your response woke up some neurons - why wouldn't it be :
>
> let w = (area#misc#realize ()); area#misc#window ;;
... and the easy way to remember the reason it parses this way is to recall
that O'Caml is a *function*al language, so *function* application binds
most tightly. :) So in ``area#misc#realize (); area#mist#window'', the
application of ``area#misc#realize'' -- and not the `;' -- gets the ().
It's the same as with things like ``f x + g 3'' (= ``(f x) + (g 3)'', not
``f (x + g) 3'', or any other weird thing like that).
Hope this helps.
cheers,
William
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 10+ messages in thread
* Baffeld by manual (Was: [Caml-list] baffled by semicolon)
2004-08-26 3:22 [Caml-list] baffled by semicolon briand
2004-08-26 3:30 ` David Brown
2004-08-26 4:18 ` Alex Valdez
@ 2004-08-30 7:12 ` Florian Hars
2004-08-30 8:06 ` skaller
2 siblings, 1 reply; 10+ messages in thread
From: Florian Hars @ 2004-08-30 7:12 UTC (permalink / raw)
To: briand; +Cc: caml-bugs, caml-list
briand@aracnet.com wrote:
> I went back through the manual and really couldn't find anything which
> explained the difference between ; and ;;
Yeah, this is a bug in section 1.5 "Imperative Features" of the manual. (IMHO,
the whole Part I is not what it claims to be and the Ocaml team should stop
calling it an introduction into the language, but I've said that before
http://caml.inria.fr/archives/200210/msg00451.html)
Now, if you want to be anal, you might say that all that needs to be said is
indeed said in the manual, in section 6.7.2 "Control Structures"
| The expression expr1 ; expr2 evaluates expr1 first, then expr2, and returns
| the value of expr2.
together with section 6.11.2 "Structures":
| For compatibility with toplevel phrases (chapter 9) and with Caml Light, an
| optional ;; is allowed after each definition in a structure. The ;; has no
| semantic meaning. Also for compatibility, ;; expr is allowed as a component
| of a structure, meaning let _ = expr, i.e. evaluate expr for its
| side-effects.
But to find this section (and to see why it is indeed the answer to your
question) requires some ingenuity, since you are not dealing with a structure
proper, but with a compilation unit, and section 6.12 doesn't mention this
peculiarity. You have to guess its relevance from the "behaves roughly as".
With this knoledge, you can see let your version with a single semicolon
> let w = area#misc#realize ();
> area#misc#window;;
is equivalent to the definition:
let w = let _ = area#misc#realize () in area#misc#window
while the version with the double semicolon is
let w = area.misc.realize ()
let _ = area#misc#window
which binds w to unit and ignores the value of area#misc#window.
Yours, Florian
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Baffeld by manual (Was: [Caml-list] baffled by semicolon)
2004-08-30 7:12 ` Baffeld by manual (Was: [Caml-list] baffled by semicolon) Florian Hars
@ 2004-08-30 8:06 ` skaller
2004-08-30 8:23 ` Radu Grigore
2004-08-30 8:25 ` Ville-Pertti Keinonen
0 siblings, 2 replies; 10+ messages in thread
From: skaller @ 2004-08-30 8:06 UTC (permalink / raw)
To: caml-list; +Cc: briand, caml-bugs
On Mon, 2004-08-30 at 17:12, Florian Hars wrote:
> briand@aracnet.com wrote:
> > I went back through the manual and really couldn't find anything which
> > explained the difference between ; and ;;
The single ; is (usually) left associative binary sequencing
operator of type unit that takes two expressions of
type unit as an argument:
e1 ; e2
evaluates e1, then e2 (for side effects).
I said 'usually' because it has another role
in some contexts -- [();()] is a list of two
units whereas [(();())] is a list of one unit.
[This is like the ugly C hackery with , ]
The top level of Ocaml 'executes' statements in sequence.
For example:
let _ = e1
let _ = e2
The role of ;; has nothing to so with top level sequencing:
the sequencing is already built in.
The purpose of ;; is much simpler -- it is nothing
more than a piece of punctuation marking the end
of a statement.
As you can see above it is not required if
(a) there is a next statement and
(b) that next statement starts with a keyword
You may need ;; in the interpreter to tell it
'there isn't another statement, start evaluating'.
You may also need it if you use a statement
not starting with a keyword, for example here:
let x = ref 0
;; x := 1 (* doesn't start with a keyword *)
where you can see that ;; is more or less the keyword
you use to start a statement when it doesn't start
with a keyword.. that isn't quite correct, since you
don't need one at the start (its really a separator
which is 'infered' when the parser hits an unexpected
keyword :)
This code is actually very ugly because you can't
lift it out of the top level.
let _ =
let x = ref 0 in
x := 1
is the way to do this properly -- and here the x := 1
is an expression contained in the let/in expression.
--
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850,
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Baffeld by manual (Was: [Caml-list] baffled by semicolon)
2004-08-30 8:06 ` skaller
@ 2004-08-30 8:23 ` Radu Grigore
2004-08-30 8:25 ` Ville-Pertti Keinonen
1 sibling, 0 replies; 10+ messages in thread
From: Radu Grigore @ 2004-08-30 8:23 UTC (permalink / raw)
To: caml-list
On 30 Aug 2004 18:06:58 +1000, skaller <skaller@users.sourceforge.net> wrote:
> The single ; is (usually) left associative binary sequencing
> operator of type unit that takes two expressions of
> type unit as an argument:
Only the left argument should be unit (otherwise a warning is emitted
since the result is silently ignored). The result of "e1; e2" is
(except side effects) the same as the result of e2. So something like
this:
(); (); (); 1;;
is perfectly legal and safe. The expression:
1; 2;;
Gives a warning since the result of the first expression is ignored
implicitely. To silence the warning you write:
ignore (1); 2;;
i.e., you ignore it explicitely.
regards,
radu
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Baffeld by manual (Was: [Caml-list] baffled by semicolon)
2004-08-30 8:06 ` skaller
2004-08-30 8:23 ` Radu Grigore
@ 2004-08-30 8:25 ` Ville-Pertti Keinonen
2004-08-30 11:07 ` skaller
1 sibling, 1 reply; 10+ messages in thread
From: Ville-Pertti Keinonen @ 2004-08-30 8:25 UTC (permalink / raw)
To: skaller; +Cc: caml-list, briand
skaller wrote:
>The single ; is (usually) left associative binary sequencing
>operator of type unit that takes two expressions of
>type unit as an argument:
>
> e1 ; e2
>
>evaluates e1, then e2 (for side effects).
>
>
e1 is evaluated for side-effects, the value of e2 is the value of the
entire expression, it does not need to be of type unit.
You could think of ; as having the type unit -> 'a -> 'a
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Baffeld by manual (Was: [Caml-list] baffled by semicolon)
2004-08-30 8:25 ` Ville-Pertti Keinonen
@ 2004-08-30 11:07 ` skaller
0 siblings, 0 replies; 10+ messages in thread
From: skaller @ 2004-08-30 11:07 UTC (permalink / raw)
To: Ville-Pertti Keinonen; +Cc: caml-list, briand
On Mon, 2004-08-30 at 18:25, Ville-Pertti Keinonen wrote:
> skaller wrote:
> > e1 ; e2
> >
> >evaluates e1, then e2 (for side effects).
> >
> >
> e1 is evaluated for side-effects, the value of e2 is the value of the
> entire expression, it does not need to be of type unit.
>
> You could think of ; as having the type unit -> 'a -> 'a
Yup, that's more correct -- sry for prior inaccuracy.
--
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850,
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2004-08-30 11:07 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-26 3:22 [Caml-list] baffled by semicolon briand
2004-08-26 3:30 ` David Brown
2004-08-26 4:18 ` Alex Valdez
2004-08-26 5:05 ` briand
2004-08-29 4:45 ` William Lovas
2004-08-30 7:12 ` Baffeld by manual (Was: [Caml-list] baffled by semicolon) Florian Hars
2004-08-30 8:06 ` skaller
2004-08-30 8:23 ` Radu Grigore
2004-08-30 8:25 ` Ville-Pertti Keinonen
2004-08-30 11:07 ` skaller
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).