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

This is just one of the axioms of the language.  A single
string literal in OCaml source code yields the same value
each time it is evaluated, even though arguably analgous
constructs are handled differently:

 [1;2;3] == [1;2;3] may be true or false
 [|1;2|] == [|1;2|] will be false
 "const" == "const" will be false
 let f () = "const" in f () == f () will be true

I would rather have identical string constants merged, as many C
compilers do.  (I am aware that it is possible to write programs
with bugs.)  The compiler could also treat string constants like
array constants and generate a new value each time the expression
is evaluated.  In that case my last example would be false.

If you want to argue analogies, consider

  let f () = F "hello"

as an alias for something like

  let hello = [|'h';'e';'l';'l';'o'|] (* at top level *)

  let f () = F hello

The name hello is as constant as any other top level binding.

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

You can use Obj.dup to force a copy.  I don't know whether there is
a type safe interface.

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