caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Weak type, Open type
@ 2002-08-05  7:10 SooHyoung Oh
  2002-08-05  9:30 ` Jacques Garrigue
  0 siblings, 1 reply; 3+ messages in thread
From: SooHyoung Oh @ 2002-08-05  7:10 UTC (permalink / raw)
  To: Caml-List


Hi, Ocamlers!

While I've studying design pattern with ocaml,
I made the followding 3 classes (PART1):
(1) class virtual entry: base class
(2) class file: subclass of "entry" class
(3) class dir: subclass of "entry" class

I made some code using these classes on top level and it's ok. (PART 2).
While I translate the top level code into new class definition (PART3), it
makes some error.
The error message is on (PART 4).

It seems that (PART2) is weak type and (PART 3) is open type.

[Q 1] Is (PART2) is correct? I don't know why it doesn't make type error.
[Q 2] (depend on Q 1)
Can I translate (PART2) into new class definition like (PART 3) without type
error?
Then how?

============ (PART 1) class definition ===============
class virtual entry () =
object
    method virtual name: unit -> string
end;;

class file name =
object(self)
    inherit entry ()
    method name () = name
    method file_size () = 100
end;;

class dir name =
object(self)
    inherit entry ()
    method name () = name
    method dir_size () = 3000
end;;

=============== (PART 2) NO ERROR with weak type (?) ===================
let file_list = ref ([]: #entry list);;
let add e = file_list := (e:>entry) :: !file_list;;
let _ = add (new dir "root");;
let _ = add (new file "hello.txt");;

================ (PART 3) ERROR with open type (?) ================
class dir2 name =
object(self)
    inherit entry ()

    val file_list = ref ([]: #entry list)

    method name () = name
    method add e = file_list := (e:>entry) :: !file_list
end;;

================ (PART 4) error message  ================
    Some type variables are unbound in this type:
      class dir2 :
        string ->
        object
         val file_list : entry list ref
          method add : #entry -> unit
          method name : unit -> string
       end
    The method add has type #entry -> unit where .. is unbound

---
SooHyoung Oh
tel: 02)583-8709, 042)861-8649
cell. phone: 011-453-4303
web: http://www.duonix.com


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Weak type, Open type
  2002-08-05  7:10 [Caml-list] Weak type, Open type SooHyoung Oh
@ 2002-08-05  9:30 ` Jacques Garrigue
  2002-08-06  1:04   ` SooHyoung Oh
  0 siblings, 1 reply; 3+ messages in thread
From: Jacques Garrigue @ 2002-08-05  9:30 UTC (permalink / raw)
  To: shoh; +Cc: caml-list

From: "SooHyoung Oh" <shoh@duonix.com>

> While I've studying design pattern with ocaml,
> I made the followding 3 classes (PART1):
> (1) class virtual entry: base class
> (2) class file: subclass of "entry" class
> (3) class dir: subclass of "entry" class
> 
> I made some code using these classes on top level and it's ok. (PART 2).
> While I translate the top level code into new class definition (PART3), it
> makes some error.
> The error message is on (PART 4).
> 
> It seems that (PART2) is weak type and (PART 3) is open type.

Not weak: file_list has a weak type variable, but it gets immediatly
instanciated to entry; and add is really polymorphic.

> [Q 1] Is (PART2) is correct? I don't know why it doesn't make type error.
Yes. What's wrong?
Note that your #entry is meaningless, since it is immediatly
instanciated to entry. You could as well write entry list from the
beginning, or nothing at all.

> [Q 2] (depend on Q 1)
> Can I translate (PART2) into new class definition like (PART 3) without type
> error?

The problem is witht the polymorphism of add.
Implicit polymorphism is not allowed in methods.
You can either remove the polymorphism, or make it explicit.

No polymorphism:
class dir2 name =
object(self)
    inherit entry ()

    val file_list = ref ([]: entry list)

    method name () = name
    method add e = file_list := e :: !file_list
end;;

Explicit polymorphism (3.05 only):
class dir2 name =
object(self)
    inherit entry ()

    val file_list = ref []

    method name () = name
    method add : 'a. (#entry as 'a) -> unit =
      fun e -> file_list := (e:>entry) :: !file_list
end;;

Jacques Garrigue

> ============ (PART 1) class definition ===============
> class virtual entry () =
> object
>     method virtual name: unit -> string
> end;;
> 
> class file name =
> object(self)
>     inherit entry ()
>     method name () = name
>     method file_size () = 100
> end;;
> 
> class dir name =
> object(self)
>     inherit entry ()
>     method name () = name
>     method dir_size () = 3000
> end;;
> 
> =============== (PART 2) NO ERROR with weak type (?) ===================
> let file_list = ref ([]: #entry list);;
> let add e = file_list := (e:>entry) :: !file_list;;
> let _ = add (new dir "root");;
> let _ = add (new file "hello.txt");;
> 
> ================ (PART 3) ERROR with open type (?) ================
> class dir2 name =
> object(self)
>     inherit entry ()
> 
>     val file_list = ref ([]: #entry list)
> 
>     method name () = name
>     method add e = file_list := (e:>entry) :: !file_list
> end;;
> 
> ================ (PART 4) error message  ================
>     Some type variables are unbound in this type:
>       class dir2 :
>         string ->
>         object
>          val file_list : entry list ref
>           method add : #entry -> unit
>           method name : unit -> string
>        end
>     The method add has type #entry -> unit where .. is unbound
> 
> ---
> SooHyoung Oh
> tel: 02)583-8709, 042)861-8649
> cell. phone: 011-453-4303
> web: http://www.duonix.com
> 
> 
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Weak type, Open type
  2002-08-05  9:30 ` Jacques Garrigue
