Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Powered by Rabbit 2.1.2 Ruby 3.0

Slide 4

Slide 4 text

Powered by Rabbit 2.1.2 May happen in next 10 years

Slide 5

Slide 5 text

Powered by Rabbit 2.1.2 Concurrency JIT Static typing

Slide 6

Slide 6 text

JIT Static typing !

Slide 7

Slide 7 text

• STRUCTURAL TYPING • IMPLICIT TYPING • SOFT TYPING STATIC TYPING +

Slide 8

Slide 8 text

VALUES

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

00111111 10000000 00000000 00000000 01000000 00000000 00000000 00000000 = 1.0 = 2.0 01000000 01000000 00000000 00000000 = 3.0 + = 01111111 10000000 00000000 00000000 ≠

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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)

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

TYPE SYSTEMS

Slide 16

Slide 16 text

METADATA ON THINGS IN YOUR PROGRAM

Slide 17

Slide 17 text

WHERE DOES THE METADATA GO?

Slide 18

Slide 18 text

WHAT DOES THE METADATA LOOK LIKE?

Slide 19

Slide 19 text

WHERE DOES THE METADATA COME FROM?

Slide 20

Slide 20 text

WHERE DOES THE METADATA GO?

Slide 21

Slide 21 text

NOWHERE

Slide 22

Slide 22 text

“UN”

Slide 23

Slide 23 text

“UNI”

Slide 24

Slide 24 text

ON VALUES

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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,

Slide 27

Slide 27 text

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; };

Slide 28

Slide 28 text

#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)

Slide 29

Slide 29 text

“DYNAMIC”

Slide 30

Slide 30 text

ON SOURCE CODE

Slide 31

Slide 31 text

#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)); }

Slide 32

Slide 32 text

“STATIC”

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

WHAT DOES THE METADATA LOOK LIKE?

Slide 35

Slide 35 text

NAMES

Slide 36

Slide 36 text

#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)); }

Slide 37

Slide 37 text

STRUCTURES

Slide 38

Slide 38 text

type Shape interface { Area() float64 }

Slide 39

Slide 39 text

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 }

Slide 40

Slide 40 text

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) }

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

WHERE DOES THE METADATA COME FROM?

Slide 43

Slide 43 text

THE PROGRAMMER

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

“MANIFEST”

Slide 46

Slide 46 text

THE COMPUTER

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

“IMPLICIT”

Slide 49

Slide 49 text

POSSIBLE RUBY 3.0 FEATURES

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

STATIC, NOT DYNAMIC

Slide 54

Slide 54 text

STRUCTURAL, NOT NOMINAL

Slide 55

Slide 55 text

IMPLICIT, NOT MANIFEST

Slide 56

Slide 56 text

HOW?

Slide 57

Slide 57 text

SOFT TYPING?

Slide 58

Slide 58 text

IDEALISED FUNCTIONAL LANGUAGE” “

Slide 59

Slide 59 text

NOT STRUCTURAL

Slide 60

Slide 60 text

NO SAFETY!

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

WHAT NEXT?

Slide 63

Slide 63 text

CAN WE? SHOULD WE?

Slide 64

Slide 64 text

WE CAN & WE SHOULD

Slide 65

Slide 65 text

SOFTWARE QUALITY MATTERS

Slide 66

Slide 66 text

RUBY ALREADY HAS TYPES

Slide 67

Slide 67 text

HARD PROBLEMS

Slide 68

Slide 68 text

A PHD, NOT A PR

Slide 69

Slide 69 text

LET’S TALK ABOUT IT

Slide 70

Slide 70 text

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