Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Base cast specialization #149

Open
GoogleCodeExporter opened this issue Jul 8, 2015 · 1 comment
Open

Allow Base cast specialization #149

GoogleCodeExporter opened this issue Jul 8, 2015 · 1 comment

Comments

@GoogleCodeExporter
Copy link

This is not a bug but a feature request

I love the builtin casting ability of Base, but there are situations where I 
would like to customize coercion.  I have enclosed a small patch to enhance 
Base with this ability.  

Basically, it will look for a static coerce method in the class or its 
ancestors.  If one is found, it is called and the return value is used as the 
cast.  Otherwise, the default casting is performed. 


What version of the product are you using? On what operating system?
Currently on Mac OS X 10.9.4

Here is the diff (attached as well)

Index: base2/Base.js
===================================================================
--- base2/Base.js   (revision 310)
+++ base2/Base.js   (working copy)
@@ -23,6 +23,12 @@
         delete this.__constructing;
       } else {
         // Casting.
+        var coerce = _class;
+        do {
+          if (coerce.coerce)
+            return coerce.coerce.call(_class, arguments[0]);
+          coerce = coerce.ancestor;
+        } while (coerce && (coerce != Base));
         return extend(arguments[0], _prototype);
       }
     }

Original issue reported on code.google.com by [email protected] on 14 Jul 2014 at 6:56

Attachments:

@GoogleCodeExporter
Copy link
Author

Here is a minor tweak that will fall through to default cast if no coercion is 
performed

Index: Base.js
===================================================================
--- Base.js (revision 310)
+++ Base.js (working copy)
@@ -23,6 +23,13 @@
         delete this.__constructing;
       } else {
         // Casting.
+        var cls = _class;
+        do {
+          if (cls.coerce) {
+            var cast = cls.coerce.call(_class, arguments[0]);
+            if (cast) return cast;
+          }
+        } while ((cls = cls.ancestor) && (cls != Base));
         return extend(arguments[0], _prototype);
       }
     }

Original comment by [email protected] on 15 Jul 2014 at 12:11

Attachments:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant