[MLton] Optional Arguments and FRU

Stephen Weeks MLton@mlton.org
Tue, 23 Aug 2005 16:56:35 -0700


> >   val z = f' 10 (a 1.0 & c 2 & b 1.0)
> 
> Although it is notationally nice, I think that the extra code needed to
> implement it may not always be worth it. 

I'm generally more concerned about concision of client code than
concision of implementation.

> It might also suffer from the fact that the update functions (a, b
> and c) are specific to a particular argument record. I think that it
> is likely to be the case that many functions (in a library) would
> have optional arguments by the same name, but not always the same
> set of optional arguments.

Agreed.  I often get around such naming problems by using the module
system and controlled use of open.  That lets me pay a per call
syntactic cost instead of per optional argument.  

----------------------------------------------------------------------

signature S =
   sig
      structure FArg:
         sig
            type t

            val & : t * t -> t
            val a: int -> t
            val b: real -> t
         end
      
      val f: FArg.t -> string

      structure GArg:
         sig
            type t

            val & : t * t -> t
            val b: int -> t
            val c: real -> t
         end
      
      val g: GArg.t -> bool
   end

functor F (S: S) =
   struct
      open S

      val _ = f let open FArg in a 1 & b 2.0 end
      val _ = g let open GArg in b 2 & c 3.0 end

      val _ =
         let
            open FArg
         in
            f (a 1 & b 2.0)
            ; f (a 3 & b 4.0)
         end
   end