Type system of GoC Seminar Ralf Vogler TUMnchen

  • Slides: 25
Download presentation
Type system of Go/C# Seminar Ralf Vogler TU-München

Type system of Go/C# Seminar Ralf Vogler TU-München

Outline • • Go & C# Static vs. dynamic typing Nominal vs. structural Duck

Outline • • Go & C# Static vs. dynamic typing Nominal vs. structural Duck typing Explicit vs. implicit declaration Go: interfaces C#: CTS, LINQ, dynamic Summary 2

Go • Go (Google, 2009) – Imperative, compiled, garbage-collected, concurrent – “It's a fast,

Go • Go (Google, 2009) – Imperative, compiled, garbage-collected, concurrent – “It's a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language. ” [golang. org] 3

C# • C# (Microsoft, 2001) – Compiled, garbage-collected – Multi-paradigm: imperative & functional, objectoriented,

C# • C# (Microsoft, 2001) – Compiled, garbage-collected – Multi-paradigm: imperative & functional, objectoriented, generic –. NET: CLI, CTS, CLR, DLR 4

Static vs. dynamic typing • Static – Type checking during compile-time – Action. Script

Static vs. dynamic typing • Static – Type checking during compile-time – Action. Script 3, Ada, C, C++, C#, D, Eiffel, F#, Fortran, Go, Haskell, Java, ML, Objective-C, OCaml, Pascal, Scala • Dynamic – Type checking during run-time – APL, Erlang, Groovy, Java. Script, Lisp, Lua, PHP, Prolog, Python, Ruby, Smalltalk, Clojure, Tcl 5

