SlideShare ist ein Scribd-Unternehmen logo
1 von 11
Downloaden Sie, um offline zu lesen
1 
New C#4 Features – Part III 
Nico Ludwig (@ersatzteilchen)
2 
TOC 
● New C#4 Features – Part III 
– Why dynamic Programming 
– Dynamic Programming in VB 
– Dynamic Programming in C#4 
● Sources: 
– Jon Skeet, C# in Depth 
– Chaur Wu, Pro DLR in .NET 4 
– Programming Microsoft® LINQ in Microsoft .NET Framework 4 
– MSDN Magazine 05/10, 06/10, 0710, 02/11 
– Software Engineering Radio; Episode 28: Type Systems, <http://www.se-radio.net/2006/09/episode-28-type-systems/> 
– Software Engineering Radio; Episode 49: Dynamic Languages for Static Minds, 
<http://www.se-radio.net/2007/03/episode-49-dynamic-languages-for-static-minds/> 
● "Programming Microsoft 
® 
LINQ in Microsoft .NET 
Framework 4" provides a good overview of 
programming with Expression Trees.
3 
Dynamic Typing as major new Feature in .Net 4/C#4 
● A big topic in .Net 4 is interoperability. 
● Interoperability with other technologies is often a pain. 
– COM interop. 
– Explorative APIs that sit upon a dynamic object model (e.g. DOM). 
– Remoting technologies with loosely coupling, like SOA and cloud computing do rise. 
● Services (read: operations or methods) may be available or not. 
– Domain Specific Languages (DSLs). 
– New scripting languages came into play, (some even base on the CLR). 
● They're especially employing dynamic typing. 
– => As developers we encounter more and more things having a dynamic interface. 
● Besides interoperability, dynamic typing enables other features. 
– Scripting .Net and with .Net. opens your application's object model to be scripted. 
– Mighty data-driven patterns (expando objects and dynamic Expression Trees). 
● Interop often meant to use Object and casting all 
over. 
● It makes sense to base scripting languages on 
the CLR, because it already offers some features: 
type loader, resolver, memory manager, garbage 
collector etc. 
● In Java we found this development some years 
ago (JSR 292 and JSR 223). - Some new 
scripting languages have been built on top of the 
JRE (Groovy, Clojure and Scala) and others have 
been migrated (JRuby and Jython).
4 
Leering at VB: Dynamic Programming 
<DynamicProgrammingVB> 
Shows dynamic typing, late binding, duck typing and COM 
automation with VB. 
Key aspects of dynamic programming: 
1. Dynamic Typing 
2. Late Binding 
3. Duck Typing 
(4. Dynamic Objects) 
● The feature, which we discuss here as dynamic typing, 
is also known as loosely typing. - The main idea is that 
a dynamically typed variable is just a named 
placeholder for a value.
5 
Static Typing vs. dynamic Typing – Part I 
● Programming languages can be divided into statically and dynamically typed ones, whereby basically: 
– Statically typed languages make type checks at compile time. 
● Expressions are of a known type at compile time, variables are fixed to one type at compile time. 
– Dynamically typed languages make type identification and checks at run time. 
● Dynamically typed languages claim "convention over configuration/static check". 
– The believe is that discipline is more important than technical guidance. 
– The conventions work, because duck typing allows consistency at run time. 
– Conventions must be checked with unit-tests, a compiler can just check the syntax. 
– (Unit tests are required: they make dynamic code safe, because the compiler can't .) 
● In dynamic typing the objects' types you're dealing with could be unknown. 
– The types are checked at run time and may even change at run time! 
– In static typing the objects' types are known and checked at compile time. 
● Esp. do dynamically typed languages not require to 
declare a type at compile time. 
● Example conventions: (esp. naming conventions) Ruby 
on Rails' scaffolding pattern and also ASP.Net MVC. 
● Dynamic typing and unknown types are typically found 
in JavaScript (also called loosely typing in JavaScript) 
programming. 
● The type-changes at run time are not expressed by 
polymorphism in dynamic typing! - The needed 
consistency is solely expressed by duck typing!
6 
Static Typing vs. dynamic Typing – Part II 
● Incompatible interfaces (e.g. missing methods) are handled differently. 
– Static typing uses methods that must be bound at compile time successfully. 
– Dynamic typing rather uses messages being dispatched at run time. 
● In static typing we have typing before run time, but 
● in dynamic typing an object decides, whether it accepts a message. 
– In dynamic typing messages that are not "understood" can be handled at run time. 
● Methods and fields could be added or removed during run time. 
● Possible perception: Not: "Are you of this type?", rather: "Can you respond to this message?" 
● Static type-checks, rely on explicit conversions; dynamic type-checks are deferred to run time and explicit conversions are 
not required. 
– This is called late binding. 
– Late binding resolves textual symbols at run time (e.g. property names). 
– Early binding means to resolve all textual symbols early at compile time. 
– Static polymorphism is class-based polymorphism with static interfaces and dynamic dispatch. 
– Dynamic polymorphism means that objects don't need a common static interface like classes as we need no dynamic dispatch. 
● Missing methods: 
● Mind the problem of unavailable Services in a SOA. 
● In Ruby and Python methods can be removed during 
run time. 
● If a method wasn't found in Obj-C, the method 
invocation can be forwarded and handled at run time. 
If the case of unknown methods was not handled by 
the programmer doesNotRecognizeSelector: will be 
called, which will eventually throw 
NSInvalidArgumentException ("unrecognized 
selector sent to instance <address>"). 
● In Smalltalk doesNotUnderstand: will be called, 
which, if not handled by the programmer, will be 
handled by the runtime (or the debugger).
7 
So eventually: Dynamic Programming in .Net 4 and C#4 
● The .Net framework and C# are based on static typing, nothing changed here. 
– I.e. types and their interfaces are resolved/bound at compile time. 
● C#4 allows dynamic typing in a statically typed language. 
– C# stays a statically typed language primarily, but it has new dynamic features now. 
● Dynamic type analysis can be done via reflection, but it's difficult to read. 
– Dynamic typing makes the syntax much more appealing, even natural. 
● The .Net 4 framework introduces the Dynamic Language Runtime (DLR). 
– The DLR is a library that enables dynamic dispatching within a CLR language. 
● The new C#4 keyword dynamic is the syntactical entrance into the DLR. 
– If you apply dynamic you tell the compiler to turn off type-checks. 
● In Obj-C you can use the static type id to enable 
dynamic typing on objects. 
● The COM type IDispatch is indeed also an 
enabler for dynamic dispatching/late binding, 
when scripting and COM automation come into 
play. 
● For a long time VB was the only language, in 
which you can switch between early bound 
(static) and lately bound (dynamic) typing (Option 
Explicit On/Off, Option Strict On/Off). In past 
versions of VB, e.g. in VB 6 this ability was based 
on the VB- and COM-type VARIANT. 
● But VARIANT is best explained to be an 
example of weak typing rather than of dynamic 
typing.
8 
Syntactical Basics of dynamic Coding in C#4 
<BasicSyntax, Dispatching> 
Shows the syntax basics of dynamics, dynamic expressions in C# and 
dynamic dispatching.
9 
Syntactical "Mechanics" of dynamic in C#4 – Part I 
● The static type of dynamic objects is dynamic. Variables still need a declaration. 
– For the "programming experience" dynamic is just a type (you can have dynamic members and closed generic types using dynamic 
(e.g. List<dynamic>)). 
● Almost each type is implicitly convertible to dynamic. 
● But dynamic expressions are not convertible to anything, except in: 
– overload resolution and assignment conversion 
– and in control flow: if clauses, using clauses and foreach loops. 
● Dynamic Expressions use values of type dynamic. They're evaluated at run time. 
● From a syntax perspective dynamic works like object w/o the need to cast. 
– You can write direct calls to the methods you expect to be present (duck typing). 
● The keyword dynamic can be understood as 
"System.Object with run time binding". At compile 
time, dynamic variables have only those methods 
defined in System.Object, at run time possibly 
more. 
● But dynamic can be used as type argument for a 
generic type as base class (not a generic 
interface).
10 
Syntactical "Mechanics" of dynamic in C#4 – Part II 
● The type dynamic isn't a real CLR type, but C# mimics this for us. 
● The type dynamic is just System.Object with run time binding. 
– For non-local instances of dynamic the compiler applies the custom attribute System.Runtime.CompilerServices.DynamicAttribute. 
● (Fields, properties and method's and delegate's parameters and return types.) 
● Restrictions: 
– The type dynamic can not be used as base class and can not be extended. 
– The type dynamic can not be used as type constraint for generics. 
– You can't get the typeof(dynamic). 
– You can neither access constructors nor static members via dynamic dispatch. 
– You can only call accessible methods via dynamic dispatch. 
– Dynamic dispatch can't find extension methods. 
– Dynamic dispatch can't find methods of explicitly implemented interfaces. 
● If you need to do more with a dynamic object 
than, say, assigning values to it, e.g. calling 
methods, the DLR starts its work to dispatch the 
resulting dynamic expressions. In C#, the C# 
language binder will come into play in that 
situation, and if you apply dynamic dispatch in 
your code, you will need to add a reference to the 
assembly Microsoft.CSharp.dll in VS 2010. - The 
good news is that this assembly will be 
referenced by default, when you setup a new C# 
project in VS2010. 
● Ctors and static members can't be dynamically 
dispatched! You have to stick to instance 
methods or you have to use polymorphic static 
typing and factories.
11 
Thank you!

Weitere ähnliche Inhalte

Andere mochten auch

Introduction to Reactive Extensions
Introduction to Reactive ExtensionsIntroduction to Reactive Extensions
Introduction to Reactive Extensions
Peter Goodman
 
Reactive Extensions
Reactive ExtensionsReactive Extensions
Reactive Extensions
Kiev ALT.NET
 
Pertemuan 1. introduction to image processing
Pertemuan 1. introduction to image processingPertemuan 1. introduction to image processing
Pertemuan 1. introduction to image processing
Aditya Kurniawan
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
sarfarazali
 
Getting the MVVM Kicked Out of Your F#'n Monads
Getting the MVVM Kicked Out of Your F#'n MonadsGetting the MVVM Kicked Out of Your F#'n Monads
Getting the MVVM Kicked Out of Your F#'n Monads
Richard Minerich
 
Texas STaR
Texas STaRTexas STaR
Texas STaR
llyarbro
 
F# with Excel at F# in Finance, London Nov 2013
F# with Excel at F# in Finance, London Nov 2013F# with Excel at F# in Finance, London Nov 2013
F# with Excel at F# in Finance, London Nov 2013
Adam Mlocek
 

Andere mochten auch (18)

Introduction to F#x
Introduction to F#xIntroduction to F#x
Introduction to F#x
 
Introduction to Reactive Extensions
Introduction to Reactive ExtensionsIntroduction to Reactive Extensions
Introduction to Reactive Extensions
 
Reactive Extensions
Reactive ExtensionsReactive Extensions
Reactive Extensions
 
Introduction to C# 3.0
Introduction to C# 3.0Introduction to C# 3.0
Introduction to C# 3.0
 
Pertemuan 1. introduction to image processing
Pertemuan 1. introduction to image processingPertemuan 1. introduction to image processing
Pertemuan 1. introduction to image processing
 
F:\Universidad Tecnologica Israel
F:\Universidad Tecnologica IsraelF:\Universidad Tecnologica Israel
F:\Universidad Tecnologica Israel
 
New c sharp3_features_(linq)_part_v
New c sharp3_features_(linq)_part_vNew c sharp3_features_(linq)_part_v
New c sharp3_features_(linq)_part_v
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 
Getting the MVVM Kicked Out of Your F#'n Monads
Getting the MVVM Kicked Out of Your F#'n MonadsGetting the MVVM Kicked Out of Your F#'n Monads
Getting the MVVM Kicked Out of Your F#'n Monads
 
The Power of F#
The Power of F#The Power of F#
The Power of F#
 
Texas STaR
Texas STaRTexas STaR
Texas STaR
 
Texas
TexasTexas
Texas
 
F# with Excel at F# in Finance, London Nov 2013
F# with Excel at F# in Finance, London Nov 2013F# with Excel at F# in Finance, London Nov 2013
F# with Excel at F# in Finance, London Nov 2013
 
C# 4.0 - Whats New
C# 4.0 - Whats NewC# 4.0 - Whats New
C# 4.0 - Whats New
 
Beginning linq
Beginning linqBeginning linq
Beginning linq
 
Csharp4 generics
Csharp4 genericsCsharp4 generics
Csharp4 generics
 
Generics C#
Generics C#Generics C#
Generics C#
 
Module 3: Introduction to LINQ (PowerPoint Slides)
Module 3: Introduction to LINQ (PowerPoint Slides)Module 3: Introduction to LINQ (PowerPoint Slides)
Module 3: Introduction to LINQ (PowerPoint Slides)
 

Mehr von Nico Ludwig

New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
Nico Ludwig
 

Mehr von Nico Ludwig (20)

Grundkurs fuer excel_part_v
Grundkurs fuer excel_part_vGrundkurs fuer excel_part_v
Grundkurs fuer excel_part_v
 
Grundkurs fuer excel_part_iv
Grundkurs fuer excel_part_ivGrundkurs fuer excel_part_iv
Grundkurs fuer excel_part_iv
 
Grundkurs fuer excel_part_iii
Grundkurs fuer excel_part_iiiGrundkurs fuer excel_part_iii
Grundkurs fuer excel_part_iii
 
Grundkurs fuer excel_part_ii
Grundkurs fuer excel_part_iiGrundkurs fuer excel_part_ii
Grundkurs fuer excel_part_ii
 
Grundkurs fuer excel_part_i
Grundkurs fuer excel_part_iGrundkurs fuer excel_part_i
Grundkurs fuer excel_part_i
 
(2) gui drawing
(2) gui drawing(2) gui drawing
(2) gui drawing
 
(2) gui drawing
(2) gui drawing(2) gui drawing
(2) gui drawing
 
(1) gui history_of_interactivity
(1) gui history_of_interactivity(1) gui history_of_interactivity
(1) gui history_of_interactivity
 
(1) gui history_of_interactivity
(1) gui history_of_interactivity(1) gui history_of_interactivity
(1) gui history_of_interactivity
 
New c sharp4_features_part_i
New c sharp4_features_part_iNew c sharp4_features_part_i
New c sharp4_features_part_i
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
 
New c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iiiNew c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iii
 
New c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_iiNew c sharp3_features_(linq)_part_ii
New c sharp3_features_(linq)_part_ii
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_i
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_i
 
Review of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iiiReview of c_sharp2_features_part_iii
Review of c_sharp2_features_part_iii
 
Review of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiReview of c_sharp2_features_part_ii
Review of c_sharp2_features_part_ii
 
Review of c_sharp2_features_part_i
Review of c_sharp2_features_part_iReview of c_sharp2_features_part_i
Review of c_sharp2_features_part_i
 
(7) c sharp introduction_advanvced_features_part_ii
(7) c sharp introduction_advanvced_features_part_ii(7) c sharp introduction_advanvced_features_part_ii
(7) c sharp introduction_advanvced_features_part_ii
 

Kürzlich hochgeladen

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Kürzlich hochgeladen (20)

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 

New c sharp4_features_part_iii

  • 1. 1 New C#4 Features – Part III Nico Ludwig (@ersatzteilchen)
  • 2. 2 TOC ● New C#4 Features – Part III – Why dynamic Programming – Dynamic Programming in VB – Dynamic Programming in C#4 ● Sources: – Jon Skeet, C# in Depth – Chaur Wu, Pro DLR in .NET 4 – Programming Microsoft® LINQ in Microsoft .NET Framework 4 – MSDN Magazine 05/10, 06/10, 0710, 02/11 – Software Engineering Radio; Episode 28: Type Systems, <http://www.se-radio.net/2006/09/episode-28-type-systems/> – Software Engineering Radio; Episode 49: Dynamic Languages for Static Minds, <http://www.se-radio.net/2007/03/episode-49-dynamic-languages-for-static-minds/> ● "Programming Microsoft ® LINQ in Microsoft .NET Framework 4" provides a good overview of programming with Expression Trees.
  • 3. 3 Dynamic Typing as major new Feature in .Net 4/C#4 ● A big topic in .Net 4 is interoperability. ● Interoperability with other technologies is often a pain. – COM interop. – Explorative APIs that sit upon a dynamic object model (e.g. DOM). – Remoting technologies with loosely coupling, like SOA and cloud computing do rise. ● Services (read: operations or methods) may be available or not. – Domain Specific Languages (DSLs). – New scripting languages came into play, (some even base on the CLR). ● They're especially employing dynamic typing. – => As developers we encounter more and more things having a dynamic interface. ● Besides interoperability, dynamic typing enables other features. – Scripting .Net and with .Net. opens your application's object model to be scripted. – Mighty data-driven patterns (expando objects and dynamic Expression Trees). ● Interop often meant to use Object and casting all over. ● It makes sense to base scripting languages on the CLR, because it already offers some features: type loader, resolver, memory manager, garbage collector etc. ● In Java we found this development some years ago (JSR 292 and JSR 223). - Some new scripting languages have been built on top of the JRE (Groovy, Clojure and Scala) and others have been migrated (JRuby and Jython).
  • 4. 4 Leering at VB: Dynamic Programming <DynamicProgrammingVB> Shows dynamic typing, late binding, duck typing and COM automation with VB. Key aspects of dynamic programming: 1. Dynamic Typing 2. Late Binding 3. Duck Typing (4. Dynamic Objects) ● The feature, which we discuss here as dynamic typing, is also known as loosely typing. - The main idea is that a dynamically typed variable is just a named placeholder for a value.
  • 5. 5 Static Typing vs. dynamic Typing – Part I ● Programming languages can be divided into statically and dynamically typed ones, whereby basically: – Statically typed languages make type checks at compile time. ● Expressions are of a known type at compile time, variables are fixed to one type at compile time. – Dynamically typed languages make type identification and checks at run time. ● Dynamically typed languages claim "convention over configuration/static check". – The believe is that discipline is more important than technical guidance. – The conventions work, because duck typing allows consistency at run time. – Conventions must be checked with unit-tests, a compiler can just check the syntax. – (Unit tests are required: they make dynamic code safe, because the compiler can't .) ● In dynamic typing the objects' types you're dealing with could be unknown. – The types are checked at run time and may even change at run time! – In static typing the objects' types are known and checked at compile time. ● Esp. do dynamically typed languages not require to declare a type at compile time. ● Example conventions: (esp. naming conventions) Ruby on Rails' scaffolding pattern and also ASP.Net MVC. ● Dynamic typing and unknown types are typically found in JavaScript (also called loosely typing in JavaScript) programming. ● The type-changes at run time are not expressed by polymorphism in dynamic typing! - The needed consistency is solely expressed by duck typing!
  • 6. 6 Static Typing vs. dynamic Typing – Part II ● Incompatible interfaces (e.g. missing methods) are handled differently. – Static typing uses methods that must be bound at compile time successfully. – Dynamic typing rather uses messages being dispatched at run time. ● In static typing we have typing before run time, but ● in dynamic typing an object decides, whether it accepts a message. – In dynamic typing messages that are not "understood" can be handled at run time. ● Methods and fields could be added or removed during run time. ● Possible perception: Not: "Are you of this type?", rather: "Can you respond to this message?" ● Static type-checks, rely on explicit conversions; dynamic type-checks are deferred to run time and explicit conversions are not required. – This is called late binding. – Late binding resolves textual symbols at run time (e.g. property names). – Early binding means to resolve all textual symbols early at compile time. – Static polymorphism is class-based polymorphism with static interfaces and dynamic dispatch. – Dynamic polymorphism means that objects don't need a common static interface like classes as we need no dynamic dispatch. ● Missing methods: ● Mind the problem of unavailable Services in a SOA. ● In Ruby and Python methods can be removed during run time. ● If a method wasn't found in Obj-C, the method invocation can be forwarded and handled at run time. If the case of unknown methods was not handled by the programmer doesNotRecognizeSelector: will be called, which will eventually throw NSInvalidArgumentException ("unrecognized selector sent to instance <address>"). ● In Smalltalk doesNotUnderstand: will be called, which, if not handled by the programmer, will be handled by the runtime (or the debugger).
  • 7. 7 So eventually: Dynamic Programming in .Net 4 and C#4 ● The .Net framework and C# are based on static typing, nothing changed here. – I.e. types and their interfaces are resolved/bound at compile time. ● C#4 allows dynamic typing in a statically typed language. – C# stays a statically typed language primarily, but it has new dynamic features now. ● Dynamic type analysis can be done via reflection, but it's difficult to read. – Dynamic typing makes the syntax much more appealing, even natural. ● The .Net 4 framework introduces the Dynamic Language Runtime (DLR). – The DLR is a library that enables dynamic dispatching within a CLR language. ● The new C#4 keyword dynamic is the syntactical entrance into the DLR. – If you apply dynamic you tell the compiler to turn off type-checks. ● In Obj-C you can use the static type id to enable dynamic typing on objects. ● The COM type IDispatch is indeed also an enabler for dynamic dispatching/late binding, when scripting and COM automation come into play. ● For a long time VB was the only language, in which you can switch between early bound (static) and lately bound (dynamic) typing (Option Explicit On/Off, Option Strict On/Off). In past versions of VB, e.g. in VB 6 this ability was based on the VB- and COM-type VARIANT. ● But VARIANT is best explained to be an example of weak typing rather than of dynamic typing.
  • 8. 8 Syntactical Basics of dynamic Coding in C#4 <BasicSyntax, Dispatching> Shows the syntax basics of dynamics, dynamic expressions in C# and dynamic dispatching.
  • 9. 9 Syntactical "Mechanics" of dynamic in C#4 – Part I ● The static type of dynamic objects is dynamic. Variables still need a declaration. – For the "programming experience" dynamic is just a type (you can have dynamic members and closed generic types using dynamic (e.g. List<dynamic>)). ● Almost each type is implicitly convertible to dynamic. ● But dynamic expressions are not convertible to anything, except in: – overload resolution and assignment conversion – and in control flow: if clauses, using clauses and foreach loops. ● Dynamic Expressions use values of type dynamic. They're evaluated at run time. ● From a syntax perspective dynamic works like object w/o the need to cast. – You can write direct calls to the methods you expect to be present (duck typing). ● The keyword dynamic can be understood as "System.Object with run time binding". At compile time, dynamic variables have only those methods defined in System.Object, at run time possibly more. ● But dynamic can be used as type argument for a generic type as base class (not a generic interface).
  • 10. 10 Syntactical "Mechanics" of dynamic in C#4 – Part II ● The type dynamic isn't a real CLR type, but C# mimics this for us. ● The type dynamic is just System.Object with run time binding. – For non-local instances of dynamic the compiler applies the custom attribute System.Runtime.CompilerServices.DynamicAttribute. ● (Fields, properties and method's and delegate's parameters and return types.) ● Restrictions: – The type dynamic can not be used as base class and can not be extended. – The type dynamic can not be used as type constraint for generics. – You can't get the typeof(dynamic). – You can neither access constructors nor static members via dynamic dispatch. – You can only call accessible methods via dynamic dispatch. – Dynamic dispatch can't find extension methods. – Dynamic dispatch can't find methods of explicitly implemented interfaces. ● If you need to do more with a dynamic object than, say, assigning values to it, e.g. calling methods, the DLR starts its work to dispatch the resulting dynamic expressions. In C#, the C# language binder will come into play in that situation, and if you apply dynamic dispatch in your code, you will need to add a reference to the assembly Microsoft.CSharp.dll in VS 2010. - The good news is that this assembly will be referenced by default, when you setup a new C# project in VS2010. ● Ctors and static members can't be dynamically dispatched! You have to stick to instance methods or you have to use polymorphic static typing and factories.