C# 7

453 views

Published on

Great Scott! C# 7 is almost out!

Time to hop into the DeLorean with Doc Brown. If my calculations are correct, when this baby hits 88 miles per hour, we'll be traveling back to the future with C# 7. Switches? Where we're going, we don't need Switches we got Pattern Matching. Just hold on for one second. Let's get this straight, in the future Tuples will be usable! This will beg the questions. Where are we? When are we? We are in the future and we now have Local Functions and Records.

You'll walk away with a sense of where C# is going and how you can learn about its new features today by looking back to the future. This is heavy.

Published in: Software
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
453
On SlideShare
0
From Embeds
0
Number of Embeds
33
Actions
Shares
0
Downloads
52
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

C# 7

  1. 1. 88
 mph
  2. 2. YEAR 2016 GitHub
  3. 3. YEAR 440 BCE Herodotus
  4. 4. “Herodotus of Halicarnassus here presents his research so that human events do not fade with time.” Herodotus, The Histories
 Book I, Proem
 trans. Andrea L. Purvis

  5. 5. YEAR 404 BCE Lysander
  6. 6. “[Sparta] they would never reduce to slavery a city which was itself an integral portion of Hellas, and had performed a great and noble service to Hellas in the most perilous of emergencies.” Xenophon, Hellenica
 Book 2, 2.20
 trans. H. G. Dakyns

  7. 7. YEAR 399 BCE Socrates
  8. 8. “[Writing offers readers] the appearance of wisdom, not true wisdom, for they will read many things without instruction and will therefore seem to know many things, when they are for the most part ignorant and hard to get along with, since they are not wise, but only appear wise.” Plato, Phaedrus 275a-b
 trans. Harold N. Fowler

  9. 9. YEAR 1822 Hegel
  10. 10. “World history is the record of the spirit's efforts to attain knowledge of what it is in itself.” Hegel, Lectures on the Philosophy of World History
 Introduction
 trans. Johannes Hoffmeister

  11. 11. Timeline • Local Functions • Tuples • Records • Pattern Matching • Ref Locals / Ref Returns • Binary Literals / Digit Separators
  12. 12. Timeline • Local Functions • Tuples • Pattern Matching
  13. 13. Timeline • Local Functions • Tuples • Pattern Matching
  14. 14. YEAR 1960 Algol 60
  15. 15. Local Functions
 {Algol 60} begin comment classic recursive procedure; integer nn, nf; integer procedure factorial(n); value n; integer n; begin if n <= 1 then factorial := 1 else factorial := n * factorial(n-1) end; nn := 5; nf := factorial(nn); outinteger ( 1 , nf) end taken from: http://algol60.org/lego/procedure11.a60Algol 60
  16. 16. Local Functions
 {Algol 60} integer procedure factorial(n); value n; integer n; begin if n <= 1 then factorial := 1 else factorial := n * factorial(n-1) end; nn := 5; nf := factorial(nn); taken from: http://algol60.org/lego/procedure11.a60Algol 60
  17. 17. YEAR 2015 C# 6
  18. 18. Local Functions
 {without language support} private struct locals { public int x; } static void Main(string[] args) { var x = 42; var local = new locals {x = x}; AddOne(ref local); x = local.x; WriteLine($"x + 1 = {x}"); } private static void AddOne(ref locals implicits) { implicits.x += 1; } C# 6
  19. 19. YEAR 2017 C# 7
  20. 20. Local Functions
 {with language support} static void Main(string[] args) { var x = 42; void AddOne() { x += 1; } AddOne(); WriteLine($"x + 1 = {x}"); } C# 7
  21. 21. Functions
  22. 22. Functions
 {basic} function outputinput
  23. 23. Functions
 {real world} function outputinput effect side
 effect
  24. 24. Functions
 {basic} Hello Lengthname
  25. 25. Functions
 {basic} public int Hello(string name) { var time = DateTime.Now; Console.Write($"Hello {name} it is now {time}"); return name.Length; } C# 6
  26. 26. Functions
 {real world} Hello Lengthname DateTime Console
  27. 27. Functions
 {real world} public int Hello(string name) { var time = DateTime.Now; Console.Write($"Hello {name} it is now {time}"); return name.Length; } C# 6
  28. 28. YEAR 2017 C# 7
  29. 29. Local Functions
 {“realistic”} static void Main(string[] args) { var program = new Program(); IEnumerable<int> values = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; string FormatValues() { return string.Join(",", values); } Console.WriteLine($"before: {FormatValues()}"); values = program.RealistCode(values); Console.WriteLine($"after: {FormatValues()}"); } public IEnumerable<int> RealistCode(IEnumerable<int> values) { if (values == null) throw new ArgumentException("values cannot be null"); if (!values.Any()) throw new ArgumentException("must have at least one element in values"); IEnumerable <int> Rules() { return values .Where(x => x > 2) .Where(x => x%2 == 1); } return Rules(); } C# 7
  30. 30. Local Functions
 {“realistic”} static void Main(string[] args) { var program = new Program(); IEnumerable<int> values = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; string FormatValues() { return string.Join(",", values); } Console.WriteLine($"before: {FormatValues()}"); values = program.RealistCode(values); Console.WriteLine($"after: {FormatValues()}"); } C# 7
  31. 31. Local Functions
 {really “realistic”} public IEnumerable<int> RealistCode(IEnumerable<int> values) { if (values == null) throw new ArgumentException("values cannot be null"); if (!values.Any()) throw new ArgumentException( "must have at least one element in values"); IEnumerable <int> Rules() { return values .Where(x => x > 2) .Where(x => x%2 == 1); } return Rules(); } C# 7
  32. 32. Local Functions
 {really “realistic”} public IEnumerable<int> RealistCode(IEnumerable<int> values) { Precondition(); return values .Where(x => x > 2) .Where(x => x%2 == 1); void Precondition() { if (values == null) throw new ArgumentException("values cannot be null"); if (!values.Any()) throw new ArgumentException("must have at least one element in values"); } } C# 7
  33. 33. Design
 By Contract
  34. 34. YEAR 1985 Eiffel
  35. 35. Local Functions
 {design by contract} note description : "example of factorial" class APPLICATION create make feature -- Initialization make local n: NATURAL do n := 3 print ("%NFactorial of " + n.out + " = ") print (recursive_factorial (n)) end feature -- Access recursive_factorial (n: NATURAL): NATURAL require n >= 0 do if n = 0 then Result := 1 else Result := n * recursive_factorial (n - 1) end end end Eiffel
  36. 36. Local Functions
 {design by contract} feature -- Access recursive_factorial (n: NATURAL): NATURAL require n >= 0 do if n = 0 then Result := 1 else Result := n * recursive_factorial (n - 1) end end end Eiffel
  37. 37. YEAR 2009 Clojure
  38. 38. Local Functions
 {design by contract} (ns com.blogspot.comp-phil.factorial) ;; no tail call optimization (defn factorial [n] {:pre [((comp not neg?) n)]} (if (= n 0) 1 (* n (factorial (dec n))))) (factorial 1) (factorial 5) Clojure
  39. 39. Local Functions
 {design by contract} (defn factorial [n] {:pre [((comp not neg?) n)]} (if (= n 0) 1 (* n (factorial (dec n))))) Clojure
  40. 40. YEAR 2017 C# 7
  41. 41. Local Functions
 {design by contract} static void Main(string[] args) { // no tail call optimization long Factorial(int x) { if (x < 0) throw new ArgumentException(“value < 0”); if (x <= 1) return 1; return x * Factorial(x-1); } Console.WriteLine($"1! = {Factorial(1)}"); Console.WriteLine($"3! = {Factorial(3)}"); Console.WriteLine($"20! = {Factorial(20)}"); } C# 7
  42. 42. Tail Call
 Optimization
  43. 43. YEAR 2017 C# 7
  44. 44. Local Functions
 {no tail call} static void Main(string[] args) { // no tail call optimization long Factorial(int x) { if (x <= 1) return 1; return x * Factorial(x-1); } Console.WriteLine($"1! = {Factorial(1)}"); Console.WriteLine($"3! = {Factorial(3)}"); Console.WriteLine($"20! = {Factorial(20)}"); } C# 7
  45. 45. Console.WriteLine($"3! = {Factorial(3)}"); long Factorial(int x) { if (x <= 1) return x; return x * Factorial(x-1); } x = 3
 3 * ? C# 7
  46. 46. Console.WriteLine($"3! = {Factorial(3)}"); long Factorial(int x) { if (x <= 1) return x; return x * Factorial(x-1); } x = 3
 3 * x = 2
 2 * ? C# 7
  47. 47. Console.WriteLine($"3! = {Factorial(3)}"); x = 3
 3 * long Factorial(int x) { if (x <= 1) return x; return x * Factorial(x-1); } x = 2
 2 * x = 1
 1 C# 7
  48. 48. Console.WriteLine($"3! = {Factorial(3)}"); x = 3
 3 * long Factorial(int x) { if (x <= 1) return x; return x * Factorial(x-1); } x = 2
 2 C# 7
  49. 49. Console.WriteLine($"3! = {Factorial(3)}"); x = 3
 6 long Factorial(int x) { if (x <= 1) return x; return x * Factorial(x-1); } C# 7
  50. 50. Local Functions
 {tail call} static void Main(string[] args) { // would have tail call optimization if supported long Factorial(int x) { long Aux(long acc, int n) { if (n <= 1) return acc; return Aux(acc * n, n - 1); } return Aux(1, x); } Console.WriteLine($"1! = {Factorial(1)}"); Console.WriteLine($"3! = {Factorial(3)}"); Console.WriteLine($"20! = {Factorial(20)}"); } C# 7
  51. 51. Console.WriteLine($"3! = {Factorial(3)}"); acc = 1
 n = 3 long Aux(long acc, int n) { if (n <= 1) return acc; return Aux(acc * n, n - 1); } C# 7
  52. 52. Console.WriteLine($"3! = {Factorial(3)}"); acc = 1
 n = 3 acc = 3
 n = 2 long Aux(long acc, int n) { if (n <= 1) return acc; return Aux(acc * n, n - 1); } C# 7
  53. 53. Console.WriteLine($"3! = {Factorial(3)}"); acc = 1
 n = 3 acc = 3
 n = 2 acc = 6
 n = 1 long Aux(long acc, int n) { if (n <= 1) return acc; return Aux(acc * n, n - 1); } C# 7
  54. 54. Local Functions
 {“tail call optimization”} static void Main(string[] args) { // simulated tail call optimization long Factorial(int n) { long acc = 1; top: if (n <= 1) return acc; acc *= n; n--; goto top; } Console.WriteLine($"1! = {Factorial(1)}"); Console.WriteLine($"3! = {Factorial(3)}"); Console.WriteLine($"20! = {Factorial(20)}"); } C# 7
  55. 55. Local Functions
 {“tail call optimization”} static void Main(string[] args) { // simulated tail call optimization long Factorial(int n) { long acc = 1; top: if (n <= 1) return acc; acc *= n; n--; goto top; } Console.WriteLine($"1! = {Factorial(1)}"); Console.WriteLine($"3! = {Factorial(3)}"); Console.WriteLine($"20! = {Factorial(20)}"); } C# 7
  56. 56. Console.WriteLine($"3! = {Factorial(3)}"); acc = 1
 n = 3 long Factorial(int n) { long acc = 1; top: if (n <= 1) return acc; acc *= n; n--; goto top; }C# 7
  57. 57. Console.WriteLine($"3! = {Factorial(3)}"); acc = 3
 n = 2 long Factorial(int n) { long acc = 1; top: if (n <= 1) return acc; acc *= n; n--; goto top; }C# 7
  58. 58. Console.WriteLine($"3! = {Factorial(3)}"); acc = 6
 n = 1 long Factorial(int n) { long acc = 1; top: if (n <= 1) return acc; acc *= n; n--; goto top; }C# 7
  59. 59. YEAR 2010 F#
  60. 60. Local Functions
 {tail call optimization} F# module Factorial = let factorial x = let rec aux m x = match x with | 0 -> m | _ -> aux (m*x) (x-1) aux 1 x
  61. 61. Timeline • Local Functions • Tuples • Pattern Matching
  62. 62. Tuples tuple item 1 item 2
  63. 63. Tuples “Hello” 42
  64. 64. YEAR 2010 C# 4.0
  65. 65. Tuple
 {mostly everything} static void Main(string[] args) { Console.WriteLine("1! = “ + Factorial(1)); Console.WriteLine("5! = “ + Factorial(5)); Console.WriteLine("20! = “ + Factorial(20)); } private static long Factorial(int x) { return FactorialAux(new Tuple<int, int>(1, x)); } private static long FactorialAux(Tuple<int, int> t) { if (t.Item2 <= 1) return t.Item1; var r = new Tuple<int, int>(t.Item1*t.Item2, t.Item2 - 1); return FactorialAux(r); } C# 4.0
  66. 66. YEAR 2017 C# 7
  67. 67. Tuple
 {mostly everything} static void Main(string[] args) { long Factorial(int x) { long Aux((int acc, int n) a) { if (a.n <= 1) return a.acc; var r = (a.acc * a.n, a.n - 1); return Aux(r); } (int, int) t = (1, x); return Aux(t); } Console.WriteLine($"1! = {Factorial(1)}"); Console.WriteLine($"3! = {Factorial(3)}"); Console.WriteLine($"20! = {Factorial(20)}"); } C# 7
  68. 68. Timeline • Local Functions • Tuples • Pattern Matching
  69. 69. Pattern Matching type 1 type 2 other type 2 Matcher
  70. 70. YEAR 2010 C# 4.0
  71. 71. Pattern Matching
 {types} static void Main(string[] args) { var values = new List<object> {1, (short) 2, (Int32) 3, null, new {}, "no", 1.2d}; foreach (var value in values) { var x = value as int; if (x != null) Console.WriteLine("got “ + x); } } C# 4.0
  72. 72. YEAR 2017 C# 7
  73. 73. Pattern Matching
 {types} static void Main(string[] args) { var values = new List<object> {1, (short) 2, (Int32) 3, null, new {}, "no", 1.2d}; foreach (var value in values) { if (value is int x) Console.WriteLine($"got {x}"); } } C# 7
  74. 74. Function
 Pattern
 Matching
  75. 75. YEAR 1973 ML
  76. 76. Pattern Matching
 {functional pattern matching} fun factorial(0) = 1 | factorial(n) = n * factorial(n-1); print( "5! =" ^ (Int.toString (factorial(5))) ^ "n"); ML
  77. 77. YEAR 2004 Scala
  78. 78. Pattern Matching
 {functional pattern matching} import scala.annotation.tailrec object factorial { def apply(x: Int): Int = { @tailrec def go(m: Int, x: Int): Int = x match { case 0 => m case _ => go(x*m, x-1) } go(1, x) } } println(s"5! = ${factorial(5)}") Scala
  79. 79. YEAR 2018 C# 8
  80. 80. Pattern Matching
 {functional pattern matching} long Factorial(int x) { long Aux(int m, int x) { match(x) { case 0: return m; default: return Aux(x*m, x-1); } } return Aux(1, x); } Console.WriteLine($"5! = {Factorial(5)}"); C# 8
  81. 81. YEAR 2018 C# 8
  82. 82. Pattern Matching
 {types} static void Main(string[] args) { var values = new List<object> {1, (short) 2, (Int32) 3, null, new {}, "no", 1.2d}; foreach (var value in values) { t = match(x) { case int _: "int"; case short _: "short"; case object _: "object"; case string _: "string"; default: "something"; } Console.WriteLine("got “ + t); } } C# 8
  83. 83. Pattern Matching
 {types} foreach (var value in values) { t = match(x) { case int _: "int"; case short _: "short"; case object _: "object"; case string _: "string"; default: "something"; } Console.WriteLine("got “ + t); } C# 8
  84. 84. Thank you! Mike Harris
 
 @MikeMKH
 http://comp-phil.blogspot.com/
  85. 85. Biography • Tomas Petricek - "Coeffects: Context-aware programming languages" http://tomasp.net/coeffects/ • https://github.com/dotnet/roslyn/blob/features/patterns/ docs/features/local-functions.md • https://github.com/dotnet/roslyn/issues/347 • https://github.com/dotnet/roslyn/blob/features/patterns/ docs/features/patterns.md • https://github.com/dotnet/roslyn/blob/master/docs/ Language%20Feature%20Status.md
  86. 86. Images • DeLorean DMC-12 by en:user:Grenex - Wikipedia en, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=2500249 • Herodotos by © Marie-Lan Nguyen / Wikimedia Commons, Public Domain, https://commons.wikimedia.org/w/index.php?curid=12886457 • Lysander by Walter Crane - The story of Greece : told to boys and girls (191-?) by Macgregor, Mary, Public Domain, https:// commons.wikimedia.org/w/index.php?curid=32804563 • Socrates by Walter Crane - The story of Greece : told to boys and girls (191-?) by Macgregor, Mary, Public Domain, https:// commons.wikimedia.org/w/index.php?curid=32804549 • Hegel by Unknown - http://portrait.kaar.at/, Public Domain, https:// commons.wikimedia.org/w/index.php?curid=3308762

×