Skip to content
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

Easy-to-use macro? #10

Open
MilesCranmer opened this issue Mar 20, 2024 · 3 comments
Open

Easy-to-use macro? #10

MilesCranmer opened this issue Mar 20, 2024 · 3 comments

Comments

@MilesCranmer
Copy link
Contributor

MilesCranmer commented Mar 20, 2024

Just wondering if anybody would be interested in the following macro @bkamins @markmbaum?

A function would be fine too of course; it just feels a bit more appropriate as a macro due to the behavior as both an operation on types and on instances.

macro readonly(expr)
    esc(:($(_readonly)($expr)))
end
_readonly(::Type{A}) where {T,N,A<:AbstractArray{T,N}} = ReadOnlyArray{T,N,A}
_readonly(ar::AbstractArray) = ReadOnlyArray(ar)

The use-cases are as follows:

julia> @readonly Vector{UInt8}
ReadOnlyArray{UInt8, 1, Vector{UInt8}}

julia> @readonly Array{Float32, 3}
ReadOnlyArray{Float32, 3, Array{Float32, 3}}

julia> @readonly [1, 2, 3]
3-element ReadOnlyArray{Int64, 1, Vector{Int64}}:
 1
 2
 3

I've got it as an internal macro but I could just upstream it here if interested.

@MilesCranmer
Copy link
Contributor Author

Actually changed my mind... A readonly function is better than a macro. But I think an easy to use function for MyArrayType{T,N} -> ReadOnlyArray{T,N,MyArrayType{T,N}} would be a huge help for readability.

@bkamins
Copy link
Collaborator

bkamins commented Mar 27, 2024

How would you want the function to work (i.e. how would it be different from what we have now with ReadOnlyArray)?

@MilesCranmer
Copy link
Contributor Author

MilesCranmer commented Mar 27, 2024

ReadOnlyArray is okay for construction, albeit a bit verbose.

The more useful thing is the type alias. It’s very wordy to type out the whole parametrized ReadOnlyArray.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants