[Java-gnome-developer] FAQ item #15 - Variable arguments
Brought to you by:
afcowie
From: Ryan G. <rya...@ho...> - 2001-08-16 13:54:41
|
Hey all, Just joined the list, reading over the documentation getting familar with things. Considering looking into the gnome-postal app, we'll see. Anyway, looking at the FAQ. Not sure how dated it is. But item #15, I had a general suggestion for enhancing the text, add the following: --- start --- In general, check the javadoc API to see if there is an overloaded method version that takes an array type. If so, array constructor intialization allows you to use any number of parameters with very minor modifications to your code. For example, instead of the GnomeMessageBox constructor: new GnomeMessageBox(message, messagebox_type, button1, button2, button3, button4); You could use: new GnomeMessageBox(message, messagebox_type, new String[]{button1, button2, button3, button4}); --- end --- This got me thinking. I've not delved into the parser code, or the .defs files to examine how java-gnome deals with variable argument functions from Gnome/GTK, but one could define Java equivalents in you really wanted. For example, given an argument list for gnome_message_box as taken from the .h on my system: GtkWidget* gnome_message_box_new( const gchar * message, const gchar * messagebox_type, ...); But, this could be minimally generated as the constructor set: // Full vararg version GnomeMessageBox(String message, String messagebox_type, Object[] varargs); // Enumerated versions... GnomeMessageBox(String message, String messagebox_type, Object vararg1); GnomeMessageBox(String message, String messagebox_type, Object vararg1, Object vararg2); ... And with proper hints in the .defs file, I suppose we could go even further and do it as: // Full vararg version GnomeMessageBox(String message, String messagebox_type, String[] buttons); // Enumerated versions, which are just wrappers // to call on full version. GnomeMessageBox(String message, String messagebox_type, String button1) { this(message, messagebox_type, new String[]{button1}); } GnomeMessageBox(String message, String messagebox_type, String button1, String button2) { this(message, messagebox_type, new String[]{button1, button2}); } ... Where the hints we need for the variable arguments are: 1) Type information. For now, just assume an array of same typed objects to keep it simple. For the above the hint would be "String". 2) Suggested variable name. Here we'd use "button". 3) Maximum number of enumerated overloads. Default to 10. My preference is to keep the overloads short, maybe 2 or 3, anymore is bloat, use the array version in such cases. Anyway, just some quick comments. Sorry if this is a rehash of solved problems. =) Later, Ryan _________________________________________________________________ Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp |