-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Document how to cast / recast / change type of pointer inside Ptr docs #58641
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
base: master
Are you sure you want to change the base?
Conversation
It took me a while to figure out how to do this and wished it would be documented here or in a similar place.
Hello! I am a bot. Thank you for your pull request! I have assigned
Note: If you are a Julia committer, please make sure that your organization membership is public. |
This is a pure change to documentation making it easier for people to mess or observe via Julias memory. |
Thanks! Would you please format this as a It doesn't necessarily have to be a "see also", but this note should come after the rest of the docstring as it is not core functionality of |
I took the syntax from an See also section but decided to add additional information as the documentation of I also just noticed However https://github.com/freemin7/julia/blob/6c425515e68e29944c06852906e2f8754aca2268/base/pointer.jl#L21 is ambigous who deep it refers to. This turned a bit more complicated then i thought. I see 3 top level options i have no preference between:
There are smaller minimal examples which involve pointer to integer conversion. 3.2 Add note to
Any choice {1, 2, 3.1, 3.2, 3.3} is fine by me but also open to hear other ideas. |
Perhaps a variant of 2 would be good: adding a sentence after the existing |
This information is based on a conversation in the Julia Slack internals channel.
I added a more sections which elaborates on the role the type T plays in pointers. I think this information is best collected in a central place. It also makes the reinterpret sentence stand out less. |
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.
Thanks, this is much better :)
memory is actually valid, or that it actually represents data of the specified type. | ||
Some operations like [`unsafe_load`](@ref) are only supported when T is an [`isbitstype`](@ref) | ||
while [`unsafe_wrap`](@ref) also supports abstract types. |
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.
unsafe_wrap
is a good see also, but it is not an alternative to Ptr
in many cases so this is not appropriate as a "while" clause.
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 is not meant as an alternative to pointers. unsafe_wrap
takes a pointer as an argument and if it is a Ptr{T} where T is an abstract type it will still worked, while unsafe_load
will throw an error if given such a pointer.
|
||
!!! warn | ||
When T is an [`Union`](@ref) type, the pointer does not contain the necessary information | ||
to reconstruct what elements in the array had which type. Such pointers can't be use to |
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.
Pointers are not just for arrays. For example, you can get a pointer to a field in an mutable struct.
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.
s/immutable/mutable I assume
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 data type it pointed to." would that be better?
!!! warn | ||
When T is an [`Union`](@ref) type, the pointer does not contain the necessary information | ||
to reconstruct what elements in the array had which type. Such pointers can't be use to | ||
retrieve any Julia objects. |
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's still possible to retrieve an object, you just have to know what type you're looking for.
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.
Well then you would manually de-unionize the pointer and it no longer the "same" pointer.
I will think of a different phrasing.
Co-authored-by: Lilith Orion Hafner <[email protected]>
Co-authored-by: Lilith Orion Hafner <[email protected]>
Co-authored-by: Lilith Orion Hafner <[email protected]>
It took me a while to figure out how to do this and wished it would be documented here or in a similar place.