Sistemas de tipos: Lo bueno, lo malo y lo feo

54
Sistemas de tipos: lo bueno, lo malo y lo feo Enrique Zamudio @chochosmx github.com/chochos @ceylonlang #Ceylon

Transcript of Sistemas de tipos: Lo bueno, lo malo y lo feo

Sistemas de tipos: lo bueno, lo malo y lo feo

Enrique Zamudio @chochosmx

github.com/chochos @ceylonlang

#Ceylon

• Programando desde 1982

• Profesionalmente desde 1994

• En Java desde 2000

• Proyectos FOSS

• j8583

• jAlarms

• Ceylon

Sistemas de tipos

• Todos los lenguajes tienen uno

• Una característica más?

• Nótese el término: SISTEMA

• Consideración superficial

Static

Dynamic

Static

Dynamic

Ceylon

• Creado a partir de un sistema de tipos

• Es una de sus características principales

• Sintaxis, gramática, operadores obedecen al ST

• Arreglar problemas comunes y/o graves en el sistema de tipos de Java

Java

null

ALGOL 1965

"I call it my billion-dollar mistake"

Tony Hoare 2009

core dump

NullPointerException

try { print(s.length()); } catch (NullPointerException ex) { System.out.println("Yay"); }

String s = null;

Object o = null; Integer i = null; List<String> l = null; PrintWriter p = null; WorkerThreadFactory wtf = null;

String s = null;

¿Cuál es el tipo de null?

Object

String Number Collection

Integer Float List Map

null

null

Object

Object Null

Object Null

Anything

Object Null

Anything

String Number

Object Null

Anything

String Number null

String s = null; Integer s = null; Null n = null;

String s = null; Integer s = null; Null n = null;✘

Unión de tipos: Tipo unión

String | Integer

String | Null

String|Integer a = 1; String|Integer b = "uno";

String|Null s1 = "s"; String|Null s2 = null;

String|Integer a = 1; String|Integer b = "uno";

String? s1 = "s"; String? s2 = null;

String? s = algo(); print(s.size); //NO COMPILA if (exists s) { ... }

Object Null

Anything

String Number null

Tipos algebraicos (o enumerados)

class Anything of Object|Null

class Null of null

object null extends Null

class Boolean of true|false

class Comparison of larger|smaller|equal

Usuario login(String username, String password);

Usuario login(String username, String password) throws LoginException;

Usuario|Integer login( String username, String password);

casting

Object algo; if (algo instanceof String) { ((String)algo).length(); }

Object algo; if (algo instanceof String) { //50 líneas ((Collection)algo).clear(); }

Object algo; if (is String algo) { algo.length(); }

Flow typing: Tipado por flujo

String|Integer? algo; if (exists algo) { print(algo.hash); if (is String algo) { print(algo.uppercased); } else { print(algo/2); } }

Inferencia de tipos (local)

value a = 1; //Integer value b = "2"; //String value c = login("a","b"); //Usuario|Integer value d = a <=> 2; //Comparison

Intersección de tipos: Tipo intersección

void enviar(Runnable r) { if (r instanceof Serializable) { stream.write((Serializable)r); } else { throw; } }

interface Runnalizable extends Runnable, Serializable { }

void enviar(Runnalizable r) { stream.write(r); }

void enviar( Runnable & Serializable r) { stream.write(r); }

Tipos intersección

Tipos no denotables

private java.util.List<?> lista;

<T> void boom(java.util.List<T> l, T arg) { lista = l; lista.add(arg); }

Demo.java:7: error: no suitable method found for add(T) lista.add(arg); ^ method List.add(int,CAP#1) is not applicable (actual and formal argument lists differ in length) method List.add(CAP#1) is not applicable (actual argument T cannot be converted to CAP#1 by method invocation conversion) method Collection.add(CAP#1) is not applicable (actual argument T cannot be converted to CAP#1 by method invocation conversion) where T is a type-variable: T extends Object declared in method <T>boom(List<T>,T) where CAP#1 is a fresh type-variable: CAP#1 extends Object from capture of ?

Resumen• Tipos unión

• Tipos intersección

• Tipos enumerados

• Tipado por flujo / acotación de tipos

• Inferencia local de tipos

• Tipos denotables

Otras cosas

• Reified generics

• Varianza en el sitio de declaración

• Covarianza y contravarianza

• Tuplas

¿?

ceylon-lang.orggithub.com/ceylon

@ceylonlang

[email protected]