Date post: | 30-May-2018 |
Category: |
Documents |
Upload: | best-tech-videos |
View: | 224 times |
Download: | 0 times |
of 21
8/14/2019 Statically Dynamic Typing
1/21
Statically Dynamic TypingStatically Dynamic Typing
The Why?s, Why on Earth?s and Why the Heck?s
Neal Gafter, Microsoft
(thanks to Mads Torgersen)
8/14/2019 Statically Dynamic Typing
2/21
Why?Why?
Interop with other object systems:
Dynamic languages
Iron-this, Iron-that Programmatic object-like behavior
var length=(int)control.GetProperty(Length);
type.Invoke(target,M,arguments);
Includes reflection
COM
8/14/2019 Statically Dynamic Typing
3/21
PythonPython
BinderBinder
RubyRuby
BinderBinder
COMCOM
BinderBinder
JavaScriptJavaScript
BinderBinder
ObjectObject
BinderBinder
Dynamic Language RuntimeDynamic Language Runtime
DynamicLanguageRuntime
ExpressionTreesExpressionTrees DynamicDispatchDynamicDispatch Call SiteCachingCall SiteCaching
IronPythonIronPython IronRubyIronRuby C#C# VB.NETVB.NET OthersOthers
8/14/2019 Statically Dynamic Typing
4/21
Terminology: Dynamic BindingTerminology: Dynamic Binding
Binding:
Determining the meaning of an operation
based on the type of constituents
Static binding:
Binding is based on compile time (static)
types of expressions
Dynamic binding:Binding is based on runtime (actual) types
of objects
8/14/2019 Statically Dynamic Typing
5/21
Binding not typingBinding not typing
Dynamic bindingenables programmatic
interop
Connect different worlds by mapping actions,
not types
Dynamic typingis a consequence, not a
feature
8/14/2019 Statically Dynamic Typing
6/21
Dynamically Typed ObjectsDynamically Typed Objects
Calculator calc=GetCalculator();
int sum=calc.Add(10,20);
object calc=GetCalculator();
Type calcType =calc.GetType();
object res
=
calcType.InvokeMember("Add",BindingFlags.InvokeMethod,null,
new object[]{10,20});
int sum=Convert.ToInt32(res);
ScriptObject calc=GetCalculator();
object res=calc.Invoke("Add",10,20);
int sum=Convert.ToInt32(res);
dynamic calc=GetCalculator();int sum=calc.Add(10,20);
Staticallytyped to
be dy
namic
Dynamic method
invocation
Dynamic
conversion
8/14/2019 Statically Dynamic Typing
7/21
SyntaxSyntax
Knee-jerk: Its got to look different!
Safety first
Secret dream: Its got to look similar!
Comfort first
8/14/2019 Statically Dynamic Typing
8/21
SyntaxSyntax
Explicitly dynamic operations:
objectd=GetDynamicObject();
stringresult=~(string)d~[d~.Length ~- 1];
Ugh
8/14/2019 Statically Dynamic Typing
9/21
SyntaxSyntax
Dynamic contexts:
objectd=GetDynamicObject();
stringresult=dynamic((string)d[d.Length - 1]);
Everythingis dynamic inside
A static context as well to opt out with?
A whole different dialect of C# inside
You lose sight of big contexts
8/14/2019 Statically Dynamic Typing
10/21
SyntaxSyntax
Contagious bit on expressions:
objectd=GetDynamicObject();
stringresult=(string)d[dynamic(d).Length- 1];
Somethingis dynamic but what?
Rules of propagation?
factoring out subexpressions?
8/14/2019 Statically Dynamic Typing
11/21
SyntaxSyntax
Dynamic type:
dynamicd =GetDynamicObject();
stringresult=d[d.Length - 1];
Pro: theres no difference!
Easy to see what the code does
Con: Theres no difference!
No local indication that it is dynamic
8/14/2019 Statically Dynamic Typing
12/21
Why is this OK?Why is this OK?
Safety about throwing exceptions
Member access already throws exceptions
You already need types to guess meaning
This is for making unsafe, error prone,
bloated code less so
8/14/2019 Statically Dynamic Typing
13/21
Type or Type Modifier?Type or Type Modifier?
Generality:dynamicFoo d = GetDynamicFoo();
Static binding of Foos members
Dynamic binding of the rest
Simplicity:dynamic d =GetDynamicFoo();
Dynamic binding of all members Even those on Object
8/14/2019 Statically Dynamic Typing
14/21
Dynamic binding when?Dynamic binding when?
When the receiver is dynamic?
What to do when arguments are dynamic?
dynamic result =Math.Sin((double)d);
Forces you to choose a type
When anyconstituent expression is
dynamic!dynamic result =Math.Sin(d);
8/14/2019 Statically Dynamic Typing
15/21
The typeThe type dynamicdynamic
At object in the type lattice
Subtype conversion from every type Conversion from any type
May fail at runtime
8/14/2019 Statically Dynamic Typing
16/21
Result typeResult type
Dynamic type:
Method call Math.Sin(d)
Invocation d(Hello)
Member access d.Length
Operator application 4+d
Indexing d[Hello]
Static type: Conversions (double)d
Object creation new Foo(d)
8/14/2019 Statically Dynamic Typing
17/21
Runtime BindingRuntime Binding
IDynamicObject
Implemented by dynamic objects
Handle their own binding
C# runtime binder
Handles binding of ordinary objects
Mimics compile time semantics
8/14/2019 Statically Dynamic Typing
18/21
Runtime Binding SemanticsRuntime Binding Semantics
Constituents typed dynamic:
Use their runtime type
Statically typed constituents:
Use their static type
Use other static info like literalness
Principle of least surprise:
How dynamic do you want to be today?
8/14/2019 Statically Dynamic Typing
19/21
Multiple Dispatch?Multiple Dispatch?
dynamicd1,d2,d3;
d1.Foo(d2,d3);
8/14/2019 Statically Dynamic Typing
20/21
SummarySummary
dynamic means
use my runtime type for binding
8/14/2019 Statically Dynamic Typing
21/21