[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [Libguestfs] example script for GObject



On 01/26/2012 11:54 AM, Hilko Bengen wrote:
* Richard W.M. Jones:

   var optargs = new Guestfs.AddDriveOpts ({readonly: true});
   g.add_drive_opts (filename, optargs);

This awkward style is required because GObject Introspection doesn't
support optional arguments.

This is really, really ugly. Couldn't one use some sort of list instead?

Something that could be translated to Javascript as

     g.add_drive_opts (filename, [ Guestfs.AddDriveOpts.FORMAT, "raw" ] )

or even

     g.add_drive_opts (filename, Guestfs.AddDriveOpts.FORMAT, "raw" )

would be much nicer.

We went round the houses on this one. The main advantage of the optargs object is that it's typesafe. It's also quite a lot more concise than the [ GUESTFS_VERYVERYVERYLONG_OPTIONNAME, "value" ] format as soon as you've got 2 optional arguments.

Note that you can shorten the syntax further by declaring the optargs object inline. Compare:

g.add_drive_opts (filename, [Guestfs.AddDriveOpts.FORMAT, "raw"])

with:

g.add_drive_opts (filename, new Guestfs.AddDriveOpts({format: "raw}))

There's barely any difference, and for 2 options the latter would be much shorter.

Yes, it would be nicer to have proper optional arguments, but GObject Introspection is constrained somewhat by being a meta-binding.

Matt
--
Matthew Booth, RHCA, RHCSS
Red Hat Engineering, Virtualisation Team

GPG ID:  D33C3490
GPG FPR: 3733 612D 2D05 5458 8A8A 1600 3441 EA19 D33C 3490


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]