Error with inherited objects

Hello, guys!
There is code:

exception Unmatched_Struct;;

class virtual flow aid =
        object
          val id : int = aid
          method gid : int = aid
        end;;

class rnone =
object
  inherit flow 0 as super
end;;

class ['a] rsome avalue =
object
  inherit flow 1 as super
  val fvalue : 'a = avalue
  method gvalue : 'a = avalue
end;;

let maybeMap m f = 
  match (m)#gid with
    | (*None*) 0 -> new rnone 
    | (*Some*) 1 -> let v = m#gvalue in
      new rsome (f (v)) 
    | _ -> raise Unmatched_Struct ;;

Compilation gives:
Error: This expression has type 'a rsome
but an expression was expected of type rnone
The second object type has no method gvalue

Second object definitely has no gvalue method, but what about having different type of rnone and rsome? Both are inherited from flow.

First in OCaml, inheritance does not implies subtyping because the self type can sometimes appear in contravariant position, for instance in binary method.

More directly related to your problem, there is no implicit coercition to subtype in OCaml. Consequently, you need to cast the result of your second branch to rnone explictely:

let maybeMap m f = 
  match m#gid with
    | (*None*) 0 -> new rnone 
    | (*Some*) 1 -> let v = m#gvalue in
      (new rsome (f v) :> rnone) 
    | _ -> raise Unmatched_Struct ;;