Writing Processing in Kotlin


#21

Now I’ve got it! You want to edit classes in such a way that all past & future instances of them have new members, right? :thinking:

Indeed Java can’t do that! Creating a custom subclass doesn’t replace the original. :disappointed:

Nor does it force other places instantiating the original class to use our own “hacked” version. Even less can it upgrade already existing object instances. :sob:

1st languages that come to mind which can easily edit classes & objects on-the-fly are JavaScript & Lua. :first_quarter_moon_with_face:

Lua I barely know anything about it. But in JS we merely append new properties on a constructor’s prototype{} object. :money_mouth_face:

After that, all object instances of that constructor immediately receive those new added properties. :partying_face:

Here’s an example sketch that adds a new method called zero() to the p5js’ p5.Vector class: :zero:

function setup() {
  noCanvas();

  const vec = p5.Vector.random3D();
  print(vec);
  print(`Method zero() exists in PVector: ${'zero' in vec}\n`);

  p5.Vector.prototype.zero = function () {
    this.x = this.y = this.z = 0;
    return this;
  };

  vec.zero();
  print(vec);
  print('Method zero() exists in PVector: ' + ('zero' in vec));
}

Just copy & paste the sketch above on OpenProcessing.org in order to run it btW: :wink:
www.OpenProcessing.org/sketch/create

Of course there are many more other languages which can edit classes & objects on-the-fly as well. :airplane:

And among those, Python of course: :snake:

vec = PVector.random3D(this)
print vec
print 'Method zero() exists in PVector: %s\n' % hasattr(vec, 'zero')

def zero(v):
    v.x = v.y = v.z = 0
    return v

setattr(PVector, zero.__name__, zero)

vec.zero()
print vec
print 'Method zero() exists in PVector: ' + `hasattr(vec, 'zero')`

exit()

#22

Hi @hamoid

Yes I am using Kotlin as main language for my processing projects. I really love the syntactic sugar it offers (extension methods, optional parameters, type inference …) and the null-safety, coroutines, a simpler functional interface for lists (more like LINQ from C#) and much more.

But I mainly use it for larger projects, where I need a good software architecture. You can find some of my projects here:

  • led-forest3 - very adaptive interactive installation
  • realsense-processing - smaller sketch as a proof of concept
  • anna - based on led-forest3 but another installation (then one of my presentation)

Extension methods are a great too to polish an API which adds too much clutter to your code. For example, I use the following method a lot which gives you the security that you won’t ever forget the endDraw():

fun PGraphics.draw(block: (g: PGraphics) -> Unit) {
    this.beginDraw()
    block(this)
    this.endDraw()
}

And you find many more here:

But really the most interesting part of kotlin is, that you do not use nullable types. So usually you can be sure that no variable is null. That leads to another way of how you write your code. Mostly processing uses the nullability, so sometimes you have to work around a bit.

For me kotlin is not “better” or “faster” then java, just more fun to use and in some ways helps me to write cleaner code.


#23

5 posts were split to a new topic: Getting the depth image from a Real Sense camera