Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Camlp4-tutorial-Example
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-09-07 (15:52)
From: Daniel de Rauglaudre <daniel.de_rauglaudre@i...>
Subject: Re: [Caml-list] Camlp4-tutorial-Example

On Sat, Sep 07, 2002 at 01:10:53AM +0200, Oliver Bandel wrote:

> Yes, i've seen that it is possible to get back functions
> that, if applied, will give the resulting int.
> But a) it's again int and b) all functions have same type.
> What if I need a function that returns back a string?

No problem. A grammar entry, like a function, can return any type.

But if you want that the arithmetic calculator of the example in
the Camlp4 tutorial can return any type, in this case, it is not
a problem of the Camlp4 grammar, but a problem of typing in OCaml.

How would you make a function returning values of several possible
types? Indeed with the type like you say:

> type myresult = Int of int | String of string

> P.S.: I looked at the example with List.assoc on page 11 and 12.
>       I think that's a way to implement own variables of a simple
>       language. Very easy, and again surprising.
>       (Here again: how to use string-variables in a language
>        to implement?)

In this case, the "env" list of the example must be of type:
    (string * myresult) list ref
And yo must enter the variable "foo" by:
    env := ("foo", Int 27) :: env

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: