Consider static typing

Cd9b247e4507fed75312e9a42070125d?s=47 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 http://codon.com/consider-static-typing.

Cd9b247e4507fed75312e9a42070125d?s=128

Tom Stuart

February 05, 2015
Tweet

Transcript

  1. 2.
  2. 8.
  3. 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
  4. 10.

    00111111 10000000 00000000 00000000 01000000 00000000 00000000 00000000 = 1065353216

    = 1073741824 01111111 10000000 00000000 00000000 = 2139095040 + =
  5. 11.

    00111111 10000000 00000000 00000000 01000000 00000000 00000000 00000000 = 1.0

    = 2.0 01000000 01000000 00000000 00000000 = 3.0 + = 01111111 10000000 00000000 00000000 ≠
  6. 12.

    ADD R0, R1, R2 LDR R0, [R1] LDF F0, [R1]

    STR R0, [R2, R1] MOV PC, R1
  7. 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)
  8. 14.

    LET X = I #+ N LET Y = N

    #* 3.14 LET Z = !N !N := 1.2 * 3.4
  9. 21.
  10. 22.
  11. 23.
  12. 24.
  13. 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,
  14. 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; };
  15. 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)
  16. 31.

    #include <stdio.h> 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)); }
  17. 33.

    • NOWHERE (“UN”) • ON VALUES (“DYNAMIC”) • ON SOURCE

    CODE (“STATIC”) WHERE DOES THE METADATA GO?
  18. 35.
  19. 36.

    #include <stdio.h> 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)); }
  20. 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 }
  21. 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) }
  22. 44.

    long factorial(int n) { long result = 1; for (int

    i = 1; i <= n; ++i) { result *= i; } return result; }
  23. 47.

    > let factorial n = if n == 0 then

    1 else n * factorial (n - 1) > :type factorial factorial :: (Num a, Eq a) => a -> a
  24. 50.
  25. 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
  26. 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)
  27. 56.
  28. 61.