-
Notifications
You must be signed in to change notification settings - Fork 8
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
Property should support validator from phetioType #235
Comments
Certainly desirable not to have both. I don't have a feel for how much work is involved. |
@zepumph can you make a proposal for how we should deal with this in files like BooleanProperty? |
I'm going to transfer this issue from dot to axon, because it's about Property, not specific to dot. |
It may be simpler to start with thinking just about Property and not sub types. In Emitter we developed a strategy for having EmitterIO define the validators for each argument, and then in Emitter it was simple to say that either the client provided Here is just one possible solution. I would propose a similar logic but perhaps not as strict. I think it would be best to say that if you provide any validator keys, those take precident and the phetioType is ignored, but IF you provide a phetioType and NO validator keys to Property, then it will get the validator from the phetioType. The logic in PropertyIO will be extremely simple, because it just needs to export the value typeIO's validator. Usages looks like this:
This would not error, but could be simplified to:
Above, the validator comes from NumberIO.validator More cases create more choices for us to decide direction: new Property( {
valueType: 'number',
validValues: [23,3,4],
phetioType: 'PropertyIO( NumberIO )'
}); I think that the simplest logic in the code would be to say that we ignore the validator from the phetioType here. So there would be no simplification available. Another, more complicated but more paralleled to Emitter approach would be to only be able to specify the validator through the phetioType if the phetioType is provided. We could have many more assertions here, and would be more strict. In general there would be less options to Property too (because more validator keys would move to the PropertyIO type definition).
As it pertains to Property sub types, BooleanProperty would only define the phetioType, and not the |
Feedback on previous comment... First, I think it would be preferable to synthesize Re these 2 examples, which I'll give names here:
About Example 2, you said:
I don't understand why no simplification would be available, or why it would be more complicated (or any different) to implement than Example 1. Re:
I don't see the advantage to this. It has the same duplication of type information that |
Above, I said:
Over in #228, we came to the consensus that we should support It makes synthesizing
... is not expressible as a |
Here's an untested function to illustrate what I had in mind. See especially the "NEW" comment. function valueTypesToPhetioType( valueTypes ) {
assert && assert( Array.isArray( valueType ), 'valueTypes must be an Array: ' + valueTypes );
assert && assert( valueTypes.length === 1 || ( valueTypes.length === 2 && valueTypes.indexOf( null ) !== -1 ),
'valueTypes must be either [ Type ] or [ Type, null ]' );
const typeName = _.first( valueTypes, value => value !== null );
const nullable = ( valueTypes.indexOf( null ) !== -1 );
if ( typeName === 'boolean' ) {
return nullable ? NullableIO( BooleanIO ) : BooleanIO;
}
else if ( typeName === 'number' ) {
return nullable ? NullableIO( NumberIO ) : NumberIO;
}
else if ( typeName === 'string' ) {
return nullable ? NullableIO( StringIO ) : StringIO;
}
else {
assert && assert( typeName.hasOwnProperty( 'prototype' ), `not a type or class: ' + typeName );
// NEW: Every type that supports PhET-iO must have a phetioType field that identifies its associated IO type.
assert && assert( typeName.hasOwnProperty( 'phetioType' ), 'missing phetioType for ' + typeName );
return nullable ? NullableIO( typeName.phetioType ) : typeName.phetioType;
}
} |
I would be willing to experiment with the above if we agree it is the right direction, but I feel like it is backwards. A phetioType already has a validator built into it. It is easy to map from phetioType to validator, and it is easy to assert that if a phetioType is specified, then we don't need the valueType specified through Property options. This is what is done in Emitter as well. Note that the "NEW" comment above illustrates a pattern that could work, but would involve quite a bit of refactoring, because each type would now need a phetioType assigned to it, and with so many parametric phetioTypes around, it seems fragile and like we may be boxing ourselves into a corner. |
Assigning @pixelzoom and @samreid for comment |
Discussed with @zepumph on Zoom, and I'm convinced that we can't map from It's also currently the case that you can't remove |
Is it your recommendation to run |
After a conversation with @pixelzoom, we came up with my favorite recommendation for a path forward for Property so far:
Here phetioType validation would occur even in phet brand. This helps us remove the duplication of needing valueType in phet brand, and phetioType for phet-io brand. It is also good to note that @pixelzoom and I discussed that it is still a bummer to need to refactor |
Bumping priority. |
Yes indeed we are looking for discussion. Our conclusion is that What do you think about that? |
Also note that as we speak, support is being added to ValidatorDef to support phetioType as a validator key, see #258 |
What does "then |
The direct answer to your question is
If For this issue I'm insterested in discussing #235 (comment) and the pieces that I now feel have changed because of the example in #235 (comment). Here is a new bullet list of items that I think should be the understanding of how Property behaves.
NOTE: all of these are now supported. Property gained support for phetioType in #258 along with all other core Types that are validating. Specifically in b55a3bf#diff-a24406f5f2879d5846b0ae23318df8d3R65. I don't think there is anything else to do with this issue so long as we agree on the above bullets. Assigning for confirmation or discussion. |
Everything in the preceding comment sounds great to me, thanks for your efforts on this. |
#235 (comment) offers this hypothetical example as the (sole?) reason for keeping both // We want a more general public phet-io api, but more specific phet core validation
new SubType1( {
phetioType: SuperTypeIO,
valueType: SubType1
}) Please convince me. Why would you want a "more general public phet-io api"? Can you provide concrete cases where we would want That said... I'm disappointed that we will apparently continue to have 2 ways of specifying validation, with 2 entirely different syntaxes. I look forward to seeing guidelines for when to use |
For instance,
I believe generally they will not be semantically different, which is why @zepumph said:
Going to the next part:
To clarify, the primary function of a
Yes, the syntaxes for
I'd say a simple but (mostly?) accurate rule of thumb would be that the
Other reasons to keep
If we are worried about too many redundant |
My understanding is that IO types are stubs when running with |
IO types used to be implemented in the phet-io repo and buried behind
I tested this by changing QuadraticIO.validator to read: validator: {
isValidValue: v => {
console.log( 'hello' );
return true;
}
}, and when running Graphing Quadratics in "phet" brand, I indeed saw several "hello"s. This confirms that IO types are loading and validating in "phet" brand. |
Great, thanks for confirming. That makes me more comfortable with replacing |
In case more is needed. . . I think it is likely that in the future we will have a single
I understand that! The choice is between keeping both, and removing @pixelzoom do you feel ready to close this issue? |
I said:
@samreid said:
I would like to see something related to this in the "How to Instrument" document before closing. |
Please review. |
Doc looks good, fixed a typo. Closing. |
Related to phetsims/dot#88 because there we are talking about how to support phetioType and validator keys in Property. Similar to #204 because we did similar work in Emitter to omit duplication of supplying validators and phetioType when not necessary.
I created this issue because I saw work being done in phetsims/dot#88 like
335ef9f which worried me. Would it be possible to have that
valueType
come from thephetioType
so that it isn't duplicated?The text was updated successfully, but these errors were encountered: