Something that's currently very annoying about CPs is that whether arguments are literals or properties is something that must be decided at macro-design time, rather than at macro-use time.
Consider for instance Ember.computed.equal
. It would be nice to be able to
use the same macro for creating CPs that compare against literal values as well
as against properties.
This is quite easy to do with composable computed properties. As long as
equal
is changed to treat all arguments as properties, it simply takes an
Ember.computed.literal
cp to support literal values again.
Some examples:
var equal = Em.computed.equal,
// "literal"
l = Em.computed.literal;
equal(l('Jaime Lannister'), l('Jaime Lannister')); // "Jaime Lannister" === "Jaime Lannister"
equal('name', l('Jaime Lannister')); // get("name") === "Jaime Lannister"
equal('name', 'otherName'); // get("name") === get("otherName")
Unfortunately this approach creates a backwards compatibility problem as the semantics of the following code changes:
equal('name', 'Jaime Lannister'); // get("name") === get("Jaime Lannister")
We add Ember.computed.literal
and Ember.computed.property
, both under the
feature flag composable-computed-properties
, and the feature flag
macros-default-to-properties
.
The implementation would simply convert all non-CP arguments to literal
CPs
when macros-default-to-properties
was disabled, provided that
composable-computed-properties
was enabled.
The upgrade path would be fairly simple. Users could enable
macros-default-to-properties
to discover parts of their code that needed
changing. Then, they could write code like the following:
var equal = Em.computed.equal,
l = Em.computed.literal;
// this would break with `macros-default-to-properties` disabled
equal('name', 'Jaime Lannister');
// so we change it to this
equal('name', l('Jaime Lannister');
Even with macros-default-to-properties
disabled, users could gain the benefits
of literal/property agnosticism with the following:
var equal = Em.computed.equal,
p = Em.computed.property;
// This would still work with `macros-default-to-properties` disabled
equal('name', p('otherName'));
It occurs to me that
Em.computed.property
is just another name forEm.computed.alias
. The name is a bit nonobvious as the pair toEm.computed.literal
. Anybody have thoughts on whether it's worth havingEm.computed.property = Em.computed.alias
?