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 3 times, most recently from 88fc759 to 97c931f Compare October 4, 2025 21:22
@chqrlie chqrlie force-pushed the cast branch 5 times, most recently from d02610d to 69f8f60 Compare October 12, 2025 11:02
@chqrlie chqrlie force-pushed the cast branch 6 times, most recently from 41db61a to f60f1ac Compare October 20, 2025 20:57
@chqrlie chqrlie force-pushed the cast branch 3 times, most recently from 2acbfbc to 4bdd8f7 Compare November 5, 2025 07:57
@chqrlie chqrlie force-pushed the cast branch 6 times, most recently from d7dd7e6 to 91f4bde Compare November 10, 2025 11:08
@chqrlie chqrlie force-pushed the cast branch 5 times, most recently from 04b4c8c to 5cfa267 Compare November 22, 2025 15:05
* 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