caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* -'a (doc request)
@ 2005-09-07 12:48 david baelde
  2005-09-07 13:00 ` [Caml-list] " David MENTRE
  2005-09-07 13:34 ` Olivier Andrieu
  0 siblings, 2 replies; 3+ messages in thread
From: david baelde @ 2005-09-07 12:48 UTC (permalink / raw)
  To: caml-list

I just saw in cairo.mli a type declaration which I don't understand:
type -'a surface
What's the difference with 'a ?
Thanks.
-- 
David


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] -'a (doc request)
  2005-09-07 12:48 -'a (doc request) david baelde
@ 2005-09-07 13:00 ` David MENTRE
  2005-09-07 13:34 ` Olivier Andrieu
  1 sibling, 0 replies; 3+ messages in thread
From: David MENTRE @ 2005-09-07 13:00 UTC (permalink / raw)
  To: david.baelde; +Cc: caml-list

Hello David,

2005/9/7, david baelde <david.baelde@gmail.com>:
> I just saw in cairo.mli a type declaration which I don't understand:
> type -'a surface
> What's the difference with 'a ?

See http://caml.inria.fr/pub/docs/manual-ocaml/manual016.html#s:type-defs
[the copy/paste is missing greek letters, look at the web page]

The type variables appearing as type parameters can optionally be
prefixed by + or - to indicate that the type constructor is covariant
or contravariant with respect to this parameter. This variance
information is used to decide subtyping relations when checking the
validity of >: coercions (see section 6.7.5).

For instance, type +'a t declares t as an abstract type that is
covariant in its parameter; this means that if the type t is a subtype
of the type s, then t t is a subtype of s t. Similarly, type -'a t
declares that the abstract type t is contravariant in its parameter:
if t is subtype of s, then s t is subtype of t t. If no + or -
variance annotation is given, the type constructor is assumed
invariant in the corresponding parameter. For instance, the abstract
type declaration type 'a t means that t t is neither a subtype nor a
supertype of s t if t is subtype of s.

The variance indicated by the + and - annotations on parameters are
required only for abstract types. For abbreviations, variant types or
record types, the variance properties of the type constructor are
inferred from its definition, and the variance annotations are only
checked for conformance with the definition.


Yours,
d.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] -'a (doc request)
  2005-09-07 12:48 -'a (doc request) david baelde
  2005-09-07 13:00 ` [Caml-list] " David MENTRE
@ 2005-09-07 13:34 ` Olivier Andrieu
  1 sibling, 0 replies; 3+ messages in thread
From: Olivier Andrieu @ 2005-09-07 13:34 UTC (permalink / raw)
  To: david.baelde; +Cc: caml-list

 david baelde [Wednesday 7 September 2005] :
 >
 > I just saw in cairo.mli a type declaration which I don't understand:
 > type -'a surface
 > What's the difference with 'a ?

This is a variance annotation. From the manual:
,----
| The type variables appearing as type parameters can optionally be
| prefixed by + or - to indicate that the type constructor is covariant
| or contravariant with respect to this parameter. This variance
| information is used to decide subtyping relations when checking the
| validity of >: coercions
`----

So it means the `surface' type is contravariant wrt the
parameter. This allows the caml API to reflect the subtyping relations
of the underlying C type.

Typically, you have constructor functions:

  val image_surface_create       : ... -> [ `Any | `Image ] surface
  val surface_create_for_channel : ... -> [ `Any | `PDF ] surface

, functions that work on any kind of surface:

  val surface_flush : [> `Any ] surface -> unit

and some functions that are specific to a subtype of surface:

  val image_surface_get_width : [> `Image ] surface -> int

HTH,
-- 
   Olivier


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-09-07 13:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-09-07 12:48 -'a (doc request) david baelde
2005-09-07 13:00 ` [Caml-list] " David MENTRE
2005-09-07 13:34 ` Olivier Andrieu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).