Skip to content

Easy-to-use macro? #10

@MilesCranmer

Description

@MilesCranmer

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions