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

Why the builder generate nullable types fields for not nullable type fields? #47

Closed
elect86 opened this issue Nov 11, 2023 · 6 comments
Closed

Comments

@elect86
Copy link

elect86 commented Nov 11, 2023

First of all, super cool project, this is something I wanted to write myself, but I didn't have enough skills for that :p

Anyway, as titled, why AutoDsl generated for class Person(val name: String) a builder with the name field as String? instead of String?

@F43nd1r
Copy link
Owner

F43nd1r commented Nov 11, 2023

When the builder is created, no value for name is known. The builder field is initially null and that is reflected in the type.

@elect86
Copy link
Author

elect86 commented Nov 11, 2023

What about using lateinit?

@F43nd1r
Copy link
Owner

F43nd1r commented Nov 11, 2023

I guess that is a question of style. In my opinion public lateinit fields just make it easier to write code that breaks

@elect86
Copy link
Author

elect86 commented Nov 11, 2023

I guess that is a question of style.

It could be. But on the other side, it's coherent with the underlying object to build. Also, since the field is mandatory, it does fit perfectly the logic of lateinit: the field is not supposed to be (left) null, but initialized (at least once)

(and it still can be checked with ::name.isInitialized)

@F43nd1r
Copy link
Owner

F43nd1r commented Nov 11, 2023

I don't see value in that, and it makes problems in implementation, e.g. primitive values can't be lateinit.

Do you have an example where this would improve something beyond personal preference?

@elect86
Copy link
Author

elect86 commented Nov 11, 2023

Primitives shall be initialized to their default value, as in Java, 0

This looks simply weird, it caught my eye at the very first example I was experimenting with

@elect86 elect86 closed this as completed Nov 12, 2023
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