Skip to content

Conversation

@chqrlie
Copy link
Contributor

@chqrlie chqrlie commented Jun 23, 2025

  • parse compound cast expressions: depending on surrounding code, explicit casts can be more readable than implicit conversions as function arguments or return values.
  • add tests

@bvdberg
Copy link
Member

bvdberg commented Jun 24, 2025

We had 'compound literals' before this commit right? Only without the cast. The type was inferred from the LHS. Adding the cast only seems to make the code longer, or am I missing something?

@chqrlie
Copy link
Contributor Author

chqrlie commented Jun 24, 2025

We had 'compound literals' before this commit right? Only without the cast. The type was inferred from the LHS. Adding the cast only seems to make the code longer, or am I missing something?

Yes, this commit implements compound literals:

  • it is much simpler as an extension to the cast operator.
  • the type cannot always be inferred from the LHS, eg: if the LHS is a pointer or if there is no LHS.
  • it is sometimes more readable to spell the type explicitly for the next casual reader of the code.

@bvdberg
Copy link
Member

bvdberg commented Jun 24, 2025

What would happen with?

Expr e = (SubExpr){ 1, 2, 3 };

@chqrlie
Copy link
Contributor Author

chqrlie commented Jun 24, 2025

What would happen with?

Expr e = (SubExpr){ 1, 2, 3 };

This would be a constraint violation, but if we have implicit upcasts, this would work:

Expr* e = &(SubExpr){ 1, 2, 3 };

And if we allow structures to implicitly decay to pointers like arrays, the & would be optional too.
Note that this works at the global scope too.

A more interesting example:

i32* p = (i32[]){ 1, 2, 3, 4, 5, 6 };
fn i32 sum(i32* p, usize count) { ... }
sum((i32[]){ 1, 2, 3, 4, 5, 6 }, 6);

Or this one for string conversions:

fn char* toBinary(u64 val, char* dest, usize len) { ... }

// no need to name the temporary char buffer used for the conversion
// it remains in scope for the duration of the current block, which is exactly what we need for this purpose.
printf("%d in binary: %s\n", val, toBinary(val, (char[65]){}, 65));

@chqrlie chqrlie force-pushed the cast branch 6 times, most recently from d7c864b to 9ac741f Compare July 2, 2025 09:44
@chqrlie chqrlie force-pushed the cast branch 4 times, most recently from 4bcb1ce to 6d5b83b Compare July 10, 2025 07:00
@chqrlie chqrlie force-pushed the cast branch 4 times, most recently from 586ba9c to e18038a Compare July 15, 2025 06:43
@chqrlie chqrlie force-pushed the cast branch 11 times, most recently from 0a6dba5 to 86e9cd8 Compare July 27, 2025 21:28
@chqrlie chqrlie force-pushed the cast branch 7 times, most recently from bd2d7fe to d7dd7e6 Compare November 9, 2025 08:51
@chqrlie chqrlie force-pushed the cast branch 3 times, most recently from 6152319 to 0dd9d89 Compare November 17, 2025 09:03
@chqrlie chqrlie force-pushed the cast branch 6 times, most recently from 250f813 to 9edab7f Compare November 25, 2025 07:45
@chqrlie chqrlie force-pushed the cast branch 3 times, most recently from dea0460 to a92ad5d Compare December 8, 2025 14:23
@chqrlie chqrlie force-pushed the cast branch 3 times, most recently from 8a9a1b9 to d33339a Compare December 16, 2025 14:20
@chqrlie chqrlie force-pushed the cast branch 2 times, most recently from 93be48c to 60106af Compare December 22, 2025 10:22
@chqrlie chqrlie force-pushed the cast branch 4 times, most recently from f7cec52 to fa8f704 Compare December 31, 2025 10:40
* parse compound cast expressions: depending on surrounding code, explicit
  casts can be more readable than implicit conversions as function arguments
  or return values.
* add tests.
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

Successfully merging this pull request may close these issues.

2 participants