# Generic API verb for independent copy

**URL:** <https://discourse.julialang.org/t/generic-api-verb-for-independent-copy/140130>\
**Category:** General Usage\
**Tags:** question, api\
**Created:** [October 21, 2026, 7:20am UTC](https://discourse.julialang.org/t/generic-api-verb-for-independent-copy/140130 "2026-10-21T07:20:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 21, 2026, 7:20am UTC](https://discourse.julialang.org/t/generic-api-verb-for-independent-copy/140130/1 "2026-10-21T07:20:41Z")

</div>

Suppose that an API supports

```julia
y = calculate(a, x)

```

where `a` may or may not be modified by `calculate`, without affecting the `x -> y` mapping. For example, it could memoize results, collect statistics, etc.

Also, assume that `calculate` is not thread-safe, the intended usage is to make thread-local copies.

The question is whether there is a generic function one should use for that purpose, for which one would define a method for types that support `calculate`. One could define a method for `Base.copy`, but that’s not what it promises, it explicitly mentions a shallow copy. `Base.deepcopy` is too deep. Is there something in-between that is generally used?

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [October 21, 2026, 9:32am UTC](https://discourse.julialang.org/t/generic-api-verb-for-independent-copy/140130/2 "2026-10-21T09:32:47Z")

</div>

There are two approaches I could imagine:

1. What needs to be copied to what depth depends strongly on what you’re using it for, as well as the implementation of the data-type. There can be conventions that this is verb is called `copyForThreading`, but it’s really `FooModule.copyForThreading` or `BarModule.copyForThreading`. No need to make this different methods of the same function – generic code has no business trying to do something like that, it depends too much on implementation details.
2. Maybe there is value in having a single function with multiple methods? This would look like

```julia-auto
"""For datastructures where "read-only"-style access is not threadsafe (e.g. due to 
internal memoization), this creates an appropriate partial copy/view; otherwise, the 
same datastructure is returned.

Note that type-piracy is especially bad in methods for this function.
"""
function Base.copyForThreading(data)
    data
end

"""For datastructures where "read-only"-style accesses performed by `operation`
are not threadsafe (e.g. due to internal memoization), this creates an appropriate
copy/view; otherwise, the same datastructure is returned.

Defaults to `copyForThreading(data)`."""
function Base.copyForThreading(operation, data)
    copyForThreading(data)
end

```
