Skip to content

cgen: fix reference variable str() method call (fix #19430) - #21753

Merged
spytheman merged 1 commit into
vlang:masterfrom
yuyi98:fix_ref_str
Jun 28, 2024
Merged

spytheman merged 1 commit into
vlang:masterfrom
yuyi98:fix_ref_str

Conversation

@yuyi98

@yuyi98 yuyi98 commented Jun 28, 2024 •

Copy link
Copy Markdown
Member

This PR fix reference variable str() method call (fix #19430).

  • Fix reference variable str() method call.
  • Add test.
@[heap]
struct Foo {
	bar string = 'bar'
	baz string = 'baz'
}

fn (foo Foo) str() string {
	return 'bar: ${foo.bar}, baz: ${foo.baz}'
}

fn main() {
	mut many_foos := []&Foo{len: 3, init: &Foo{}}
	println(many_foos.map(it.str()).join('\n'))
	println(many_foos)
	assert many_foos.map(it.str()) == ['&bar: bar, baz: baz', '&bar: bar, baz: baz',
		'&bar: bar, baz: baz']
}

PS D:\Test\v\tt1> v run .
&bar: bar, baz: baz
&bar: bar, baz: baz
&bar: bar, baz: baz
[&bar: bar, baz: baz, &bar: bar, baz: baz, &bar: bar, baz: baz]

@Delta456

Copy link
Copy Markdown
Member

I believe the OP links to the incorrect issue

@yuyi98

yuyi98 commented Jun 28, 2024

Copy link
Copy Markdown
Member Author

@Delta456 Thanks! My mistake.

@yuyi98 yuyi98 changed the title cgen: fix reference variable str() method call (fix #10430) cgen: fix reference variable str() method call (fix #19430) Jun 28, 2024

@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.

Good work.

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.

custom .str() function returns reference address instead of string

3 participants