How Ballerina is different from other programming languages - Medium [PDF]

Feb 26, 2017 - Communication is all about messages and data. XML and JSON are the most common and heavily used data type

5 downloads 4 Views 109KB Size

Recommend Stories


Programming Languages
Keep your face always toward the sunshine - and shadows will fall behind you. Walt Whitman

Programming languages
No amount of guilt can solve the past, and no amount of anxiety can change the future. Anonymous

programming languages
Goodbyes are only for those who love with their eyes. Because for those who love with heart and soul

Other languages
Ego says, "Once everything falls into place, I'll feel peace." Spirit says "Find your peace, and then

How is EBLI Different?
The happiest people don't have the best of everything, they just make the best of everything. Anony

How different is Oxbridge?
Everything in the universe is within you. Ask all from yourself. Rumi

[PDF] Concepts of Programming Languages
Learning never exhausts the mind. Leonardo da Vinci

Ballerina
The beauty of a living thing is not the atoms that go into it, but the way those atoms are put together.

How is human cooperation different?
Love only grows by sharing. You can only have more for yourself by giving it away to others. Brian

2008 Other Languages
Live as if you were to die tomorrow. Learn as if you were to live forever. Mahatma Gandhi

Idea Transcript


Chanaka Fernando Follow Engineer, Philanthropist, Sportsman, Writer, Believe humanity Feb 25, 2017 · 6 min read

How Ballerina is different from other programming languages

In this post, we’re going to talk about special features of the Ballerina language which are unique to itself. These features are specifically designed to address the requirements of the technology domain we are targeting with this new language.

XML , JSON and datatable are native data types Communication is all about messages and data. XML and JSON are the most common and heavily used data types in any kind of integration eco system. In addition to those 2 types, interaction with databases (SQL, NoSQL) is the other most common use case. We have covered all 3 scenarios with native data types. You can define xml and json data types inline and manipulate them easily with utility methods in jsons and messages packages.

json j = `{"company":{"name":"wso2", "country":"USA"}}`; messages:setJsonPayload(m, j);

With the above 2 lines, you can define your own json message and replace the current message with your message. You can do the same thing for XML messages as well. If you need to extract some data from a message which is of type application/json, you can easily do that with following lines of code.

json newJson = jsons:getJson(messages:getJsonPayload(m), "$.company");

The above code will set the following json message to the

newJson

variable. {"name":"wso2","country":"USA"}

Another cool feature of this inline representation is the variable access within these template expressions. You can access any variable when you define your XML/JSON message like below.

string name = "WSO2"; xml x = `{$name}`;

The above 2 lines create an xml message with following data in it.

WSO2

You can do the same thing for JSON messages in a similar fashion. Datatable is a representation of a pointer to a result set returned from a database query. It works in a streaming manner. The data will be consumed as it is used in the program. Here is a sample code for reading data within a ballerina program using the datatable type.

string s; datatable dt = sql:ClientConnector.select(testDB, "SELECT int_type, long_type, float_type, double_type, boolean_type, string_type from DataTable LIMIT 1",parameters); while (datatables:next(dt)) { s = datatables:getString(dt, "string_type"); // do something with s }

You can find the complete set of functions in Ballerina API documentation.

Parallel processing is as easy as it can get The term “parallel processing” scares even experienced programmers. But with Ballerina, you can do parallel processing as you do any other action. The main concept of term “Ballerina” stems from the concept of a ballet dance where so many different ballet dancers synchronized with each other during the dance act by sending messages between each other. The technical term for this process is called “Choreography”. Ballerina (language) brings this concept into a more programmer friendly concept with following 2 features.

Parallel processing with worker The concept of a worker is that, it is an execution flow. The execution will be carried by the “Default Worker”. If the Ballerina programmer wants to delegate his work to another “Worker” which is working in parallel to the “Default Worker”, he can create a worker and send a message to that worker with the following syntax.

worker friend(message m) { //Do some work here reply m'; } msg -> friend; //Do my own work replyMsg

Smile Life

When life gives you a hundred reasons to cry, show life that you have a thousand reasons to smile

Get in touch

© Copyright 2015 - 2024 PDFFOX.COM - All rights reserved.