The ref keyword doesn't work for int, string, vector, struct...
-
There were some discussions on Discord yesterday about the new ref keyword, and that it doesn't seem to work as people expect.
So I did some testing, and it seems that either it hasn't been clearly communicated on which type of data ref is supposed to work with, or there is a major bug in Fuze 2.15.
Take a look at this code:
function addTen(ref b) b += 10 return void a = 5 addTen(a) print(a) update() sleep(10)
I would expect this to output 15, but it actually outputs 5. The ref keyword doesn't have any effect on ints. This also applies to vectors and strings. As for structs, it also does not seem to work if the whole struct is changed, but it does work on the struct's members.
Maybe this was always how ref was supposed to work, but I think it seems counterintuitive, and it does not seem to align with how the ref keyword has been described. The Help page actually explicitly says that the parameter can be "Any type".
-
Thanks, this does sound like a major one to me. As far as I was aware it should apply to everything. Some oddities with complex structs wouldn't desperately surprise me, but it not working with int's certainly does.
-
I did try to ask a question about this in begin of June in this post: https://fuzearena.com/forum/topic/1166/the-ref-keyword-coming-to-fuze/16
Basically I asked what the behavior should be of the
assignRef
function in that code block, regarding this exact behavior.The reason that I had my suspicions that it would go this way, is because FUZE is very relaxed about assigning a different type to a variable. So remembering a ref but forgetting the type does not really rhyme well. But as I said in my first post in that threat: "I think that would be really cool." (if it would work the way Vinicity expected)
Since I didn't get a response to my question, I kind of already adjusted my expectations.
-
My example function doesn’t change the type, though.
It could, however, but even then I would expect it to work, and the only thing that should happen is a run-time error in case the user tries to use the changed variable incorrectly with regards to types...
That would be my expected behaviour for ref.