kotlin c
play

Kotlin & C# A Comparison of Two Modern Languages Kirill Rakhman - PowerPoint PPT Presentation

Kotlin & C# A Comparison of Two Modern Languages Kirill Rakhman Syntax Properties Kotlin C# val immutable : String = "Hello" public string Immutable { get ; } = "Hello" ; var mutable : String = "World" public


  1. Kotlin & C# A Comparison of Two Modern Languages Kirill Rakhman

  2. Syntax

  3. Properties Kotlin C# val immutable : String = "Hello" public string Immutable { get ; } = "Hello" ; var mutable : String = "World" public string Mutable { get ; set ; } = "World" ; val computed : String get () = "!" public string Computed => "!" ;

  4. Classes & Constructors Kotlin C# class Foo( public class Foo val bar: String, { val baz: Int public string Bar { get ; } ) public int Baz { get ; } public Foo( string bar, int baz) { Bar = bar; Baz = baz; } }

  5. Class Instantiation Kotlin C# class Foo( public class Foo val bar: String, { val baz: Int ... ) } val foo = Foo( "A String" , 42) var foo = new Foo( "A String" , 42);

  6. Class Initialization Syntax Kotlin C# class Foo( public class Foo val bar: String, { val baz: Int public string Bar { get ; set ; } ) public int Baz { get ; set ; } } val foo = Foo( Foo foo = new Foo bar = "A String" , { baz = 42 Bar = "A String" , ) Baz = 42 };

  7. Primary Constructors Kotlin C# class Foo( public class Foo( string bar, int baz) val bar: String, { val baz: Int public string Bar { get ; } = bar; ) public int Baz { get ; } = baz; Planned for C# 6. Removed }

  8. Data Classes / Records Kotlin C# data class Foo( public class Foo( string Bar, int Baz); val bar: String, val baz: Int) val foo = Foo( "A String" , 42) var foo = new Foo( "A String" , 42); Planned for C# 7. Postponed foo .copy(bar = "Another String" ) foo.With(Bar: "Another String" )

  9. Weird Tuple Hack C# public class Person { public string Name { get ; } public int Age { get ; } public Person( string name, int age) => ( Name , Age ) = (name, age); }

  10. Scoping and Pattern Matching

  11. Let / Out Variables Kotlin C# val map = mapOf <String, String>() Dictionary< string , string > dictionary = ... map [ "key" ]?. let { value -> if (dictionary .TryGetValue( "key" , out string value)) { println (value) Console.WriteLine(value); } }

  12. Let / Var Pattern Kotlin C# fun getValue(): String? { … } public string ? GetValue() { … } getValue()?. let { value -> if (GetValue() is string value) { println (value) Console.WriteLine(value); } }

  13. Out Variables & Pattern Matching Kotlin C# val map = mapOf <String, Any>() Dictionary< string , object > dictionary = ... ( map [ "key" ] as? String)?. let { value -> if (dictionary .TryGetValue( "key" , out object value) && value is string s) { println (value) Console.WriteLine(s); } }

  14. When / Switch Kotlin C# fun format(foo: Any): String { public string Format( object foo) { return when (foo) { return foo switch { "0" -> "Zero" "0" => "Zero" , is String -> foo. toUpperCase () string s => s.ToUpper(), is Pair<*,*> -> ( string a, string b) => "(${ foo. first}, ${ foo. second})" $"({ a }, { b })" , else -> foo.toString() _ => foo.ToString() } }; } }

  15. Advanced Pattern Matching C# static string Display( object o) => o switch { Point { X: 0, Y: 0 } p => "origin" , Point { X: var x, Y: var y } p => $"({ x }, { y })" , _ => "unknown" };

  16. Type System

  17. Nullable Reference Types Kotlin C# class Foo { class Foo { var bar : String? = null string ? bar ; fun doThings(thing: Thing?) { void DoThings(Thing? thing) { thing?.call() thing?.Call(); if (thing != null ) thing.call() if (thing != null ) thing.Call(); if ( bar != null ) bar. split ( "" ) if ( bar != null ) bar .Split( "" ); } } } }

  18. Nullable Structs C# public struct Nullable<T> where T : struct { private T value ; public bool HasValue { get ; } } Nullable< int > foo = new Nullable< int >(); if (foo. HasValue ) { ... } int ? bar = null ; if (bar != null ) { … }

  19. Refresher: Nothing Type http://www.natpryce.com/articles/000818.html

  20. Nothing Typed Operators Kotlin C# val nullableVariable : String? = null ; public Foo( string ? bar) { val value : String = nullableVariable this . bar = bar ?: throw Exception() ?? throw new ArgumentNullException(); } val value : String = nullableVariable ?: return val value : String = nullableVariable ?: break val value : String = nullableVariable ?: continue val value : String = nullableVariable ?: exitProcess(0)

  21. Function Types, Lambdas, Method References Kotlin C# delegate string Format( int message); fun foo(f: (Int) -> String) { … } static void Foo(Format f) { … } fun intToString(x: Int) static string IntToString( int x) = "Number: $ x " => $"Number: { x }" ; foo(::intToString) Foo(IntToString); foo { it.toString() } Foo(x => x.ToString()); foo(Int::toString)

  22. Function Types, Lambdas, Method References Kotlin C# fun foo(f: (Int) -> String) { … } static void Foo(Func<int, string> f) {} fun intToString(x: Int) static string IntToString( int x) = "Number: $ x " => $"Number: { x }" ; foo(::intToString) Foo(IntToString); foo { it.toString() } Foo(x => x.ToString()); foo(Int::toString)

  23. Events C# public delegate void EventHandler( object sender, EventArgs e); public event EventHandler ThresholdReached; // no initializer ThresholdReached += (sender, e) => { … } void OnThresholdReached(EventArgs e) { // Watch out for race conditions EventHandler handler = ThresholdReached; if (handler != null ) { handler.Invoke( this , e); } }

  24. Events C# public delegate void EventHandler( object sender, EventArgs e); public event EventHandler ThresholdReached; ThresholdReached += (sender, e) => { … } void OnThresholdReached(EventArgs e) { ThresholdReached?.Invoke( this , e); }

  25. Asynchronicity

  26. Couroutines / Async Await Kotlin C# suspend fun getFoo(): String { async Task< string > GetFooAsync() { val s = bar() var s = await BarAsync(); return s. toUpperCase () return s.ToUpper(); } } suspend fun bar() Task< string > BarAsync() => = "Hello" Task.FromResult( "Hello" );

  27. Asynchronous Branching Kotlin C# val deferred = async { Task task = GetFooAsync(); getFoo() } // do things // do things val foo = deferred.await() var foo = await task;

  28. Async Parallelism Kotlin C# val results = awaitAll ( var results = await Task.WhenAll( async { getFoo() } , GetFooAsync(), async { getBar() } ) GetBarAsync());

  29. Forgetting to call await

  30. Cancellation Kotlin C# using var tokenSource = new CancellationTokenSource(); val job = launch { doThings () } await DoThingsAsync(tokenSource. Token ); job.cancel() tokenSource.Cancel(); suspend fun doThings() { async Task DoThingsAsync( delay (100) CancellationToken token) coroutineScope { launch {} } { yield () token.ThrowIfCancellationRequested(); // or // or if (! isActive ) return if (token. IsCancellationRequested ) return } }

  31. Thx for Listening @Cypressious rakhman.info

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend