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

Easy syntax for variable construction in expression context #11

Open
Panzerschrek opened this issue Oct 7, 2023 · 4 comments
Open

Easy syntax for variable construction in expression context #11

Panzerschrek opened this issue Oct 7, 2023 · 4 comments

Comments

@Panzerschrek
Copy link
Owner

Right now it is possible to construct temp variable in expression context by using type name and () operator (for constructor call). It works fine, but works only for cases where constructor initializer is applicable - for fundamental types, classes, for copying of arrays/tuples.
It might be good to introduce some syntax for construction of temp variable with arbitrary initializer - sequence initializer (arrays, tuples), struct named initializer, zero initializer uninitialized initializer.

For structs it may my possible to allow struct named initializer as expression postfix operator. For now introducing {} in expression context doesn't break something.

Sequence initializer "as is" is not possible to use in expression context. It starts with [ - exactly like indexation operator.

Alternative way to simplify temp variables construction for arrays and tuples is to introduce some special operators for this. Right now there are make_array and make_tuple standard library functions. They work somehow, but it may be better to implement same functionality inside language core - at least compilation may be faster.

Yet another way - to add some proxy syntax operator. Like some lexem or sequence of lexems that triggers parsing of initializer. For example (with @):

auto some_var = MyStruct @ { .x= 0, .y= 42 };
auto another_var= [ i32, 4 ] @ [ 5, 55, 555, 5555 ];
auto trash= unsafe( MyStruct @ uninitialized );
auto zeros = tup[ f32, i32 ] @ zero_init;
@Panzerschrek
Copy link
Owner Author

Approach with make_array and make_tuple as separate operators seems to be interesting. It doesn't require type name specifying - exact type will be evaluated based on actual argument count and types.

@Panzerschrek
Copy link
Owner Author

It may be possible to always parse postfix [ as sequence initializer. Later in CodeBuilder its processing will depend on kind of name before [. If it is variable - process it as indexation operator. If it is type name - process it as sequence initializer.

@Panzerschrek
Copy link
Owner Author

Suggestions for initialization invocation syntax:

  • -> - it's now used only in switch operator branches. [ i32, 4 ] -> [ 5, 55, 555, 5555 ].
  • @ - is now used only in reference notation for functions. It's possible to use it for initializer invocation, but it seems to be confusing.
  • Some keyword, like init. [ i32, 4 ] init [ 5, 55, 555, 5555 ]. It looks nice only with syntax highlighting enabled.
  • # isn't used by the language at all, so, it's possible to start using it for initialization. But again, it may be confused, because in other languages it is used for preprocessor detectives or comments.

It's better to avoid lexems like => , because it may be confused with >= and <=> - which both mean comparison.

@Panzerschrek
Copy link
Owner Author

4dc6272

Now it's possible to create structs in expression context with {} initializer.

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

1 participant