A bird's eye view on some programming languages, focusing on concepts like typing, execution model or style. Presented on T3chFest 2016 in Leganés, Madrid, Spain.
result management system report for college project
T3chFest 2016 - The polyglot programmer
1. 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
3. 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
4. C (1972) - structured
#include <stdio.h>
void update(int i) {
i = i + 1;
}
int main() {
int i = 0;
println(“i = %dn”, i); // i = 0
update(i);
println(“i = %dn”, i); // i = ?
char * str = (char*)i; // ?
return 0;
}
hero
5. C (1972) - structured
#include <stdio.h>
void update(int i) {
i = i + 1;
}
int main() {
int i = 0;
println(“i = %dn”, i); // i = 0
update(i);
println(“i = %dn”, i); // i = ?
char * str = (char*)i; // ?
return 0;
}
master
static typing
pass by value
it’s a high level assembly!
weak type system
7. 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;
}
8. 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
9. 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
10. 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
13. 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
14. Python (1991)
high level
object oriented
dynamic, strong typing
vm, gc
pretty*syntax, significant whitespace
huge community
(probably there’s a python library for that!)
CPython / Jython / PyPy / IronPython
15. what if I describe you a language without
letting you see actual code?
16. high level
object oriented
vm jit, gc
static, explicit, strong* typing
(messy) generic types
simple*, verbose, bureaucratic
enormous community
17. 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; // ?
}
}
18. ~ java + ML
functional + object oriented
jvm
static, strong rock-solid adamantium-like
heavier-than-the-gods-of-metal typing
nice* syntax with (partial) type inference
A language build on its type system
into JVM? learn. it. now.
Scala (2003)
19. 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
20. 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