-
Notifications
You must be signed in to change notification settings - Fork 5
Open
Description
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
3I've got it as an internal macro but I could just upstream it here if interested.
Metadata
Metadata
Assignees
Labels
No labels