Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static method on generic type is not supported #21056

Closed
blackshirt opened this issue Mar 18, 2024 · 0 comments · Fixed by #21071
Closed

Static method on generic type is not supported #21056

blackshirt opened this issue Mar 18, 2024 · 0 comments · Fixed by #21071
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Unit: Parser Bugs/feature requests, that are related to the V parser or syntax (*not* semantic ones).

Comments

@blackshirt
Copy link
Contributor

blackshirt commented Mar 18, 2024

Describe the bug

This bug/missing feautures is already discussed on discord before this submitted, see missing feature of support static method on generic type thanks @spytheman

Code: https://vosca.dev/p/db03a26fcd

module main 

type Tag = int
type Octet = string

fn (o Octet) tag() Tag {
	return Tag(1)
}

fn (o Octet) pack() ![]u8 {
	return o.bytes()
}

fn (o Octet) bytes() []u8 {
	s := string(o)
	return s.bytes()
}

// static method on concrete type 
fn Octet.unpack(b []u8) Octet {
	return Octet(b.bytestr())
}

// This is generic type 
struct Elm[T] {
mut:
	val T
}

fn Elm.new[T](val T) Elm[T] {
	return Elm[T]{
		val: val
	}
}

fn (el Elm[T]) the_t() T {
	return el.val
}

//  static method on generic type, this not working
fn Elm.unpack[T](src []u8) !Elm[T] {
	t := T.unpack[T](src)
	return Elm[T]{t}
}

fn (el Elm[T]) tag() Tag {
	return el.val.tag()
}

fn (el Elm[T]) pack() ![]u8 {
	return el.val.pack()!
}

fn main() {
	b := Octet('xx')
	mut el := Elm.new[Octet](b)
	dump(el)
	bytes := el.pack()!
	dump(bytes)

	// unpack
	ab := Elm.unpack[Octet](bytes)!
	dump(ab)
	dump(el == ab)
	
	dump(el.the_t())
}

Reproduction Steps

Use above snippet to reproduce the results

Expected Behavior

Define static method on generic type should be supported.

Current Behavior

Output:

code.v:37:7: error: unknown function: T.unpack
   35 | 
   36 | fn Elm.unpack[T](src []u8) !Elm[T] {
   37 |     t := T.unpack[T](src)
      |          ~~~~~~~~~~~~~~~~
   38 |     return Elm[T]{t}
   39 | }
code.v:37:4: error: assignment mismatch: 1 variable(s) but `T.unpack()` returns 0 value(s)
   35 | 
   36 | fn Elm.unpack[T](src []u8) !Elm[T] {
   37 |     t := T.unpack[T](src)
      |       ~~
   38 |     return Elm[T]{t}
   39 | }
code.v:38:16: error: `t` (no value) used as value
   36 | fn Elm.unpack[T](src []u8) !Elm[T] {
   37 |     t := T.unpack[T](src)
   38 |     return Elm[T]{t}
      |                   ^
   39 | }
   40 |
If the code of your project is in multiple files, try with `v .` instead of `v code.v`
Exited with error status 1

Possible Solution

No response

Additional Information/Context

when its changed to instance based method,

// changes static to instance method
fn (mut o Octet) unpack(b []u8) Octet {
    return Octet(b.bytestr())
}

// // changes static to instance method
fn (mut el Elm[T]) unpack[T](src []u8) !Elm[T] {
    t := el.val.unpack(src)
    out := Elm.new[T](t)
    return out
}

its produces expected behavour

[code.v:49] el: Elm[Octet]{
    val:     Octet(xx)
}
[code.v:51] bytes: [120, 120]
[code.v:55] ab: Elm[Octet]{
    val:     Octet(xx)
}

### V version

V 0.4.3 c3cf9ee.cc220e6

### Environment details (OS name and version, etc.)

V full version: V 0.4.4 c3cf9ee.a36c693
OS: linux, Ubuntu 22.04.2 LTS
Processor: 2 cpus, 64bit, little endian, Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz

getwd: /home/pmakhnev/playground
vexe: /home/pmakhnev/v/v
vexe mtime: 2024-03-18 12:00:08

vroot: OK, value: /home/pmakhnev/v
VMODULES: OK, value: /root/.vmodules
VTMP: OK, value: /tmp/v_0

Git version: git version 2.34.1
Git vroot status: weekly.2024.11-45-ga36c693f
.git/config present: true

CC version: cc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3

> [!NOTE]
> You can use the 👍 reaction to increase the issue's priority for developers.
>
> Please note that only the 👍 reaction to the issue itself counts as a vote.
> Other reactions and those to comments will not be taken into account.
@blackshirt blackshirt added the Bug This tag is applied to issues which reports bugs. label Mar 18, 2024
@felipensp felipensp self-assigned this Mar 21, 2024
@felipensp felipensp added Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Unit: Parser Bugs/feature requests, that are related to the V parser or syntax (*not* semantic ones). Unit: cgen Bugs/feature requests, that are related to the default C generating backend. labels Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Unit: Parser Bugs/feature requests, that are related to the V parser or syntax (*not* semantic ones).
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants