Monkeying around the line of hard and dynamic code

So a few months back I had a discussion about the difference of using the 3rd party binding across dynamic languages. This is nothing new, and there has been quite a few of these bridges. However I am not sure how much has these been taking up.

The conversation was a comparison between using C# or other languages like Ruby for .NET. Granted that this was not really a .NET conversation but rather on the options in coding under the same API or VM. The JAVA version of the conversation would have been between things like using Java to use JVM or using something like JRuby or the more rooted language Groovy.

So after this conversation I went to understand the question better. Is really a whole new language needed to use an API? Should we just start becoming language driven as opposed of API/VM driven? After all given the options out there, these seem like a viable option.

So I have two different roads, one was making easy things (dynamic approach) to hard languages (JAVA), and using hard languages on toolkits that are usually develop with dynamic languages (GTK). So I took a look at some of the following implementations:

  • Using Jython with SWING
  • Groovy cookbook and hello world
  • Gnome-Java
  • GTK in C
  • PyGTK

So here are some snippets about how to use Jython with SWING (Java GUI Toolkit).

from javax.swing import JFrame

JFrame(‘Hello, World!’, defaultCloseOperation=JFrame.EXIT_ON_CLOSE, size=(300, 300), locationRelativeTo=None).setVisible(True)

This simple two line code will generate already a window, label and action.

The next example is the opposite, and doing a Toolkit like GTK in Java language:

package button;

import org.gnome.gdk.Event;
import org.gnome.gtk.Button;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Label;
import org.gnome.gtk.VBox;
import org.gnome.gtk.VBox;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;

public class ExamplePressMe
{
public static void main(String[] args) {
final Window w;
final VBox x;
final Label l;
final Button b;

Gtk.init(args);

w = new Window();
x = new VBox(false,3);
l = new Label(“Go ahead:nMake my day”);
x.add(l);
b = new Button(“Press me!”);
x.add(b);

b.connect(new Button.Clicked() {
public void onClicked(Button source) {
System.out.println(“I was clicked: ” + b.getLabel());
}
});
w.add(x);
w.setTitle(“Hello World”);
w.showAll();
w.connect(new Widnow.DeleteEvent() {
public boolean onDeleteEvent(Widget source, Event event) {
Gtk.mainQuit();
return false;
}
});

Gtk.main();
}
}

Pardon my verbosity but half is becaus of JAVA and half is because of GTK. However we can see here that is just a matter of declaring variables classes and execution. Now we move to the C version:

#include <gtk/gtk.h>

static void hello( GtkWidget *widget, gpointer data ) {
g_print (“Hello Worldn”);
}

static gboolean delete_event ( GtkWidget *widget, GdkEvent *event, gpointer data ) {
g_print (“delete event occurredn”);
return TRUE;
}

static void destroy ( GtkWidget *widget, gpointer data ) {
gtk_main_quit();
}

int main( int argc, char *argv[] ) {
GtkWidget *window;
GtkWidget *button;

gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, “delete-event”, G_CALLBACK (delete_event), NULL);
g_signal_connect (window, “destroy”, G_CALLBACK (destroy), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
button = gtk_button_new_with_label (“Hello GTK+”);
g_signal_connect (button, “clicked”, G_CALLBACK (hello), NULL);
g_signal_connect (button, “clicked”, G_CALLBACK (gtk_widget_destroy), window);
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (button);
gtk_widget_show (window);
gtk_main ();

return 0;
}

 

Advertisements