Null Setters should not be ignored

I have just found an annoying “silent trap” in haxe, that maybe extends to other languages, but never noticed it: null setters simply do nothing! I had n null setters like the following one

private var __cultureCode: String;
public var cultureCode(get, null): String;

I was trying to set __cultureCode (locally) by setting

cultureCode = "it-it";

instead of

__cultureCode = "it-it";

and the value was “obviously” not being saved, but I found out this only months later: luckily the affected variables were not used yet.
Now it is fixed, but what left me thoughtful is this question: “why the compiler doesn’t tell me anything if I am trying to set a null setter?”. Imho it would be more obvious to receive an error, the operation is wrong and easily causes hidden errors/bugs, you think you’re setting a value but you are just doing nothing.

What do you think about it?

If you remove private var __cultureCode:String

public var cultureCode(get, null):String;

This means cultureCode has a getter, but is read-only. It can be set privately, but not publicly.

private function get_cultureCode () {
    if (cultureCode == null) cultureCode = "en-US";
    return cultureCode;

If you prefer to make the variable read-only, but not otherwise need a getter, then you can use “default” instead of “get”

public var cultureCode(default, null):String;


I have a getter, I simple didn’t report it here :wink:

It was returning this

function get_cultureCode() : String {
 return cultureCode != null ? cultureCode : DEFAULT_CULTURE;

Now I see it was completely wrong, and it is still now, as it doesn’t set the default value even if I replaced the "cultureCode"s with “__cultureCode”.

I think I will remove the __xxx private var declarations and beat who adopted this approach.

It’s common in AS3, where there’s no other way to do read-only, but I avoid the extra variable where I can, since (default, null) is really just all-around nicer :slight_smile:

I agree, absolutely.

It sounds like you already figured this out, but I’ll post it in case someone else comes along with the same question:

You absolutely can set a (get, null) property. That’s why there’s no error message.

Yes, the error was in the getter: I replaced all getters with the one Joshua suggested.

I have always set variables with null setters, but this class (partially not mine) confused my mind: I have just came back from holidays, maybe my brain is still somewhere else :smiley:

Do you suggest a different title to avoid confusing someone?