Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What are the requirements on the target? Does it work for any type with
void*, int
length constructor?(I assume that this is answered in the spec for the currently allowed case without initializer. The link to that spec may be helpful.)
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.
It works with any type that has implicit conversion from
Span<T>
.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.
From the language semantics
stackalloc T[size]
expression hasSpan<T>
type, unless it matches backwards-compatible pattern where it must have typeT*
.The implementation will call well-known constructor
Span<T>(void*, int)
. Essentially there is one "magic" type -Span<T>
and one "magic" methodSpan<T>(void*, int)
, that we know about.Other types opt-in by providing a conversion operator. It is more intentional than matching
(void*, int)
constructors on random types - who knows what they expect, is that always{ptr, size}
, is thatsize
in bytes on elements?With
Span<T>
there could be no doubts on{ptr, size}
part.This is also how
stackalloc
always worked. - you could always participate by having a conversion fromT*
. Opting in via a conversion is not a new feature.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.
Ok, that make sense. I was under impression that you are somehow doing this without knowing about magic
Span<T>
type.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 tried avoiding magic types, but arrived to a conclusion that for a robust pattern we would need one type - a generally accepted representation of
{ptr, size}
tuple.And then realized, that
Span<T>
works just fine for that purpose :-)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.
Since it's based on
Span<T>
, I assume I'd be able to write:? Today to do the same thing I need to write:
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.
@stephentoub Yes. That should work.