C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D!...
Transcript of C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D!...
![Page 1: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/1.jpg)
C# to D
Adam Wilson Director of So3ware Development
Prospec8ve So3ware
![Page 2: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/2.jpg)
Topics Covered
• Syntax • Proper8es • Namespaces • Generics
• LINQ • Collec8ons
• My Favorite Thing I can’t do in D
2
![Page 3: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/3.jpg)
Random Syntax Notes
C# • var • readonly • string • [AIributes] • params int[]
D • auto • immutable • wstring • @(AIributes) • int[] …
3
![Page 4: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/4.jpg)
Properties
• C# public int data { get; protected set; } public int data { get { return m_data; } set { m_data = value; } } • D @property int data() { return m_data; } @property int data(int value) { return m_data = value; }
4
![Page 5: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/5.jpg)
Converting C# Properties to D
• Rela8vely straight-‐forward for standard C# proper8es • Can be somewhat 8me consuming • Find-‐and-‐Replace not useful • Well suited to automated tooling
5
![Page 6: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/6.jpg)
Differences in Properties
• C# cannot take the address of a property • Resolves D’s awkward syntax when property is delegate
• C# Example: public Ac8on<int> DelegateProp { get; } //declara8on DelegateProp(1); //call the delegate
• D Example: @property delegate(int) DelegateProp() { return m_data; } DelegateProp()(1); //UFCS requires the first parentheses
• The difference is seman8c, but highly confusing. • Have yet to encounter real-‐world need to take address of a property
6
![Page 7: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/7.jpg)
Namespaces
• C# namespace System { } namespace System.Text { } • D module System; module Text;
7
![Page 8: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/8.jpg)
Systemic Differences
• C# Namespaces • Composable • Single namespace can span many source files • Structure is determined by the namespace itself • Used for logical source organiza8on
• D Modules • Not Composable • Name can only exist in a single file • Uses directory structure to determine namespace • No intrinsic organiza8on capability
8
![Page 9: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/9.jpg)
Converting C# Namespaces to D
• Create the corresponding directory structure • Move source all C# source into a single file • Place single source file in correct directory • Set correct module name in source file • Example:
• namespace System.Text.RegularExpressions { } • System directory
• Text directory • RegularExpressions.d
9
![Page 10: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/10.jpg)
Observations on Modules in D
• Modules are NOT Namespaces! • Modules are more analogous to C# Nested Types • A Module with the same name as the parent directory is an error • Leaf namespace is really a source file • Leaf-‐namespace-‐as-‐source-‐file encourages source files of unusual size • Lack of composability also encourages source files of unusual size • Lack of composability prevents programmer from extending module • Example of Namespace Composability in C#: System.U8li8es
• hIps://github.com/prospec8veso3ware/systemu8li8es
10
![Page 11: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/11.jpg)
More Observations on Modules in D
• Public imports reduce typing but also reduce import granularity • Public import example in System directory:
module Imports; public import Text; public import Collec8ons; public import IO; public import Net; public import ServiceModel;
11
![Page 12: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/12.jpg)
Potential Workaround for Public Imports
• Create an Imports.d for every package in the library • System
• Imports.d • Text
• Imports.d • RegularExpressions.d
• Collec8ons • Imports.d • Generic.d • Concurrent.d • Specialized.d
12
![Page 13: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/13.jpg)
DIP15 & DIP16
• Resolves most limita8ons of D Modules in rela8on to C# Namespaces • Renders previously demonstrated workaround unneeded • S8ll not composable • Credit to Mar8n Nowak and Andrei Alexandrescu • Links: hIp://wiki.dlang.org/DIP15 -‐ hIp://wiki.dlang.org/DIP16 • Please support these DIP’s!
13
![Page 14: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/14.jpg)
Generics • C# Generics allow the users of the code to specify one or more types. • Less flexible than D’s template system • Generics can be considered a subset of D’s template system • D’s template constraints are considerably more expressive than C#’s • C# Generic Constraints:
• struct • class • new() • Base Class • Interface • Another generic type
14
![Page 15: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/15.jpg)
C# Generics Example
public class SomeBaseClass {} public class SomeOtherClass : SomeBaseClass {} public class SomeClass<T> where T : SomeBaseClass {
private List<T> values; void Add(T input) { values.Add(T); }
}
15
![Page 16: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/16.jpg)
D Generics Example
public class SomeBaseClass {} public class SomeOtherClass : SomeBaseClass {} class SomeClass(T : SomeBaseClass) {
private T[] values; public void Add(T input) { values ~= input; }
}
16
![Page 17: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/17.jpg)
LINQ Set Operations
C# • Dis8nct • Except • Intersect • Union
D • uniq • setDifference • setIntersec8on • setUnion
17
![Page 18: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/18.jpg)
LINQ Aggregation Operations
C# • Aggregate • Average • Count • LongCount • Min • Max • Sum
D • reduce
• count
• min • max
18
![Page 19: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/19.jpg)
LINQ Sorting Operations
C# • OrderBy • OrderByDescending • ThenBy • ThenByDescending • Reverse
D • sort • sort
• reverse
19
![Page 20: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/20.jpg)
Other LINQ Operations
C# • Where • GroupBy • Contains • Repeat • SequenceEqual • Concat
D • filter • group • canFind • fill • equal • joiner
20
![Page 21: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/21.jpg)
LINQ vs. std.algorithm
• This list may not be complete • Neither has everything available in the other, for example: • There is no equivalent to map() in LINQ, use SelectMany and GroupBy • Most common opera8ons exist in both • Equivalent opera8on names not always obvious • Internal seman8cs of equivalent func8ons may differ • Verify that your output is equivalent!
21
![Page 22: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/22.jpg)
D Containers for C# Programmers
C# • T[] • Dic8onary<TKey, TValue> • List<T> • Not Available • LinkedList<T> • SortedSet<TValue> (.NET 4.0+) SortedDic8onary<TKey, TValue>
D • T[] • TValue[TKey] • Array(T) • SList(T) • DList(T) • RedBlackTree(TValue)
22
![Page 23: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/23.jpg)
D Containers That Can
• Phobos provides equivalent func8onality for basic C# collec8ons opera8ons
• Opera8ons: Add/Insert/Remove/Clear/Empty/Length/Capacity • Both support LINQ/std.algorithm opera8ons.
23
![Page 24: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/24.jpg)
D Containers That Can’t
• Std.container is missing many of the containers available in C#: • Generic: Queue, Stack, HashSet, ObservableCollec8on • Concurrent: ConcurrentDic8onary, ConcurrentQueue, ConcurrentStack, ConcurrentBag, BlockingCollec8on
• Synchronized: SynchronizedCollec8on, SynchronizedKeyedCollec8on • Read-‐Only: ReadOnlyCollec8on, ReadOnlyDic8onary, ReadOnlyObservableCollec8on, SynchronizedReadOnlyCollec8on
24
![Page 25: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/25.jpg)
My Favorite Thing I can’t do in D public string Name { get { return GetValue(NameProperty); } set { SetValue(NameProperty, value); } } public sta8c readonly DeltaProperty<string> NameProperty = DeltaProperty<string>.Register("Name", typeof(Customer), default(string), null);
© Copyright Prospec8ve So3ware 2013
25
![Page 26: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/26.jpg)
My Favorite Thing I can’t do in D public abstract class DeltaObject {
private ConcurrentDic8onary<HashID, object> values; public T GetValue<T>(DeltaProperty<T> de) { object value; return values.TryGetValue(de.ID, out value) == false ? de.DefaultValue : (T)value; } public void SetValue<T>(DeltaProperty<T> de, T value) { object temp; if (EqualityComparer<T>.Default.Equals(value, de.DefaultValue)) values.TryRemove(de.ID, out temp); else temp = values.AddOrUpdate(de.ID, value, (p, v) => value); }
} © Copyright Prospec8ve So3ware 2013
26
![Page 27: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/27.jpg)
My Favorite Thing I can’t do in D
• A PaIern for Concurrently Mutable Data • Why can’t D do this?
• Uses the ‘default’ keyword • Uses ConcurrentDic8onary and ConcurrentQueue • Uses SHA2 hashing func8ons
• Implementa8on is simple, but relies on library and language features not present in D.
• Source: hIps://github.com/prospec8veso3ware/netpathsdk
27
![Page 28: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/28.jpg)
.NET Namespace Comparison
• The following namespaces have no analog in Phobos: • System.Collec8ons (Except System.Collec8ons.Generics) • System.IO • System.Text (Except Regular Expressions) • System.Data • System.Timers • System.Prin8ng
28
![Page 29: C# to Ddconf.org/2013/talks/wilson.pdfA Call to Arms • Much!of!the!C#language!can!be!ported!to!D! • Significantpor8ons!of!the!Base!Class!Library!cannotbe!ported!to!D! • In!prac8cal!examples](https://reader034.fdocuments.in/reader034/viewer/2022042413/5f2cc5feebbdc969405e8c15/html5/thumbnails/29.jpg)
A Call to Arms
• Much of the C# language can be ported to D • Significant por8ons of the Base Class Library cannot be ported to D • In prac8cal examples, so3ware that relies on even basic .NET BCL types cannot be easily ported
• However, there are many cases where D meets and even exceeds the capabili8es of C#
• Containers are one of D’s major weaknesses compared to C# and .NET
• How can we push containers forward today?
29