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

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

Merged
merged 2 commits into from
Mar 22, 2024

Conversation

felipensp
Copy link
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
Copy link
Contributor

blackshirt commented Mar 22, 2024

is it has been merged ? ..thanks @felipensp

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

@spytheman spytheman left a comment

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
54 checks passed
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