Hacker News new | past | comments | ask | show | jobs | submit login

So we could (by default) assume that all getter (returns) are invariant, and all setter (arguments) are contravariant. If we forget about invariance for a moment then this means essentially that there is only one default pattern and hence no need in explicitly distinguishing and annotating covariance and contravariance - everybody simply knows this default rule. (I am simply reasoning after reading the article - I do not know if it can be done or makes sense.)



It can mostly be done. The OCaml programming language does exactly this - it infers co/contravariance. However, when describing a generic interface which has as part of it a function on types (usually called a "type constructor"), you need to say whether that type constructor is supposed to be covariant, contravariant, or invariant.

For example, if I were describing a sequence interface, I would write

    module type SEQ =
    sig
      (* the "+" means that seq must be covariant *)
      type +'a seq 
      val empty : 'a seq
      val singleton : 'a -> 'a seq
      val append : 'a seq -> 'a seq -> 'a seq
      val lookup : int -> 'a seq -> 'a option
      exception IndexOutOfBounds
    end;;
See https://blogs.janestreet.com/a-and-a/ for a nice explanation.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: