the why?’s, why on earth?’s and why the heck?’s neal...

21
Statically Dynamic Typing Statically Dynamic Typing The Why?’s, Why on Earth?’s and Why the Heck?’s Neal Gafter, Microsoft (thanks to Mads Torgersen)

Upload: others

Post on 28-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

Statically Dynamic TypingStatically Dynamic Typing

The Why?’s, Why on Earth?’s and Why the Heck?’s

Neal Gafter, Microsoft(thanks to Mads Torgersen)

Page 2: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

Why?Why?

Interop with other object systems: Dynamic languages◦ Iron-this, Iron-that…

Programmatic object-like behaviorvar length = (int)control.GetProperty(“Length”);type.Invoke(target, “M”, arguments);

◦ Includes reflection

COM

Page 3: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

PythonPythonBinderBinder

RubyRubyBinderBinder

COMCOMBinderBinder

JavaScriptJavaScriptBinderBinder

ObjectObjectBinderBinder

Dynamic Language RuntimeDynamic Language Runtime

Dynamic Language RuntimeExpression TreesExpression Trees Dynamic DispatchDynamic Dispatch Call Site CachingCall Site Caching

IronPythonIronPython IronRubyIronRuby C#C# VB.NETVB.NET Others…Others…

Page 4: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

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

Page 5: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

Binding not typingBinding not typing

Dynamic binding enables programmatic interop◦ Connect different worlds by mapping actions,

not types

Dynamic typing is a consequence, not a feature

Page 6: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

Dynamically Typed ObjectsDynamically Typed ObjectsCalculator 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);

Statically typed to be dynamic

Dynamic method invocation

Dynamic conversion

Page 7: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

SyntaxSyntax

Knee-jerk: It’s got to look different!◦ Safety first

Secret dream: It’s got to look similar!◦ Comfort first

Page 8: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

SyntaxSyntax

Explicitly dynamic operations:

object d = GetDynamicObject(…);string result = ~(string)d~[d~.Length ~- 1];

Ugh…

Page 9: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

SyntaxSyntax

Dynamic contexts:

object d = GetDynamicObject(…);string result = dynamic((string)d[d.Length - 1]);

Everything is dynamic inside◦ A static context as well to opt out with?◦ A whole different dialect of C# inside◦ You lose sight of big contexts

Page 10: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

SyntaxSyntax

Contagious bit on expressions:

object d = GetDynamicObject(…);string result = (string)d[dynamic(d).Length - 1];

Something is dynamic – but what?◦ Rules of propagation?◦ factoring out subexpressions?

Page 11: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

SyntaxSyntax

Dynamic type:

dynamic d = GetDynamicObject(…);string result = d[d.Length - 1];

Pro: there’s no difference!◦ Easy to see what the code does

Con: There’s no difference!◦ No local indication that it is dynamic

Page 12: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

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

Page 13: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

Type or Type Modifier?Type or Type Modifier?

Generality:dynamic Foo d = GetDynamicFoo(…);

◦ Static binding of Foo’s members◦ Dynamic binding of the rest

Simplicity:dynamic d = GetDynamicFoo(…);

◦ Dynamic binding of all members◦ Even those on Object

Page 14: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

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 any constituent expression is dynamic!dynamic result = Math.Sin(d);

Page 15: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

The type The type dynamicdynamic

At object in the type lattice Subtype conversion from every type Conversion from any type◦ May fail at runtime

Page 16: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

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)

Page 17: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

Runtime BindingRuntime Binding

IDynamicObject◦ Implemented by dynamic objects◦ Handle their own binding

C# runtime binder◦ Handles binding of “ordinary objects”◦ Mimics compile time semantics

Page 18: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

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?

Page 19: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

Multiple Dispatch?Multiple Dispatch?

dynamic d1, d2, d3;…d1.Foo(d2, d3);

Page 20: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal

SummarySummary

dynamic means“use my runtime type for binding”

Page 21: The Why?’s, Why on Earth?’s and Why the Heck?’s Neal ...wiki.jvmlangsummit.com/images/8/80/DynamicInCSharp.pdf · The Why?’s, Why on Earth?’s and Why the Heck?’s Neal