-
-
Notifications
You must be signed in to change notification settings - Fork 740
Add std.array.isSliceOf #2416
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 std.array.isSliceOf #2416
Conversation
Returns whether $(D part) is a slice of $(D whole). | ||
+/ | ||
@trusted | ||
pure nothrow bool isSliceOf(T)(in T[] part, in T[] whole) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we be using in
so pervasively when the semantics for scope
are still up in the air? Why not just use const?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in
is fine here even if it does imply scope
. I'm guessing the code breakage will mean that when/if scope
is implemented then in
will stop implying scope
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine, because here, the passed data never actually leaves the scope, so it's not just miss-use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing the code breakage will mean that when/if scope is implemented then in will stop implying scope.
The problem with in
and code breakage is when scope
actually starts meaning something and in
still means const scope
, because right now, there's now difference between const
and in
in the vast majority of cases, but once scope
is actually properly defined and implemented, then presumably in at least some of those cases, in
's behavior would change due to scope
changing, and if scope
wasn't valid in that case or was violated, then the code would break. It could be that it's decided at some point that in
will just mean const
in order to avoid that kind of breakage, but it's the fact that in
means const scope
and scope
currently does nothing but will probably do something eventually which makes it so that I almost always tell people to not use in
. I honestly think that we'd be better off deprecating in
than continuing to use it when its eventual semantics are unknown. So, at this point, I would never use in
. That being said, in this particular case, I'd expect that in
will be valid even once scope
has been properly defined and implemented, since the arrays do not and cannot escape the function. So, it probably isn't a problem to use it. I still wouldn't though.
Does it have a use-case? |
What he said. Also, does this take into account we already have I don't think Though to be honest, the only thing I've ever had a need for is |
I needed a couple of times for writing contracts. The |
AFAICT, |
@quickfur: overlap is not officially supported and at the moment I don't think that the optimizer can reduce as good as to the isSliceOf code. isSliceOf is complementary to the documented sameHead, sameTail. Your comments would also apply for those. |
Well, technically,
One issue I'm seeing here is that |
Any conclusions - yay or nay? |
Replace |
I see no reason why |
I can't help but wonder what the use case for this function is. Not a single one was given. Without a use case, I see this as nothing more than a self serving function, so I have to say 'Nay' (sorry). Related: An |
As I said before, I need it a couple of times while writing contracts. I want to be sure that the returned array is not new allocated, but a slice of a preallocated buffer. The |
@dcarp It would be nice if you turned your use case (suitably simplified, of course) into a nice, motivating ddoc'd unittest. That will not only convince reviewers, but will also serve as an excellent example of how to use this function, and more importantly, what to use it for. |
When so many people find this PR not useful, probably it isn't... so I retract it. |
isSliceOf
is a complement forsameHead
andsameTail
.