Closed pixelzoom closed 1 year ago
Natural Selection is not currently a publication priority, so unassigning.
- public static readonly BunnyIO = new IOType( 'BunnyIO', {
+ public static readonly BunnyIO = new IOType<Bunny, BunnyStateObject>( 'BunnyIO', {
As part of https://github.com/phetsims/tandem/issues/297, @zepumph and I made it possible for composite state schemas to use underscore keys (as well as continuing to support non-underscored keys), and to access underscored or non-underscored class attributes. There is a TODO that relates to this:
export default class Bunny extends Organism {
//TODO https://github.com/phetsims/natural-selection/issues/327 father, mother, isAlive, age should be readonly for clients
public father: Bunny | null;
public mother: Bunny | null;
public readonly generation: number;
In this case, if desired, you could rename those like: _father
etc and have a public getter (and no public setter). You will likely still need to leave it non-readonly
if you want to null it out in dispose. But _father
it could be private.
Changes completed in the above commits. Tested in State wrapper. Migraton wrapper indicates that no additional migration rules are needed.
Closing.
Discovered during https://github.com/phetsims/natural-selection/issues/326 TypeScript conversion...
There are mutliple PhET-iO problems with Bunny.ts. I'll need assistance from @samreid or @zepumph for this issue.
this.father
,this.mother
, andthis.isAlive
are currently writeable as part of the Bunny API. They should be readonly, so should be converted to privatethis._father
,this._mother
, andthis._isAlive
, with ES5 getters. This will require modifyingstateToArgsForConstructor
andapplyState
.defaultApplyState
will no longer work because the core-type field names do not match the stateSchema field names.this.generation
is readonly, so it's inappropriate forapplyState
to write to it. I should probably reimplementstateToArgsForConstructor
so that most of the state (includinggeneration
) is restored via constructor, and any remaining state (everything in_private
?) is restored viaapplyState
. This feels like a general PhET-iO problem -- when state is restored, TypeScript access modifiers are ignored.this.genotype
andthis.phenotype
are getting restored. Neither of those fields is currently part of BunnyStateObject. Shouldoptions.genotypeOptions
be included in BunnyStateObject, so it can be used bystateToArgsForConstructor
to reconstruct the instance? This feels like a bug, but may just be a misunderstanding by me. I do see this comment ingetStateSchema
, but I don't understand how this works:getStateSchema()
.getStateSchema
needs to be defined as a method (vs inline asstateSchema: {...}
because it references BunnyIO. This is the current implementation:... and that results in this TS error for the last line:
I've also tried this:
... but that results in this TS error for the last line:
_private
part of a StateSchema. See https://github.com/phetsims/tandem/issues/282.