Skip to content
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

[Feature] Getrid of the postinit and use init for initialization #1411

Open
ThakeeNathees opened this issue Oct 28, 2024 · 0 comments
Open
Assignees

Comments

@ThakeeNathees
Copy link
Collaborator

The goal of the feature request

  • 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.

obj Foo {
  has var1: int = 1;
  has var2: int = 2;
  can init() {} # <-- 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
obj Foo {
  has var1: int; # <-- We'll initialize it with the init method.
  has var2: int = 42; # <-- has default initialization.

  can init() {
    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.
obj Foo {
  has var1;  # <-- no need to mark as by init, cause we can detect with semantic analysis in pass.
  has var2; # <-- Syntax error because var2 was never initialized.
  has var3 = 42;

  can init() {
    self.var1 = 123;
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants