Allow programmer to pass by reference when wanted #4990
Replies: 8 comments
-
This would make the logic of the language dependant of a per-project setting, making code sharing more complicated and would introduce subtle, very opaque bugs. |
Beta Was this translation helpful? Give feedback.
-
Reference types are always an useful thing to have, but I suspect this would be hard to implement, specially if you want to reference between different scripting languages. But your example using arrays is not the best, since Object, Array and Dictionary are the variant types that are already only passed by reference, there's no way on gdscript to pass an array, object or dictionary by value. In fact, I usually use arrays as a container to pass by reference other pass-by-value types, like numbers. |
Beta Was this translation helpful? Give feedback.
-
But again, I think reference types are really useful in a lot of ways. You can have a function "return" several values by having "out" variables, which is just a reference with mandatory assignment. You can easily implement several helper functions that act upon it's parameters without having to worry if the caller will assign the return value back to the original variable. For example, you can implement yourself a substring method that, instead of returning a string copy, just overwrites the original string with the new value, which is useful for string cleanup functions, or string masking functions. |
Beta Was this translation helpful? Give feedback.
-
inout/volatile parameters and invisible mutable reference copies for basic primitive types (like numbers etc) are bad and make code harder to maintain and navigate because the flow of data through it is significantly more opaque than if the reference copies were explicit. Pointer types would be OK, but they don't fit in with the rest of gdscript's design, and you can just use an array with a single element in it instead anyway. You probably want reference wrapping (pointers/single-value arrays), tuple unpacking, and/or built-in-type method overrides (like how you can modify object prototypes in javascript) instead. |
Beta Was this translation helpful? Give feedback.
-
I know it's kind of hoping for a miracle, but it'd be nice if this was managed by a smarter compiler, instead. For example, when using an Array containing only one Variant to cleverly and intuitively pass said Variant by reference, the compiler would optimise the influenced parts of code to unaccount for the expensive creation and assignment of an array and directly affecting the singular Variant by reference. |
Beta Was this translation helpful? Give feedback.
-
I said I suspected it would be hard to implement but I failed to see the obviousness in my own words. If Object, Array and Dictionary are always passed by reference, and they are all variants, then it should not be hard to make any other variant types to be passable by reference. The problem pointed by wareya, that "inout/volatile parameters and invisible mutable reference copies for basic primitive types (like numbers etc) are bad and make code harder to maintain and navigate because the flow of data through it is significantly more opaque than if the reference copies were explicit" already has the solution it the phrase itself, you just have to make references explicit, like out parameters in C#, where syntax mandates usage of a new syntactic element both in the function definition and in the call site. If a new "ref" keyword where to be implemented, then it would have to be always used both on function definition and on call site. Another "copy" keyword could also exist to force shallow copies of arrays, dictionaries and objects. And the default behavior (without ref or copy keywords) would have to be like it is today: Object, Dictionary and Array are always passed by reference whereas other variant types are passed by value |
Beta Was this translation helpful? Give feedback.
-
This just generally seems outside the scope of GDScript to me. It's meant to be a simple language that allows for fast code writing, with ease of use prioritized over optimum performance, instead delegating high-performance operations to the C++ engine behind it. I'm not in favor of adding special keywords to handle passing by copy or reference for a given data type (nor even adding a special token for it). Wrapping a value type with an Array is a simple means by which to add reference-passing for value types and I don't think much more complexity than that is needed. Making it a clear-cut rule about what types are pass-by-reference ( The only thing I could really see a big use for with regard to reference types would be to have methods with multiple pass-by-reference |
Beta Was this translation helpful? Give feedback.
-
it would be good to have the option to pass by ref stuff that by default is passed by value
to pass by value stuff that is passed by reference by default we already have |
Beta Was this translation helpful? Give feedback.
-
My idea is to implement a new keyword called
ref
that works like in C#example:
this should be an advanced feature, so adding in project settings a option
enable_manual_referencing
(which is off by default) is important, so beginner users doesnt need to worry about this, and the language will pass arrays and dicts as reference by default, but when the option is enabled this is not true, the user must explicity callref
advantages:
we get more performance :D
we have more control over our code
Beta Was this translation helpful? Give feedback.
All reactions