Content deleted Content added
GoingBatty (talk | contribs) m General fixes and manual cleanup, typo(s) fixed: Github → GitHub |
m Grammar |
||
(42 intermediate revisions by 27 users not shown) | |||
Line 1:
{{Short description|Programming language}}
{{Infobox programming language
| name = Ballerina
| logo =
| logo alt = Ballerina Language
| designer = [[Sanjiva Weerawarana]], [[James Clark (programmer)|James Clark]], Sameera Jayasoma, Hasitha Aravinda, Srinath Perera, [[Frank Leymann]] and [[WSO2]]<ref>{{cite web |url=https://ballerina.io/spec/lang/master/ |publisher=WSO2 |title=Ballerina Language Specification |access-date=2020-04-24 |archive-date=2020-08-11 |archive-url=https://web.archive.org/web/20200811095205/https://ballerina.io/spec/lang/master/ |url-status=dead}}</ref>
| developer = [[WSO2]]
| released = {{Start date and age|2019|09|10}}
| latest release version = 2201.12.3 (Swan Lake Update 12)
| latest release date = {{Start date and age|2025|04|10}}
| typing = [[Structural typing|Structural]], [[Strong and weak typing|strong]], [[static typing|static]], [[type inference|inferred]]
| influenced by = [[Java (programming language)|Java]], [[JavaScript]], [[Go (programming language)|Go]], [[Rust (programming language)|Rust]], [[C Sharp (programming language)|C#]]<ref>{{cite web |title=Ballerina, A modern programming language focused on integration |pages=15 |url=https://opensource.ellak.gr/wp-content/uploads/sites/5/2018/06/2018-06-Ballerina-GFOSS.pdf}}</ref>
| programming language = [[Java (programming language)|Java]], Ballerina, [[TypeScript]]<ref name="sourcecode"/>
| platform = [[x86-64]]
| operating system = [[Cross-platform software|Cross-platform]]: [[Microsoft Windows|Windows]], [[Linux]], [[macOS]]
| license = [[Apache License|Apache]] 2.0<ref name="license">{{cite web
|url=https://github.com/ballerina-platform/ballerina-lang/blob/master/LICENSE
|title=WSO2 / LICENSE
|date=2017-03-08 |access-date=2018-03-01
|website=github.com |publisher=[[WSO2]]
}}</ref>
| website = {{URL|
}}
'''Ballerina''' is a general-purpose [[programming language]] designed by [[WSO2]] for cloud-era application [[programmers]].<ref name="sourcecode">{{cite web |url=https://github.com/ballerina-platform/ballerina-lang |title=Ballerina source code |author=((Open Source Contributors)) |date=18 June 2019 |publisher=[[GitHub]]}}</ref> It is [[free and open-source software]] released under [[Apache License]] 2.
It has various constructs geared toward cloud-native development including support for
== History ==
Ballerina was first publicly announced in 2017 and version 1.0 was released on September 10, 2019.<ref>{{Cite press release|url=https://www.globenewswire.com/news-release/2019/09/10/1913510/0/en/Ballerina-Reinvents-Cloud-Native-Middleware-as-a-Programming-Language-Puts-ESB-on-the-Path-to-Extinction.html|title=Ballerina Reinvents Cloud-Native Middleware as a Programming Language|website=GlobeNewswire|date=10 September 2019 |access-date=2020-09-16}}</ref>
== Design ==
Ballerina is a general-purpose language with a familiar syntax along with a direct graphical representation of the code in the form of sequence diagrams. It has fundamental abstractions designed to make integration problems easier to program.<ref>{{cite web |last1=Ratnayake |first1=Dakshitha |title=Ballerina Swan Lake: 10 Compelling Language Characteristics for Cloud Native Programming |url=https://www.infoq.com/articles/ballerina-cloud-native-programming/ |website=InfoQ |access-date=1 March 2022}}</ref> Ballerina was designed by WSO2 to improve productivity for application developers that have to work with [[distributed computing]]. It is easy to write and modify and is suitable for application programmers.<ref>{{Cite news|url=https://thenewstack.io/ballerina-an-api-first-programming-language/|title=Ballerina: An API-First Programming Language|last=Jackson|first=Joab|work=The New Stack|access-date=2018-06-11|language=en-US}}</ref><ref>{{Cite news|url=https://www.zdnet.com/article/ballerina-a-language-of-integration-of-technology-and-the-arts/|title=Technology and the Arts: Celebrating Ballerina, a computer language of integration|first=Tom|last=Foremski|date=2019-03-01|access-date=2019-07-14}}</ref><ref>{{Cite news|url=https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Ballerina-language-promises-to-improve-app-integration|title=Ballerina language promises to improve app integration|first=George|last=Lawton|date=2018-11-01|access-date=2019-07-23}}</ref>
The designers, who provided [[enterprise integration]] products for over 10 years, used their knowledge of the industry when designing the language,<ref>{{Cite news|url=https://blog.jclark.com/2019/09/ballerina-programming-language-part-0.html|title=Ballerina Programming Language Part 0 - Context|last=Clark|first=James|access-date=2020-09-16|language=en-GB}}</ref><ref>{{Cite news|url=https://blog.jclark.com/2019/09/ballerina-programming-language-part-1.html|title=Ballerina Programming Language Part 1 - Concepts|last=Clark|first=James|access-date=2020-09-16|language=en-GB}}</ref> says WSO2 director and Ballerina founder James Clark.
== Examples ==
=== Hello World
The regular Hello World program:
<syntaxhighlight lang="typescript">
import ballerina/io;
public function main() {
io:println("Hello World!");
}
</syntaxhighlight>
To execute the above program, place the source code in a <code>.bal</code> file and provide the path of the file to the <code>bal run</code> command.
<syntaxhighlight lang="console">
$ ballerina run hello_world.bal
Hello World!
</syntaxhighlight>
The service version of the Hello World program:
<syntaxhighlight lang="typescript">
import ballerina/http;
service
resource function get . () returns string {
return "Hello World!";
}
}
</syntaxhighlight>
Services are executed in the same manner, except they don't terminate like regular programs do. Once the service is up and running, one can use an HTTP client to invoke the service. For example, the above service can be invoked using the following cURL command:
<syntaxhighlight lang="console">
$ curl http://localhost:9090/greet
Hello World!
</syntaxhighlight>
<ref name="example1">{{cite web |url=https://ballerina.io/learn/by-example/hello-world-service.html |title=Hello world service |author=Ballerina Team |date=16 September 2020 |publisher=ballerina.io |access-date=16 September 2020 |archive-date=16 September 2020 |archive-url=https://web.archive.org/web/20200916185848/https://ballerina.io/learn/by-example/hello-world-service.html |url-status=dead}}</ref>
=== REST API ===
<syntaxhighlight lang="d">
import ballerina/http;
service on new http:Listener(9090) {
resource function post factorial(@http:Payload string payload) returns http:Ok|http:BadRequest {
int|error num = int:fromString(payload);
if num is error {
return <http:BadRequest>{body: "Invalid integer: " + payload};
}
if num < 0 {
return <http:BadRequest>{body: "Integer should be >= 0"};
}
int result = 1;
foreach int i in 2 ... num {
result *= i;
}
return <http:Ok>{body: result};
}
}
</syntaxhighlight>
<syntaxhighlight lang="console">
$ curl http://localhost:9090/factorial -d 5
120
</syntaxhighlight>
=== GraphQL API ===
<syntaxhighlight lang="typescript">
import ballerina/graphql;
service /stocks on new graphql:Listener(4000) {
resource function get quote() returns StockQuote {
return {
ticker: "EXPO",
price: 287.5,
open: 285,
prevClose: 285.5,
low: 276.25,
high: 297
};
}
}
type StockQuote record {|
string ticker;
float price;
float open;
float prevClose;
float low;
float high;
|};
</syntaxhighlight>
<syntaxhighlight lang="console">
$ curl -H "Content-type: application/json" -d '{"query": "{ quote { ticker, price } }" }' 'http://localhost:4000/stocks'
{"data":{"quote":{"ticker":"EXPO", "price":287.5}}}
</syntaxhighlight>
===
The generated [[sequence diagram]] is a canonical representation of the source code. The two representations can be used interchangeably. The diagram support is provided through the Ballerina VS Code plugin. The following are a couple of such generated sequence diagrams, compared with its associated code.
A sample program for retrieving and processing COVID-19 data:
[[File:Working-with-data-code+diagram.png]]
A sample program for creating a report out of pull request data retrieved from GitHub:
[[File:Consuming-services-code+diagram.png]]
=== JSON support ===
The language provides support for working with JSON values. The builtin type `json` is defined as the following union: <code>()|boolean|int|float|decimal|string|json[]|map<json></code>
<syntaxhighlight lang="typescript">
import ballerina/io;
public function main() returns error {
// Syntax for `json` object values is very similar to the syntax of JSON
json person = {name: "John Doe", age: 25};
// Serialized `json` values conforms to the JSON specification
io:println(person);
// The fields of the `json` value can be accessed as follows
string name = check person.name;
int age = check person.age;
}
</syntaxhighlight>
=== Code to cloud ===
Docker and Kubernetes artifacts required for deploying the code to the cloud can be generated when building the code. Values required for these artifacts are derived from the code. Additionally, one can override these values as well using the <code>Cloud.toml</code> file. To enable generation of the cloud artifacts, the users can use the <code>cloud</code> build option in the <code>Ballerina.toml</code> file. Use <code>docker</code> to generate just the Docker image and the Dockerfile and use <code>k8s</code> to generate Kubernetes artifacts as well. Minimal sample config [[TOML]] files would look something like the following:
<code>Ballerina.toml</code> file:
<syntaxhighlight lang="toml">
[package]
distribution = "2201.0.0"
[build-options]
cloud="k8s"
</syntaxhighlight>
<code>Cloud.toml</code> file:
<syntaxhighlight lang="toml">
[container.image]
repository="bal_user"
name="greet"
tag="v0.1.0"
</syntaxhighlight>
=== Workers ===
<syntaxhighlight lang="d">
import ballerina/http;
import ballerina/lang.'int;
Line 78 ⟶ 199:
// to avoid deadlocks.
public function main() {
@strand {thread: "any"}
worker w1 {
int w1val = checkpanic calculate("2*3");
Line 97 ⟶ 219:
// A worker can have an explicit return type, or else, if a return type is not mentioned,
// it is equivalent to returning ().
@strand {thread: "any"}
worker w2 {
int w2val = checkpanic calculate("17*5");
Line 117 ⟶ 240:
function calculate(string expr) returns int|error {
http:Client httpClient = check new ("https://api.mathjs.org");
return check 'int:fromString(
}
</syntaxhighlight>
<ref name="example2">{{cite web |url=https://github.com/ballerina-platform/ballerina-distribution/blob/master/examples/worker-interaction/worker_interaction.bal |title= Worker interaction |author=Ballerina Team|date=16 September 2020 |publisher=ballerina.io}}</ref>
==References==
Line 167 ⟶ 254:
== External links ==
* {{Official website|ballerina.io}}
* {{GitHub|ballerina-platform}}
{{Programming languages}}
Line 181 ⟶ 268:
[[Category:Free software projects]]
[[Category:Cross-platform free software]]
<!-- Hidden categories below -->
[[Category:Articles with example code]]
|