Version franaise
Home About Download Resources Contact us
Browse thread
The Implicit Accumulator: a design pattern using optional arguments
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Mattias_Engdegrd <mattias@v...>
Subject: Re: [Caml-list] The Implicit Accumulator: a design pattern using optional arguments
>I don't have a top-level with me right now, so I'm shooting from the 
>hip, but I don't think it is a compile-time error to attempt to use the 
>referential equality operator on two different types.

No, (==) : 'a -> 'a , so this would work:

module Sym : sig
  type t 
  val symbol : string -> t  
  val str : t -> string
end = struct 
  type t = string
  let symbol =
    let m = Hashtbl.create 1 in
      fun s ->
       try Hashtbl.find m s
       with Not_found -> (Hashtbl.add m s s; s)
  let str s = s
end;;

# "toto" == "alpha" ;;
- : bool = false
# Sym.symbol "alpha" == Sym.symbol "alpha" ;;
- : bool = true
# Sym.symbol "alpha" == "alpha" ;;
Characters 22-29:
  Sym.symbol "alpha" == "alpha" ;;
                        ^^^^^^^
This expression has type string but is here used with type Sym.t

The cost is a slightly clumsier use of symbols as strings (Symbol.str),
but my feeling is that the extra safety is worth it.