mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
generics: generic methods, cast to T
This commit is contained in:

committed by
Alexander Medvednikov

parent
7d02eccbce
commit
280c7d396c
@ -2,15 +2,15 @@ fn simple<T>(p T) T {
|
||||
return p
|
||||
}
|
||||
|
||||
fn sum<T>(l []T, nil T) T {
|
||||
mut r := nil
|
||||
fn sum<T>(l []T) T {
|
||||
mut r := T(0)
|
||||
for e in l {
|
||||
r += e
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
fn map_f<T,U,R>(l []T, f fn(T)U) []U {
|
||||
fn map_f<T,U>(l []T, f fn(T)U) []U {
|
||||
mut r := []U
|
||||
for e in l {
|
||||
r << f(e)
|
||||
@ -44,15 +44,39 @@ fn assert_eq<T>(a, b T) {
|
||||
assert r
|
||||
}
|
||||
|
||||
fn print_nice<T>(x T, indent int) {
|
||||
mut space := ''
|
||||
for i in 0..indent {
|
||||
space = space + ' '
|
||||
}
|
||||
println('$space$x')
|
||||
}
|
||||
|
||||
fn test_generic_fn() {
|
||||
assert_eq(simple(0+1), 1)
|
||||
assert_eq(simple('g') + 'h', 'gh')
|
||||
assert_eq(sum([5.1,6.2,7.0], 0.0), 18.3)
|
||||
assert_eq(sum([5.1,6.2,7.0]), 18.3)
|
||||
assert_eq(plus(i64(4), i64(6)), i64(10))
|
||||
a := [1,2,3,4]
|
||||
$if !windows {
|
||||
b := map_f(a, square)
|
||||
assert_eq(sum(b, 0), 30) // 1+4+9+16 = 30
|
||||
assert_eq(foldl(b, 1, mul_int), 576) // 1*4*9*16 = 576
|
||||
}
|
||||
b := map_f(a, square)
|
||||
assert_eq(sum(b), 30) // 1+4+9+16 = 30
|
||||
assert_eq(foldl(b, 1, mul_int), 576) // 1*4*9*16 = 576
|
||||
print_nice('str', 8)
|
||||
}
|
||||
|
||||
struct Point {
|
||||
mut:
|
||||
x f64
|
||||
y f64
|
||||
}
|
||||
|
||||
fn (p mut Point) translate<T>(x, y T) {
|
||||
p.x += x
|
||||
p.y += y
|
||||
}
|
||||
|
||||
fn test_generic_method() {
|
||||
mut p := Point{}
|
||||
p.translate(2, 1.0)
|
||||
assert p.x == 2.0 && p.y == 1.0
|
||||
}
|
||||
|
Reference in New Issue
Block a user