Crystal: Would it be possible to add an alias for `__dir__`?

Created on 2 Dec 2019  路  6Comments  路  Source: crystal-lang/crystal

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.

Most helpful comment

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.

All 6 comments

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.

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.

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.

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.

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RX14 picture RX14  路  3Comments

jhass picture jhass  路  3Comments

asterite picture asterite  路  3Comments

asterite picture asterite  路  3Comments

will picture will  路  3Comments