Busy Developer's Guide to Ballerina

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

Objectives

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

Ballerina

The Programming Language

Ballerina

Ballerina Programming Language

Concepts

The things you really need to know

Concepts

Built around a few key concepts

Concepts

Core entities

Getting Started

From zero to hello world

Getting Started

Try Ballerina

Getting Started

Installing Ballerina

Getting Started

Learning Ballerina

Getting Started

Generating a new package

$ bal new greeter

Getting Started

Ballerina Syntax

Getting Started

Hello, Ballerina

import ballerina/io;

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

Getting Started

Ballerina Semantics

Getting Started

Running Ballerina

Getting Started

Running

$ bal run

Build-and-run

$ bal build; bal run 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; 
    }
}

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

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?