Busy Developer's Guide to Ballerina

ted.neward@newardassociates.com | Blog: http://blogs.newardassociates.com | Github: tedneward | LinkedIn: tedneward

Objectives

What if you were to build a language....

The Ballerina Programming Language

... in a nutshell

The Ballerina Programming Language

What is this thing?

The Ballerina Programming Language

An Albums HTTP API (1 of 2)

configurable int port = 8080;
    
type Album readonly & record {|
    string id;
    string title;
    string artist;
    decimal price;
|};
    
table<Album> key(id) albums = table [
        {id: "1", title: "Blue Train", artist: "John Coltrane", price: 56.99},
        {id: "2", title: "Jeru", artist: "Gerry Mulligan", price: 17.99},
        {id: "3", title: "Sarah Vaughan and Clifford Brown", artist: "Sarah Vaughan", price: 39.99}
    ];

The Ballerina Programming Language

An Albums HTTP API (2 of 2)

service / on new http:Listener(port) {
    resource function get albums() returns Album[] {
        return albums.toArray();
    }
    
    resource function get albums/[string id]() returns Album|http:NotFound {
        Album? album = albums[id];
        if album is () {
            return http:NOT_FOUND;
        } else {
            return album;
        }
    }
    
    resource function post albums(@http:Payload Album album) returns Album {
        albums.add(album);
        return album;
    }
}

Concepts

The things you really need to know

Concepts

Built around a few key concepts

Concepts

Core entities

Getting Started with Ballerina

From zero to hello world

Getting Started with Ballerina

Try Ballerina

Getting Started with Ballerina

Installation

Getting Started with Ballerina

Learning Ballerina

Getting Started with Ballerina

Generating a new package

$ bal new greeter

Getting Started with Ballerina

Ballerina Syntax

Getting Started with Ballerina

Hello, Ballerina

import ballerina/io;

public function main() {
    io:println("Hello, World!");
}

from https://github.com/tedneward/Demo-Ballerina/greeter

Getting Started with Ballerina

Ballerina Semantics

Getting Started with Ballerina

Running Ballerina

Getting Started with Ballerina

Run

bal run greeter

Compile and run built JAR

bal build greeter
java -jar greeter/target/bin/greeter.jar

Getting RESTful

REST API for Greetings

import ballerina/http;

listener http:Listener httpListener = new (8080);

service / on httpListener {
    resource function get greeting() returns string { 
        return "Hello, World!"; 
    }

    resource function get greeting/[string name]() returns string { 
        return "Hello " + name; 
    }
}

from https://github.com/tedneward/Demo-Ballerina/restgreeter

Getting RESTful

Running

$ bal run

Client access

$ curl localhost:8080/greeting
Hello, World!
$ curl localhost:8080/greeting/Ballerina
Hello Ballerina

Getting Cloudy

Build a Docker

Getting Cloudy

Cloud.toml

[container.image]
repository="tedneward" # ex - Docker hub name
name="greeter" # container name
tag="v0.1.0"

[settings]
buildImage=true 
    # set this false to just gen the Dockerfile

from https://github.com/tedneward/Demo-Ballerina/restgreeter

Getting Cloudy

Run the Docker container

$ docker run -d -p 8080:8080 tedneward/restgreeter:v0.1.0

Example

Tic-Tac-Toe!

Example

Tic-Tac-Toe REST Server

Summary

Ballerina is...

Deeper

More about the language itself

Type System

Values, Types, and Variables

Type System

Values are of four kinds of types:

Type System

Variable declarations and usage

Type System

Simple values

Type System

Casting and conversions

Type System

Structured values

Type System

array: numeric-indexed vectors of types

Type System

tuple: collection of unnamed fields

Type System

mapping: `map`: a collection of named fields

Type System

mapping: `record`: a collection of named fields

Type System

Composing record types

type Date record {
    int year;
    int month;
    int day;
};

type TimeOfDay record {
    int hour;
    int minute;
    decimal seconds;
};

type Time record {
    *Date;
    *TimeOfDay;
};

Type System

Closed and open records

Type System

Sequence values

Type System

`xml`: object representations of hierarchy

Type System

Behavioral values

Type System

`error`: multi-valued fault condition type

Type System

`object`: collection of named fields and functions

Type System

Object construction

type Hashable object {
    function hash() returns int;
};

function h() returns Hashable {
    var obj = object {
        function hash() returns int {
            return 42;
        }
    };
    return obj;
}

Type System

Object type inclusion

type Cloneable object {
    function clone() returns Cloneable;
};

type Shape object {
    *Cloneable;
    
    function draw();
};

class Circle {
    *Shape;

    function clone() returns Circle { return new; }
    function draw() { }
}

Type System

distinct object types: significantly-named types

Type System

Distinct types

type Person distinct object {
    public string name;
};

distinct class Employee {
    *Person;
    
    function init(string name) {
        self.name = name;
    }
}

distinct class Manager {
    *Person;
    
    function init(string name) {
        self.name = name;
    }
}

Type System

Union "types"

Type System

Immutability, Isolation, and Concurrency

Type System

Isolation

Flow Control

Looping and breaking and ...

Flow Control

C-style flow control operations

Flow Control

conditional iteration

Flow Control

iteration over collections/streams

Flow Control

conditional decision-making

Flow Control

Pattern-matching

Flow Control

Pattern-matching

Flow Control

Nonstandard C-style flow control

Functions

Blocks of code in Ballerina

Functions

Basic syntax

Functions

Default parameter values

Functions

Record parameters

Functions

Rest parameters

Functions

Asynchronous invocation

JVM FFI

Connecting to external (JVM-based) code

JVM FFI

Nutshell

JVM FFI

Connecting to JVM field

// Returns the `out` field in the `java.lang.System` class.
// `java.lang.System#out` is of the `java.io.PrintStream` type.
public function getStdOut() returns handle = @java:FieldGet {
    name: "out",
    'class: "java/lang/System"
} external;

JVM FFI

Connecting to JVM method

// Invoke the `println` method on `java.lang.System#out`, 
// which accepts a `java.lang.String` parameter.
public function printInternal(handle receiver, handle strValue) = @java:Method {
    name: "println",
    'class: "java/io/PrintStream",
    paramTypes: ["java.lang.String"]
} external;

JVM FFI

Using both

public function main() {
    handle stdOut = getStdOut();
    handle str = java:fromString("Hello World");
    printInternal(stdOut, str);
}

Credentials

Who is this guy?