Static vs. dynamic typing Example (C# 4. 0) // static int a = "1";

Static vs. dynamic typing Example (C# 4. 0) // static int a = "1"; // type error at compile-time // dynamic b = "1"; // b. Get. Type() = System. String int c = b; // type error at run-time int d = int. Parse(b); // ok 6

Static vs. dynamic typing • Static – Verification: type errors caught early – Type

Static vs. dynamic typing • Static – Verification: type errors caught early – Type checking done once -> faster – Developer needs to think about types • Dynamic – Testing: type errors might be hard to find – Overhead of type checking during run-time – Easier to learn, fast prototyping 7

Static vs. dynamic typing Example (PHP) 8

Static vs. dynamic typing Example (PHP) 8

Static vs. dynamic typing Example (PHP) 9

Static vs. dynamic typing Example (PHP) 9

Nominal vs. structural Example Nominal typing in C# struct A { public int i;

Nominal vs. structural Example Nominal typing in C# struct A { public int i; } struct B { public int i; } … A a; B b; b = a; // compile-time error 10

Nominal vs. structural • Nominal (name-based) – Type-compatible if declarations name same type –

Nominal vs. structural • Nominal (name-based) – Type-compatible if declarations name same type – C, C++, C#, Java… • Structural (property-based) – Type-compatible if types have the same structure – Go (interfaces), ML, OCaml, Haskell, C++ templates 11

Duck typing Example Duck typing in C# dynamic s = "Hello"; s. To. Upper();

Duck typing Example Duck typing in C# dynamic s = "Hello"; s. To. Upper(); // ok, method of System. String s. foo(); // run-time exception 12

Duck typing • checks whether object has called method at run-time -> style of

Duck typing • checks whether object has called method at run-time -> style of dynamic typing • Structural & nominative typing: checks at compile-time 13

Duck typing • Go – Interfaces are implemented by implementing their methods, no type

Duck typing • Go – Interfaces are implemented by implementing their methods, no type inheritance – “Static duck typing” -> safe • C# – Duck typing for objects of type dynamic 14

Explicit vs. implicit declaration • Explicit: – Java… • Implicit: – Local type inference:

Explicit vs. implicit declaration • Explicit: – Java… • Implicit: – Local type inference: Go & C# (since 3. 0) – Complete type inference: Haskell, Standard ML, OCaml 15

Explicit vs. implicit declaration • Type inference in Go: : = – Explicit: –

Explicit vs. implicit declaration • Type inference in Go: : = – Explicit: – Implicit: var s string = “hello” s = “good bye” s : = “hello” s = “good bye” • Type inference in C#: var – 2. 0: List<string> list = new List<string>(); – Added in 3. 0: var list = new List<string>(); 16

package main import "fmt" type Walker interface { walk() } type Talker interface {

package main import "fmt" type Walker interface { walk() } type Talker interface { talk() } type Duck struct {} type Human struct {} func (x Duck) walk() { fmt. Printf("Duck walksn") } // Duck now automatically implements the Walker interface func (x Human) walk() { fmt. Printf("Human walksn") } func (x Human) talk() { fmt. Printf("Human talksn") } // Human implements the Walker and Talker interfaces now func do. The. Walk(x Walker) { x. walk() } func main() { d : = new(Duck) // type inference, d is of type Duck h : = new(Human) // h of type Human do. The. Walk(d) // call over the interface do. The. Walk(h) h. talk() // call on the struct var d 2 Walker = new(Duck) // explicit typing var h 2 Walker = new(Human) d 2 = h 2 // ok because same interface d 2. walk() // h 2. talk() couldn't be accessed because it's not in the Walker interface } Go: interfaces 17

C#: CTS Fig. 1. Common Type System [2] 18

C#: CTS Fig. 1. Common Type System [2] 18

C#: LINQ Example LINQ query with implicitly typed variables and lambda expression string[] names

C#: LINQ Example LINQ query with implicitly typed variables and lambda expression string[] names = { "Delta", "Charlie", "Bravo", "Alfa" }; // type IEnumerable<string> var query = from s in names where s. Length == 5 orderby s select s. To. Upper(); foreach (var item in query. Select(x => x+1)) Console. Write. Line(item); // BRAVO 1, DELTA 1 19

C#: LINQ Example Anonymous types var book 1 = new { Title = "A",

C#: LINQ Example Anonymous types var book 1 = new { Title = "A", Pages = 123 }; var book 2 = new { Title = "B", Pages = 321 }; var book 3 = new { Title = "C", Pages = 321, Author = "D" }; Console. Write. Line(book 1); // { Title = A, Pages = 123 } book 1 = book 2; // ok because same type book 2 = book 3; // compile error because implicit conversion not possible 20

C#: LINQ Example Nullable types int? n = 3; //same as using Nullable<int> n

C#: LINQ Example Nullable types int? n = 3; //same as using Nullable<int> n = 3 Console. Write. Line(n); // 3, same as n. Value n = null; Console. Write. Line(n. Has. Value); // False 21

C#: dynamic • Since C# 4. 0 (2010) • Still static type checking •

C#: dynamic • Since C# 4. 0 (2010) • Still static type checking • Type checking for objects of type dynamic is just deferred to run-time • Main reason: interoperability (COM), LINQ, DLR, reflection much easier 22

C#: dynamic Example dynamic method dispatch, member lookup and object creation Action<dynamic> Print =

C#: dynamic Example dynamic method dispatch, member lookup and object creation Action<dynamic> Print = x => Console. Write. Line(x); Func<dynamic, int> Get. Length = x => x. Length; Print(123); // calls Write. Line(int) Print("abc"); // calls Write. Line(string) Print(Get. Length("Hello, world")); // a string has a Length property Print(Get. Length(new int[] { 1, 2, 3 })); // an array too Print(Get. Length(42)); // but not an integer -> run-time exception dynamic foo = new Expando. Object(); foo. att 1 = "I'm dynamic"; foo. meth 1 = Print; foo. meth 1(foo. att 1); // prints "I'm dynamic" 23

Summary Go C# • Static type checking • Structural type system • “Duck typing”

Summary Go C# • Static type checking • Structural type system • “Duck typing” through interfaces • Static type checking • Nominative type system • Duck typing on dynamic type • Extra dynamic type 24

References 1. The Go Programming Language, http: //golang. org/ 2. The C# Language, http:

References 1. The Go Programming Language, http: //golang. org/ 2. The C# Language, http: //msdn. microsoft. com/enus/vcsharp/aa 336809 25