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

Joe DeCapo snoogansbc at gmail.com
Tue Dec 5 18:21:56 CST 2017


> On Dec 5, 2017, at 5:47 AM, Benjamin G <benjamin.garrigues at gmail.com> wrote:
> 
> About C#, in which i did program a few years ago (but before dynamic was in the language), it already had powerful metaprogramming and introspection capabilities, as well as very convenient  generics and interfaces ( easier to work with than what swift offers today, but that was a long time ago so my memory may be wrong). So, in some way, the potential for abusing dynamic or "stringly typed" programming was a lot lower.

I find this perspective interesting because one of the articles I linked to on `dynamic` explicitly pointed out how it made some code far clearer than using reflection.

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

The example code using reflection:
public class ReflectiveTester : Tester
{

    static void WritePropertyReflectively(object instance, string propertyName)
    {
        Type type = instance.GetType();
        PropertyInfo propertyInfo = type.GetProperty(propertyName);
        if (propertyInfo == null)
            Console.WriteLine("Property \"{0}\" not found, propertyInfo " + 
                              "is null\r\npropertyInfo.GetValue(...) will result " + 
                              "in a NullReferenceException", propertyName);
        else
            Console.WriteLine(propertyInfo.GetValue(instance, null));
    }

    static void CallMethodReflectively(object instance, string methodName)
    {
        Type type = instance.GetType();
        MethodInfo methodInfo = type.GetMethod(methodName);
        if (methodInfo == null)
            Console.WriteLine("Method \"{0}\" not found, " + 
                              "methodInfo set to null\r\nmethodInfo.Invoke(...) " + 
                              "will result in a NullReferenceException", methodName);
        else
            methodInfo.Invoke(instance, null);
    }

    static void WriteClassDetails(object instance)
    {
        Type type = instance.GetType();
        WritePropertyReflectively(instance, "Property");
        CallMethodReflectively(instance, "Method");
    }
}

The example code using `dynamic`:
public class DynamicTester : Tester
{
    void WriteClassDetails(dynamic instance)
    {
        try
        {
           Console.WriteLine(instance.Property);
           instance.Method();
        }
        catch (RuntimeBinderException ex)
        {
            ErrorWriters.WriteRuntimeBinderException(ex);
        }
    }
}
And a quote from the article:
> One thing to conclude on is that this is not less "OO" than the reflective call; it is directly equivalent, but it has a neat syntax and less obtuse exception mechanism. If viewed this way, the addition of dynamicis a large benefit, even before the dynamic language support is considered.


As I've said, I'm as far from a C# expert as they come, but the C# devs seem to really appreciate this feature and it doesn't appear to have been abused in the way people here seem to fear for the proposed protocols.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171205/e1a483f5/attachment.html>


More information about the swift-evolution mailing list