Instance Interfaces and Mix-ins for Dynamic Languages
20180165106 ยท 2018-06-14
Assignee
Inventors
Cpc classification
International classification
Abstract
Various technologies and techniques are disclosed for using contracts in dynamic languages. For example, a contract can be directly associated with an object. The contract can then be used to provide type safety for the object. As another example, contracts can be used with mix-ins. A declaration for a contract is provided with a mix-in. The contract is associated with a target object at runtime when applying the mix-in. Conditions can be assigned to mix-ins that must be met before the mix-in can be applied to the target object. At runtime, if the target object meets the one or more conditions, then the mix-in can be applied to the target object.
Claims
1. A system for restricting the use of mix-ins based on meeting of a condition, comprising: a computer comprising a processing unit coupled to a memory, the memory storing computer-executable instructions which, when executed, cause the processing unit to perform the steps of: by calling an add interface method of a target object, associating at least one condition with a mix-in that must be met by the target object before the mix-in can use the target object; enforcing type safety for the mix-in by, at runtime of the target object, before applying the mix-in to the target object, determining whether the target object meets the at least one condition; if the target object meets the at least one condition, at runtime, applying the mix-in to the target object; and if the target object does not meet the at least one condition, at runtime, not allowing the mix-in to be used and throwing an exception.
2. The system of claim 1, wherein the condition is a constraint contract that constrains objects to which the mix-in can be applied.
3. The system of claim 1, wherein the condition is evaluated based data contained in the target object.
4. The system of claim 1, wherein determining whether the target object meets the at least one condition is performed using reflection.
5. The system of claim 1, the memory storing further computer-executable instructions which, when executed, cause the processing unit to perform the steps of: receiving a method call to register the mix-in, the method call including as a parameter at least one of an interface implementation provided by the mix-in, a set of types the mix-in is able to extend, or a set of base mix-ins from which the mix-in inherits implementations.
6. The system of claim 1, the memory storing further computer-executable instructions which, when executed, cause the processing unit to perform the steps of: allowing the mix-in to be extended by another mix-in, wherein the another mix-in inherits an interface from the mix-in.
7. The system of claim 1, wherein the target object is an instance of a class.
8. A method, comprising: by calling an add interface method of a target object, associating at least one condition with a mix-in that must be met by the target object before the mix-in can use the target object; enforcing type safety for the mix-in by, at runtime of the target object, before applying the mix-in to the target object, determining whether the target object meets the at least one condition; if the target object meets the at least one condition, at runtime, applying the mix-in to the target object; and if the target object does not meet the at least one condition, at runtime, not allowing the mix-in to be used and throwing an exception.
9. The method of claim 8, wherein the condition is a constraint contract that constrains objects to which the mix-in can be applied.
10. The method of claim 8, wherein the condition is evaluated based data contained in the target object.
11. The method of claim 8, wherein determining whether the target object meets the at least one condition is performed using reflection.
12. The method of claim 8, further comprising: receiving a method call to register the mix-in, the method call including as a parameter at least one of an interface implementation provided by the mix-in, a set of types the mix-in is able to extend, or a set of base mix-ins from which the mix-in inherits implementations.
13. The method of claim 8, further comprising: allowing the mix-in to be extended by another mix-in, wherein the another mix-in inherits an interface from the mix-in.
14. The method of claim 8, wherein the target object is an instance of a class.
15. A computer storage memory having computer-executable instructions for causing a computer to perform steps comprising: by calling an add interface method of a target object, associating at least one condition with a mix-in that must be met by the target object before the mix-in can use the target object; enforcing type safety for the mix-in by, at runtime of the target object, before applying the mix-in to the target object, determining whether the target object meets the at least one condition; if the target object meets the at least one condition, at runtime, applying the mix-in to the target object; and if the target object does not meet the at least one condition, at runtime, not allowing the mix-in to be used and throwing an exception.
16. The computer storage memory of claim 15, wherein the condition is a constraint contract that constrains objects to which the mix-in can be applied.
17. The computer storage memory of claim 15, wherein the condition is evaluated based data contained in the target object.
18. The computer storage memory of claim 15, wherein determining whether the target object meets the at least one condition is performed using reflection.
19. The computer storage memory of claim 15 having further computer-executable instructions for causing the computer to perform steps comprising: receiving a method call to register the mix-in, the method call including as a parameter at least one of an interface implementation provided by the mix-in, a set of types the mix-in is able to extend, or a set of base mix-ins from which the mix-in inherits implementations.
20. The computer storage memory of claim 15 having further computer-executable instructions for causing the computer to perform steps comprising: allowing the mix-in to be extended by another mix-in, wherein the another mix-in inherits an interface from the mix-in.
Description
BRIEF DESCRIPTION OF THE DRAWINGS
[0016]
[0017]
[0018]
[0019]
[0020]
[0021]
[0022]
[0023]
[0024]
[0025]
DETAILED DESCRIPTION
[0026] The technologies and techniques herein may be described in the general context as an application that allows creation and/or execution of dynamic language script applications, but the technologies and techniques also serve other purposes in addition to these. In one implementation, one or more of the techniques described herein can be implemented as features within a dynamic programming language or framework such as MICROSOFT ASP.NET Ajax, JavaScript, or from any other type of program or service that allows for creation and/or execution of applications created in a dynamic language.
[0027] As shown in the diagram 10 of
[0028] Turning now to
[0029] Alternatively or additionally, one or more parts of dynamic language scripting application 200 can be part of system memory 104, on other computers and/or applications 115, or other such variations as would occur to one in the computer software art.
[0030] Dynamic language scripting application 200 includes program logic 204, which is responsible for carrying out some or all of the techniques described herein. Program logic 204 includes logic for accessing a dynamic language script that directly associates contract(s) with object(s) in a dynamic language 206 (as described below with respect to
[0031] Turning now to
[0032] Turning now to
[0033] As described previously, contracts or interfaces can be directly associated with objects. To allow for that scenario, it is necessary to have some API method or other way of adding a contract or interface declaration to any object. In one implementation (and using the API illustrated in
Object.AddInterface(instance, interfaceType);
[0034] After executing the line of code above, the object will contain a direct association with the interface contained as a parameter above. Similarly, in one implementation (and the example shown in
[0035] The GetInterfaces method 268 is used in the exemplary API to allow for reflection. Reflection allows a program to inspect its own structure and functionality, which can be useful for various purposes. One common use for reflection is for programmatically generating documentation of a program. Another common use of reflection is to perform duck-typing as described in the background, such as to help ensure that a particular method uses the parameters in the way expected.
[0036] A code example of how the GetInterfaces method 268 could be used is shown below:
Object.GetInterfaces(instance)
[0037] In one implementation, use of the GetInterfaces method 268 such as illustrated above will get the interfaces that have been directly associated with the particular object. Alternatively or additionally, the GetInterfaces method 268 may return other associations that are indirect through the object's type.
[0038] Let's now turn to the Type API 272 shown in
[0039]
[0040] Before jumping to
TABLE-US-00001 var a = [Zero, One, Two, Three, Four, Five, Six]; Object.mixin(a, Mixin.Collections.EnumerableArrayEx); if (Mixin.Collections.IEnumerable.isImplementedBy(a)) { var enumerator = a.getEnumerator( ); while (enumerator.moveNext( )) { write(enumerator.get_current( ), Item); } }
[0041] In the example above, EnumerableArrayEx is a mixin that provides an implementation of IEnumerable. The EnumerableArrayEx is being added to an array. As soon as this is done, a implements the interface, even though its type (Array) doesn't. The interface can then be used with the certainty that checking for the contract provides. In other words, by associating the interface with the mix-in, type safety can be provided. The mix-in packages more information here than just implementation: it also packages the set of interface types that it implements.
[0042] In the previous code example, the EnumerableArrayEx mix-in is assuming that its target is an array. Other mix-ins may use one or several interfaces that they expect on their target. In one implementation, the Object.mixin method is responsible for checking that those types are present on the target at runtime. This is described in further detail in
[0043]
[0044]
[0045] While not strictly necessary, in one implementation, a helper method is provided to declare mixins (RegisterMixin method 352), as well as additional reflection methods that apply to mix-ins (IsMixin 354 and Mixin 356). The RegisterMixin method 352 registers a mix-in, declares which interfaces it provides an implementation for, what types of instances it can extend, and a set of base mix-ins that it will inherit implementations from. In one implementation, all parameters but the type name are optional. A code example is shown below:
TABLE-US-00002 Type.RegisterMixin( typeName, implementedInterfaces, extendedTypes, mixins)
[0046] Let's now look at an example of a mix-in declaration (note that the actual implementation is not included):
TABLE-US-00003 Mixin.Collections.CollectionArrayEx.registerMixin( Mixin.Collections.CollectionArrayEx, [Mixin.Collections.ICollection], [Array], [Mixin.Collections.EnumerableArrayEx]);
[0047] In the example shown, the CollectionArrayEx mix-in implements ICollection (and any interface that the mix-ins it inherits from implement, in this case IEnumerable). The mix-in in this example applies only to Array instances and inherits from EnumerableArrayEx.
[0048] The IsMixin method 354 is used to determine if a type is a mix-in. The method returns a true or a false value to indicate whether or not the specified type is a mix-in. Here is an example line of code for how that method could be used:
Type.isMixin(type)
[0049] To extend an object or all instances of a type with a mix-in, the Mixin method 342 (for the Object API 340) or the Mixin method 356 (for the Type API 350) can be used. Here is some example code for extending an object using the Mixin method 342.
Object.mixin(target, mixin)
[0050] Similarly, here is some example code for extending a type (or all instances of a type) using the Mixin method 356:
Type.mixin(target, mixin)
[0051] In one implementation, a RemoveMixin method can also be provided in the Type API 350 to remove a mix-in from an object.
[0052]
[0053]
[0054] As shown in
[0055] Additionally, device 500 may also have additional features/functionality. For example, device 500 may also include additional storage (removable and/or non-removable) including, but not limited to, magnetic or optical disks or tape. Such additional storage is illustrated in
[0056] Computing device 500 includes one or more communication connections 514 that allow computing device 500 to communicate with other computers/applications 515. Device 500 may also have input device(s) 512 such as keyboard, mouse, pen, voice input device, touch input device, etc. Output device(s) 511 such as a display, speakers, printer, etc. may also be included. These devices are well known in the art and need not be discussed at length here. In one implementation, computing device 500 includes dynamic language scripting application 200 (described in
[0057] Although the subject matter has been described in language specific to structural features and/or methodological acts, it is to be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or acts described above. Rather, the specific features and acts described above are disclosed as example forms of implementing the claims. All equivalents, changes, and modifications that come within the spirit of the implementations as described herein and/or by the following claims are desired to be protected.
[0058] For example, a person of ordinary skill in the computer software art will recognize that the examples discussed herein could be organized differently on one or more computers to include fewer or additional options or features than as portrayed in the examples.