Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* problem with optional arguments
@ 2000-09-20 10:38 Dr. Wolfgang Gehrke
  2000-09-20 20:15 ` Remi VANICAT
  2000-09-21  0:08 ` Jacques Garrigue
  0 siblings, 2 replies; 4+ messages in thread
From: Dr. Wolfgang Gehrke @ 2000-09-20 10:38 UTC (permalink / raw)
  To: caml-list

Hello,

could someone advice me how to resolve the following problem:

The following works fine:

class test1 ~(a:int) () =
  object
    val a = abs a
    method a = a
    method strange x = new test1 ~a:x ()
  end

let t1 = new test1 ~a:(-10) ()

But on the other hand with an optional argument it does not compile,
neither:

class test2 ?(a:int) () =
  object
    val a = abs a
    method a = a
    method strange x = new test2 ~a:x ()
  end

nor in the form below:

class test3 ?(a:int) () =
  object
    val a = abs a
    method a = a
    method strange x = new test3 ?a:x ()
  end

I do need the use of "new" and do not want to use {<...>} since any new
instance internally changes a.

Thank you,
Wolfgang



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

* Re: problem with optional arguments
  2000-09-20 10:38 problem with optional arguments Dr. Wolfgang Gehrke
@ 2000-09-20 20:15 ` Remi VANICAT
  2000-09-21 18:59   ` Gerd Stolpmann
  2000-09-21  0:08 ` Jacques Garrigue
  1 sibling, 1 reply; 4+ messages in thread
From: Remi VANICAT @ 2000-09-20 20:15 UTC (permalink / raw)
  To: caml-list

"Dr. Wolfgang Gehrke" <wgehrke@dia.uniroma3.it> writes:

> Hello,
> 
> could someone advice me how to resolve the following problem:
> 
> The following works fine:
> 
> class test1 ~(a:int) () =
>   object
>     val a = abs a
>     method a = a
>     method strange x = new test1 ~a:x ()
>   end
> 
> let t1 = new test1 ~a:(-10) ()
> 
> But on the other hand with an optional argument it does not compile,
> neither:
> 
> class test2 ?(a:int) () =
>   object
>     val a = abs a
>     method a = a
>     method strange x = new test2 ~a:x ()
>   end

it's seem normal, which value have a if the optional argument is not
given ? in fact, ?a avec type 'a option, you should give a default
value. then there is another problem :

class test2 ?(a=0) () =
   object
     val a = abs a
     method a = a
     method strange x = new test2 ~a: x ()
   end

don't work, but i don't see realy why (the message of error is :

The expression "new test2" has type ?a:int -> unit -> test2
but is used with type 'a -> unit -> 'b
)

but 

class test2 ?(a=0) () =
   object
     val a = abs a
     method a = a
     method strange x = new test2 ?a: (Some x) ()
   end

work, and let you see that the argument of an optional argument is
realy of type something option


-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat



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

* Re: problem with optional arguments
  2000-09-20 10:38 problem with optional arguments Dr. Wolfgang Gehrke
  2000-09-20 20:15 ` Remi VANICAT
@ 2000-09-21  0:08 ` Jacques Garrigue
  1 sibling, 0 replies; 4+ messages in thread
From: Jacques Garrigue @ 2000-09-21  0:08 UTC (permalink / raw)
  To: wgehrke; +Cc: caml-list

From: "Dr. Wolfgang Gehrke" <wgehrke@dia.uniroma3.it>

> But on the other hand with an optional argument it does not compile,
> neither:
> 
> class test2 ?(a:int) () =
>   object
>     val a = abs a
>     method a = a
>     method strange x = new test2 ~a:x ()
>   end

The point is that optional arguments, when you do not give a default
value, are of type [t option] rather than [t], so that [?(a:int)]
produces immediately a type clash (int is not compatible with 'a option).
The simplest solution is to specify a default:
        class test2 ?(a:int=2) () = ...
Otherwise, you have to explicitely handle the Some and None cases for
a.

Regards,

Jacques Garrigue
---------------------------------------------------------------------------
Jacques Garrigue      Kyoto University     garrigue at kurims.kyoto-u.ac.jp
		<A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>



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

* Re: problem with optional arguments
  2000-09-20 20:15 ` Remi VANICAT
@ 2000-09-21 18:59   ` Gerd Stolpmann
  0 siblings, 0 replies; 4+ messages in thread
From: Gerd Stolpmann @ 2000-09-21 18:59 UTC (permalink / raw)
  To: Remi VANICAT, caml-list

On Wed, 20 Sep 2000, Remi VANICAT wrote:
>then there is another problem :
>
>class test2 ?(a=0) () =
>   object
>     val a = abs a
>     method a = a
>     method strange x = new test2 ~a: x ()
>   end
>
>don't work, but i don't see realy why (the message of error is :
>
>The expression "new test2" has type ?a:int -> unit -> test2
>but is used with type 'a -> unit -> 'b
>)

See bug # 151,
http://caml.inria.fr/bin/caml-bugs/fixed?id=151;page=8;user=guest

-- 
----------------------------------------------------------------------------
Gerd Stolpmann      Telefon: +49 6151 997705 (privat)
Viktoriastr. 100             
64293 Darmstadt     EMail:   gerd@gerd-stolpmann.de
Germany                     
----------------------------------------------------------------------------



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

end of thread, other threads:[~2000-09-21 19:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-09-20 10:38 problem with optional arguments Dr. Wolfgang Gehrke
2000-09-20 20:15 ` Remi VANICAT
2000-09-21 18:59   ` Gerd Stolpmann
2000-09-21  0:08 ` Jacques Garrigue

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox