23
Except where otherwise noted, this work is licensed under: http: //creativecommons.org/licenses/by-nc-sa/3.0/ The polyglot programmer Leganés, feb 11, 12 David Muñoz @voiser

T3chFest 2016 - The polyglot programmer

Embed Size (px)

Citation preview

Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0/

The polyglot programmer

Leganés, feb 11, 12

David Muñoz@voiser

objective:

qualitative description of languages

let’s focus on some simple concepts

style (structured / OO / prototype / functional / ...)

typing (static / dynamic / strong / weak)

execution model (high / low level, native, vm, thread safety, ...)

difference between syntax, semantics, idioms, libraries and tools

C (1972) - structured

#include <stdio.h>

void update(int i) { i = i + 1;}

int main() { int i = 0; println(“i = %d\n”, i); // i = 0 update(i); println(“i = %d\n”, i); // i = ? char * str = (char*)i; // ? return 0;}

hero

C (1972) - structured

#include <stdio.h>

void update(int i) { i = i + 1;}

int main() { int i = 0; println(“i = %d\n”, i); // i = 0 update(i); println(“i = %d\n”, i); // i = ? char * str = (char*)i; // ? return 0;}

master

static typing

pass by value

it’s a high level assembly!

weak type system

C (1972) - structured

0000000000400506 <update>: 400506: 55 push %rbp 400507: 48 89 e5 mov %rsp,%rbp 40050a: 89 7d fc mov %edi,-0x4(%rbp) 40050d: 83 45 fc 01 addl $0x1,-0x4(%rbp) 400511: 5d pop %rbp 400512: c3 retq

0000000000400513 <main>: 400513: 55 push %rbp 400514: 48 89 e5 mov %rsp,%rbp 400517: 48 83 ec 10 sub $0x10,%rsp 40051b: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%rbp) 400522: 8b 45 fc mov -0x4(%rbp),%eax 400525: 89 c6 mov %eax,%esi 400527: bf e4 05 40 00 mov $0x4005e4,%edi 40052c: b8 00 00 00 00 mov $0x0,%eax 400531: e8 aa fe ff ff callq 4003e0 <printf@plt> 400536: 8b 45 fc mov -0x4(%rbp),%eax 400539: 89 c7 mov %eax,%edi 40053b: e8 c6 ff ff ff callq 400506 <update> 400540: 8b 45 fc mov -0x4(%rbp),%eax 400543: 89 c6 mov %eax,%esi 400545: bf e4 05 40 00 mov $0x4005e4,%edi 40054a: b8 00 00 00 00 mov $0x0,%eax 40054f: e8 8c fe ff ff callq 4003e0 <printf@plt> 400554: b8 00 00 00 00 mov $0x0,%eax 400559: c9 leaveq 40055a: c3 retq 40055b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)

god of metal

C++ (1983) - Object oriented

#include <iostream>

using namespace std;

class Point { public: float _x, _y; Point(float x, float y) : _x(x), _y(y) {}};

ostream &operator<< (ostream &os, Point const &p) { return os << "Point(" << p._x << "," << p._y << ")"; }

void update(Point p) { p._x += 1; p._y += 1;}

int main() { Point a(1, 2); cout << a << endl; // Point(1, 2) update(a); cout << a << endl; // Point(?, ?) char * str = (char*)a; // ? char * str = (char*)&a; // ? return 0;}

C++ (1983) - Object oriented

#include <iostream>

using namespace std;

class Point { public: float _x, _y; Point(float x, float y) : _x(x), _y(y) {}};

ostream &operator<< (ostream &os, Point const &p) { return os << "Point(" << p._x << "," << p._y << ")"; }

void update(Point p) { p._x += 1; p._y += 1;}

int main() { Point a(1, 2); cout << a << endl; update(a); cout << a << endl; char * str = (char*)a; char * st2 = (char*)&a; return 0;}

static typing

pass by value (also by ref, not shown in this example)

a little bit higher level than assembly

weak type system

namespaces

(operator) overloading

module Example where

data Point a = Point a a deriving (Show)

update (Point x y) = Point (x+1) (y+1)

applyTwice f x = f (f x)

main = let a = Point 1 1 b = applyTwice update a b = update b -- ? in print b

Haskell (1990) - functional

module Example where

data Point a = Point a a deriving (Show)

update (Point x y) = Point (x+1) (y+1)

applyTwice f x = f (f x)

main = let a = Point 1 1 b = applyTwice update a b = update b -- ? in print b

Haskell (1990) - functional

data type and constructor

strong, static typing, type inference

looks and feels different. Don’t take a look at the native code generated.

immutability

Let’s give it a try

Python (1991)

describe me!

Python (1991) code example

class AgentManager:

def add(self, name, source, sender=None, locale="en"):

""" Add an agent to the list. """

self.set_locale(locale)

if not self.has_permissions(sender):

print(MSG_NO_PERM)

return self.feedback(MSG_NO_PERM, sender)

alist = self.read_list()

$ python3...>>> 1 + "2"Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: unsupported operand type(s) for +: 'int' and 'str'

>>> a = 1>>> a = "this is a string"

https://github.com/voiser/zoe-startup-kit/blob/master/agents/zam/zam.py

Python (1991)

high levelobject oriented

dynamic, strong typingvm, gc

pretty*syntax, significant whitespacehuge community

(probably there’s a python library for that!)CPython / Jython / PyPy / IronPython

what if I describe you a language without letting you see actual code?

high levelobject oriented

vm jit, gcstatic, explicit, strong* typing

(messy) generic typessimple*, verbose, bureaucratic

enormous community

Java - let me break your strong type system

import java.util.List;

public class T3chFest {

private Object[] stuff;private List<Object> rooms;

public T3chFest(String[] talks, List<String> rooms) {this.stuff = talks;stuff[0] = Thread.currentThread(); // ?this.rooms = rooms; // ?List rooms2 = rooms; // ?this.rooms = rooms2; // ?

}}

~ java + MLfunctional + object oriented

jvmstatic, strong rock-solid adamantium-like

heavier-than-the-gods-of-metal typingnice* syntax with (partial) type inference

A language build on its type systeminto JVM? learn. it. now.

Scala (2003)

scala> val a = Array("a", "b", "c")a: Array[String] = Array(a, b, c)

scala> val b: Array[Object] = a<console>:11: error: type mismatch; found : Array[String] required: Array[Object]Note: String <: Object, but class Array is invariant in type T.

Scala (2003) - fun with invariance

scala> class Animal;scala> class Mammal extends Animal;scala> class Human extends Mammal;

scala> class Group[ +A] scala> def sizeof(g: Group[ Animal]) = ...scala> sizeof(new Group[ Animal]())scala> sizeof(new Group[ Mammal]())

scala> class Veterinary[ -A]scala> def treat(g: Group[Mammal], v: Veterinary[ Mammal]) = ...scala> treat(..., new Veterinary[ Mammal]())scala> treat(..., new Veterinary[ Animal]())

scala> class Treatment[ T <: Mammal]scala> def cure[B](x: Group[ B]) : Treatment[B] = …error: type arguments [B] do not conform to class Treatment's type parameter bounds [T <: Mammal]scala> def cure[B <: Mammal](x: Group[B]) : Treatment[B] = ...

Scala - basic types

typeclassesstructural types (a.k.a. duck typing)

refined typesself-recursive types

path-dependent types...

tip of the iceberg

The recruiter’s infinite knowledge

Why are you interested in Scala, when Java 8 already has lambdas?

Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0/

Leganés, feb 11, 12

Thanks!