V: Generic struct methods

Created on 22 Dec 2019  Â·  5Comments  Â·  Source: vlang/v

V version: V 0.1.23 4dc4f12
OS: Windows 10

What did you do?

struct Test<T, U> {
    a T
    b U
}

pub fn (t Test) str() string {
    return t.a.str() + " : " + t.b.str()
}

fn main() {}

What did you expect to see?
nothing.

What did you see instead?

test.v:7:11: type `Test` has no field or method `a`
    5|
    6| pub fn (t Test) str() string {
    7|  return t.a.str() + " : " + t.b.str()
                   ^
    8| }
    9|
Bug

Most helpful comment

struct Test<T, U> {
    t T
    u U
}
pub fn (a Test<T, U>) str() string {
    return "Test"
}
fn main() {}

test.v:5:21: unknown receiver type `Test_T`

All 5 comments

Affects every fn (generic struct) name().

fn(generic struct\structname_T

under playground:
syntax error: unexpected <, expecting {
1| struct Test {

pub fn (t Test) str() string {
return t.a.str() + " : " + t.b.str()
}

As the docs say, it should be pub fn (t Test<T, U>).

struct Test<T, U> {
    t T
    u U
}
pub fn (a Test<T, U>) str() string {
    return "Test"
}
fn main() {}

test.v:5:21: unknown receiver type `Test_T`

The error is now (V 0.1.27 c9f3a05):
test2.v:1:8: error: Test lacks body
1 | struct Test {
| ~~~~
2 | t T
3 | u U

Was this page helpful?
0 / 5 - 0 ratings

Related issues

choleraehyq picture choleraehyq  Â·  3Comments

penguindark picture penguindark  Â·  3Comments

markgraydev picture markgraydev  Â·  3Comments

radare picture radare  Â·  3Comments

taojy123 picture taojy123  Â·  3Comments