Can structs be instantiated?
-
Sort of like
person = [ .name = "John Doe" .age = 43 .sex = "male" ] person personA person personB personA.name = "Jimmy" personB.name = "Sally" print(personB.name," is married to ",personA.name) // returns Sally is married to Jimmy print(person.name," is just an average guy") // returns John Doe is just an average guy
-
@_JKDOS Yes:
struct person string name = "John Doe" int age = 43 string sex = "male" endstruct
-
-
PersonA and PersonB should work just as you’re expecting provided you use the form of struct definition used by @Nisse5 In this case you cannot use the base struct directly with just person, it has to be an instance
Edit: Now I'm on a computer, I can be a bit clearer!
This works (note the commas):
person = [ .name = "Bob", .age = "20", .sex = "M" ] loop clear() print(person) update() repeat
Or this works
struct person string name = "John Doe" // default value int age = 20 string sex = "M" endstruct person personA personA.name = "BoB" loop clear() print(personA) update() repeat
(ignore the {1} I don't know where that is coming from)
But not a mix of both.
Personally I find the first use awkward and confusing but its probably just because I'm used to OO programming these days.
-
@_JKDOS It seems like it's only possible to access variables created from structs.
-