-
-
Notifications
You must be signed in to change notification settings - Fork 382
Refactor constructors #2409
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
base: master
Are you sure you want to change the base?
Refactor constructors #2409
Conversation
@jourdain @finetjul @martinken @luciemac This pull request is a proposal to solve the problem mentioned two months ago about the refactoring of constructors. I propose a way to handle that issue on the three highlighted cases as a PoC. |
3ef5f30
to
a183c04
Compare
a183c04
to
0e72346
Compare
e4d538d
to
eff689a
Compare
Object.assign(model, DEFAULT_VALUES, initialValues); | ||
|
||
if (!model.empty && !model.values && !model.size) { | ||
if (initialValues.empty === false && !initialValues.values) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about: if (!initialValues.empty && !initialValues.values)
It would support undefined and null case for initialValues.empty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
empty flag was intended at construction time so you can allocate the array later on rather than ahead of time. Basically to ensure the user was aware that it created an empty dataArray.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looking at the code in master, it seems that an array is ALWAYS provided or created, no ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not providing it require an empty
flag. But yes, I think we always get something. Meaning that with the empty flag, we still allocate the TypedArray based on size/type...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fair enough, so let's see if we all agree on the following:
if initialValues = {empty: true, size: undefined|null|0} then model.values = null
if initialValues = {empty: true, size: 10} then model.values = new TypedArray(10)
if initialValues = {empty: undefined|false} then throw error
if initialValues = {empty: undefined|false, data: aTypedArray} then model.values = aTypedArray
if initialValues = {empty: undefined|false, data: aJSArray} then model.values = TypedArray.from(aJSArray)
if setData(null) then model.values = null
if setData(aTypedArray) then model.values = aTypedArray
if setData(aJSArray) then model.values = TypedArray.from(aJSArray)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, if initialValues = {empty: true, size: undefined|null|0}
then model.values = new TypedArray(0)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jourdain Are you ok with #2409 (comment) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, sorry about that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For if initialValues = {empty: true, size: undefined|null|0}
I will be fine with model.values = null || TypedArray(0)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if initialValues = {empty: true, size: undefined|null|0} then model.values = null if initialValues = {empty: true, size: 10} then model.values = new TypedArray(10) if initialValues = {empty: undefined|false} then throw error if initialValues = {empty: undefined|false, data: aTypedArray} then model.values = aTypedArray if initialValues = {empty: undefined|false, data: aJSArray} then model.values = TypedArray.from(aJSArray) if setData(null) then model.values = null if setData(aTypedArray) then model.values = aTypedArray if setData(aJSArray) then model.values = TypedArray.from(aJSArray)
It should be implemented this way in last commit.
eff689a
to
0761730
Compare
894a3e3
to
34a6681
Compare
if (!typedArray) { | ||
vtkErrorMacro(`Cannot call setData with given array : ${typedArray}`); | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not allow setData(null)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous implementation would have raised an error in that case. It was not checking the typed array but was doing model.size = typedArray.length
, which would have raised an error. It put the values
to null but not update the other parameters. Such implementation avoiding an error can also be implemented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's support setData(null)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then is it that we want that model.values
is null or an "empty" array (of size model.size
filled with zero) like it is the case in the constructor ?
78e38b9
to
91c8a3c
Compare
After some troubleshooting while applying the changes on the DataArray to classes using it, it appeared that it was necessary to handle the field |
improvement of getArrayWithIndexFix
…them in constructor put some values in default rather than set them in constructor
fix after refactoring
remove useless initialValues setting
call setters when creating the object
fix after refactor (remove tmp true in newinstance)
Temporary to only call setters for modified files
fix refactoring
refactor constructor to call setters
remove .only
refactor constructor to call setters
some more sources refactoring
add some tests for points (similar to dataarray tests)
…actor constructors refactor constructors to call setters. Change setData to handle instanciation and setting
fix refactoring (put table in defaultValues)
refactor constructor to call setters
document some implementation choices
fix previous commit
fix refactoring
refactor constructors to call setters at instanciation
remove undesired checks and document some choices
fix previous commit
fix refactoring
coherence in choices + fix to have exact same behavior
coherence in choices
refactor constructor to call setters at instanciation
273158c
to
7f15397
Compare
bufferShift: [0, 0, 0, 0], | ||
|
||
propID: undefined, | ||
bufferShift: undefined, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joannacirillo which bufferShift to keep ?
Hello, no news for a while, but where do we stand? |
@MilanPlFn2, it hasn't moved much due to lack of time. Is it something you would like to move forward ? |
Context
When creating a new object, no setters are currently called. However, if they have a side effect (which is the case if they have been created through the use of
macro.set(...)
for instance), we have to call them so we can see those side effects. Therefore, setters need to be called when creating a new instance if they exist.In particular, with
A
andB
two classes,B
being a child class ofA
and the functionsetProp1
existing in both classes, only the setter from child classB
must be called when creating a new objectB
.Results
The setters are called if they exist when instanciating a new object. Changes might be needed to be done in classes that customly handle properties in constructor (
extend
function) like it is the case of theDataArray
class.For classes having internal properties set in the extend function that don't correspond to properties that can be given in the initialValues, they are now set at the beginning of the defaultValues (in order to be set first).
Changes
In the function
newInstance
,publicAPI.set(initialValues)
is called. The correct setters are thus called when creating a new object, i.e. the one of the child class (defined in parent class or overwritten/created in child class) when one class extend another. Classes need to be created this way:Parent class constructor example:
Child class constructor example:
PR and Code Checklist
npm run reformat
to have correctly formatted codeTesting