[swift-evolution] Proposal: Introduce User-defined "Dynamic Member Lookup" Types

Joe DeCapo snoogansbc at gmail.com
Mon Dec 4 19:21:53 CST 2017


I'm curious to get some feedback on this from people who would prefer a marker at every call site where a `DynamicMemberLookup` or `DynamicCallable` type is used. I haven't done any C# development before, but I read a few articles on the usage of `dynamic` and `var` in the language. I found it interesting that there are a number of posts cautioning against the use of `var` in C# for type inference, in preference to explicitly specifying types.

https://www.brad-smith.info/blog/archives/336
https://www.intertech.com/Blog/the-use-and-abuse-of-the-c-var-keyword/
https://weblogs.asp.net/stevewellens/can-the-c-var-keyword-be-misused
https://blogs.msdn.microsoft.com/ericlippert/2011/04/20/uses-and-misuses-of-implicit-typing/

It's almost comical to read those from the vantage point of a Swift developer, given that we tend to avoid explicit typing in every scenario we can, save for those where it's required. And I think most would agree that we've suffered no real problems due to it.

C# 3.0 introduced the `var` keyword in 2007. In 2010, C# 4.0 introduced the `dynamic` keyword. Here are a couple of quotes from devs about the `dynamic` keyword:

https://www.codeproject.com/Articles/69407/The-Dynamic-Keyword-in-C

> Earlier, I said that my initial reaction was negative, so what changed my mind? To quote Margret Attwood, context is all.

https://stackoverflow.com/a/245537

> The first one, has no static type info, no compile time checking, it's not self documenting, no type inference so people will be forced to use a dynamic reference at the call site to store the result, leading to more type loss, and all this spirals down.
> I'm already starting to fear dynamic.
> Edit: The danger has passed (Phew!) ... and dynamic wasn't been abused after all, no need to down vote me after 3 years :)

From what I can gather, `dynamic` is used when declaring types, but there's no indication at call sites that what is being invoked is dynamic. And it even allows for casting basically anything to the `dynamic` type.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/dynamic

So here we have a language community that was (is?) very vocal about caution when it comes to type inference with `var`, but seems to have accepted the validity of `dynamic`. This seems to show that at least one community has absorbed this sort of change (arguably a more "dangerous" version than what is being proposed here) with no real issues.

So I have a few questions:

- Would it be enough to require annotation of the dynamic nature of a type at the declaration sites, if that still means the call sites aren't explicitly annotated? 
- Is it really much different to require annotation with a keyword at type declaration sites, than to just inspect the type for conformance to `DynamicMemberLookup` and/or `DynamicCallable` (especially now that Chris added sections to the proposal forbidding retroactive conformance and the option of not allowing it as part of another protocol definition)? Admittedly it's one extra step of indirection when investigating an issue, but I assume we could have reasonable error messages nudging us toward the specific problem.
- Why do some think the Swift community would be more at risk of abuse of this feature than the C# community seems to have been? 

And if there's anyone with experience in C# where this was actually a legitimate issue, I would be happy to hear about your experience.

-Joe



More information about the swift-evolution mailing list