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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add atRawUnsafe
and atUnsafe
to array classes
#3327
Conversation
Shoot, I forgot these APIs are also used for array implementations, not just for users 馃槄 maybe we have to introduce a new API then ... 馃 |
Yes, I agree. We can do it in one of two ways:
|
I think something like the |
This reverts commit 61d46ae.
.at(n)
for arrays of length n
atRawUnsafe
and atUnsafe
to array classes
Thanks for the feedback. I updated the PR to add |
implicit class UnsafeRichArray[T](val value: Array[T]) extends AnyVal { | ||
@inline def at(i: Int): Ptr[T] = value.asInstanceOf[runtime.Array[T]].at(i) | ||
@inline def atUnsafe(i: Int): Ptr[T] = | ||
value.asInstanceOf[runtime.Array[T]].atUnsafe(i) | ||
} |
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.
We are already in the unsafe
package. Actually I would prefer to make at
directly delegate to the unsafe one because I am never interested in the safe one. But maybe that's too confusing...
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 can understand we want to make atUnsafe
as at
since we are already in Unsafe
package, but I believe that's too confusing as you mentioned and better to keep as it is 馃憤
When we enrich our library using import scalanative.unsafe._
, people won't see at
method comes from unsafe
package at a glance, so naming atUnsafe
makes sense to me even though atUnsafe
sounds a bit redundant, my two cents.
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.
Except https://github.com/scala-native/scala-native/pull/3327/files#r1283253067 it looks good from my side 馃憖
implicit class UnsafeRichArray[T](val value: Array[T]) extends AnyVal { | ||
@inline def at(i: Int): Ptr[T] = value.asInstanceOf[runtime.Array[T]].at(i) | ||
@inline def atUnsafe(i: Int): Ptr[T] = | ||
value.asInstanceOf[runtime.Array[T]].atUnsafe(i) | ||
} |
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 can understand we want to make atUnsafe
as at
since we are already in Unsafe
package, but I believe that's too confusing as you mentioned and better to keep as it is 馃憤
When we enrich our library using import scalanative.unsafe._
, people won't see at
method comes from unsafe
package at a glance, so naming atUnsafe
makes sense to me even though atUnsafe
sounds a bit redundant, my two cents.
Currently if you do:
The
buf.at(0)
will throw an index-out-of-bounds exception, because the array is of zero length and there is no element at that position.Does anyone else find this annoying? Whenever using
.at
I have to remember to to add a special case to handle 0-length arrays, even though the actual logic would not be broken. I keep forgetting and making bugs whenever I try to use this optimization 馃槄The fundamental conflict is that I'm not interested in the element at the 0th position, I'm interested in the pointer at which the array data starts (no matter what length it is).
If nobody thinks it is harmful, I'd like to change the semantic here.