net 2015: Будущее рядом

66
.NET 2015: Будущее рядом Андрей Акиньшин, JetBrains .NEXT 2015 Moscow 1/53

Upload: andrey-akinshin

Post on 15-Apr-2017

280 views

Category:

Software


0 download

TRANSCRIPT

.NET 2015: Будущее рядом

Андрей Акиньшин, JetBrains

.NEXT 2015 Moscow

1/53

2/53 Overview

3/53 Overview

4/53 Overview

5/53 Overview

6/53 Overview

7/53 Overview

8/53 Overview

9/53 Overview

10/53 Overview

11/53 Overview

12/53 Overview

13/53 Overview

14/53 Overview

15/53 Overview

16/53 Overview

17/53 Overview

18/53 Overview

.NET Platform Standard

19/53 .NET Platform Standard

.NET Platform StandardTerms.NET Platform Standard — a specific versioned set ofreference assemblies that all .NET Platforms must support asdefined in the CoreFX repo.Principles

• Platform owners implement reference assemblies from aparticular .NET Platform Standard version.

• Platform owners may implement a subset of referenceassemblies from a particular .NET Platform Standardversion.

• Any change in a reference assembly’s API surface causesthe .NET Platform Standard to version.

• Lower versions are always compatible with higherversions.

20/53 .NET Platform Standard

Relationship to Platforms

21/53 .NET Platform Standard

Mapping to platforms

22/53 .NET Platform Standard

Portable Profiles

23/53 .NET Platform Standard

NuGet

24/53 .NET Platform Standard

Contracts from CoreFX

...25/53 .NET Platform Standard

C#7

26/53 C#7

Tuples (Background)

public void Tally(IEnumerable<int> values, out int sum, out int count)

int s, c;Tally(myValues, out s, out c);Console.WriteLine($"Sum: {s}, count: {c}");

public Tuple<int, int> Tally(IEnumerable<int> values)

var t = Tally(myValues);Console.WriteLine($"Sum: {t.Item1}, count: {t.Item2}")

public struct TallyResult { public int Sum; public int Count; }public TallyResult Tally(IEnumerable<int> values)

var t = Tally(myValues);Console.WriteLine($"Sum: {t.Sum}, count: {t.Count}");

27/53 C#7

Tuples (Background)

public void Tally(IEnumerable<int> values, out int sum, out int count)

int s, c;Tally(myValues, out s, out c);Console.WriteLine($"Sum: {s}, count: {c}");

public Tuple<int, int> Tally(IEnumerable<int> values)

var t = Tally(myValues);Console.WriteLine($"Sum: {t.Item1}, count: {t.Item2}")

public struct TallyResult { public int Sum; public int Count; }public TallyResult Tally(IEnumerable<int> values)

var t = Tally(myValues);Console.WriteLine($"Sum: {t.Sum}, count: {t.Count}");

27/53 C#7

Tuples (Background)

public void Tally(IEnumerable<int> values, out int sum, out int count)

int s, c;Tally(myValues, out s, out c);Console.WriteLine($"Sum: {s}, count: {c}");

public Tuple<int, int> Tally(IEnumerable<int> values)

var t = Tally(myValues);Console.WriteLine($"Sum: {t.Item1}, count: {t.Item2}")

public struct TallyResult { public int Sum; public int Count; }public TallyResult Tally(IEnumerable<int> values)

var t = Tally(myValues);Console.WriteLine($"Sum: {t.Sum}, count: {t.Count}");

27/53 C#7

Tuples (Background)

public void Tally(IEnumerable<int> values, out int sum, out int count)

int s, c;Tally(myValues, out s, out c);Console.WriteLine($"Sum: {s}, count: {c}");

public Tuple<int, int> Tally(IEnumerable<int> values)

var t = Tally(myValues);Console.WriteLine($"Sum: {t.Item1}, count: {t.Item2}")

public struct TallyResult { public int Sum; public int Count; }public TallyResult Tally(IEnumerable<int> values)

var t = Tally(myValues);Console.WriteLine($"Sum: {t.Sum}, count: {t.Count}");

27/53 C#7

Tuples types

public (int sum, int count) Tally(IEnumerable<int> values)

var t = Tally(myValues);Console.WriteLine($"Sum: {t.sum}, count: {t.count}");

public async Task<(int sum, int count)>TallyAsync(IEnumerable<int> values)

var t = await TallyAsync(myValues);Console.WriteLine($"Sum: {t.sum}, count: {t.count}");

28/53 C#7

Tuples types

public (int sum, int count) Tally(IEnumerable<int> values)

var t = Tally(myValues);Console.WriteLine($"Sum: {t.sum}, count: {t.count}");

