Skip to content

Commit 770174e

Browse files
committed
added: _ in all places where #auto was used
1 parent df8862f commit 770174e

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed

compiler/src/parser.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ static void parse_arguments(OnyxParser* parser, TokenType end_token, Arguments*
529529
}
530530
}
531531

532-
static b32 arg_is_placeholder(AstTyped *arg) {
532+
static b32 value_is_placeholder(AstTyped *arg) {
533533
if (arg->kind != Ast_Kind_Symbol) return 0;
534534
if (arg->token->length > 1) return 0;
535535
if (arg->token->text[0] != '_') return 0;
@@ -559,7 +559,7 @@ static AstCall* parse_function_call(OnyxParser *parser, AstTyped *callee) {
559559
bh_arr_each(AstTyped *, arg, call_node->args.values) {
560560
if ((*arg) == NULL) continue;
561561

562-
if (arg_is_placeholder(*arg)) {
562+
if (value_is_placeholder(*arg)) {
563563
if (call_node->placeholder_argument_position > 0) {
564564
onyx_report_error((*arg)->token->pos, Error_Critical, "Cannot have more than one placeholder argument ('_').");
565565
}
@@ -1763,6 +1763,11 @@ static i32 parse_possible_compound_symbol_declaration(OnyxParser* parser, AstNod
17631763
// See comment in parse_possible_symbol_declaration about "#auto"
17641764
if (!parse_possible_directive(parser, "auto")) {
17651765
type_for_all = parse_type(parser);
1766+
1767+
// Placeholders (_) are discarded and allow for type inference.
1768+
if (value_is_placeholder((AstTyped *) type_for_all)) {
1769+
type_for_all = NULL;
1770+
}
17661771
}
17671772

17681773
forll (AstLocal, local, first_local, next) {
@@ -1820,6 +1825,11 @@ static i32 parse_possible_symbol_declaration(OnyxParser* parser, AstNode** ret)
18201825
// typed on the first assignment.
18211826
} else {
18221827
type_node = parse_type(parser);
1828+
1829+
// Placeholders (_) are discarded and allow for type inference.
1830+
if (value_is_placeholder((AstTyped *) type_node)) {
1831+
type_node = NULL;
1832+
}
18231833
}
18241834
}
18251835

@@ -3264,6 +3274,10 @@ static AstFunction* parse_function_definition(OnyxParser* parser, OnyxToken* tok
32643274
func_def->return_type = (AstType *) &basic_type_auto_return;
32653275
} else {
32663276
func_def->return_type = parse_return_type(parser, &func_def->named_return_locals);
3277+
3278+
if (value_is_placeholder((AstTyped *) func_def->return_type)) {
3279+
func_def->return_type = (AstType *) &basic_type_auto_return;
3280+
}
32673281
}
32683282
}
32693283

examples/20_auto_return.onyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
main :: (args: [] cstr) {
77
// If you don't want to explicitly write the return type
88
// of a function, you can use #auto:
9-
f :: (x: i32) -> #auto {
9+
f :: (x: i32) -> _ {
1010
return x * 2;
1111
}
1212

@@ -27,7 +27,7 @@ main :: (args: [] cstr) {
2727
// come from? The only way to do this is use an auto-return
2828
// type and let the compiler fill it in.
2929

30-
consume :: (it: $T) -> #auto where iter.Iterable(T) {
30+
consume :: (it: $T) -> _ where iter.Iterable(T) {
3131
consume_inner :: macro (it: Iterator($V)) -> [] V {
3232
arr: [..] V;
3333
for v in it do arr << v;

examples/21_quick_functions.onyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// determine a return type with #auto, you can write a completely type free
44
// procedure like so:
55

6-
no_types :: (x: $__type_x, y: $__type_y) -> #auto {
6+
no_types :: (x: $__type_x, y: $__type_y) -> _ {
77
return x + ~~ y;
88
}
99

tests/new_auto_types

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
100
2+
i32
3+
123
4+
[] u8
5+
Allocator
6+
f64

tests/new_auto_types.onyx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use core {*}
2+
3+
main :: () {
4+
x: _
5+
x = 100
6+
println(x)
7+
println(typeof x)
8+
9+
func_with_auto_return() |> println()
10+
11+
a, b, c: _
12+
a = "Test"
13+
b = context.allocator
14+
c = 45.56
15+
println(typeof a)
16+
println(typeof b)
17+
println(typeof c)
18+
}
19+
20+
func_with_auto_return :: () -> _ {
21+
return 123
22+
}
23+
24+

0 commit comments

Comments
 (0)