On Tue, Feb 9, 2010 at 2:31 AM, Tiphaine Turpin <Tiphaine.Turpin@irisa.fr> wrote:
Jacques Garrigue a écrit :
> From: Rich Neswold <rich.neswold@gmail.com>There is at least a partial solution using polymorhic records or other
>> My question is this: Is there a way to make the compiler reject a function
>> parameter from returning the context parameter?
> The short answer is no.
> Types are not sufficient to prevent values from escaping.
> In ocaml, you have both functions and references.
>
ways of quantifying type variables inside a type expression : If you
artificially parameterise the type context with an unused parameter (and
hide the type definition), you can then require the argument function to
be polymorphic with respect to this parameter, which should prevent it
from returning or storing its argument.
module type Test =sigtype 'a context = Context of int * intval usingContext : ('a context -> 'b) -> 'bend;;module InsTest : Test =structtype phantom = unittype 'a context = Context of int * intlet usingContext f = f (Context (1, 2) : phantom context)end;;
let usingContext (f : 'a context -> 'b) = f (Context (1, 2) : phantom context)