9

Clojurescript: abstraction and data structure clarification - Part 1

 3 years ago
source link: https://blog.klipse.tech/clojurescript/2016/04/01/clojure-oop-part1.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Clojurescript: abstraction and data structure clarification - Part 1

Apr 1, 2016 • @RaphaelBoukara

1- Protocols

As define in the documentation,

A protocol is a named set of named methods and their signatures, defined using defprotocol.

(defprotocol IFoo
  "IFoo doc"
  (fooA [this] "fooA doc")
  (fooB [this] [this a] "fooB doc"))

You can add some documentation to the protocol and to each methods it contains. You don’t need to provide methods’ implemantation. After created your protocol, you can use deftype, defrecord or reify for defining its methods implementation.

In our case, we will use deftype because the major difference between deftype and defrecord is that deftype provides just the functionalities implemented by the user, contrary to defmethod that implements a lot of things that will not here to help us to understand the generated javascript code.

Let’s see the below example:

Now try to understand how the clojurescript compiler generates a basic protocol in javascript. Use KLIPSE to see the generated javascript code:

OK! If you are like me, you give me that the generated code is difficult to understand! So, let’s simplify it:

/**
 * Protocol Fly receive a javascript object
 * if the object implement fly => call fly
 * else throw exception
 */
fly = function(this$) {
    if(this$._Fly$fly != null) {
        return this$._Fly$fly();
    } else {
        throw "fly not supported!";
    }
};

/**
 * Type Bird that implement fly function
 */
Bird = function() {};
Bird.prototype._Fly$fly = function() {
    return "Bird uses natural wings.";
};

/**
 * Type Plane that implement fly function
 */
Plane = function() {};
Plane.prototype._Fly$fly = function() {
    return "Plane uses metal wings.";
};

/**
 * Type Empty that not implement fly function
 */
Empty = function() {};

/**
 * Call Fly.fly for each type you build
 */
a380 = new Plane();
eagle = new Bird();
none = new Empty();
console.log(fly.call(null, a380)); 
console.log(fly.call(null, eagle));
console.log(fly.call(null, none));

I hope now it’s more clear, when you define a protocol, you define a set of functions. Each function receive a javascript object, checks if this object implements a specific function, if that is the case this function launch the specific function. That’s all!

If you enjoy this kind of interactive articles would you consider a (small) donation💸 on Patreon or at least giving a star⭐ for the Klispe repo on Github?

to stay up-to-date with the coolest interactive articles around the world.

Discover more cool interactive articles about javascript, clojure[script], python, ruby, scheme, c++ and even brainfuck!

Give Klipse a Github star to express how much you appreciate Code Interactivity.

Subscribe to the Klipse newsletter:

Feel free to email me [email protected] for getting practical tips and tricks in writing your first interactive blog post.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK