checker: type inference over a generic type should compile

This commit introduce the code to produce the error reported in the issue

In fact the error that is produced is the following one

```
➜  v git:(master) ./v vlib/v/checker/tests/generic_type_inference.vv
vlib/v/checker/tests/generic_type_inference.vv:19:13: error: inferred generic type `T` is ambiguous: got `KeyVal<int>`, expected `int`
   17 | fn main() {
   18 |     mut bst := datatypes.BSTree<KeyVal<int>>{}
   19 |     bst.insert(KeyVal<int>{key: "alibaba", val: 12})
      |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   20 |     println(bst.in_order_traversals())
   21 | }
vlib/v/checker/tests/generic_type_inference.vv:20:14: error: unknown method or field: `datatypes.BSTree<KeyVal<int>>.in_order_traversals`
   18 |     mut bst := datatypes.BSTree<KeyVal<int>>{}
   19 |     bst.insert(KeyVal<int>{key: "alibaba", val: 12})
   20 |     println(bst.in_order_traversals())
      |                 ~~~~~~~~~~~~~~~~~~~~~
   21 | }
vlib/datatypes/bstree.v:84:17: error: cannot use `>` as `<=` operator method is not defined
   82 |         node.right = new_node(node, value)
   83 |         return true
   84 |     } else if node.value > value {
      |                    ~~~~~~~~~~~~~
   85 |         if node.left != 0 && node.left.is_init {
   86 |             return bst.insert_helper(mut node.left, value)
vlib/datatypes/bstree.v:107:17: error: cannot use `>` as `<=` operator method is not defined
  105 |     if node.value < value {
  106 |         return bst.contains_helper(node.right, value)
  107 |     } else if node.value > value {
      |                    ~~~~~~~~~~~~~
  108 |         return bst.contains_helper(node.left, value)
  109 |     }
```
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
pull/13824/head
Vincenzo Palazzo 2022-03-31 18:47:07 +02:00
parent 9c1981a309
commit 8d63db9731
No known key found for this signature in database
GPG Key ID: 8B6DC2B870B80D5F
1 changed files with 21 additions and 0 deletions

View File

@ -0,0 +1,21 @@
import datatypes
struct KeyVal<T> {
mut:
key string
val T
}
fn (a KeyVal<T>) == (b KeyVal<T>) bool {
return a.key == b.key
}
fn (a KeyVal<T>) < (b KeyVal<T>) bool {
return a.key < b.key
}
fn main() {
mut bst := datatypes.BSTree<KeyVal<int>>{}
bst.insert(KeyVal<int>{key: "alibaba", val: 12})
println(bst.in_order_traversal())
}