Quyen
Quyen

Reputation: 1373

Idea working in types

Please see details in My previous question

1) cpf0.ml:
type string = char list
type name = string
type symbol =
| Symbol_name of name

2) problem.ml:

type symbol =
  | Ident of Cpf0.string

In this problem.ml it has two definitions for type string, and surely it's giving me an error, but is it posible that I can make them have a same type? I need an idea.

module Str = struct type t = string end;;
module StrOrd = Ord.Make (Str);;
module StrSet = Set.Make (StrOrd);;
module StrMap = Map.Make (StrOrd);;

module SymbSet = Set.Make (SymbOrd);;
let rec ident_of_symbol = function
  | Ident s -> s

let idents_of_symbols s =
  SymbSet.fold (fun f s -> StrSet.add (ident_of_symbol f) s) s StrSet.empty;;

This expression has type Cpf0.string = char list but an expression was expected of type Util.StrSet.elt = string

Upvotes: 2

Views: 83

Answers (2)

string is a predefined type in Ocaml (ie in the Pervasives module); it is e.g. the type of string literal constants like "this string". Use some other name (otherwise you, and any one reading your code, will be very confused)

Upvotes: 1

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66813

You can use the name "string" for different types in different modules if you like, though (as Basile Starynkevitch points out) it's confusing. It would be better to pick a different name. If you really need to reuse the name, you can specify the module every time. If you don't specify a module, you'll get the predefined meaning (or the meaning from the innermost opened module).

It seems to me the problem in your quoted code is that this line:

module Str = struct type t = string end;;

doesn't specify a module name for string, so it refers to the predefined string. It seems possible you wanted to say:

module Str = struct type t = Cpf0.string end;;

It's hard to tell, however. There's not enough context for me to really understand what you're trying to do.

Upvotes: 2

Related Questions