1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

table: fix interface embedding with interface parameter (#10567)

This commit is contained in:
yuyi
2021-06-25 17:52:25 +08:00
committed by GitHub
parent e648578f79
commit e9de30373f
2 changed files with 48 additions and 0 deletions

View File

@@ -116,6 +116,11 @@ pub mut:
pub fn (f Fn) new_method_with_receiver_type(new_type Type) Fn {
mut new_method := f
new_method.params = f.params.clone()
for i in 1 .. new_method.params.len {
if new_method.params[i].typ == new_method.params[0].typ {
new_method.params[i].typ = new_type
}
}
new_method.params[0].typ = new_type
return new_method
}
@@ -123,6 +128,11 @@ pub fn (f Fn) new_method_with_receiver_type(new_type Type) Fn {
pub fn (f FnDecl) new_method_with_receiver_type(new_type Type) FnDecl {
mut new_method := f
new_method.params = f.params.clone()
for i in 1 .. new_method.params.len {
if new_method.params[i].typ == new_method.params[0].typ {
new_method.params[i].typ = new_type
}
}
new_method.params[0].typ = new_type
return new_method
}

View File

@@ -0,0 +1,38 @@
interface Eq {
eq(other Eq) bool
}
interface Ord {
Eq
lt(other Ord) bool
}
// implement Ord for Int
struct Int {
value int
}
fn (i Int) eq(other Ord) bool {
if other is Int {
return i.value == other.value
}
return false
}
fn (i Int) lt(other Ord) bool {
if other is Int {
return i.value < other.value
}
return false
}
fn compare(x Ord, y Ord) {
println(x.eq(y))
assert !x.eq(y)
println(x.lt(y))
assert x.lt(y)
}
fn test_interface_embedding_with_interface_para() {
compare(Int{1}, Int{2})
}