-
Notifications
You must be signed in to change notification settings - Fork 23
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
Decide on how to implement computed properties #25
Comments
I was assuming lets use both to be honest. I wrote my PR based on the assumption we would merge #18 first (because i needed As long as we can be sure that the dependencies in the getter are up to date when the getter is called (as we are referencing For any complex situations where you want splices, change objects, etc, we should just pass a function to I would definitely avoid adding any more complex "handles everything" solution as we risk just blowing it out of proportion and ending up with some twisty, roundabout code. Also totally forgot i could use |
I think I'm also leaning towards supporting both styles. The The
So would this look like |
@aomarks i think in my PR the syntax is now using spread, but yes you are pretty much right:
Its a bit strange because the getter is replaced, and its no longer really what you wrote in the code. But its nicer than having the untyped link of a method name (string) to a method. |
Can we add an example for the second way of adding a computed property (using |
Sorry I'm late to the party, but this all looks good to me. However, part of me wants to support @justinfagnani's last proposal: class X {
@property() a: number;
@property() b: number;
@property({
computedFrom: ['a', 'b'],
computed: (a: number, b: number) => a + b,
})
readonly c: number;
} This allows us to use any function, which will make conversion from PolymerTS easier. It's also a little more straightforward since we're not defining a getter that's going to be replaced. It is more verbose and not as straightforward to code, though. |
@kito99 if we did that, we would have to either add support for that form of anyhow, best to open an issue to track that wherever seems appropriate |
Now that I've looked into things more, it's clear that the current use of the class X {
@property() a: number;
@property() b: number;
@property({computed: `_c(a,b)`})
readonly c: number;
_c(a: number, b: number): number {
return a + b;
}
} And I don't think the get() syntax will be a big problem for migration. |
@43081j you've got a good point about support in Polymer core. I'm glad to see they now support functions for observers, but no such luck for computed properties. That being said, since we have support for a real function via the getter, I'm not sure it's all that necessary. So unless someone else really needs this, I think we're fine as-is. |
Created pull request #35, which documents the |
We have two PRs that add computed properties in two different ways: #18 and #20.
Both have their merits, and slightly different capabilities, and also we'd like to see if we can get some more type safety from compute function declarations.
@aomarks and I played with a few options yesterday, none of which seemed like a slam dunk. I wanted to open the issue here so we could all discuss the pros and cons, with @43081j and @danielvanmil. @kito99 might be interested too.
There's a few facts that come into play, and are a bit in tension:
@computed()
The getter approach in #20 is pretty nice on the TypeScript side because we don't use arguments that can't be verified by tsc:
Pros:
c
is fully type-checked.Cons:
c
is accessed, but whena
andb
change, as Polymer caches the value.a.*
@computed
that are valid from@property
, likereflectToAttribute
.@Property()
The
@property()
options approach in #18 is more similar to standard Polymer:Pros:
reflectToAttribute
Cons:
@property
decoration is longer.@property()
admits invalid options.type
andreadonly
aren't needed.Why Not Both?
Al and I talked about both possibly being necessary - one for simple cases and the other for more complex dependencies.
Better Type-Checking
If we can get the decorator parameterized by the class type (which doesn't happen automatically, unfortunately), we can constrain the options so that they're valid keys, though not valid properties in the Polymer sense:
Al and I were also playing with deconstructing the Polymer computed property syntax, within
@property()
:This is kind of nice in that it forces the computing function to be pure, since it can't access instance fields, and it keeps the computing function close to the property declaration.
OK... discuss :)
The text was updated successfully, but these errors were encountered: