# Use a,b = (1,2) within struc definition?

**URL:** <https://discourse.julialang.org/t/use-a-b-1-2-within-struc-definition/100873>\
**Category:** General Usage\
**Tags:** struct\
**Created:** [June 27, 2023, 5:41am UTC](https://discourse.julialang.org/t/use-a-b-1-2-within-struc-definition/100873 "2023-06-27T05:41:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [June 27, 2023, 5:41am UTC](https://discourse.julialang.org/t/use-a-b-1-2-within-struc-definition/100873/1 "2023-06-27T05:41:56Z")

</div>

Is there any way to do something like

```julia
@with_kw struct my_struct
       a,b = (1,2)
end

```

Use `a,b = (1,2)` syntax within a struct definition.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [June 27, 2023, 5:56am UTC](https://discourse.julialang.org/t/use-a-b-1-2-within-struc-definition/100873/2 "2023-06-27T05:56:36Z")

</div>

why do you want this?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 27, 2023, 7:38am UTC](https://discourse.julialang.org/t/use-a-b-1-2-within-struc-definition/100873/3 "2023-06-27T07:38:46Z")

</div>

On a more general note, it seems most of your questions are of the type “I want this syntax to mean that”, so you probably should just start writing your own macros?

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [June 27, 2023, 10:42am UTC](https://discourse.julialang.org/t/use-a-b-1-2-within-struc-definition/100873/4 "2023-06-27T10:42:56Z")

</div>

Nils - I should improve my macro skills yes. Though sometimes someone has a package with the requested syntax, which is great.

oxinabox - Maybe I’m misusing it but I really like the struct syntax.  
Consider the below, similar functionality as a function or a struct. I find the struct syntax much cleaner. But with structs you have to re-start the kernel to make any changes and they don’t support syntax like a,b=(1,2).

```julia
function my_func(a,b,c)

    d=2c
    e=2d
    f=2e

    (;a,b,c,d,e,f)
end

struct my_struct

    a
    b
    c

    d=2c
    e=2d
    f=2e
    
end

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 27, 2023, 11:27am UTC](https://discourse.julialang.org/t/use-a-b-1-2-within-struc-definition/100873/5 "2023-06-27T11:27:09Z")

</div>

> [@Lincoln\_Hannah](#):
>
> similar functionality

What you mean? The struct syntax there is not valid, and don’t seem to be related to the function, which returns a tuple.

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [June 27, 2023, 11:31am UTC](https://discourse.julialang.org/t/use-a-b-1-2-within-struc-definition/100873/6 "2023-06-27T11:31:46Z")

</div>

Sorry forgot to add @with\_kw

```julia
@with_kw struct my_struct

    a
    b
    c

    d=2c
    e=2d
    f=2e
    
end

```

Similar in that they calculate the same fields which can then be accessed externally.  
e.g.

```julia
    my_func( a,b,c ).f

    my_struct(; a, b, c ).f

```

---

<div class="post-metadata">

**Author:** ![Alexander\_Knudson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander_knudson/32/215656_2.png) [@Alexander\_Knudson](https://discourse.julialang.org/u/Alexander_Knudson)\
**Post date:** [June 27, 2023, 3:56pm UTC](https://discourse.julialang.org/t/use-a-b-1-2-within-struc-definition/100873/7 "2023-06-27T15:56:56Z")

</div>

Maybe an unrelated answer, but it seems like you want to be able to do struct destructuring? You can do that with keyword arguments. See this discussion: [https://discourse.julialang.org/t/unpacking-struct-properties/](https://discourse.julialang.org/t/unpacking-struct-properties/)
