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. >>> people = [] >>> people.append({ “name”: “Foo”, “age”: “26”

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

    }) >>> people.append({ “name”: “Bar” “age”: 28 }) >>> [ person[“age”] + 2 for person in people ] PYTHON
  3. >>> 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
  4. >>> 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
  5. var people = new List<Person>(); C# class Person { public

    string Name {get; set;} public int Age {get; set;} }
  6. var people = new List<Person>(); 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;} }
  7. var people = new List<Person>(); 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;} }
  8. var people = new List<Person>(); 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'
  9. var people = new List<Person>(); 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
  10. def get_greeting(name): if len(name) > 5: return “Helloooo “ +

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

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

    name get_greeting(“Jon Snow”).upper() get_greeting(“Jon”).upper() PYTHON
  13. 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'
  14. 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
  15. string GetGreeting(string name) { if (name.Length > 5) return “Helloooo

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

    “ + name } C# else? Not all code paths return a value COMPILE TIME