caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* default arguments in classes
@ 2009-06-16 21:12 Jacques Le Normand
  2009-06-16 22:06 ` [Caml-list] " Paolo Donadeo
  2009-06-16 22:34 ` Martin Jambon
  0 siblings, 2 replies; 3+ messages in thread
From: Jacques Le Normand @ 2009-06-16 21:12 UTC (permalink / raw)
  To: caml-list caml-list

[-- Attachment #1: Type: text/plain, Size: 404 bytes --]

Hello list,
I'm trying to define a method with a default argument, but I keep getting an
error. Here's the code:

class foo  =
  object (self)
    method bar ?(baz="moo") () =
    (new foo)#bar ~baz ()
  end

and the error:

The expression "new foo" has type foo but is used with type
  < bar : baz:string -> unit -> 'a; .. >
Types for method bar are incompatible

how do I fix this?
cheers
--Jacques L.

[-- Attachment #2: Type: text/html, Size: 508 bytes --]

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

* Re: [Caml-list] default arguments in classes
  2009-06-16 21:12 default arguments in classes Jacques Le Normand
@ 2009-06-16 22:06 ` Paolo Donadeo
  2009-06-16 22:34 ` Martin Jambon
  1 sibling, 0 replies; 3+ messages in thread
From: Paolo Donadeo @ 2009-06-16 22:06 UTC (permalink / raw)
  To: OCaml mailing list

This should work:

# class foo =
  object
    method bar =
      let helper a b ?(c = "") () =
        Printf.printf "a = %d; b = %b; c = %s\n" a b c in
        helper
  end;;
class foo : object method bar : int -> bool -> ?c:string -> unit -> unit end

Any undesired side effects?


-- 
Paolo
~
~
:wq


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

* Re: [Caml-list] default arguments in classes
  2009-06-16 21:12 default arguments in classes Jacques Le Normand
  2009-06-16 22:06 ` [Caml-list] " Paolo Donadeo
@ 2009-06-16 22:34 ` Martin Jambon
  1 sibling, 0 replies; 3+ messages in thread
From: Martin Jambon @ 2009-06-16 22:34 UTC (permalink / raw)
  To: Jacques Le Normand; +Cc: caml-list caml-list

Jacques Le Normand wrote:
> Hello list,
> I'm trying to define a method with a default argument, but I keep
> getting an error. Here's the code:
> 
> class foo  =
>   object (self)
>     method bar ?(baz="moo") () =
>     (new foo)#bar ~baz ()
>   end
> 
> and the error:
> 
> The expression "new foo" has type foo but is used with type
>   < bar : baz:string -> unit -> 'a; .. >
> Types for method bar are incompatible
> 
> how do I fix this?


Adding type annotations often helps:

class foo =
object (self)
  method bar ?(baz="moo") () : unit =
    ((new foo) # bar : ?baz:_ -> _) ~baz ()
end



Martin

-- 
http://mjambon.com/


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

end of thread, other threads:[~2009-06-16 22:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-06-16 21:12 default arguments in classes Jacques Le Normand
2009-06-16 22:06 ` [Caml-list] " Paolo Donadeo
2009-06-16 22:34 ` Martin Jambon

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