public async Task<(int sum, int count)>TallyAsync(IEnumerable<int> values)

var t = await TallyAsync(myValues);Console.WriteLine($"Sum: {t.sum}, count: {t.count}");

28/53 C#7

Tuple literals and deconstruction

var t = new (int sum, int count) { sum = 0, count = 0 };

public (int sum, int count) Tally(IEnumerable<int> values){

var s = 0; var c = 0;foreach (var value in values) { s += value; c++; }

return (s, c); // target typed to (int sum, int count)}

public (int sum, int count) Tally(IEnumerable<int> values){

// infer tuple type from names and valuesvar res = (sum: 0, count: 0);foreach (var value in values) { res.sum += value; res.count++; }

return res;}

(var sum, var count) = Tally(myValues); // deconstruct resultConsole.WriteLine($"Sum: {sum}, count: {count}");

29/53 C#7

Tuple literals and deconstruction

var t = new (int sum, int count) { sum = 0, count = 0 };

public (int sum, int count) Tally(IEnumerable<int> values){

var s = 0; var c = 0;foreach (var value in values) { s += value; c++; }

return (s, c); // target typed to (int sum, int count)}

public (int sum, int count) Tally(IEnumerable<int> values){

// infer tuple type from names and valuesvar res = (sum: 0, count: 0);foreach (var value in values) { res.sum += value; res.count++; }

return res;}

(var sum, var count) = Tally(myValues); // deconstruct resultConsole.WriteLine($"Sum: {sum}, count: {count}");

29/53 C#7

Tuple literals and deconstruction

var t = new (int sum, int count) { sum = 0, count = 0 };

public (int sum, int count) Tally(IEnumerable<int> values){

var s = 0; var c = 0;foreach (var value in values) { s += value; c++; }

return (s, c); // target typed to (int sum, int count)}

public (int sum, int count) Tally(IEnumerable<int> values){

// infer tuple type from names and valuesvar res = (sum: 0, count: 0);foreach (var value in values) { res.sum += value; res.count++; }

return res;}

(var sum, var count) = Tally(myValues); // deconstruct resultConsole.WriteLine($"Sum: {sum}, count: {count}");

29/53 C#7

Tuple literals and deconstruction

var t = new (int sum, int count) { sum = 0, count = 0 };

public (int sum, int count) Tally(IEnumerable<int> values){

var s = 0; var c = 0;foreach (var value in values) { s += value; c++; }

return (s, c); // target typed to (int sum, int count)}

public (int sum, int count) Tally(IEnumerable<int> values){

// infer tuple type from names and valuesvar res = (sum: 0, count: 0);foreach (var value in values) { res.sum += value; res.count++; }

return res;}

(var sum, var count) = Tally(myValues); // deconstruct resultConsole.WriteLine($"Sum: {sum}, count: {count}");

29/53 C#7

Tuples: проблемы

• Struct or class• Mutability• Tuples as fields• Conversions• . . .

Cм. также: roslyn#347

30/53 C#7

Pattern matching (Background)// class Person(string Name);class Person{

public Person(string name) { this.Name = name; }public string Name { get; }

}

// class Student(string Name, double Gpa) : Person(Name);class Student : Person{

public Student(string name, double gpa) : base(name){ this.Gpa = gpa; }

public double Gpa { get; }}

// class Teacher(string Name, string Subject) : Person(Name);class Teacher : Person{

public Teacher(string name, string subject) : base(name){ this.Subject = subject; }

public string Subject { get; }}

31/53 C#7

Pattern matching (Background)static string PrintedForm(Person p){

Student s;Teacher t;if ((s = p as Student) != null && s.Gpa > 3.5){

return $"Honor Student {s.Name} ({s.Gpa})";}else if (s != null){

return $"Student {s.Name} ({s.Gpa})";}else if ((t = p as Teacher) != null){

return $"Teacher {t.Name} of {t.Subject}";}else{

return $"Person {p.Name}";}

}

32/53 C#7

Pattern matching (Background)

static void Main(string[] args){

Person[] oa = {new Student("Einstein", 4.0),new Student("Elvis", 3.0),new Student("Poindexter", 3.2),new Teacher("Feynmann", "Physics"),new Person("Anders"),

};foreach (var o in oa){

Console.WriteLine(PrintedForm(o));}Console.ReadKey();

}

33/53 C#7

Pattern matching: is

static string PrintedForm(Person p){

if (p is Student s && s.Gpa > 3.5){

return $"Honor Student {s.Name} ({s.Gpa})";}else if (p is Student s){

return $"Student {s.Name} ({s.Gpa})";}else if (p is Teacher t){

return $"Teacher {t.Name} of {t.Subject}";}else{

return $"Person {p.Name}";}

}

34/53 C#7

Pattern matching: switch

static string PrintedForm(Person p){

switch (p){

case Student s when s.Gpa > 3.5 :return $"Honor Student {s.Name} ({s.Gpa})";

case Student s :return $"Student {s.Name} ({s.Gpa})";

case Teacher t :return $"Teacher {t.Name} of {t.Subject}";

default :return $"Person {p.Name}";

}}

35/53 C#7

Pattern matching: match

static string PrintedForm(Person p){

return p match (case Student s when s.Gpa > 3.5 :

$"Honor Student {s.Name} ({s.Gpa})"case Student s :

$"Student {s.Name} ({s.Gpa})"case Teacher t :

$"Teacher {t.Name} of {t.Subject}"case * :

$"Person {p.Name}");

}

36/53 C#7

Pattern matching: Exceptions

return p match (case Student s when s.Gpa > 3.5 :

$"Honor Student {s.Name} ({s.Gpa})"case Student s :

$"Student {s.Name} ({s.Gpa})"case Teacher t :

$"Teacher {t.Name} of {t.Subject}"case null :

throw new ArgumentNullException(nameof(p))case * :

$"Person {p.Name}");

37/53 C#7

Pattern matching: members

return p match (case Student s when s.Gpa > 3.5 :

$"Honor Student {s.Name} ({s.Gpa})"case Student { Name is "Poindexter" } :

"A Nerd"case Student s :

$"Student {s.Name} ({s.Gpa})"case Teacher t :

$"Teacher {t.Name} of {t.Subject}"case null :

throw new ArgumentNullException(nameof(p))case * :

$"Person {p.Name}");

38/53 C#7

Pattern matching: =>

static string PrintedForm(Person p) => p match (case Student s when s.Gpa > 3.5 :

$"Honor Student {s.Name} ({s.Gpa})"case Student { Name is "Poindexter" } :

"A Nerd"case Student s :

$"Student {s.Name} ({s.Gpa})"case Teacher t :

$"Teacher {t.Name} of {t.Subject}"case null :

throw new ArgumentNullException(nameof(p))case * :

$"Person {p.Name}");

39/53 C#7

Readonly locals

readonly int foo = /* ... */ ;readonly var foo = /* ... */ ;val foo = /* ... */ ;public void Bar(readonly int foo = 0)public void Bar(readonly ref Matrix3D matrix)

40/53 C#7

Immutable Typespublic immutable struct Tuple<T1, T2>{

public Tuple(T1 item1, T2 item2){ Item1 = item1; Item2 = item2; }

public T1 Item1; // Implicitly readonlypublic T2 Item2; // Implicitly readonly

}

public immutable struct ImmutableTuple<T1, T2>(T1 item1, T2 item2)

where T1 : immutablewhere T2 : immutable

{public ImmutableTuple(T1 item1, T2 item2)

{ Item1 = item1; Item2 = item2; }public T1 Item1;public T2 Item2;

}

41/53 C#7

Immutable Typespublic immutable struct Tuple<T1, T2>{

public Tuple(T1 item1, T2 item2){ Item1 = item1; Item2 = item2; }

public T1 Item1; // Implicitly readonlypublic T2 Item2; // Implicitly readonly

}

public immutable struct ImmutableTuple<T1, T2>(T1 item1, T2 item2)

where T1 : immutablewhere T2 : immutable

{public ImmutableTuple(T1 item1, T2 item2)

{ Item1 = item1; Item2 = item2; }public T1 Item1;public T2 Item2;

}

41/53 C#7

Nullability?!

1 Добавляем в язык только ? и ! приобъявления и кастах

2 NullReferenceException невозможен3 Процесс компиляции не меняется4 Runtime ничего не знает про not-nullable5 Обратная совместимость

42/53 C#7

Nullability?!

1 Добавляем в язык только ? и ! приобъявления и кастах

2 NullReferenceException невозможен3 Процесс компиляции не меняется4 Runtime ничего не знает про not-nullable5 Обратная совместимость

42/53 C#7

Nullability?!

1 Добавляем в язык только ? и ! приобъявления и кастах

2 NullReferenceException невозможен

3 Процесс компиляции не меняется4 Runtime ничего не знает про not-nullable5 Обратная совместимость

42/53 C#7

Nullability?!

1 Добавляем в язык только ? и ! приобъявления и кастах

2 NullReferenceException невозможен3 Процесс компиляции не меняется

4 Runtime ничего не знает про not-nullable5 Обратная совместимость

42/53 C#7

Nullability?!

1 Добавляем в язык только ? и ! приобъявления и кастах

2 NullReferenceException невозможен3 Процесс компиляции не меняется4 Runtime ничего не знает про not-nullable

5 Обратная совместимость

42/53 C#7

Nullability?!

1 Добавляем в язык только ? и ! приобъявления и кастах

2 NullReferenceException невозможен3 Процесс компиляции не меняется4 Runtime ничего не знает про not-nullable5 Обратная совместимость

42/53 C#7

С#7: много разных идей

• Local functions• Extensions properties• AsyncEnumerable• params IEnumerable• digits separators• binary literals• ...

43/53 C#7

corefxlab

44/53 corefxlab

ILSub

[MethodImpl(MethodImplOptions.AggressiveInlining)][ILSub(@"

.maxstack 2

.locals ([0] uint8& addr)ldarg.0 // load the objectstloc.0 // convert the object pointer to a byrefldloc.0 // load the object pointer as a byrefldarg.1 // load the offsetadd // add the offsetldobj !!T // load a T value from the computed addressret")]

public static T Get<T>(object obj, UIntPtr offset){

return default(T);}

45/53 corefxlab

System.Buffers

public sealed class ManagedBufferPool<T> where T : struct{

// 2MB taken as the default since the average HTTP page// is 1.9MB per http://httparchive.org/interesting.phppublic ManagedBufferPool(int maxBufferSizeInBytes = 2048000)

static ManagedBufferPool<byte> SharedByteBufferPool{get;}T[] RentBuffer(int size, bool clearBuffer = false)void EnlargeBuffer(ref T[] buffer, int newSize,

bool clearFreeSpace = false)void ReturnBuffer(ref T[] buffer, bool clearBuffer = false)

}

46/53 corefxlab

System.Slices

public partial struct Span<T> : IEnumerable<T>, IEquatable<Span<T>>{

readonly object _object;readonly UIntPtr _offset;public readonly int Length;// ...Span(T[] array, int start, int length)unsafe Span(void* ptr, int length)bool TryCopyTo(Span<T> dest)Span<T> Slice(int start, int length)

}

// Extensionsstatic Span<T> Slice<T>(this T[] array, int start, int length)static Span<char> Slice(this string str, int start, int length)static Span<U> Cast<[Primitive]T, [Primitive]U>(this Span<T> slice)static bool SequenceEqual<T>(this Span<T> first, Span<T> second)// ...

47/53 corefxlab

System.Text.Utf8public partial struct Utf8String :

IEnumerable<Utf8CodeUnit>,IEquatable<Utf8String>,IComparable<Utf8String>

{private Span<byte> _buffer;

Utf8String(Span<byte> buffer)Utf8String(byte[] utf8bytes, int index, int length)Utf8String(string s)

Utf8CodeUnit this[int i]Utf8String Substring(int index, int length)Utf8String Trim()byte[] CopyBytes()// ...

}

48/53 corefxlab

MemCmp

/// <summary>/// platform independent fast memory comparison/// for x64 it is as fast as memcmp of msvcrt.dll,/// for x86 it is up to two times faster!!/// </summary>internal static bool MemCmp<[Primitive]T>

(Span<T> first, Span<T> second)where T : struct

{unsafe{

// ...

49/53 corefxlab

Array slicing syntax (roslyn#120)

int[] primes = new int[] { 2, 3, 5, 7, 9, 11, 13 };int item = primes[1]; // Regular array access (value 3)int[:] a = primes[0:3]; // A slice {2, 3, 5}int[:] b = primes[1:2]; // A slice {3}int[:] c = primes[:5]; // A slice {2, 3, 5, 7, 9}int[:] d = primes[2:]; // A slice {5, 7, 9, 11, 13}int[:] e = primes[:]; // A slice {2, 3, 5, 7, 9, 11, 13}int[:] f = a[1:2]; // A slice {3}int[:] g = primes[:]; // A slice {2, 3, 5, 7, 9, 11, 13}int[:] h = primes; // A slice {2, 3, 5, 7, 9, 11, 13}int[:] i = h[:]; // A slice {2, 3, 5, 7, 9, 11, 13}

50/53 corefxlab

И другие пространства имён• System.Collections.Generic.MultiValueDictionary• System.CommandLine• System.Drawing.Graphics• System.IO.FileSystem.Watcher.Polling• System.Net.Libuv• System.Numerics.Matrices• System.Reflection.Metadata.Cil• System.Text.Formatting.Globalization• System.Text.Formatting• System.Text.Http• System.Text.Json• System.Threading.Tasks.Channels• System.Time

51/53 corefxlab

Будущее рядом!

52/53

Вопросы?

Андрей Акиньшин, JetBrainshttp://aakinshin.net

https://github.com/AndreyAkinshinhttps://twitter.com/andrey_akinshin

[email protected]

53/53