Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A gentle introduction to types

A gentle introduction to types

- What are types?
- Why types?
- Dynamic v Static typing
- Gradual typing
- Examples
- Advantages

Shrayas Rajagopal

February 25, 2017
Tweet

More Decks by Shrayas Rajagopal

Other Decks in Programming

Transcript





  1. AGENDA

    View full-size slide

  2. CLASS OF VALUES

    View full-size slide

  3. CLASS OF VALUES
    OPERATIONS

    View full-size slide

  4. CLASS OF VALUES
    OPERATIONS
    Set of Rules
    for a variable

    View full-size slide

  5. HUMANS make
    MISTAKES!!!!

    View full-size slide

  6. COMPUTERS can
    REPEAT!!!!!!!!!!!!

    View full-size slide

  7. COMPUTERS
    HUMANS

    View full-size slide

  8. TYPE SYSTEMS.

    View full-size slide

  9. TYPE SYSTEMS

    View full-size slide

  10. TYPE SYSTEMS
    SOURCE

    View full-size slide

  11. TYPE SYSTEMS
    SOURCE

    View full-size slide

  12. TYPE SYSTEMS
    SOURCE

    View full-size slide

  13. TYPE SYSTEMS
    SOURCE
    TypeError: unsupported operand type(s) for +: 'int' and 'str'

    View full-size slide

  14. TYPE SYSTEMS
    ?

    View full-size slide

  15. CLASS OF VALUES
    OPERATIONS

    View full-size slide

  16. VAR
    VAR
    VAR
    VAR
    VAR
    VAR

    View full-size slide

  17. VAR VAR
    VAR
    VAR VAR VAR

    View full-size slide

  18. VAR VAR
    VAR
    VAR VAR VAR

    View full-size slide

  19. VAR VAR
    VAR
    VAR VAR VAR
    VAR

    View full-size slide

  20. VAR VAR
    VAR
    VAR VAR VAR
    VAR

    View full-size slide

  21. TYPES
    STATIC
    TYPES
    DYNAMIC

    View full-size slide

  22. TYPES
    STATIC
    TYPES
    DYNAMIC
    WRONG!!!1

    View full-size slide

  23. ???
    STATIC
    TYPES
    ???
    DYNAMIC
    TYPES

    View full-size slide

  24. COMPILE TIME
    STATIC
    TYPES
    RUN TIME
    DYNAMIC
    TYPES

    View full-size slide

  25. >>> people = []
    PYTHON

    View full-size slide

  26. >>> people = []
    >>> people.append({
    “name”: “Foo”,
    “age”: “26”
    })
    >>> people.append({
    “name”: “Bar”
    “age”: 28
    })
    PYTHON

    View full-size slide

  27. >>> people = []
    >>> people.append({
    “name”: “Foo”,
    “age”: “26”
    })
    >>> people.append({
    “name”: “Bar”
    “age”: 28
    })
    >>> [ person[“age”] + 2 for person in people ]
    PYTHON

    View full-size slide

  28. >>> people = []
    >>> people.append({
    “name”: “Foo”,
    “age”: “26”
    })
    >>> people.append({
    “name”: “Bar”
    “age”: 28
    })
    >>> [ person[“age”] + 2 for person in people ]
    ...
    TypeError: cannot concatenate 'str' and 'int' objects
    PYTHON

    View full-size slide

  29. >>> people = []
    >>> people.append({
    “name”: “Foo”,
    “age”: “26”
    })
    >>> people.append({
    “name”: “Bar”
    “age”: 28
    })
    >>> [ person[“age”] + 2 for person in people ]
    ...
    TypeError: cannot concatenate 'str' and 'int' objects
    PYTHON
    RUNTIME

    View full-size slide

  30. C#
    class Person
    {
    public string Name {get; set;}
    public int Age {get; set;}
    }

    View full-size slide

  31. var people = new List();
    C#
    class Person
    {
    public string Name {get; set;}
    public int Age {get; set;}
    }

    View full-size slide

  32. var people = new List();
    people.Add(new Person
    {
    Name = “Foo”,
    Age = “26”
    });
    people.Add(new Person
    {
    Name = “Bar”,
    Age = 28
    });
    C#
    class Person
    {
    public string Name {get; set;}
    public int Age {get; set;}
    }

    View full-size slide

  33. var people = new List();
    people.Add(new Person
    {
    Name = “Foo”,
    Age = “26”
    });
    people.Add(new Person
    {
    Name = “Bar”,
    Age = 28
    });
    people.Select(p => p.Age + 2);
    C#
    class Person
    {
    public string Name {get; set;}
    public int Age {get; set;}
    }

    View full-size slide

  34. var people = new List();
    people.Add(new Person
    {
    Name = “Foo”,
    Age = “26”
    });
    people.Add(new Person
    {
    Name = “Bar”,
    Age = 28
    });
    people.Select(p => p.Age + 2);
    C#
    class Person
    {
    public string Name {get; set;}
    public int Age {get; set;}
    }
    Cannot implicitly convert type 'string' to 'int'

    View full-size slide

  35. var people = new List();
    people.Add(new Person
    {
    Name = “Foo”,
    Age = “26”
    });
    people.Add(new Person
    {
    Name = “Bar”,
    Age = 28
    });
    people.Select(p => p.Age + 2);
    C#
    class Person
    {
    public string Name {get; set;}
    public int Age {get; set;}
    }
    Cannot implicitly convert type 'string' to 'int'
    COMPILE TIME

    View full-size slide

  36. def get_greeting(name):
    if len(name) > 5:
    return “Helloooo “ + name
    PYTHON
    string GetGreeting(string name) {
    if (name.Length > 5)
    return “Helloooo “ + name
    }
    C#

    View full-size slide

  37. def get_greeting(name):
    if len(name) > 5:
    return “Helloooo “ + name
    PYTHON

    View full-size slide

  38. def get_greeting(name):
    if len(name) > 5:
    return “Helloooo “ + name
    get_greeting(“Jon Snow”).upper()
    PYTHON

    View full-size slide

  39. def get_greeting(name):
    if len(name) > 5:
    return “Helloooo “ + name
    get_greeting(“Jon Snow”).upper()
    get_greeting(“Jon”).upper()
    PYTHON

    View full-size slide

  40. def get_greeting(name):
    if len(name) > 5:
    return “Helloooo “ + name
    get_greeting(“Jon Snow”).upper()
    get_greeting(“Jon”).upper()
    PYTHON
    AttributeError: 'NoneType' object has no attribute 'upper'

    View full-size slide

  41. def get_greeting(name):
    if len(name) > 5:
    return “Helloooo “ + name
    get_greeting(“Jon Snow”).upper()
    get_greeting(“Jon”).upper()
    PYTHON
    AttributeError: 'NoneType' object has no attribute 'upper'
    RUNTIME

    View full-size slide

  42. string GetGreeting(string name) {
    if (name.Length > 5)
    return “Helloooo “ + name
    }
    C#

    View full-size slide

  43. string GetGreeting(string name) {
    if (name.Length > 5)
    return “Helloooo “ + name
    }
    C#
    else?

    View full-size slide

  44. string GetGreeting(string name) {
    if (name.Length > 5)
    return “Helloooo “ + name
    }
    C#
    else?
    Not all code paths return a value

    View full-size slide

  45. string GetGreeting(string name) {
    if (name.Length > 5)
    return “Helloooo “ + name
    }
    C#
    else?
    Not all code paths return a value
    COMPILE TIME

    View full-size slide

  46. DYNAMIC TYPING
    STATIC TYPING

    View full-size slide

  47. DYNAMIC TYPING
    STATIC TYPING

    View full-size slide

  48. DYNAMIC TYPING
    STATIC TYPING
    ANY GOOD?

    View full-size slide

  49. DYNAMIC TYPING





    View full-size slide

  50. DYNAMIC TYPING
    STATIC TYPING
    ANY BAD?

    View full-size slide

  51. STATIC TYPING




    View full-size slide

  52. DYNAMIC TYPING
    STATIC TYPING

    View full-size slide

  53. DYNAMIC TYPING
    STATIC TYPING

    View full-size slide

  54. DYNAMIC TYPING
    STATIC TYPING

    View full-size slide

  55. GRADUAL TYPING
    DYNAMIC TYPING
    STATIC TYPING

    View full-size slide

  56. GRADUAL TYPING

    View full-size slide

  57. GRADUAL TYPING
    DYNAMIC TYPING STATIC TYPING

    View full-size slide

  58. GRADUAL TYPING
    DYNAMIC TYPING STATIC TYPING
    Compile Time Rigidity
    Runtime Fluidity

    View full-size slide

  59. GRADUAL TYPING FRAMEWORKS

    View full-size slide

  60. GRADUAL TYPING POWERS





    View full-size slide






  61. AGENDA

    View full-size slide