# Implementing argument ownership on my own function

**URL:** <https://discourse.julialang.org/t/implementing-argument-ownership-on-my-own-function/103096>\
**Category:** General Usage\
**Created:** [August 23, 2023, 12:39am UTC](https://discourse.julialang.org/t/implementing-argument-ownership-on-my-own-function/103096 "2023-08-23T00:39:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![minetest2048](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/minetest2048/32/45961_2.png) [@minetest2048](https://discourse.julialang.org/u/minetest2048)\
**Post date:** [August 23, 2023, 12:39am UTC](https://discourse.julialang.org/t/implementing-argument-ownership-on-my-own-function/103096/1 "2023-08-23T00:39:56Z")

</div>

I want my function to take ownership of the arguments, like how `String` constructor takes ownership of the `UInt8[]` array:

```julia
julia> mybytes = UInt8.([125, 119, 48, 126, 40]);

julia> mystring = String(mybytes)
"}w0~("

julia> mybytes
0-element Array{UInt8,1}

```

This is what I want from my function:

```julia
julia> sock = MySocket(1,2)
julia> fn_that_takes_ownership(sock)
julia> sock
nothing

```

How do I implement that `fn_that_takes_ownership` function?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 23, 2023, 1:02am UTC](https://discourse.julialang.org/t/implementing-argument-ownership-on-my-own-function/103096/2 "2023-08-23T01:02:52Z")

</div>

this can’t be done for two reasons:

1. The `String(::Vector{UInt8})` is a special case:

```julia
String(v::Vector{UInt8}) = ccall(:jl_array_to_string, Ref{String}, (Any,), v)

```

1. conceptually, you can at least mimic the first case by calling `empty!()` yourself (after copying out the data), but in order to change `sock` to `nothing`, you actually need to evaluate something like `sock = nothing`, which can’t be done by a function, only by a macro

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [August 23, 2023, 1:09am UTC](https://discourse.julialang.org/t/implementing-argument-ownership-on-my-own-function/103096/3 "2023-08-23T01:09:58Z")

</div>

Before going into code - please keep in mind that the `String` constructor empties the `UInt8` collection - and your `mybytes` variable does not get assigned to another type.

Regarding your use-case, you seem to want to reassign the global scope `sock` name to the `nothing` value.

Now, an example that is closer to how `String` constructor consumes the `UInt8` collection might imply putting your `MySocket` object in some kind of wrapper/container: and, in your constructor you make sure you _empty_ the container.

This might be something close to what you are looking for:

```julia
struct MySocket
    a::Int
    b::Int
end

struct SocketUser
    sock::MySocket
    function SocketUser(sock::Base.RefValue{Union{Nothing,MySocket}})
        s = sock[]
        isnothing(s) && error("Cannot use an empty container")
        sock[] = nothing
        new(s)
    end
end

sock = Ref{Union{Nothing,MySocket}}(MySocket(1, 2))

# this constructor will `consume` the `sock` content
SocketUser(sock) # SocketUser(MySocket(1, 2))

# true
isnothing(sock[])

```
