Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0007240OCamltypingpublic2016-04-25 17:212017-03-15 06:55
Reportersliquister 
Assigned Togarrigue 
PrioritynormalSeverityminorReproducibilityalways
StatusresolvedResolutionno change required 
PlatformOSOS Version
Product Version4.02.3 
Target VersionlaterFixed in Version 
Summary0007240: limitation on how the typer fills in missing optional arguments
DescriptionThe following code is accepted:

# let f g = g ();;
val f : (unit -> 'a) -> 'a = <fun>
# let g1 ?a:_ () = ();;
val g1 : ?a:'a -> unit -> unit = <fun>
# f g1;;
- : unit = ()

so I don't understand why the following isn't:

# let f g = g ~b:1 ();;
val f : (b:int -> unit -> 'a) -> 'a = <fun>
# let g1 ?a:_ ~b:_ () = ();;
val g1 : ?a:'a -> b:'b -> unit -> unit = <fun>
# f g1;;
Error: This expression has type ?a:'a -> b:'b -> unit -> unit
       but an expression was expected of type b:int -> unit -> 'c

This is the behavior documented in http://caml.inria.fr/pub/docs/manual-ocaml/lablexamples.html, [^] but I don't understand what would be problematic with allowing to fill in the missing optional arguments whether what follows are labelled arguments or positional arguments.

Also the behavior of the typer doesn't seem to be the documented behavior, as the documentation says this is correct, but the typer rejects it:

# let f g = g () ();;
val f : (unit -> unit -> 'a) -> 'a = <fun>
# let g1 ?a:_ () () = ();;
val g1 : ?a:'a -> unit -> unit -> unit = <fun>
# let g2 () ?a:_ () = ();;
val g2 : unit -> ?a:'a -> unit -> unit = <fun>
# f g1;;
- : unit = ()
# f g2;;
Error: This expression has type unit -> ?a:'a -> unit -> unit
       but an expression was expected of type unit -> unit -> 'b
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
(0017663)
garrigue (manager)
2017-03-15 06:55

Concerning your first example, the problem is that, when there are other labeled arguments, the evaluation order becomes hard to establish correctly.
Namely, g1 seems ok, but the following g3 could be a problem:
  let g3 ~b = print_endline "g3"; fun ?a () -> ...
Here we would need to generate extra code to ensure that partial applications on b are correctly evaluated.

For your last example, the intent of the documentation is that the optional arguments should come first, for the same reason as above. So g2 cannot be used.

- Issue History
Date Modified Username Field Change
2016-04-25 17:21 sliquister New Issue
2016-05-03 11:29 doligez Status new => acknowledged
2016-05-03 13:47 doligez Target Version => 4.04.0 +dev / +beta1 / +beta2
2016-09-08 11:12 shinwell Target Version 4.04.0 +dev / +beta1 / +beta2 => later
2017-02-23 16:45 doligez Category OCaml typing => typing
2017-03-15 06:55 garrigue Note Added: 0017663
2017-03-15 06:55 garrigue Status acknowledged => resolved
2017-03-15 06:55 garrigue Resolution open => no change required
2017-03-15 06:55 garrigue Assigned To => garrigue


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker