Binding restrictions

Jan 4, 2010 at 12:49 PM

I've been digging through the source code of the DLR for the last week and a half, reading the docs and implementing my own version of Sympl (following the tutorial) also reading the IronPython sources quite a lot. But there is one thing that I've not groked fully: Binding Restrictions. Take this source example from Runtime.cs in the Sympl C# implementation:

  public static BindingRestrictions GetTargetArgsRestrictions(
                DynamicMetaObject target, DynamicMetaObject[] args,
                bool instanceRestrictionOnTarget)
	{
            var restrictions = target.Restrictions.Merge(BindingRestrictions.Combine(args));

            /* ... snipped for brevity ... */

            for (int i = 0; i < args.Length; i++) {

                BindingRestrictions r;

                if (args[i].HasValue && args[i].Value == null) {
                    r = BindingRestrictions.GetInstanceRestriction(
                            args[i].Expression, null);
                } else {
                    r = BindingRestrictions.GetTypeRestriction(
                            args[i].Expression, args[i].LimitType);
                }

                restrictions = restrictions.Merge(r);
            }

            return restrictions;
        }

I've snipped parts of it for brevity, if you look at the first line: The restrictions from the args[] array is merged with the target restrictions, but then before the method returns all the values args[] are looped through and created restrictions for again. Now to my understanding what's happening here is: 

1) First, if the DynamicBetaObject instances in args[] contain any restrictions (which may or may not be type/instance restrictions), merge them with the ones for target
2) Then, create a type/instance restriction for each value in args[]
 so that we are sure we have a type/instance restriction for each argument, no matter if we already had one from step 1

If we, in step 2, managed to create an type/instance restriction for one of the values in args[] that already existed from step 1 they are just ignored/merged.

Have I groked this correctly?

Coordinator
Jan 4, 2010 at 4:42 PM

Yes, you grokked correctly.  The DLR removes dup restrictions.  Also, it may sound odd when you say "you're merging target with arg restrictions", but what's really going on is that we're just building the list of all restrictions that need to be true for the binding rule to be useful.  That is, all restrictions must be true for the resulting DynamicMetaObject if its expression is to be used to perform the operation.

I'll add a comment to the code and doc to make that more clear.  I thought I mentioned restriction dups being removed in the Sympl doc, but even if I did, it might not be located well.

Thanks,

Bill

From: fholm [mailto:notifications@codeplex.com]
Sent: Monday, January 04, 2010 4:50 AM
To: Bill Chiles
Subject: Binding restrictions [dlr:79676]

From: fholm

I've been digging through the source code of the DLR for the last week and a half, reading the docs and implementing my own version of Sympl (following the tutorial) also reading the IronPython sources quite a lot. But there is one thing that I've not groked fully: Binding Restrictions. Take this source example from Runtime.cs in the Sympl C# implementation:

  public static BindingRestrictions GetTargetArgsRestrictions(
                DynamicMetaObject target, DynamicMetaObject[] args,
                bool instanceRestrictionOnTarget)
  {
            var restrictions = target.Restrictions.Merge(BindingRestrictions.Combine(args));
 
            /* ... snipped for brevity ... */
 
            for (int i = 0; i < args.Length; i++) {
 
                BindingRestrictions r;
 
                if (args[i].HasValue && args[i].Value == null) {
                    r = BindingRestrictions.GetInstanceRestriction(
                            args[i].Expression, null);
                } else {
                    r = BindingRestrictions.GetTypeRestriction(
                            args[i].Expression, args[i].LimitType);
                }
 
                restrictions = restrictions.Merge(r);
            }
 
            return restrictions;
        }

I've snipped parts of it for brevity, if you look at the first line: The restrictions from the args[] array is merged with the target restrictions, but then before the method returns all the values args[] are looped through and created restrictions for again. Now to my understanding what's happening here is:

1) First, if the DynamicBetaObject instances in args[] contain any restrictions (which may or may not be type/instance restrictions), merge them with the ones for target
2) Then, create a type/instance restriction for each value in args[] so that we are sure we have a type/instance restriction for each argument, no matter if we already had one from step 1

If we, in step 2, managed to create an type/instance restriction for one of the values in args[] that already existed from step 1 they are just ignored/merged.

Have I groked this correctly?

Read the full discussion online.

To add a post to this discussion, reply to this email (dlr@discussions.codeplex.com)

To start a new discussion for this project, email dlr@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com