-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add SliceType and ArrayPointerType to std.meta #2782
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
Conversation
39fc0fd
to
7915ded
Compare
7915ded
to
f15f1a8
Compare
f15f1a8
to
5de9199
Compare
This should be unblocked now. |
b3fd6ea
to
efd714d
Compare
std/meta.zig
Outdated
} | ||
|
||
test "std.meta.SliceType" { | ||
testing.expect(SliceType([]u8) == []u8); |
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.
Not sure how much testing I should do here...
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.
Basic functionality is fine.
std/meta.zig
Outdated
@@ -115,6 +115,66 @@ test "std.meta.Child" { | |||
testing.expect(Child(?u8) == u8); | |||
} | |||
|
|||
/// Given an array/pointer type, return the slice type `[]Child`. | |||
/// Preserves all pointer attributes such as `const`/`volatile` etc. | |||
pub fn SliceType(comptime T: type) 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.
Is Type
redundant? Should it just be Slice
and ArrayPointer
rather than SliceType
and ArrayPointerType
?
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 prefer without the Type
suffix since we have a pretty consistent formatting style that types are capitalized.
efd714d
to
39d9cd0
Compare
std/meta.zig
Outdated
testing.expect(Slice(*const u8) == []const u8); | ||
testing.expect(Slice([*]u8) == []u8); | ||
testing.expect(Slice([*]const u8) == []const u8); | ||
//testing.expect(Slice([10]u8) == []const u8); |
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.
Let's have this either be a real test case or deleted. It's not clear what this comment is communicating here.
How about this test case?
testing.expect(Slice(*[10]u8) == []const u8);
Same comment for the other commented out test case below.
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 was thinking we would integrate #3173 first, then uncomment this.
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 a smell that the function would accept a non-reference type and return a reference type. I think the test case I provided is the one you're looking for here.
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.
Oh sorry I misunderstood your point. Yes I think *[N]T
makes more sense than [N]T
. Will change
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.
So the question is, should Slice(*[N]T)
become [][N]T
or []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.
Slice(*[N]T)
should become []T
to be consistent with how type coercion works in the rest of the language.
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.
Hmmm, given that answer, I'm not sure what the rules are. It sounds like we have one or more special cases for single-value pointers? What would these cases be?
if (single_value_pointer)
if (pointer_to_array) return []T.child.child
else return []T.child
Are there any other cases where we would return []T.child.child
?
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.
Are there any other cases where we would return []T.child.child?
Single item pointer to array is the only case that matches type coercion semantics
39d9cd0
to
9224978
Compare
Feel free to re-open when this is ready for review |
@andrewrk I had an outstanding question I was waiting for you to answer. |
Provide a couple functions in
std.meta
to transform pointer/array types to the slice/array pointer variations. Note that the key feature here is that it preserves all pointer attributes (i.e.const
,volatile
, etc)I've converted 3 projects to
zig
and have already made use of these functions in all 3 projects.