$30 off During Our Annual Pro Sale. View Details »

Consider static typing

Tom Stuart
February 05, 2015

Consider static typing

Matz announced in his RubyConf 2014 keynote that Ruby 3.0 might have a static type system. What does that really mean? How should we feel about it? Will Ruby 3.0 still be Ruby? In this talk I’ll unpack what Matz said and make some educated guesses at what it tells us about the future of the language.

Given at RubyConf Australia 2015 (http://rubyconf.org.au/2015). There’s a video of this talk at https://www.youtube.com/watch?v=efzHrOxzrNE, and an expanded transcript is available at https://tomstu.art/consider-static-typing.

Tom Stuart

February 05, 2015
Tweet

More Decks by Tom Stuart

Other Decks in Programming

Transcript

  1. CONSIDER
    STATIC
    TYPING
    http://codon.com/consider-static-typing

    View Slide

  2. View Slide

  3. Powered by Rabbit 2.1.2

    Ruby 3.0

    View Slide

  4. Powered by Rabbit 2.1.2

    May happen in next 10 years

    View Slide

  5. Powered by Rabbit 2.1.2

    Concurrency
    JIT
    Static typing

    View Slide

  6. JIT
    Static typing
    !

    View Slide

  7. • STRUCTURAL TYPING
    • IMPLICIT TYPING
    • SOFT TYPING
    STATIC TYPING
    +

    View Slide

  8. VALUES

    View Slide

  9. 11011010 01101100 11101101 01101101
    11011111 11011011 00010100 11010010
    10101101 01100001 10010010 00100101
    01110111 10110110 01101101 11011011
    11011010 10110111 10110101 00100101
    00010011 11111101 10100010 10010101
    01111011 01110100 00010010 11001101
    11011010 10010100 01101101 11110110
    11010101 01010010 00100110 10010101
    00010111 11101101 10000111 01001010

    View Slide

  10. 00111111 10000000 00000000 00000000
    01000000 00000000 00000000 00000000
    = 1065353216
    = 1073741824
    01111111 10000000 00000000 00000000 = 2139095040
    +
    =

    View Slide

  11. 00111111 10000000 00000000 00000000
    01000000 00000000 00000000 00000000
    = 1.0
    = 2.0
    01000000 01000000 00000000 00000000 = 3.0
    +
    =
    01111111 10000000 00000000 00000000

    View Slide

  12. ADD R0, R1, R2
    LDR R0, [R1]
    LDF F0, [R1]
    STR R0, [R2, R1]
    MOV PC, R1

    View Slide

  13. GET "LIBHDR"
    LET START() = VALOF $(
    FOR I = 1 TO 5 DO
    WRITEF("%N! = %I4*N", I, FACT(I))
    RESULTIS 0
    $)
    AND FACT(N) = N = 0 -> 1, N * FACT(N - 1)

    View Slide

  14. LET X = I #+ N
    LET Y = N #* 3.14
    LET Z = !N
    !N := 1.2 * 3.4

    View Slide

  15. TYPE
    SYSTEMS

    View Slide

  16. METADATA
    ON THINGS
    IN YOUR
    PROGRAM

    View Slide

  17. WHERE
    DOES THE
    METADATA
    GO?

    View Slide

  18. WHAT
    DOES THE
    METADATA
    LOOK LIKE?

    View Slide

  19. WHERE
    DOES THE
    METADATA
    COME FROM?

    View Slide

  20. WHERE
    DOES THE
    METADATA
    GO?

    View Slide

  21. NOWHERE

    View Slide

  22. “UN”

    View Slide

  23. “UNI”

    View Slide

  24. ON VALUES

    View Slide

  25. typedef unsigned long VALUE;
    struct RBasic {
    VALUE flags;
    const VALUE klass;
    }

    View Slide

  26. enum ruby_value_type {
    RUBY_T_NONE = 0x00,
    RUBY_T_OBJECT = 0x01,
    RUBY_T_CLASS = 0x02,
    RUBY_T_MODULE = 0x03,
    RUBY_T_FLOAT = 0x04,
    RUBY_T_STRING = 0x05,
    RUBY_T_REGEXP = 0x06,
    RUBY_T_ARRAY = 0x07,
    RUBY_T_HASH = 0x08,
    RUBY_T_STRUCT = 0x09,
    RUBY_T_BIGNUM = 0x0a,
    RUBY_T_FILE = 0x0b,

    View Slide

  27. struct RString {
    struct RBasic basic;
    union {
    struct {
    long len;
    char *ptr;
    union {
    long capa;
    VALUE shared;
    } aux;
    } heap;
    char ary[RSTRING_EMBED_LEN_MAX + 1];
    } as;
    };
    struct RArray {
    struct RBasic basic;
    union {
    struct {
    long len;
    union {
    long capa;
    VALUE shared;
    } aux;
    const VALUE *ptr;
    } heap;
    const VALUE ary[RARRAY_EMBED_LEN_MAX];
    } as;
    };
    struct RRegexp {
    struct RBasic basic;
    struct re_pattern_buffer *ptr;
    const VALUE src;
    unsigned long usecnt;
    };
    struct RObject {
    struct RBasic basic;
    union {
    struct {
    long numiv;
    VALUE *ivptr;
    struct st_table *iv_index_tbl;
    } heap;
    VALUE ary[ROBJECT_EMBED_LEN_MAX];
    } as;
    };

    View Slide

  28. #define TYPE(x) rb_type((VALUE)(x))
    static inline int
    rb_type(VALUE obj)
    {
    if (IMMEDIATE_P(obj)) {
    if (FIXNUM_P(obj)) return T_FIXNUM;
    if (FLONUM_P(obj)) return T_FLOAT;
    if (obj == Qtrue) return T_TRUE;
    if (STATIC_SYM_P(obj)) return T_SYMBOL;
    if (obj == Qundef) return T_UNDEF;
    }
    else if (!RTEST(obj)) {
    if (obj == Qnil) return T_NIL;
    if (obj == Qfalse) return T_FALSE;
    }
    return BUILTIN_TYPE(obj);
    }
    #define BUILTIN_TYPE(x) (int)(((struct RBasic*)(x))->flags & T_MASK)

    View Slide

  29. “DYNAMIC”

    View Slide

  30. ON SOURCE
    CODE

    View Slide

  31. #include
    long factorial(int n) {
    long result = 1;
    for (int i = 1; i <= n; ++i) {
    result *= i;
    }
    return result;
    }
    int main(void) {
    int n;
    scanf("%d", &n);
    printf("%d! = %ld\n", n, factorial(n));
    }

    View Slide

  32. “STATIC”

    View Slide

  33. • NOWHERE (“UN”)
    • ON VALUES (“DYNAMIC”)
    • ON SOURCE CODE (“STATIC”)
    WHERE DOES
    THE METADATA
    GO?

    View Slide

  34. WHAT
    DOES THE
    METADATA
    LOOK LIKE?

    View Slide

  35. NAMES

    View Slide

  36. #include
    long factorial(int n) {
    long result = 1;
    for (int i = 1; i <= n; ++i) {
    result *= i;
    }
    return result;
    }
    int main(void) {
    int n;
    scanf("%d", &n);
    printf("%d! = %ld\n", n, factorial(n));
    }

    View Slide

  37. STRUCTURES

    View Slide

  38. type Shape interface {
    Area() float64
    }

    View Slide

  39. type Rectangle struct {
    width, height float64
    }
    func (r Rectangle) Area() float64 {
    return r.width * r.height
    }
    type Circle struct {
    radius float64
    }
    func (c Circle) Area() float64 {
    return math.Pi * c.radius * c.radius
    }

    View Slide

  40. func main() {
    shapes := []Shape{
    Rectangle{width: 6, height: 7},
    Circle{radius: 8},
    }
    totalArea := 0.0
    for _, shape := range shapes {
    totalArea += shape.Area()
    }
    fmt.Println(totalArea)
    }

    View Slide

  41. • NAMES (“NOMINAL”)
    • STRUCTURES (“STRUCTURAL”)
    WHAT DOES
    THE METADATA
    LOOK LIKE?

    View Slide

  42. WHERE
    DOES THE
    METADATA
    COME FROM?

    View Slide

  43. THE
    PROGRAMMER

    View Slide

  44. long factorial(int n) {
    long result = 1;
    for (int i = 1; i <= n; ++i) {
    result *= i;
    }
    return result;
    }

    View Slide

  45. “MANIFEST”

    View Slide

  46. THE
    COMPUTER

    View Slide

  47. > let factorial n = if n == 0 then 1 else n * factorial (n - 1)
    > :type factorial
    factorial :: (Num a, Eq a) => a -> a

    View Slide

  48. “IMPLICIT”

    View Slide

  49. POSSIBLE
    RUBY 3.0
    FEATURES

    View Slide

  50. View Slide

  51. Powered by Rabbit 2.1.2

    # x requires to have to_int
    def foo(x)
    print x.to_int
    end
    foo(1) # OK: 1 has to_int
    foo("a") # NG: "a" does not have to_int

    View Slide

  52. Powered by Rabbit 2.1.2

    Type is represented by:
    Set of methods
    name
    number and type of arguments
    Class (as set of methods)

    View Slide

  53. STATIC,
    NOT
    DYNAMIC

    View Slide

  54. STRUCTURAL,
    NOT
    NOMINAL

    View Slide

  55. IMPLICIT,
    NOT
    MANIFEST

    View Slide

  56. HOW?

    View Slide

  57. SOFT
    TYPING?

    View Slide

  58. IDEALISED
    FUNCTIONAL
    LANGUAGE”

    View Slide

  59. NOT
    STRUCTURAL

    View Slide

  60. NO SAFETY!

    View Slide

  61. View Slide

  62. WHAT
    NEXT?

    View Slide

  63. CAN WE?
    SHOULD WE?

    View Slide

  64. WE CAN &
    WE SHOULD

    View Slide

  65. SOFTWARE
    QUALITY
    MATTERS

    View Slide

  66. RUBY
    ALREADY
    HAS TYPES

    View Slide

  67. HARD
    PROBLEMS

    View Slide

  68. A PHD,
    NOT A PR

    View Slide

  69. LET’S TALK
    ABOUT IT

    View Slide

  70. THANKS.
    @tomstuart
    http://codon.com/consider-static-typing

    View Slide