It’s safe to view them kinda like Tuples without dot notation? For example, my modify item tuple method returns a new tuple, but with certain values changed.
I got hung up on dot notation, kept thinking it was acting like a class
. When it’s a local copy. However, in my mut.value = 2
example above, dot notation modification does work when you are accessing the “struct” itself, but not when accessing the local copy of the struct. Well, it does work… on that LOCAL copy, but not the main struct.
example:
if strongbox_variable = test[123]?
strongbox_variable.opened = true
# If this reassignment is not done, modifying "opened" is an illusion!
# test[123] = strongbox_variable
end
Knowing the differentiation between a local copy of a struct, and the struct itself is extremely beneficial. They can feel ambiguous sometimes. Especially when dot notation inherently implies the developer wants to change that property. When a developer is now modifying a property that is a local copy and not reassigning it…it now becomes an illusion to the developer and might create bugs?
Yeah, this is why I originally wanted to use a struct. Cause I have 2 properties (x
and y
). Which are immutable. And then I had 1 mutable property opened
, and I remember people on gitter saying structs are mutable, so I thought it might be a good idea.
Quite frankly (not directed to you), I’m really tired of the “performance” mantra. I see it everywhere on gitter, reddit, etc.
For example, my JSON benchmark thread. If you use JSON.mapping
with a struct
it’s only negligibly faster. It’s actually 3 times more faster if you don’t call from_json
, because the type checking slows it down significantly. But you have to use from_json
. Which completely nullifies the entire benchmark. And also nullifies every point, every single user said that claimed “using a struct with JSON.mapping is faster!”
In fact, I might make an issue on GitHub about that. That’s not right, using a struct should be far faster. Hell, even @oprypin recommended and helped me significantly with a struct Message
instead of using JSON.parse
. from_json
should not gimp performance that much, when the performance difference is over 3 times faster without it.
Because now, that just completely ruins the idea of a “statically typed” language. “Why create a struct when we can just do JSON.parse
and use type checking?!”