# Zero-dimensional array interface for custom type

**URL:** <https://discourse.julialang.org/t/zero-dimensional-array-interface-for-custom-type/110609>\
**Category:** General Usage\
**Created:** [February 22, 2024, 10:44pm UTC](https://discourse.julialang.org/t/zero-dimensional-array-interface-for-custom-type/110609 "2024-02-22T22:44:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sadish-d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sadish-d/32/48058_2.png) [@sadish-d](https://discourse.julialang.org/u/sadish-d)\
**Post date:** [February 22, 2024, 10:44pm UTC](https://discourse.julialang.org/t/zero-dimensional-array-interface-for-custom-type/110609/1 "2024-02-22T22:44:44Z")

</div>

I have

```julia
struct S
the_only_field
end

```

For my struct to hold data, I need it to have a field, and that means I need the field to have a name. But I would like to use it without having to define something like `the_only_field(s::S) = s.the_only_field`. So I figured I could make Julia think of `S` as a zero-dimensional array and have `s[]` give me `s.the_only_field`.

I’m trying to follow the documentaiton on the `AbstractArray` interface: [Interfaces · The Julia Language](https://docs.julialang.org/en/v1/manual/interfaces/)

Is this the right way to do it?

```julia
Base.size(::S) = 0
Base.getindex(::S, i::Integer) = error("S is zero-dimensional. It has no indices. Use [] to get its contents.")
Base.getindex(s::S) = s.the_only_field

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [February 23, 2024, 1:25am UTC](https://discourse.julialang.org/t/zero-dimensional-array-interface-for-custom-type/110609/2 "2024-02-23T01:25:49Z")

</div>

1. The indexing seems about right, it resembles how `Ref`s are implemented: `getindex(b::RefValue) = b.x`. I don’t think the erroring method for 1-dimensional indexing is necessary, but if you’re going that way you should also expand to multidimensional indexing.

2. 0D arrays e.g. `fill(1)` should have a size of `()`, not 0. The default `length` as `prod( () )` is 1.

3. Subtype `S{T}<:AbstractArray{T,0}` if you really want to leverage `AbstractArray` methods.
