Skip to content

checker, parser, cgen: allow static call on generic type - #21071

Merged
spytheman merged 2 commits into
vlang:masterfrom
felipensp:resolve_static_generic_call
Mar 22, 2024
Merged

spytheman merged 2 commits into
vlang:masterfrom
felipensp:resolve_static_generic_call

Conversation

@felipensp

Copy link
Copy Markdown
Member

Fix #21056

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(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())
}

@blackshirt

blackshirt commented Mar 22, 2024 •

Copy link
Copy Markdown
Contributor

is it has been merged ? ..thanks @felipensp

@felipensp
felipensp marked this pull request as ready for review March 22, 2024 11:10

@spytheman spytheman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work.

@spytheman
spytheman merged commit 29e5124 into vlang:master Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Static method on generic type is not supported

3 participants