@ 2002-08-06  1:04   ` SooHyoung Oh
  0 siblings, 0 replies; 3+ messages in thread
From: SooHyoung Oh @ 2002-08-06  1:04 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: caml-list

Thanks for you reply.
Fortunately, I'm using Ocaml 3.05 and polymophic method works well.
But coding as your comments on weak type makes an error. See followings.

> From: "SooHyoung Oh" <shoh@duonix.com>
>
> > While I've studying design pattern with ocaml,
> > I made the followding 3 classes (PART1):
> > (1) class virtual entry: base class
> > (2) class file: subclass of "entry" class
> > (3) class dir: subclass of "entry" class
> >
> > I made some code using these classes on top level and it's ok. (PART 2).
> > While I translate the top level code into new class definition (PART3),
it
> > makes some error.
> > The error message is on (PART 4).
> >
> > It seems that (PART2) is weak type and (PART 3) is open type.
>
> Not weak: file_list has a weak type variable, but it gets immediatly
> instanciated to entry; and add is really polymorphic.
>
> > [Q 1] Is (PART2) is correct? I don't know why it doesn't make type
error.
> Yes. What's wrong?
> Note that your #entry is meaningless, since it is immediatly
> instanciated to entry. You could as well write entry list from the
> beginning, or nothing at all.
>

The types of (PART2) are
    val file_list : _#entry list ref = {contents = []}
    val add : #entry -> unit = <fun>
After removing '#':
    val file_list : entry list ref = {contents = []}
    val add : _#entry -> unit = <fun>

Using "entry" instead of "#entry" produces the following:
    This expression has type
      file = < file_size : unit -> int; name : unit -> string >
    but is here used with type < dir_size : unit -> int; name : unit ->
string >
    Only the second object type has a method dir_size

[Deleted ...]


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

end of thread, other threads:[~2002-08-06  1:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-05  7:10 [Caml-list] Weak type, Open type SooHyoung Oh
2002-08-05  9:30 ` Jacques Garrigue
2002-08-06  1:04   ` SooHyoung Oh

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).