From f16722596cf922db253e040f574a257169203149 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 2 Dec 2022 15:22:48 +0800 Subject: [PATCH] all: replace generic '<>' with '[]' in error messages and comments (#16571) --- cmd/tools/vast/vast.v | 4 +-- vlib/datatypes/doubly_linked_list.v | 6 ++-- vlib/datatypes/linked_list.v | 2 +- vlib/datatypes/set.v | 4 +-- vlib/os/os_test.v | 2 +- vlib/v/ast/table.v | 2 +- vlib/v/checker/check_types.v | 6 ++-- vlib/v/checker/checker.v | 4 +-- vlib/v/checker/containers.v | 16 +++++------ vlib/v/checker/fn.v | 28 +++++++++---------- vlib/v/checker/interface.v | 2 +- .../array_of_generic_struct_init_err.out | 6 ++-- .../tests/generic_closure_fn_decl_err.out | 2 +- vlib/v/checker/tests/generic_fn_decl_err.out | 10 +++---- ...eric_fn_decl_without_generic_names_err.out | 12 ++++---- ...neric_interface_missing_type_names_err.out | 2 +- .../tests/generic_sumtype_decl_err_a.out | 2 +- .../tests/generic_sumtype_decl_err_b.out | 2 +- .../tests/generics_fn_arg_type_err.out | 2 +- .../tests/generics_fn_called_no_arg_err.out | 2 +- .../generics_fn_return_generic_struct_err.out | 2 +- .../generics_interface_declaration_err.out | 2 +- .../generics_method_receiver_type_err.out | 2 +- .../tests/map_of_generic_struct_init_err.out | 6 ++-- vlib/v/gen/golang/golang.v | 2 +- vlib/v/parser/fn.v | 2 +- vlib/v/tests/fn_variadic_test.v | 2 +- vlib/v/tests/generics_test.v | 7 ++--- 28 files changed, 70 insertions(+), 71 deletions(-) diff --git a/cmd/tools/vast/vast.v b/cmd/tools/vast/vast.v index 588dec94c7..69c8dbd3a9 100644 --- a/cmd/tools/vast/vast.v +++ b/cmd/tools/vast/vast.v @@ -1986,7 +1986,7 @@ fn (t Tree) asm_io(node ast.AsmIO) &Node { } // do not support yet by vlang -// fn (t Tree) array_node1(nodes []T, method_name string) &Node { +// fn (t Tree) array_node1[T](nodes []T, method_name string) &Node { // mut arr := new_array() // // call method dynamically, V do not support yet @@ -2009,7 +2009,7 @@ fn (t Tree) asm_io(node ast.AsmIO) &Node { // } // do not support yet by vlang -// fn (t Tree) array_node2(nodes []T) &Node { +// fn (t Tree) array_node2[T](nodes []T) &Node { // mut arr := new_array() // for node in nodes { diff --git a/vlib/datatypes/doubly_linked_list.v b/vlib/datatypes/doubly_linked_list.v index 9ef046bd34..a4d88ba55f 100644 --- a/vlib/datatypes/doubly_linked_list.v +++ b/vlib/datatypes/doubly_linked_list.v @@ -7,7 +7,7 @@ mut: prev &DoublyListNode[T] = unsafe { 0 } } -// DoublyLinkedList represents a generic doubly linked list of elements, each of type T. +// DoublyLinkedList[T] represents a generic doubly linked list of elements, each of type T. pub struct DoublyLinkedList[T] { mut: head &DoublyListNode[T] = unsafe { 0 } @@ -298,7 +298,7 @@ pub fn (mut list DoublyLinkedList[T]) back_iterator() DoublyListIterBack[T] { } } -// DoublyListIter is an iterator for DoublyLinkedList. +// DoublyListIter[T] is an iterator for DoublyLinkedList. // It starts from *the start* and moves forwards to *the end* of the list. // It can be used with V's `for x in iter {` construct. // One list can have multiple independent iterators, pointing to different positions/places in the list. @@ -319,7 +319,7 @@ pub fn (mut iter DoublyListIter[T]) next() ?T { return res } -// DoublyListIterBack is an iterator for DoublyLinkedList. +// DoublyListIterBack[T] is an iterator for DoublyLinkedList. // It starts from *the end* and moves backwards to *the start* of the list. // It can be used with V's `for x in iter {` construct. // One list can have multiple independent iterators, pointing to different positions/places in the list. diff --git a/vlib/datatypes/linked_list.v b/vlib/datatypes/linked_list.v index e0eb17db17..98673c4aea 100644 --- a/vlib/datatypes/linked_list.v +++ b/vlib/datatypes/linked_list.v @@ -192,7 +192,7 @@ pub fn (mut list LinkedList[T]) iterator() ListIter[T] { } } -// ListIter is an iterator for LinkedList. +// ListIter[T] is an iterator for LinkedList. // It can be used with V's `for x in iter {` construct. // One list can have multiple independent iterators, pointing to different positions/places in the list. // An iterator instance always traverses the list from start to finish. diff --git a/vlib/datatypes/set.v b/vlib/datatypes/set.v index 9c6db927f4..6c189059d2 100644 --- a/vlib/datatypes/set.v +++ b/vlib/datatypes/set.v @@ -47,7 +47,7 @@ pub fn (mut set Set[T]) clear() { } // equal checks whether the two given sets are equal (i.e. contain all and only the same elements). -[deprecated: 'use set1 == set2 instead'] +[deprecated: 'use set1[T] == set2[T] instead'] pub fn (l Set[T]) equal(r Set[T]) bool { return l == r } @@ -115,7 +115,7 @@ pub fn (l Set[T]) intersection(r Set[T]) Set[T] { } // difference returns the difference of sets. -[deprecated: 'use set1 - set2 instead'] +[deprecated: 'use set1[T] - set2[T] instead'] pub fn (l Set[T]) difference(r Set[T]) Set[T] { return l - r } diff --git a/vlib/os/os_test.v b/vlib/os/os_test.v index 0b3037459b..667d499fc5 100644 --- a/vlib/os/os_test.v +++ b/vlib/os/os_test.v @@ -678,7 +678,7 @@ fn test_uname() { assert u.machine.len > 0 } -// tests for write_file_array and read_file_array: +// tests for write_file_array and read_file_array[T]: const maxn = 3 struct IntPoint { diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index e481e8cef6..8ddf297c00 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -2005,7 +2005,7 @@ pub fn (mut t Table) unwrap_generic_type(typ Type, generic_names []string, concr return typ } -// Foo{ bar: U } to Foo{ bar: T } +// Foo[U]{ bar: U } to Foo[T]{ bar: T } pub fn (mut t Table) replace_generic_type(typ Type, generic_types []Type) { mut ts := t.sym(typ) match mut ts.info { diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index d5239fdefc..28382251af 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -260,11 +260,11 @@ fn (mut c Checker) check_expected_call_arg(got ast.Type, expected_ ast.Type, lan expected_typ_sym := c.table.sym(expected_) // Check on Generics types, there are some case where we have the following case - // `&Type == &Type<>`. This is a common case we are implementing a function - // with generic parameters like `compare(bst Bst node) {}` + // `&Type[int] == &Type[]`. This is a common case we are implementing a function + // with generic parameters like `compare(bst Bst[T] node) {}` if got_typ_sym.symbol_name_except_generic() == expected_typ_sym.symbol_name_except_generic() { // Check if we are making a comparison between two different types of - // the same type like `Type and &Type<>` + // the same type like `Type[int] and &Type[]` if (got.is_ptr() != expected.is_ptr()) || !c.check_same_module(got, expected) || (!got.is_ptr() && !expected.is_ptr() diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index faa1ae4f40..81534a6848 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -497,11 +497,11 @@ fn (mut c Checker) sum_type_decl(node ast.SumTypeDecl) { } else if mut sym.info is ast.Struct { if sym.info.is_generic { if !variant.typ.has_flag(.generic) { - c.error('generic struct `${sym.name}` must specify generic type names, e.g. Foo', + c.error('generic struct `${sym.name}` must specify generic type names, e.g. Foo[T]', variant.pos) } if node.generic_types.len == 0 { - c.error('generic sumtype `${node.name}` must specify generic type names, e.g. Foo', + c.error('generic sumtype `${node.name}` must specify generic type names, e.g. Foo[T]', node.name_pos) } else { for typ in sym.info.generic_types { diff --git a/vlib/v/checker/containers.v b/vlib/v/checker/containers.v index bb00d2b34c..5aebb390cb 100644 --- a/vlib/v/checker/containers.v +++ b/vlib/v/checker/containers.v @@ -17,10 +17,10 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type { if elem_info.generic_types.len > 0 && elem_info.concrete_types.len == 0 && !node.elem_type.has_flag(.generic) { if c.table.cur_concrete_types.len == 0 { - c.error('generic struct must specify type parameter, e.g. Foo', + c.error('generic struct must specify type parameter, e.g. Foo[int]', node.elem_type_pos) } else { - c.error('generic struct must specify type parameter, e.g. Foo', + c.error('generic struct must specify type parameter, e.g. Foo[T]', node.elem_type_pos) } } @@ -29,10 +29,10 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type { if elem_info.generic_types.len > 0 && elem_info.concrete_types.len == 0 && !node.elem_type.has_flag(.generic) { if c.table.cur_concrete_types.len == 0 { - c.error('generic interface must specify type parameter, e.g. Foo', + c.error('generic interface must specify type parameter, e.g. Foo[int]', node.elem_type_pos) } else { - c.error('generic interface must specify type parameter, e.g. Foo', + c.error('generic interface must specify type parameter, e.g. Foo[T]', node.elem_type_pos) } } @@ -41,10 +41,10 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type { if elem_info.generic_types.len > 0 && elem_info.concrete_types.len == 0 && !node.elem_type.has_flag(.generic) { if c.table.cur_concrete_types.len == 0 { - c.error('generic sumtype must specify type parameter, e.g. Foo', + c.error('generic sumtype must specify type parameter, e.g. Foo[int]', node.elem_type_pos) } else { - c.error('generic sumtype must specify type parameter, e.g. Foo', + c.error('generic sumtype must specify type parameter, e.g. Foo[T]', node.elem_type_pos) } } @@ -307,10 +307,10 @@ fn (mut c Checker) map_init(mut node ast.MapInit) ast.Type { if val_info.generic_types.len > 0 && val_info.concrete_types.len == 0 && !info.value_type.has_flag(.generic) { if c.table.cur_concrete_types.len == 0 { - c.error('generic struct `${val_sym.name}` must specify type parameter, e.g. Foo', + c.error('generic struct `${val_sym.name}` must specify type parameter, e.g. Foo[int]', node.pos) } else { - c.error('generic struct `${val_sym.name}` must specify type parameter, e.g. Foo', + c.error('generic struct `${val_sym.name}` must specify type parameter, e.g. Foo[T]', node.pos) } } diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 31e090589a..863a2f752a 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -76,11 +76,11 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { } if need_generic_names { if node.is_method { - c.add_error_detail('use `fn (r SomeType) foo() {`, not just `fn (r SomeType) foo() {`') + c.add_error_detail('use `fn (r SomeType[T]) foo[T]() {`, not just `fn (r SomeType[T]) foo() {`') c.error('generic method declaration must specify generic type names', node.pos) } else { - c.add_error_detail('use `fn foo(x T) {`, not just `fn foo(x T) {`') + c.add_error_detail('use `fn foo[T](x T) {`, not just `fn foo(x T) {`') c.error('generic function declaration must specify generic type names', node.pos) } @@ -105,7 +105,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { gs := c.table.sym(node.return_type) if gs.info is ast.Struct { if gs.info.is_generic && !node.return_type.has_flag(.generic) { - c.error('return generic struct in fn declaration must specify the generic type names, e.g. Foo', + c.error('return generic struct in fn declaration must specify the generic type names, e.g. Foo[T]', node.return_type_pos) } } @@ -136,7 +136,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { for name in generic_names { if name !in node.generic_names { fn_generic_names := node.generic_names.join(', ') - c.error('generic type name `${name}` is not mentioned in fn `${node.name}<${fn_generic_names}>`', + c.error('generic type name `${name}` is not mentioned in fn `${node.name}[${fn_generic_names}]`', node.return_type_pos) } } @@ -224,19 +224,19 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { } if arg_typ_sym.info.generic_types.len > 0 && !param.typ.has_flag(.generic) && arg_typ_sym.info.concrete_types.len == 0 { - c.error('generic struct in fn declaration must specify the generic type names, e.g. Foo', + c.error('generic struct in fn declaration must specify the generic type names, e.g. Foo[T]', param.type_pos) } } else if arg_typ_sym.info is ast.Interface { if arg_typ_sym.info.generic_types.len > 0 && !param.typ.has_flag(.generic) && arg_typ_sym.info.concrete_types.len == 0 { - c.error('generic interface in fn declaration must specify the generic type names, e.g. Foo', + c.error('generic interface in fn declaration must specify the generic type names, e.g. Foo[T]', param.type_pos) } } else if arg_typ_sym.info is ast.SumType { if arg_typ_sym.info.generic_types.len > 0 && !param.typ.has_flag(.generic) && arg_typ_sym.info.concrete_types.len == 0 { - c.error('generic sumtype in fn declaration must specify the generic type names, e.g. Foo', + c.error('generic sumtype in fn declaration must specify the generic type names, e.g. Foo[T]', param.type_pos) } } @@ -247,7 +247,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { for name in generic_names { if name !in node.generic_names { fn_generic_names := node.generic_names.join(', ') - c.error('generic type name `${name}` is not mentioned in fn `${node.name}<${fn_generic_names}>`', + c.error('generic type name `${name}` is not mentioned in fn `${node.name}[${fn_generic_names}]`', param.type_pos) } } @@ -439,7 +439,7 @@ fn (mut c Checker) anon_fn(mut node ast.AnonFn) ast.Type { c.stmts(node.decl.stmts) c.fn_decl(mut node.decl) if has_generic && node.decl.generic_names.len == 0 { - c.error('generic closure fn must specify type parameter, e.g. fn [foo] ()', + c.error('generic closure fn must specify type parameter, e.g. fn [foo] [T]()', node.decl.pos) } return node.typ @@ -551,7 +551,7 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast. if fn_name == 'main' { c.error('the `main` function cannot be called in the program', node.pos) } - mut has_generic := false // foo() instead of foo() + mut has_generic := false // foo[T]() instead of foo[int]() mut concrete_types := []ast.Type{} node.concrete_types = node.raw_concrete_types for concrete_type in node.concrete_types { @@ -593,7 +593,7 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast. typ := c.expr(node.args[0].expr) tsym := c.table.sym(typ) - if !tsym.name.starts_with('Promise<') { + if !tsym.name.starts_with('Promise[') { c.error('JS.await: first argument must be a promise, got `${tsym.name}`', node.pos) return ast.void_type @@ -918,7 +918,7 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast. c.ensure_type_exists(concrete_type, node.concrete_list_pos) or {} } if func.generic_names.len > 0 && node.args.len == 0 && node.concrete_types.len == 0 { - c.error('no argument generic function must add concrete types, e.g. foo()', + c.error('no argument generic function must add concrete types, e.g. foo[int]()', node.pos) return func.return_type } @@ -1382,7 +1382,7 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type { } } else if final_left_sym.info is ast.Array && method_name in ['first', 'last', 'pop'] { return c.array_builtin_method_call(mut node, left_type, final_left_sym) - } else if c.pref.backend.is_js() && left_sym.name.starts_with('Promise<') + } else if c.pref.backend.is_js() && left_sym.name.starts_with('Promise[') && method_name == 'wait' { info := left_sym.info as ast.Struct if node.args.len > 0 { @@ -1455,7 +1455,7 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type { } } if has_method { - // x is Bar, x.foo() -> x.foo() + // x is Bar[T], x.foo() -> x.foo[T]() rec_sym := c.table.final_sym(node.left_type) rec_is_generic := left_type.has_flag(.generic) mut rec_concrete_types := []ast.Type{} diff --git a/vlib/v/checker/interface.v b/vlib/v/checker/interface.v index 4b84a16b47..871c29a93f 100644 --- a/vlib/v/checker/interface.v +++ b/vlib/v/checker/interface.v @@ -211,7 +211,7 @@ fn (mut c Checker) interface_decl(mut node ast.InterfaceDecl) { } } if node.generic_types.len == 0 && has_generic_types { - c.error('generic interface declaration must specify the generic type names, e.g. Foo', + c.error('generic interface declaration must specify the generic type names, e.g. Foo[T]', node.pos) } } diff --git a/vlib/v/checker/tests/array_of_generic_struct_init_err.out b/vlib/v/checker/tests/array_of_generic_struct_init_err.out index 83d4c00be4..ed9f2278b9 100644 --- a/vlib/v/checker/tests/array_of_generic_struct_init_err.out +++ b/vlib/v/checker/tests/array_of_generic_struct_init_err.out @@ -1,11 +1,11 @@ vlib/v/checker/tests/array_of_generic_struct_init_err.vv:6:6: warning: unused variable: `arr` - 4 | + 4 | 5 | fn main() { 6 | mut arr := []Item{} | ~~~ 7 | } -vlib/v/checker/tests/array_of_generic_struct_init_err.vv:6:15: error: generic struct must specify type parameter, e.g. Foo - 4 | +vlib/v/checker/tests/array_of_generic_struct_init_err.vv:6:15: error: generic struct must specify type parameter, e.g. Foo[int] + 4 | 5 | fn main() { 6 | mut arr := []Item{} | ~~~~ diff --git a/vlib/v/checker/tests/generic_closure_fn_decl_err.out b/vlib/v/checker/tests/generic_closure_fn_decl_err.out index 21de87ce62..8252196fb1 100644 --- a/vlib/v/checker/tests/generic_closure_fn_decl_err.out +++ b/vlib/v/checker/tests/generic_closure_fn_decl_err.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/generic_closure_fn_decl_err.vv:5:2: error: generic closure fn must specify type parameter, e.g. fn [foo] () +vlib/v/checker/tests/generic_closure_fn_decl_err.vv:5:2: error: generic closure fn must specify type parameter, e.g. fn [foo] [T]() 3 | 4 | pub fn (mut app App) register(service T) { 5 | fn [service] () { diff --git a/vlib/v/checker/tests/generic_fn_decl_err.out b/vlib/v/checker/tests/generic_fn_decl_err.out index d5f40c1347..bd331c0157 100644 --- a/vlib/v/checker/tests/generic_fn_decl_err.out +++ b/vlib/v/checker/tests/generic_fn_decl_err.out @@ -1,32 +1,32 @@ -vlib/v/checker/tests/generic_fn_decl_err.vv:19:29: error: generic type name `P` is not mentioned in fn `create1` +vlib/v/checker/tests/generic_fn_decl_err.vv:19:29: error: generic type name `P` is not mentioned in fn `create1[U]` 17 | } 18 | 19 | fn (r Db) create1(u U, p P) { | ^ 20 | println('Yo') 21 | } -vlib/v/checker/tests/generic_fn_decl_err.vv:23:29: error: generic type name `P` is not mentioned in fn `create2` +vlib/v/checker/tests/generic_fn_decl_err.vv:23:29: error: generic type name `P` is not mentioned in fn `create2[U]` 21 | } 22 | 23 | fn (r Db) create2(u U, p &P) { | ~~ 24 | println('Yo') 25 | } -vlib/v/checker/tests/generic_fn_decl_err.vv:27:29: error: generic type name `P` is not mentioned in fn `create3` +vlib/v/checker/tests/generic_fn_decl_err.vv:27:29: error: generic type name `P` is not mentioned in fn `create3[U]` 25 | } 26 | 27 | fn (r Db) create3(u U, p []P) { | ~~~ 28 | println('Yo') 29 | } -vlib/v/checker/tests/generic_fn_decl_err.vv:31:27: error: generic type name `P` is not mentioned in fn `create4` +vlib/v/checker/tests/generic_fn_decl_err.vv:31:27: error: generic type name `P` is not mentioned in fn `create4[U]` 29 | } 30 | 31 | fn (r Db) create4(u U) P { | ^ 32 | return P{} 33 | } -vlib/v/checker/tests/generic_fn_decl_err.vv:35:27: error: generic type name `P` is not mentioned in fn `create5` +vlib/v/checker/tests/generic_fn_decl_err.vv:35:27: error: generic type name `P` is not mentioned in fn `create5[U]` 33 | } 34 | 35 | fn (r Db) create5(u U) []P { diff --git a/vlib/v/checker/tests/generic_fn_decl_without_generic_names_err.out b/vlib/v/checker/tests/generic_fn_decl_without_generic_names_err.out index 4e5d935b9e..54010333e8 100644 --- a/vlib/v/checker/tests/generic_fn_decl_without_generic_names_err.out +++ b/vlib/v/checker/tests/generic_fn_decl_without_generic_names_err.out @@ -1,24 +1,24 @@ vlib/v/checker/tests/generic_fn_decl_without_generic_names_err.vv:26:1: error: generic function declaration must specify generic type names 24 | } - 25 | + 25 | 26 | fn g_worker(g Generic) { | ~~~~~~~~~~~~~~~~~~~~~~~~~ 27 | t := <-g.ch 28 | handle(t) -Details: use `fn foo(x T) {`, not just `fn foo(x T) {` +Details: use `fn foo[T](x T) {`, not just `fn foo(x T) {` vlib/v/checker/tests/generic_fn_decl_without_generic_names_err.vv:32:1: error: generic function declaration must specify generic type names 30 | } - 31 | + 31 | 32 | fn handle(t T) { | ~~~~~~~~~~~~~~ 33 | println('hi') 34 | } -Details: use `fn foo(x T) {`, not just `fn foo(x T) {` +Details: use `fn foo[T](x T) {`, not just `fn foo(x T) {` vlib/v/checker/tests/generic_fn_decl_without_generic_names_err.vv:40:1: error: generic method declaration must specify generic type names 38 | type MayBe = None | T - 39 | + 39 | 40 | fn (m MayBe) is_some() bool { | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 41 | return m is T 42 | } -Details: use `fn (r SomeType) foo() {`, not just `fn (r SomeType) foo() {` +Details: use `fn (r SomeType[T]) foo[T]() {`, not just `fn (r SomeType[T]) foo() {` diff --git a/vlib/v/checker/tests/generic_interface_missing_type_names_err.out b/vlib/v/checker/tests/generic_interface_missing_type_names_err.out index 629053ed41..2e7d169fbe 100644 --- a/vlib/v/checker/tests/generic_interface_missing_type_names_err.out +++ b/vlib/v/checker/tests/generic_interface_missing_type_names_err.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/generic_interface_missing_type_names_err.vv:16:17: error: generic interface must specify type parameter, e.g. Foo +vlib/v/checker/tests/generic_interface_missing_type_names_err.vv:16:17: error: generic interface must specify type parameter, e.g. Foo[int] 14 | 15 | fn main() { 16 | mut outs := []Output{} diff --git a/vlib/v/checker/tests/generic_sumtype_decl_err_a.out b/vlib/v/checker/tests/generic_sumtype_decl_err_a.out index 12fac1ed81..27b3d622d1 100644 --- a/vlib/v/checker/tests/generic_sumtype_decl_err_a.out +++ b/vlib/v/checker/tests/generic_sumtype_decl_err_a.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/generic_sumtype_decl_err_a.vv:7:24: error: generic struct `Just` must specify generic type names, e.g. Foo +vlib/v/checker/tests/generic_sumtype_decl_err_a.vv:7:24: error: generic struct `Just` must specify generic type names, e.g. Foo[T] 5 | struct Nothing {} 6 | 7 | type Maybe = Nothing | Just diff --git a/vlib/v/checker/tests/generic_sumtype_decl_err_b.out b/vlib/v/checker/tests/generic_sumtype_decl_err_b.out index 4920a990c7..169f8b1067 100644 --- a/vlib/v/checker/tests/generic_sumtype_decl_err_b.out +++ b/vlib/v/checker/tests/generic_sumtype_decl_err_b.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/generic_sumtype_decl_err_b.vv:7:6: error: generic sumtype `Maybe` must specify generic type names, e.g. Foo +vlib/v/checker/tests/generic_sumtype_decl_err_b.vv:7:6: error: generic sumtype `Maybe` must specify generic type names, e.g. Foo[T] 5 | struct Nothing {} 6 | 7 | type Maybe = Nothing | Just diff --git a/vlib/v/checker/tests/generics_fn_arg_type_err.out b/vlib/v/checker/tests/generics_fn_arg_type_err.out index b12cac6bee..a7d2efff15 100644 --- a/vlib/v/checker/tests/generics_fn_arg_type_err.out +++ b/vlib/v/checker/tests/generics_fn_arg_type_err.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/generics_fn_arg_type_err.vv:14:23: error: generic interface in fn declaration must specify the generic type names, e.g. Foo +vlib/v/checker/tests/generics_fn_arg_type_err.vv:14:23: error: generic interface in fn declaration must specify the generic type names, e.g. Foo[T] 12 | } 13 | 14 | fn do_list_thing(list List) { // <--- Error here diff --git a/vlib/v/checker/tests/generics_fn_called_no_arg_err.out b/vlib/v/checker/tests/generics_fn_called_no_arg_err.out index 42128086fc..4a7aa5478f 100644 --- a/vlib/v/checker/tests/generics_fn_called_no_arg_err.out +++ b/vlib/v/checker/tests/generics_fn_called_no_arg_err.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/generics_fn_called_no_arg_err.vv:13:10: error: no argument generic function must add concrete types, e.g. foo() +vlib/v/checker/tests/generics_fn_called_no_arg_err.vv:13:10: error: no argument generic function must add concrete types, e.g. foo[int]() 11 | 12 | fn main() { 13 | q := new_queue() diff --git a/vlib/v/checker/tests/generics_fn_return_generic_struct_err.out b/vlib/v/checker/tests/generics_fn_return_generic_struct_err.out index 3fcb8879da..2d1b4f8ed7 100644 --- a/vlib/v/checker/tests/generics_fn_return_generic_struct_err.out +++ b/vlib/v/checker/tests/generics_fn_return_generic_struct_err.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/generics_fn_return_generic_struct_err.vv:13:32: error: return generic struct in fn declaration must specify the generic type names, e.g. Foo +vlib/v/checker/tests/generics_fn_return_generic_struct_err.vv:13:32: error: return generic struct in fn declaration must specify the generic type names, e.g. Foo[T] 11 | } 12 | 13 | pub fn new_channel_struct[T]() GenericChannelStruct { diff --git a/vlib/v/checker/tests/generics_interface_declaration_err.out b/vlib/v/checker/tests/generics_interface_declaration_err.out index bcf5b5db0c..879277f795 100644 --- a/vlib/v/checker/tests/generics_interface_declaration_err.out +++ b/vlib/v/checker/tests/generics_interface_declaration_err.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/generics_interface_declaration_err.vv:1:1: error: generic interface declaration must specify the generic type names, e.g. Foo +vlib/v/checker/tests/generics_interface_declaration_err.vv:1:1: error: generic interface declaration must specify the generic type names, e.g. Foo[T] 1 | interface Expr { | ~~~~~~~~~~~~~~~~ 2 | accept(v Visitor) R diff --git a/vlib/v/checker/tests/generics_method_receiver_type_err.out b/vlib/v/checker/tests/generics_method_receiver_type_err.out index 94d6ec41c1..bd03e25e10 100644 --- a/vlib/v/checker/tests/generics_method_receiver_type_err.out +++ b/vlib/v/checker/tests/generics_method_receiver_type_err.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/generics_method_receiver_type_err.vv:6:11: error: generic struct in fn declaration must specify the generic type names, e.g. Foo +vlib/v/checker/tests/generics_method_receiver_type_err.vv:6:11: error: generic struct in fn declaration must specify the generic type names, e.g. Foo[T] 4 | } 5 | 6 | pub fn (x Node) str() string { diff --git a/vlib/v/checker/tests/map_of_generic_struct_init_err.out b/vlib/v/checker/tests/map_of_generic_struct_init_err.out index bbcafb9f46..5ec3cc25ad 100644 --- a/vlib/v/checker/tests/map_of_generic_struct_init_err.out +++ b/vlib/v/checker/tests/map_of_generic_struct_init_err.out @@ -1,11 +1,11 @@ vlib/v/checker/tests/map_of_generic_struct_init_err.vv:6:6: warning: unused variable: `m` - 4 | + 4 | 5 | fn main() { 6 | mut m := map[string]Item{} | ^ 7 | } -vlib/v/checker/tests/map_of_generic_struct_init_err.vv:6:11: error: generic struct `Item` must specify type parameter, e.g. Foo - 4 | +vlib/v/checker/tests/map_of_generic_struct_init_err.vv:6:11: error: generic struct `Item` must specify type parameter, e.g. Foo[int] + 4 | 5 | fn main() { 6 | mut m := map[string]Item{} | ~~~~~~~~~~~~~~~~~ diff --git a/vlib/v/gen/golang/golang.v b/vlib/v/gen/golang/golang.v index a36c4e6621..feaeb59d15 100644 --- a/vlib/v/gen/golang/golang.v +++ b/vlib/v/gen/golang/golang.v @@ -251,7 +251,7 @@ pub fn (mut f Gen) mark_types_import_as_used(typ ast.Type) { f.mark_types_import_as_used(concrete_typ) } } - name := sym.name.split('<')[0] // take `Type` from `Type` + name := sym.name.split('[')[0] // take `Type` from `Type[T]` f.mark_import_as_used(name) } diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index da7760b5e3..53ba9c2da7 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -333,7 +333,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl { scope: 0 } } - // + // [T] _, mut generic_names := p.parse_generic_types() // generic names can be infer with receiver's generic names if is_method && rec.typ.has_flag(.generic) { diff --git a/vlib/v/tests/fn_variadic_test.v b/vlib/v/tests/fn_variadic_test.v index ea95e4f44d..38b0030665 100644 --- a/vlib/v/tests/fn_variadic_test.v +++ b/vlib/v/tests/fn_variadic_test.v @@ -22,7 +22,7 @@ fn test_fn_variadic() { /* // QTODO // generic -fn variadic_test_generic(a int, b ...T) T { +fn variadic_test_generic[T](a int, b ...T) T { b1 := b[0] b2 := b[1] return '$a $b1 $b2' diff --git a/vlib/v/tests/generics_test.v b/vlib/v/tests/generics_test.v index 00b07d5f98..6df92bc4c3 100644 --- a/vlib/v/tests/generics_test.v +++ b/vlib/v/tests/generics_test.v @@ -202,7 +202,7 @@ fn map_f[T, U](l []T, f fn (T) U) []U { } /* -fn foldl(l []T, nil T, f fn(T,T)T) T { +fn foldl[T](l []T, nil T, f fn(T,T)T) T { mut r := nil for e in l { r = f(r, e) @@ -302,8 +302,8 @@ pub mut: } // TODO: multiple type generic struct needs fixing in return for fn -// fn new_repo(db DB) Repo { -// return Repo{db: db} +// fn new_repo[T](db DB) Repo[T,U] { +// return Repo[T,Permission]{db: db} // } fn test_generic_struct() { mut a := Repo[User, Permission]{ @@ -474,7 +474,6 @@ fn test_generic_init() { } fn return_one[T](rec int, useless T) T { - // foo < bar() should work if rec == 0 || 0 < return_one[T](rec - 1, useless) { return T(1) }