Loading ...
Sorry, an error occurred while loading the content.

The type of this expression... contains type variables that cannot be generalized

Expand Messages
  • Grant Olson
    I m trying to create a hashtable with keys that are strings, and values that are OpenGL textures from the lablgl package. I get the above error. A google
    Message 1 of 4 , Dec 30, 2006
    • 0 Attachment
      I'm trying to create a hashtable with keys that are strings, and values that
      are OpenGL textures from the lablgl package. I get the above error. A
      google search turned up a 'common errors' page on the ocaml tutorial that
      indicates I need to use type annotation. However, that example is a simple
      string, and the type I have to deal with is listed as (string, (_[<
      Gl.format > `rgb ], _[< Gl.kind > `ubyte ]) GlPix.t).

      I'm not sure where to begin to figure out how to properly annotate this.
      Any tips or pointers to resources would be appreciated.

      Code follows. It requires the lablgl package.

      Thanks,

      Grant

      let image_height = 64
      and image_width = 64

      let make_generic_image () =
      let image =
      GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
      in
      for i = 0 to image_width - 1 do
      for j = 0 to image_height - 1 do
      Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
      (if (i land 2 ) lxor (j land 2) = 0
      then [|255;255;255|]
      else [|0;0;0|])
      done
      done;
      image;;

      let textures = Hashtbl.create 100;;

      Hashtbl.add textures "unknown" (make_generic_image ());;
    • Jon Harrop
      ... You are trying to store heterogeneously typed values in a language that only supports homogeneous types. There are various ways around this. The simplest
      Message 2 of 4 , Dec 30, 2006
      • 0 Attachment
        On Sunday 31 December 2006 03:38, Grant Olson wrote:
        > I'm trying to create a hashtable with keys that are strings, and values
        > that are OpenGL textures from the lablgl package. I get the above error.
        > A google search turned up a 'common errors' page on the ocaml tutorial that
        > indicates I need to use type annotation. However, that example is a simple
        > string, and the type I have to deal with is listed as (string, (_[<
        > Gl.format > `rgb ], _[< Gl.kind > `ubyte ]) GlPix.t).
        >
        > I'm not sure where to begin to figure out how to properly annotate this.
        > Any tips or pointers to resources would be appreciated.

        You are trying to store heterogeneously typed values in a language that only
        supports homogeneous types. There are various ways around this. The simplest
        is to support only one type and add a type annotation:

        let textures : (string, ([ `rgb ], [ `ubyte ]) GlPix.t) Hashtbl.t =
        Hashtbl.create 100;;

        More advanced alternatives include creating a variant type that can store any
        type of texture, or wrapping the texture in a class that exposes a uniform
        API for all textures.

        --
        Dr Jon D Harrop, Flying Frog Consultancy Ltd.
        Objective CAML for Scientists
        http://www.ffconsultancy.com/products/ocaml_for_scientists
      • Grant Olson
        ... Thanks, that worked. But just to clarify, is it heterogeneous because the value types normally need to match the key types? I thought it was homogeneous
        Message 3 of 4 , Dec 31, 2006
        • 0 Attachment
          > You are trying to store heterogeneously typed values in a language that
          > only supports homogeneous types. There are various ways around this.
          > The simplest is to support only one type and add a type annotation:
          >
          > let textures : (string, ([ `rgb ], [ `ubyte ]) GlPix.t) Hashtbl.t =
          > Hashtbl.create 100;;

          Thanks, that worked. But just to clarify, is it heterogeneous because the
          value types normally need to match the key types? I thought it was
          homogeneous because all of the keys were strings, and all of the values were
          textures.

          -Grant
        • Jon Harrop
          ... Textures is a set of types in this case. -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. Objective CAML for Scientists
          Message 4 of 4 , Dec 31, 2006
          • 0 Attachment
            On Sunday 31 December 2006 20:38, Grant Olson wrote:
            > > You are trying to store heterogeneously typed values in a language that
            > > only supports homogeneous types. There are various ways around this.
            > > The simplest is to support only one type and add a type annotation:
            > >
            > > let textures : (string, ([ `rgb ], [ `ubyte ]) GlPix.t) Hashtbl.t =
            > > Hashtbl.create 100;;
            >
            > Thanks, that worked. But just to clarify, is it heterogeneous because the
            > value types normally need to match the key types? I thought it was
            > homogeneous because all of the keys were strings, and all of the values
            > were textures.

            "Textures" is a set of types in this case.

            --
            Dr Jon D Harrop, Flying Frog Consultancy Ltd.
            Objective CAML for Scientists
            http://www.ffconsultancy.com/products/ocaml_for_scientists
          Your message has been successfully submitted and would be delivered to recipients shortly.