-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Weird behaviour with setters calling associated getter #1002
Comments
It seems that void test2__Test___setvalue___impl(test2__Test* this, lang_Numbers__Int x) {
if (this->value == 2) {
lang_String__String_println(__test2_strLit2);
} From // We are in a setter/getter and we're having a variable access. That means
// the property is not virtual.
ref as PropertyDecl setVirtual(false)
|
Yes, I'm pretty sure this is expected behavior (accessing the property inside the property decl returns the actual backing variable). |
But, why? I see no logic in reading the "backing variable" in the setter when, as I have written the code, I would not expect any variable called Also, I would expect this code to give the same result as example 1, but it does not: Test: class {
value: Int {
get { return 2 }
set (x) {
if (this getTheGetter() == 2)
"two" println()
else
"not two, but: %i" printfln(this value)
}
}
init: func
getTheGetter: func -> Int {
this value
}
}
x := Test new()
x value = 10 That code, in fact, gives me "two". Is there some logic to this that I'm missing? :) |
This code results in fetching two because of the indirection. I do think setters could use the getter function when just accessing the property instead of the backing variable, as you've brought out valid points. I'm trying to think of some weird edgecases that would break but I don't think there are any. EDIT: Btw, "expected behavior" was referring to the way rock currently handles this case, not the most intuitive way it could be done. |
The following code public class Test
{
public int X
{
get { return 2; }
set
{
System.Console.WriteLine("old value={0}", this.X);
}
}
}
public class Hello
{
public static void Main()
{
Test test = new Test();
test.X = 3;
System.Console.WriteLine("X={0}", test.X);
}
} prints
as expected. (In C# I cannot assign |
I'd argue it's a bit different still, because backing properties are explicit in (non-bleeding-edge versions of) c#. |
Ah, I though non-virtual/backing properties were automatically inferred in C# as well, guess I was wrong :) |
Example 1 (associated getter):
should print "two" but prints "not two, but: 0".
Example 2 (setter calling itself):
Same here. But:
should always print "old value=2" but prints
The text was updated successfully, but these errors were encountered: