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
End user shouldn't use postinit but the init should work like in other language constructor (java, c++, c#, go, etc), and we get rid of the need of postinit and the keyword postinit itself.
Describe the feature you'd like
The problem with the bellow code is that the init method overrides the default __init__ of the python and those var1, var2 variables won't be set.
This is because obj are compiled into dataclass in python and the way to initialize them is to use __post_init__ method (in python) thus we have the keyword and method postinit and to initialize a variable in that method we should mark it as by postinit.
objFoo {
hasvar1: int=1;
hasvar2: int=2;
caninit() {} # <-- This will override the default init and not set the above variables.
}
Because of this the end user should aware of the implementation details that the obj are basically dataclasses
Which (dataclasses) I personally not familiar with (even though I'm using python for 5+ years) and the __post_init__ method is completely new to me.
You define attributes with has and initialize some of them in the init method, this is the simple control flow in every language out there and the fact post init exists will confuse a user.
obj Foo {
has var1: int; # <-- should be initialized by the constructor parameter.
has var2: int = 42; # <-- has default initialization.
}
In the above it works as expected when we create an instance of foo we invoke it like Foo(123) however consider the following one
objFoo {
hasvar1: int; # <-- We'll initialize it with the init method.hasvar2: int=42; # <-- has default initialization.caninit() {
self.var1=123;
}
}
Now since the init method will override the initialization of the var2, the instance will only have var1.
Even thought we have postinit to solve this issue, it's too much for a simple problem.
The solution
We'll make the init method of an obj compiled to __post_init__
We get rid of by postinit syntax and use symantic analysis to check if all attributes without default initialization will be initialized in the init method.
objFoo {
hasvar1; # <-- no need to mark as by init, cause we can detect with semantic analysis in pass.hasvar2; # <-- Syntax error because var2 was never initialized.hasvar3=42;
caninit() {
self.var1=123;
}
}
The text was updated successfully, but these errors were encountered:
The goal of the feature request
postinit
but the init should work like in other language constructor (java, c++, c#, go, etc), and we get rid of the need ofpostinit
and the keywordpostinit
itself.Describe the feature you'd like
The problem with the bellow code is that the
init
method overrides the default__init__
of the python and thosevar1
,var2
variables won't be set.This is because
obj
are compiled intodataclass
in python and the way to initialize them is to use__post_init__
method (in python) thus we have the keyword and methodpostinit
and to initialize a variable in that method we should mark it asby postinit
.Because of this the end user should aware of the implementation details that the
obj
are basically dataclassesWhich (dataclasses) I personally not familiar with (even though I'm using python for 5+ years) and the
__post_init__
method is completely new to me.You define attributes with
has
and initialize some of them in theinit
method, this is the simple control flow in every language out there and the fact post init exists will confuse a user.Foo(123)
however consider the following oneThe solution
obj
compiled to__post_init__
by postinit
syntax and use symantic analysis to check if all attributes without default initialization will be initialized in the init method.The text was updated successfully, but these errors were encountered: