Dear Damien,

 if works
   then (get_ftag "funny",get_rtag "funny")
   else (Ftag "funny", Rtag (ref "funny"))
 in

In this expression, you have four interesting subexpressions.  Three of
them involve calling some function with "funny" as argument, and these
functions allocate values that point to the "funny" string.  These values
are heap-allocated and behave as you expect.

The fourth one is (Ftag "funny").  This is a constant expression and its
evaluation does not allocate in the heap.  Since it's constant, it's
allocated once by the compiler, and every time you execute this code
it's the same value that is returned.

How come (Ftag "funny") is regarded as constant while
(Rtag (ref "funny")) is not? After all, strings are mutable in OCaml,
so there really is not that much of a conceptual difference between a
string and a string ref in that respect:

http://caml.inria.fr/pub/docs/manual-ocaml/manual010.html#toc44

# type funny_str = Ftag of string;;
type funny_str = Ftag of string
# let s1 = Ftag "Hello";;
val s1 : funny_str = Ftag "Hello"
# let Ftag s = s1 in s.[0]<-'h';;
- : unit = ()
# s1;;
- : funny_str = Ftag "hello"

The way I read the spec, it nowhere says that variant values that use a
non-constant constructor plus a value can be treated as constant. I do
see that in a sense, this may be a similar issue as the one that would
arise with lisp code such as this:

(defun example ()
  (let ((text-segment-list '(1 2 3 4 5)))
    (nreverse text-segment-list)))

Calling (example) twice gives weird behaviour, as we are destructively
modifying a lisp that conceptually was constant. So, one should have
used:

(defun example2 ()
  (let ((text-segment-list (list 1 2 3 4 5)))
    (nreverse text-segment-list)))

But the problem I think I have with OCaml is: there just seems to be no
way to properly express the conceptual difference between '(1 2 3 4 5)
and (list 1 2 3 4 5): All I can say above is: Ftag "Hello".

--
best regards,
Thomas Fischbacher
[email protected]

--
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

Reply via email to