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

Redundant to_s warning #165

Closed
sardaukar opened this issue Aug 22, 2020 · 3 comments
Closed

Redundant to_s warning #165

sardaukar opened this issue Aug 22, 2020 · 3 comments
Labels

Comments

@sardaukar
Copy link

Maybe I'm making a mistake here, but I have a class with a to_s method defined, and when I write

puts "#{@cpu.pc.hex_p}\t#{instr}"

I get

0x00	#<Zaratite::Instruction:0x7fd655d34f80>

and with

puts "#{@cpu.pc.hex_p}\t#{instr.to_s}"

I get

0x00	LD SP, $fffe [0x31 0xfffe]

But ameba gives me

[W] Lint/RedundantStringCoercion: Redundant use of `Object#to_s` in interpolation
> puts "#{@cpu.pc.hex_p}\t#{instr.to_s}"

which makes sense but why is the behavior different with the to_s inside the interpolation ? Using Crystal 0.35.1

@Blacksmoke16
Copy link
Contributor

Blacksmoke16 commented Aug 22, 2020

Copying this over from Gitter:

Be sure to define to_s(io : IO) : Nil and not to_s : String, then write the content to io. The latter works when you explicitly call it, but when you remove it and let interpolation handle it; it's calling a different overload. So tl;dr this isn't a bug. However, might be worth adding a sentence/another error if you define the wrong to_s overload?

\cc @veelenga

@sardaukar
Copy link
Author

Yep, forgot to close here. Thanks!

@veelenga
Copy link
Member

Thanks guys.

However, might be worth adding a sentence/another error if you define the wrong to_s overload?

Yeah, already thought about it. Same for to_json/to_yaml.

@Sija Sija added the invalid label Oct 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants