-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
A "namespace" feature proposal #265
Comments
One thing: when you reopen a type you don't need to re-specify its superclass. So the second example can just be: class Payment
class SomeService
end
end In fact, the second one can be written like this (both in Ruby and Crystal): class Payment::SomeService
end So there you define something inside However, I think I understand what you mean. If I want to reopen Payment and add some methods, I would do this: class Payment
def some_method; ... end
end Later if I change Payment to be a module, or a struct, I would have to change it everywhere. Annoying... right? I think this could be done with macros, as you say. Something like this: macro namespace(type)
{{typeof(type).kind}} {{type}}
{{yield}}
{{:end.id}}
end But we'd need to add a few things to macros:
I think I would name it reopen String do
...
end However, I'm not totally convinced of this. The times I had to change the type of a type were very few and the refactor takes very little time, just some seconds. Another "issue" is that errors inside definitions there would end up being shown as inside macro expansions, which is always uglier than just regular errors. But I'll leave this issue open, maybe later we find more reasons to add it. |
-1 Opening classes and modules is not redeclaring them, it's opening them. And as @asterite already said, you don't change a class to a module or a module to a class very often, and doing a Another point is that reopening a class is actually a pretty rare thing to do, most of the time you reopen a module that you use as a namespace. And changing a namespace module into a class is even more rare thing to do in my experience. Additionally if we maybe get something like Rubys refinements in Crystal at some point, the cases for reopening a class are reduced to a minimum. |
I was meant to ask, is it a usual Crystal or Ruby programming practice to reopen a module? I had once to reopen a class so far, yet I have been trying to find out what is the most conventional way to spread a module across different files. What I had in mind was to place the math-related functionality in a math directory where in there I would add some files (gamma.cr, bessel.cr for example, etc). Would it be normal to then define the |
|
Thanks for the tip @jhass. |
Thank you for your answers! Prehistory (why did I proposed that): class A ; end
# b.rb
class B < A ; end
# b/c.rb
class B
class C ; end
end If Does Crystal support const-autoloading/ |
Comparison of different types of class/module/etc opening: # reopen X
reopen X
...
end
# class X
class X
...
end
# class X::Y
class X::Y
...
end
|
Autoloading is not supported, but might be supported in the future. That is, the compiler would add requires based on a type's name. I think I tried to implement that once and ran into some issues, but it's not something impossible to do. |
I'll close this for now, reopening types works as expected and there's no need to redeclare the supertype. |
@asterite Ok. But does Crystal supports const-autoloading/const_missing? I just want you to understand what the proposal is. There is difference between 'opening a class the first time' (aka class declaration/creation/etc) and 'reopening class'. If your language supports const autoloading you will never be sure in which order classes are loaded. So you will never be sure what happens first - class declaration or class reopening. If the reopening happens first and you don't declared the class' supertype - you will actually declare a new class with wrong inheritance information. PS. Yes, |
I want to know your opinion about introducing a "namespace" feature. It is better to describe it by code example (because I'm not so good in English):
Use namespace:
Instead of:
So, using a
namespace
is just like saying: hey, put a full declaration of that const right here.I think it could be implemented using macros, am I right?
Thank you! Cool project!
The text was updated successfully, but these errors were encountered: