goboscript icon indicating copy to clipboard operation
goboscript copied to clipboard

struct initialization as positional parameters?

Open faretek1 opened this issue 1 year ago • 5 comments

When writing a struct literal, the syntax requires each attribute and value to be specified. It would be neat to be able to simply pass in values positionally, and automatically assigning to the correct attribute by using the order of attributes in the struct definition

i.e.:

struct Vector {
    x, y
}

onflag {
    forever {
        Vector my_pos = Vector{x: x_position(), y: y_position()};
    }
}

becomes:

struct Vector {
    x, y  # Here, attribute order is defined. Order of these attributes will become important
}

onflag {
    forever {
        Vector my_pos = Vector{x_position(), y_position()}; # Known to be x and y, since that is the order it is defined above
    }
}

potentially, even this:

struct Vector {
    x, y
}

onflag {
    forever {
        Vector my_pos = Vector{x_position(), y: y_position()}; # Passing x positionally, and y via key-value defintion
    }
}

This would be a bit similar to python

faretek1 avatar Jan 25 '25 13:01 faretek1

This would make repetitive struct literals less painful to type, especially lines (x1, y1, x2, y2)

faretek1 avatar Jan 25 '25 13:01 faretek1

However this would conflict with #71 Potentially a character like * may be needed to change behavior like how *args works in python

faretek1 avatar Jan 25 '25 19:01 faretek1

currently the only solution for this is to use a macro

faretek1 avatar May 31 '25 17:05 faretek1

since the struct syntax is based on rust syntax, maybe this isn't a good idea. Just use a macro instead

faretek1 avatar Aug 02 '25 12:08 faretek1

the issue is that this makes the order of fields a breaking change, and there is nothing in scratch where the order matters

aspizu avatar Aug 02 '25 12:08 aspizu