Polyglot Frameworks

Andrew Grothe
It all began with a simple learning project
something simple...
small, easy to finish...
what language should I use?

Picking a Language

120+ Languages

#sadness

What if found one language or framework that is..

  • Strongly typed
  • Used on front end
  • Also on the back end
  • Provides great performance

Polyglot

pol·y·glot
/ˈpälēˌɡlät/

adjective
1. knowing or using several languages.

noun
1. a person who knows and is able to use several languages.

Polyglot (computing)

In computing, a polyglot is a computer program or script written in a valid form of multiple programming languages, which performs the same operations or output independent of the programming language used to compile or interpret it.
- wikipedia.org

Polyglot written in ANSI C, PHP and bash:


                    #define a /*
                    # /dev/null > /dev/null \ ;
                    // 2> /dev/null; x=a;
                    $x=5; // 2> /dev/null \ ;
                    if (($x))
                    // 2> /dev/null; then
                    return 0;
                    // 2> /dev/null; fi
                    #define e ?>
                    #define b */
                    #include 
                    #define main() int main(void)
                    #define printf printf(
                    #define true )
                    #define function
                    function main()
                    {
                    printf "Hello, world!\n"true/* 2> /dev/null | grep -v true*/;
                    return 0;
                    }
                    #define c /*
                    main
                    #*/
                            

Polyglot Framework

A programming framework or library that:

  1. Unifies a common API across multiple languages
  2. Compiles multiple languages into a single executable
  3. A single language that transpiles into multiple targets

- Andrew Grothe

Why?

Consolidate learning
Condense the stack
Eliminate technical debt

#happiness

Vert.x

  • Unifies a common API across multiple languages

                                // java
                                import io.vertx.core.AbstractVerticle;
                                public class Server extends AbstractVerticle {
                                  public void start() {
                                    vertx.createHttpServer().requestHandler(req -> {
                                      req.response()
                                        .putHeader("content-type", "text/plain")
                                        .end("Hello from Vert.x!");
                                    }).listen(8080);
                                  }
                                }
                        

                                // javascript
                                vertx.createHttpServer()
                                 .requestHandler(function (req) {
                                    req.response()
                                      .putHeader("content-type", "text/plain")
                                      .end("Hello from Vert.x!");
                                }).listen(8080);
                        

                                // groovy
                                vertx.createHttpServer().requestHandler({ req ->
                                    req.response()
                                      .putHeader("content-type", "text/plain")
                                      .end("Hello from Vert.x!")
                                  }).listen(8080)
                        

                                // scala
                                import io.vertx.lang.scala.ScalaVerticle
                                class Server extends ScalaVerticle {
                                  override def start(): Unit = {
                                    vertx
                                      .createHttpServer()
                                     .requestHandler(_.response()
                                       .putHeader("content-type", "text/plain")
                                       .end("Hello from Vert.x"))
                                     .listen(8080)
                                  }
                                }}
                        

Vert.x Languages

  • Java
  • JavaScript (Nashorn)
  • Groovy
  • Ruby
  • Ceylon
  • Scala
  • Kotlin

GraalVM

  • Compiles multiple languages into a single executable

GraalVM Polyglot Script


                            const express = require('express');
                            const app = express();
                            app.listen(3000);
                            app.get('/', function(req, res) {

                              var text = 'Hello World!';

                              const BigInteger = Java.type('java.math.BigInteger');
                              text += BigInteger
                                .valueOf(2)
                                .pow(100)
                                .toString(16);

                              text += Polyglot.eval('R', 'runif(100)')[0];
                              res.send(text);
                            })
                    

GraalVM Polyglot Script


                                import org.graalvm.polyglot.*;
                                public class HelloPolyglot {
                                  public static void main(String[] args) {
                                    System.out.println("Hello Java!");
                                    Context context = Context.create();
                                    context.eval("js",
                                      "print('Hello JavaScript!');");
                                  }
                                }
                        

Flutter

  • A single language that transpiles into multiple targets

Web Assembly - WASM

  • Unifies a common API across multiple languages
  • Compiles multiple languages into a single executable
  • A single language that transpiles into multiple targets

Real World Stack

You Need a Budget.com
8 languages, 2 DB, 3 frameworks,
3 devops, 5 services, 3+ IDEs

Server: Ruby on Rails, PostgreSQL
iOS: Objective-C, Swift, TypeScript, SQLite
Android: Java, Kotlin, TypeScript, SQLite
Web: Ember, TypeScript, Grunt, webpack
Services: GitHub, Heroku, Amazon Web Services, DigitalOcean, Recurly
DevOps: Ansible, Vagrant, Jenkins

Sample Polyglot Stack

4 Languages, 1 IDE

Web Tier - HTML, CSS, Blazor (C#)
Mobile - Xamarin.Forms (XAML, C#)
Application Tier (C#)
Shared Assets (C#)

Demos

  • Vert.x
  • GraalVM
  • Blazor
  • Doom 3 time permitting

Recap - Why Polyglot

Consolidate learning
Condense the stack
Eliminate technical debt
#happiness

Slides => https://agrothe.github.io/polyglot-talk/