Superando	las	limitaciones	de	Java,	
con	el	sistema	de	tipos	de	
Ceylon
Presenta:	
Enrique	Zamudio	López
• Programando desde 1982
• Profesionalmente desde 1994
• Java desde 2000
• Proyectos FOSS:
• j8583
• jAlarms
• Ceylon
• Java Champion, 2015
Ceylon
• Lenguaje de programación orientado a
aplicaciones
• Proyectos grandes (50KLOC), equipos
grandes (10+ personas)
• Sistema de módulos integrado
• Multiplataforma: JVM, JavaScript, Dart, Android,
LLVM
Sistemas de tipos
• Todos los lenguajes tienen uno
• Tan sólo una característica más?
• Énfasis en SISTEMA
• La mayoría no piensa mucho al respecto
Ceylon
• El sistema de tipos es fundamental
• Una de sus características principales
• Sintaxis, gramática, operadores emanan del ST
• Arreglar problemas comunes y graves en el
sistema de tipos de Java y otros lenguajes
Ejemplos
Login
User login(String username,
String password);
• Devuelve un User si todo sale bien
• ¿Y si hay un error?
• Usuario no existe
• Password incorrecto
• Cuenta bloqueada
• Mantenimiento planeado
• Se cayó base de datos
User login(String username,
String password)
throws LoginException;
User login(String username,
String password)
throws LoginException;
✘
public class LoginResult {
public final User user;
public final int error;
}
LoginResult login(
String username,
String password);
abstract class LoginResult {}
class LoginSuccess
extends LoginResult {
final User user;
}
class LoginFailure
extends LoginResult {
final int error;
}
User|LoginError login(
String username,
String password);
User|LoginError login(
String username,
String password);
Tipo unión
if (is User r = login(u,p)) {
//es un usuario
print(r.username);
} else {
//es un error
print(r.reason);
}
Object obj = ...;
if (obj instanceof Foo) {
print(((Foo)obj).foo);
}
Object obj = ...;
if (obj instanceof Foo) {
print(((Bar)obj).bar);
}
Serializar
Runnables
void send(??? task) {
//Enviar tarea a otro proceso
//se ejecuta de manera remota
}
interface MiTarea
extends Runnable,
Serializable {}
void send(MiTarea task) {
//send task over the wire
//is run on the other side
}
class WeirdlyNamedApacheTask
implements Runnable,
Serializable {
...
}
class CumbersomeWrapper
implements MiTarea {
final WeirdlyNamedApacheTask
wrappedTask;
}
void send(
Runnable & Serializable task) {
//Exactamente lo que nos interesa
}
void send(
Runnable & Serializable task) {
//Los contratos que nos incumben
}
Tipo intersección
null
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;
✘
String | Integer
String | Null
String|Integer a = 1;
String|Integer b = "one";
String|Null s1 = "s";
String|Null s2 = null;
String|Integer a = 1;
String|Integer b = "one";
String? s1 = "s";
String? s2 = null;
String? s = foo();
print(s.size); //NO COMPILA
if (exists s) {
...
}
Object Null
Anything
String Number null
Tipos algebraicos (o
enumerados)
abstract class Anything
of Object|Null
class Null of null
object null extends Null
class Boolean
of true|false
class Comparison
of larger|smaller|equal
Object Null
Anything
String Number null
Tipo "fondo"
Object Null
Anything
Nothing
NumberString null
Iterable<String,Null>
Iterable<String,Nothing>
Object & Null
Nothing
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
• Sólo tipos denotables
"one more thing"
• Parámetros de tipo reificados (Reified generics)
• Varianza en sitio de declaración
• Covarianza y contravarianza simples de
entender
• Tuplas
¿?
Enrique	Zamudio	López
@chochosmx	
@ceylonlang
enrique@ceylon-lang.org
ceylon-lang.org	
try.ceylon-lang.org	
github.com/ceylon

Superando las limitaciones de Java, con Ceylon