jyy
July 11, 2018, 3:02pm
1
7.4 Type expressions of the Inria manual shows that the “as” keyword can be used as part of a type expression. Initially I thought it might be used as some sort of alias for part of the type expression so it can be used in later part of expression. However all my attempts to use it are syntax error, and I can’t find any single example on google and grepping in core and a few other git repo only shows “as” usage in pattern matching.
Can anyone show an example of proper use of the “as” keyword in type expression? Thanks in advance.
1 Like
You were probably missing parentheses:
let f: ( 'a -> 'a as 'b ) -> 'b = fun x -> x
(* or even *)
let g: ((('a*'a as 'b) * 'b) as 'c) -> 'c = fun x -> x
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(** Operations on objects *)
val copy : (< .. > as 'a) -> 'a
(** [Oo.copy o] returns a copy of object [o], that is a fresh
object with the same methods and instance variables as [o]. *)
external id : < .. > -> int = "%field1"
(** Return an integer identifying this object, unique for
the current execution of the program. The generic comparison
and hashing functions are based on this integer. When an object
is obtained by unmarshaling, the id is refreshed, and thus
different from the original object. As a consequence, the internal
invariants of data structures such as hash table or sets containing
From the standard library, the type probably can’t be represented without the use of as
.
jyy
July 11, 2018, 7:28pm
4
Can the keyword be used with recursive type with parametric arguments similar to the following?
type 'a node = | A of 'a node | B
(* following line has syntax error *)
type 'a node as 'an = | A of 'an | B
Not exactly like that, but there are quite close possible uses:
type 'a node = | A of ('a node as 'an) | B of 'an | C of 'a;;
type 'a node' = [`A of 'an | `B of 'a] as 'an;;
Or
type 'a t = Node of 'at | B
constraint 'at = 'a t;;