The ref keyword coming to FUZE
-
@PB____
i would say you got it. i think a better example however would be to use two independent variables and see how they behave. i think you understand where i am going with this, but for the sake of everyone else who might find this helpful.function addOneCopy(input) input += 1 return void function addOneRef(ref input) input += 1 return void a = 1 b = 1 addOneCopy(a) addOneRef(b) print("var a is: ", a, "\n") // <-- should print 1 print("var b is: ", b, "\n") // <-- should print 2 update()
as you can see the first function takes the value of
a
and copies it toinput
then increments that value by 1 and returns out of the function.
the results of which leaving the original value ofa
untouched.the second function takes the location of (aka makes a reference back to )
b
and assigns it toinput
then increments the value stored at the location and returns out of the function.
the result of which increments the value ofb
by 1. -
@thedos
Good point, if the reference remains intact on assignment, then it would make sense to use the ref keyword for primitive data types as well, and then using an int as an example would simplify the example in code :)An off topic side note:
The
++
operator doesn't currently exist in FUZE, and it's arguable if you would want this in a loosely typed language like FUZE. To make my point:var value1 = "1" var value2 = value1++ // what are the values of value1 and value2?
What would be the values after running this code?
value1
could be2
or"11"
or even worse:"2"
value2
could be"1"
or (and although dirty, this expectation is not invalid)1
If the types don't match, there is just a lot going on with these operators that even some professional developers aren't always aware of. So leaving the operator out, even though it is quite convenient to use, seems like an understandable decision in my opinion.
-
@PB____
good catch on++
, guess my head wasn't quite out of c++ syntax. i updated my post to correct that.to answer your question on the incremental operator, it should throw an error. using
++
on a string isn't something that is possible as far as i am aware. although you could probably implement++
most loosely typed languages (python, etc) also utilize the+=
for the reasons you mentioned.for sake of conversation however, if they were both variables were
int
, then in your scenariovalue1
would be2
andvalue2
would in fact be1
. to havevalue2
be2
then it would need to bevalue2 = ++value1
-
guess my head wasn't quite out of c++ syntax
That's why I thought it would be interesting to have this discussion. Since there will be a variety of backgrounds and thus expectations to how things behave. But I also think it will help to improve acceptance to the new language feature.
The natural response when you are confronted with something that looks familiar by association, but does not behave as expected, is disgust. It's a defensive response for situations where something is off, but you're not sure what. Of course "disgust" is a strong word, and we don't normally value by one indicator. I just think it helps to have an open mind towards the update :)
++ operator discussion
I think this is an interesting discussion, especially since FUZE does not have this operator. I do feel that this discussion is off topic, but it seems most natural to respond in here anyway.My argument regarding this operator was indeed within the context of FUZE, that is loosely typed. I absolutely agree that the
++
operator works perfectly well inC++
. But FUZE is an entirely different animal.A problem I see with throwing an error for incorrect types, is that the debugging capabilities in FUZE are not the best in the industry yet. One could also wonder similar questions about floats and even vectors I guess.
Also because FUZE is loosely typed, a mismatch in type could occur in very specific scenarios that might not occur during play testing. Since FUZE in general is used for entertainment, not business applications, it would be more fun if the game would continue with an unforeseen bug, rather than crash with an error. However, during development, you'd want to know that something is wrong.
In JavaScript
var value2 = value1++
would be equivalent to:value1 = Number(value1) var value2 = value1 value1 = value1 + 1
So here the ++ operator would first cast value1 to a Number, before assigning it to value2. Then after assigning to value2, value1 is modified again for the increment. As I said, this feels dirty, but as an expectation by a developer, not invalid.
I guess the scenario
value2 = ++value1
is more simple in any case. Because, after the fact, both value1 and value2 should be equal, both by type and by value.For these reasons, I am, mildly, opposed to introducing the
++
/--
operators at all. But if the operators would be introduced to FUZE, I'd probably use them. But I don't need them.But at this point, I do feel like I'm rambling on and am off topic.
EDIT: I've edited this post a couple of times, but mostly to improve how the message comes across. The message itself generally stayed the same.
I would also like to add, that although I say I am opposed to introducing ++, I would be happy to use it at the same time :-)