Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml This function is applied to too many arguments

Tags:

module

ocaml

This is just a simple program I wrote to try to get a better understanding about modules. I'm trying to call the toS function with Id("a",Int) but it seems like I can write a type ast like this. What may be the problem?

module Typ =
struct
  type typ = Int | Bool
end

module Symbol =
struct
  type t = string
end

module Ast =
struct
  type ast = Const of int * Typ.typ | Id of Symbol.t * Typ.typ
  let rec toS ast = match ast with Id(a,b) -> "a"
    |_->"b"
end


Ast.toS Id("a",Int)
like image 766
otchkcom Avatar asked Dec 01 '25 02:12

otchkcom


1 Answers

You are getting an error because you did not surround your type constructor with parens in the function application. However, you must also reference type constructors by fully qualified names outside the module they're defined in. I.e.

Ast.toS (Ast.Id("a",Typ.Int))

Alternatively, you can open the modules. But this is considered bad practice. I.e.

open Id
open Typ
Ast.toS (Id("a",Int))
like image 106
rgrinberg Avatar answered Dec 04 '25 09:12

rgrinberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!