You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
in Python the dataclass type supports calling a factory function to retrieve the default value for a field. For more information you can see here. The parameter is named default_factory for the Field descriptor class which is used to declare extra information for each field.
Is it possible to achieve the same functionality with this package?
The text was updated successfully, but these errors were encountered:
There is no field() analogy in this library, but you can still call any functions to initialize a property, and the value is going to be used if no custom value provided:
import{Data}from"dataclass";import{v4asuuidv4}from"uuid";classUserextendsData{id: string=uuidv4();}leta=User.create();// User { id: "<id generated from uuidv4() call>" }letb=User.create();// User { id: "<id generated from uuidv4() call>" }a.equals(b);// false, IDs are generated for each object separatelyletc=User.create({id: "custom"});// User { id: "custom" }
I hope this answer the question.
Worth mentioning that the function in property initializer going to be always called during create(), even if there is an explicit property value provided as an argument.
Hi,
in Python the
dataclass
type supports calling a factory function to retrieve the default value for a field. For more information you can see here. The parameter is nameddefault_factory
for theField
descriptor class which is used to declare extra information for each field.Is it possible to achieve the same functionality with this package?
The text was updated successfully, but these errors were encountered: