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

Would it be possible to add an alias for __dir__? #8546

Closed
ndbroadbent opened this issue Dec 2, 2019 · 6 comments
Closed

Would it be possible to add an alias for __dir__? #8546

ndbroadbent opened this issue Dec 2, 2019 · 6 comments

Comments

@ndbroadbent
Copy link

ndbroadbent commented Dec 2, 2019

Hello, sorry if this has already been asked! I'm working on the advent of code puzzles, and I figured out how to read a file in the same directory as the script:

File.read(File.expand_path("../input", __FILE__)) #...

I noticed that __dir__ isn't defined. But I actually just realized that Crystal supports __DIR__, and that does work:

File.read(File.join(__DIR__, "input")) #...

I was just wondering if you might consider adding an alias for __dir__? __DIR__ does make more sense and it has the same case as __FILE__, but it's just one less surprise for people coming from Ruby.

@straight-shoota
Copy link
Member

In Crystal we try to avoid aliases as much as possible to relieve users of having to learn different equivalent solutions to the same problem when you actually only ever need a single one.

The only thing I could think of would be to enhance the error message for an undefined local variable/method __dir__ to suggest __DIR__ instead. That would point Ruby users into the right direction.

@straight-shoota
Copy link
Member

Technically, another idea would be to allow parsing __dir__ as an alias but let the formatter always transform it to __DIR__. Since it's a keyword, this would work entirely at the syntax stage.

@ndbroadbent
Copy link
Author

ndbroadbent commented Dec 2, 2019

Ok that makes sense about aliases! But I really like the idea of the formatter providing an automatic fix! I couldn't find any documentation or threads about __dir__, and the only way I figured it out is by thinking "I wonder if they decided to make it uppercase". Then I knew it was right once the syntax highlighting started working in VS Code.

But if the code formatter automatically changed it from __dir__ to __DIR__, then I would immediately understand what was going on. I spent ~3 minutes trying to figure out, including Google searches and searching on GitHub issues, so I think little things like this are really important.

@Blacksmoke16
Copy link
Member

FWIW it's documented under https://crystal-lang.org/reference/syntax_and_semantics/constants.html#pseudo-constants.

@asterite
Copy link
Member

asterite commented Dec 3, 2019

I like to think of Crystal as a new language. In 20, 30 years, when someone sees __dir__ as an alias to __DIR__ (or have the formatter change it, which is almost the same) maybe someone will ask "why do we have these two redundant ways?". Someone else will answer "oh, because in Ruby you could do that". "But I don't program in Ruby and I don't care about that! Why do we have to keep this baggage?".

Elixir got it right: it took what it liked from Ruby and dumped the rest. I think we should do the same.

So I won't approve the PR for this.

@ndbroadbent
Copy link
Author

That makes sense! I also learned why __dir__ is lower case in Ruby, since __FILE__ is a keyword, but __dir__ is a method. So it makes that they are both same case in Crystal (and it also feels much nicer!)

Either way, it's been great to have the discussion here, because this will make it easier for developers to find in the future. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants