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

Add Path#empty? #9137

Merged
merged 4 commits into from
May 7, 2020
Merged

Conversation

straight-shoota
Copy link
Member

This adds a new query method to Path to tell whether it's empty.
The existing definition of empty is expanded to also include "." which hasn't been covered before but is actually the normalized form of "".

@Sija
Copy link
Contributor

Sija commented Apr 20, 2020

I'm not sure I understand the definition of Path#empty? - what does it mean? And why . is considered as empty?

@straight-shoota
Copy link
Member Author

Empty means "" or "." which usually represents the current directory.

@Sija
Copy link
Contributor

Sija commented Apr 20, 2020

And why . is considered as empty?

@straight-shoota ☝️

@straight-shoota
Copy link
Member Author

Because it's semantically equivalent to "" and actually its normalized form.

@Sija
Copy link
Contributor

Sija commented Apr 20, 2020

Because it's semantically equivalent to "" and actually its normalized form.

I'd rather say the opposite, "" is semantically equivalent to ".". Still, I can't find one reason to call either of those empty - apart from being an empty string, which in this case is an implementation detail.

Also, note the Ruby's Pathname#empty?:

Tests the file is empty.

@straight-shoota
Copy link
Member Author

Do you have a suggestion for a better name? IMO empty is pretty much self explanatory and the typical term for this.

@Sija
Copy link
Contributor

Sija commented Apr 20, 2020

Do you have a suggestion for a better name?

I'm not sure we need this method at all, since you just came up with that with no good usecase, there was no one requesting, or even mentioning this before, so...

IMO empty is pretty much self explanatory and the typical term for this.

Well, for sure it ain't for me, hence my question. I don't think I've seen such method anywhere else.

@straight-shoota
Copy link
Member Author

The description has always been there in the doc comment for Path (it just lacked the normalized form), this just adds a method to programmatically query this property.
Immediate use cases are already provided in the diff. Apart from internal reusability I figure this method could also be useful for custom algorithms working with Path which can often implement special behaviour when an path is empty.

RX14
RX14 previously approved these changes Apr 23, 2020
@straight-shoota straight-shoota added this to the 0.35.0 milestone Apr 24, 2020
Copy link
Member

@bcardiff bcardiff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this is picking Path#empty? becuase String#empty? and @name : String.

As @Sija pointed, ruby semantics for Path is another valid use case for what Path#empty? can mean.

I'm not sure we need a public api for this, since I guess we can check a_path.normalize == Path["."].normalize.

If we don't need a public api, we can pick whatever name and keep the code cleanup in each_parent.

If we need a public api, #idempotent?, #identity? sounds more appealing to me and does not add noise to the fact that Path["."] is considered so (as it does #empty?)

@asterite
Copy link
Member

asterite commented May 5, 2020

Agreed, "." doesn't look like empty to me.

@straight-shoota
Copy link
Member Author

I think that this is picking Path#empty? becuase String#empty? and @name : String.

No, it's not. I actually copied that name from boost.

But fine, I'll make the method private.

@bcardiff bcardiff merged commit 9efe317 into crystal-lang:master May 7, 2020
@straight-shoota straight-shoota deleted the feature/path-empty branch May 7, 2020 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants