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

Is there any way to do something like

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

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

why do you want this?

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?

1 Like

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).

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

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

Sorry forgot to add @with_kw

@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.

    my_func( a,b,c ).f

    my_struct(; a, b, c ).f

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/