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

Allowing optionals in @operator(construct) #1858

Open
alexveden opened this issue Jan 17, 2025 · 2 comments
Open

Allowing optionals in @operator(construct) #1858

alexveden opened this issue Jan 17, 2025 · 2 comments
Assignees
Labels
Enhancement Request New feature or request

Comments

@alexveden
Copy link
Contributor

I'm fiddling with new @operator(construct), and it rejects to return optionals.

fn AstProject! AstProject.new_init(String proj_path, String stdlib_path) @operator(construct) {
    AstProject self;

}
>c3c 
 Error: The return type of a 'construct' method must be the method type, or a pointer to it. In this case 'AstProject' or 'AstProject*' was expected.

I think this makes no sense, because these are 2 equivalent initialization methods:

// current
AstProject proj;
proj.new_init(dir_name, "")!;

// the same effect with AstProject! return type 
AstProject proj = AstProject.new_init(dir_name, "")!;
@lerno
Copy link
Collaborator

lerno commented Jan 17, 2025

This is deliberate. I understand it makes little sense in this case, but maybe we should just not have this @operator(construct). I deliberately didn't want: Url.parse(...) which would return an optional Url. For something like that url::parse is really correct.

Why is it optional in your case?

@lerno lerno added the Enhancement Request New feature or request label Jan 17, 2025
@lerno lerno self-assigned this Jan 17, 2025
@alexveden
Copy link
Contributor Author

Why is it optional in your case?

Maybe I wanted to do stuff which is error-prone:

  1. path doesn't exist
  2. parsing errors
  3. something wrong with parameter validation?

Absence of optionals makes me to add another initialization method, let's call it load(). So the "constructor" will always succeed then, and initialization validation should be done with another method.

I don't have a strong opinion on this, and performance penalties for using optionals in constructors are also unclear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants