constrained dynamics #1116
Replies: 2 comments
-
For the record, the other proposals, suggesting to use |
Beta Was this translation helpful? Give feedback.
-
I was request something like this since roslyn and I really need it. Actually it could traced back to dotnet/roslyn#3012 And the actual use case I was needed all along is intellisense for data in database Data in database could always have dynamic record fields and overlapped common fields. Treat it as dynamic is the most sane approach but it then lose all intellisense. With dynamic that can constraint with any type it would be the best solution to deal with manipulation of dynamic data anywhere |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal
Allow to constraint dynamics with this pattern.
this means that
mydel
can be any thing that inheritsDelegate
. usage ofmydel
is not limited, for example you can still writemydel.Invoke(1)
even though Delegate does not have Invoke method.how ever this gives a level of static type checking to our dynamic type. so you can not assign an
int
ordynamic<IList>
to adynamic<Delegate>
. note that generaldynamic
is assignable todynamic<Delegate>
because itsdynamic
that has no static type checking.you can call a constrained dynamic, a semi dynamic type. because ancestor type is known at compile time but the type it self can be any of ancestors childs.
Motivation
delegates invoke fast as methods as long as you know the signature. the super class
Delegate
is not fast like a well declared delegate such asFunc
orAction
. invoking aDelegate
requiresDynamicInvoke
which is arguably slower thanInvoke
because of late-binding. (read more)dynamic on the other hand uses late-binding too, but great thing about it is how it caches late-binding results very efficiently. as a result, dynamic can perform very efficiently as opposed to what some programmers may think.
Here is how
dynamic
type outperformsDelegate
and results in my computer are (Release x64)
Delegate is about 40 times slower than dynamic. and dynamic is only about 6 times slower than direct invoking.
Why constraint?
dynamics are very great feature of c# language, but it can be confusing. so developers may avoid to use it just for that reason. with constraints there will be less complains about it.
On the other hand, Ive shown you how caching helps dynamic to become considerably faster. just as simple as that, if dynamic type is constrained, it will not necessarily become faster but it encourages programmers to write relatively fast dynamic code provided with extra type safety.
With constraints there will be more room for optimizing constrained dynamic types.
Why this proposal (and not others)?
This is different than other proposals making use of dynamic keyword for other purposes (eg structural typing or dynamic interfaces which will be (or should be) covered by concepts or other approaches). I think this syntax should remain solely for dynamic variable itself.
note: dynamic can only be constrained to classes or interfaces. not structs or value types.
Beta Was this translation helpful? Give feedback.
All reactions