Friday, December 6, 2013

PythonJS Dart Backend

I have started working on a second backend for PythonJS that outputs Dart code. Dart is a new language by Google that is very similar to JavaScript, it includes all the things missing from JavaScript like: classes, static types, operator overloading, and a compiler that checks all your code. Using dart2js you can translate Dart code into JavaScript code so that it works in all web browsers.

One of the limitations of Dart is that it lacks multiple inheritance, [1], [2]. It features Mix-ins and Interfaces, but these can not fully capture all the power that proper multiple inheritance provides. Using PythonJS you can bypass this limitation of Dart and use multiple inheritance.

Multiple Inheritance

PythonJS implements multiple inheritance in Dart using: static class methods, stub-methods, and interfaces. The method body is placed inside static class methods. The stub-methods are real methods on the instance, and simply forward calls to the static class methods and pass this as the first argument. Sub-classes can extend a method of the parent, and still call the parent's method using the normal Python syntax: parent.some_method(self).

Python Input

class A:
 def foo(self):
  print 'foo'

class B:
 def bar(self):
  print 'bar'

class C( A, B ):
 def call_foo_bar(self):
  print 'call_foo_bar in subclass C'

 ## extend foo ##
 def foo(self):
  print 'foo extended'

Dart Output

class A {
  foo() { return A.__foo(this); }
  static __foo(self) {

class B {
  bar() { return B.__bar(this); }
  static __bar(self) {

class C implements A, B {
  call_foo_bar() { return C.__call_foo_bar(this); }
  static __call_foo_bar(self) {
    print("call_foo_bar in subclass C");;;

  foo() { return C.__foo(this); }
  static __foo(self) {
    print("foo extended");

  bar() { return B.__bar(this); }

No comments:

Post a Comment