# Capture part of return type

**URL:** https://discourse.julialang.org/t/capture-part-of-return-type/43139
**Category:** General Usage
**Tags:** parametric-types
**Created:** [July 15, 2020, 5:57pm UTC](https://discourse.julialang.org/t/capture-part-of-return-type/43139 "2020-07-15T17:57:45Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![grahamas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grahamas/32/3401_2.png) [@grahamas](https://discourse.julialang.org/u/grahamas)
#### Post date: [July 15, 2020, 5:57pm UTC](https://discourse.julialang.org/t/capture-part-of-return-type/43139/1 "2020-07-15T17:57:45Z")

</div>

Is there a way to capture the type of a return in a variable (specifically a parameter of the variable type, but baby steps)

```julia
function f()
    A::AbstractArray{T} where T = [1.0 2.0]
    return T
end

```

Of course I can pretty easily get this info otherwise, but this syntax _almost_ seems like it makes sense. Almost, because it’s not quite clear how you’d get the scoping to work.

edit: Honestly, I’d also be happy with hearing about some non-syntactic way to say something like

```julia
@get_type T AbstractArray{T} typeof(A)

```

where it pattern matches and sets the T variable.

edit 2: … is the answer just to use function dispatch? That’s the answer, isn’t it. Sorry for the noise.

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [July 15, 2020, 6:07pm UTC](https://discourse.julialang.org/t/capture-part-of-return-type/43139/2 "2020-07-15T18:07:48Z")

</div>

Are you just looking for `eltype`?

```julia
julia> A = [1.0, 2.0]
2-element Array{Float64,1}:
 1.0
 2.0

julia> T = eltype(A)
Float64

```

---

<div class="post-metadata">

### Author: ![grahamas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grahamas/32/3401_2.png) [@grahamas](https://discourse.julialang.org/u/grahamas)
#### Post date: [July 15, 2020, 6:24pm UTC](https://discourse.julialang.org/t/capture-part-of-return-type/43139/3 "2020-07-15T18:24:37Z")

</div>

Not quite. I’m actually doing more like `nt::NamedTuple{names} = (a = 1.0, b = 2.0)`. But this works for my particular purposes:

```julia
typenames(nt::NamedTuple{names}) where names = names

```

It’d be nice if there were a simple syntactic way to do this, but writing a little helper function is fine

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [July 15, 2020, 6:47pm UTC](https://discourse.julialang.org/t/capture-part-of-return-type/43139/4 "2020-07-15T18:47:17Z")

</div>

I could be wrong but it seems you could write a macro which lets you have the syntax from your OP by creating the helper function you, and it could probably be pretty generic. Ie it would turn,

```julia
function f()
    A::AbstractArray{T} where T = [1.0 2.0]
    return T
end

```

into

```julia
function f()
    A::AbstractArray{T} where T = [1.0 2.0]
    T = ((::AbstractArray{T} where T)->T)(A)
    return T
end

```

And you can imagine this kind of thing can be done pretty programatically. Just an idea.

---

<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: [July 16, 2020, 10:08am UTC](https://discourse.julialang.org/t/capture-part-of-return-type/43139/5 "2020-07-16T10:08:41Z")

</div>

> [@grahamas](#):
>
> But this works for my particular purposes:
> 
> ```julia
> typenames(nt::NamedTuple{names}) where names = names
> 
> ```

I am afraid I still don’t understand the question, but would

```julia
julia> keys((a = 1, b = 2))
(:a, :b)

```

help?

Also `fieldnames` in type space.
