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

Class: redefining the name property affects the behavior of to_s #7292

Closed
Blacksmoke16 opened this issue Jan 10, 2019 · 5 comments
Closed

Class: redefining the name property affects the behavior of to_s #7292

Blacksmoke16 opened this issue Jan 10, 2019 · 5 comments

Comments

@Blacksmoke16
Copy link
Member

abstract class Command
  class_getter name : String = ""
end

class CreateUserCommand < Command
end


pp {{Command.subclasses}} # =. []

However removing the class_getter name : String = "" causes it to work fine.

https://play.crystal-lang.org/#/r/5ypk/edit

@asterite
Copy link
Member

It's not that it's empty, if you invoke size on it you'll see the number 1. It's just that you are defining the name to be empty, and for some reason it doesn't show up as "" but as an empty value. And that's because Class#inspect(io) just delegates to to_s(io), which is the class name, and that's empty. Maybe it's fine.

@Blacksmoke16
Copy link
Member Author

Blacksmoke16 commented Jan 10, 2019

Ohhhhh. TIL that name class var is actually something used internally? Guess i'll just pick a diff variable name.

@asterite
Copy link
Member

It's a method that gives you the name of the class, and it's used in the string representation. With that class getter you was overwriting it :-)

@straight-shoota
Copy link
Member

@asterite Is that really expected behaviour that user code can override class names?

@asterite
Copy link
Member

@straight-shoota You can do it in Ruby :-)

That said, I just tried it in Ruby and if you override the name method it doesn't change the behavior of to_s, so we should probably fix that.

@asterite asterite reopened this Jan 10, 2019
@asterite asterite changed the title class_* macro causes {{Klass.subclasses}} to be empty Class: redefining the name property affects the behavior of to_s Jan 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants