sqf icon indicating copy to clipboard operation
sqf copied to clipboard

type hinting

Open Fusselwurm opened this issue 6 years ago • 5 comments

Hey, is there any way I can tell sqflint to assume a certain type for a var?

Explanation: I'm working with some code that is… rather dynamic, as in:

_foo = ["methodName", _parameters] call { 
    _this call (uiNamespace getVariable ["myScript", {}]); 
};

…after which sqflint assumes _foo to be of type Nothing. Which is not very helpful, because it will then go and cry wolf at every corner.

Being able to hint to a type would be neat, for example:

/*string*/ _foo = ["methodName", _parameters] call { 
    _this call (uiNamespace getVariable ["myScript", {}]); 
}

Fusselwurm avatar Sep 01 '18 18:09 Fusselwurm

From the code alone, I would argue that _foo should be Anything, not Nothing. I.e. there is nothing from the code that sugests that _foo will be assigned to Nothing.

LordGolias avatar Sep 01 '18 22:09 LordGolias

$ python sqflint.py <<SQF
> _foo = ["methodName", _parameters] call { 
>     _this call (uiNamespace getVariable ["myScript", {}]); 
> };
> if (_foo == "bar") then {
>     hint "x";
> };
> 
> SQF
[1,22]:warning:Local variable "_parameters" is not from this scope (not private)
[1,0]:warning:Local variable "_foo" assigned to an outer scope (not private)
[4,4]:warning:Local variable "_foo" is not from this scope (not private)
[4,9]:error:Binary operator "==" arguments must be [(String,String),(Config,Config),(Display,Display),(Control,Control),(TeamMember,TeamMember),(NetObject,NetObject),(Task,Task),(Location,Location),(Number,Number),(String,String),(Object,Object),(Group,Group),(Side,Side)] (lhs is Nothing, rhs is String)

:man_shrugging:

Fusselwurm avatar Sep 01 '18 22:09 Fusselwurm

Let me re-phrase: my argument is that this is a bug in the Linter: _foo should be Anything, not Nothing.

LordGolias avatar Sep 02 '18 15:09 LordGolias

yes. so on further experimentation, it seems tthat

_foo = call {
    call foo;
};
// _foo is Nothing
_foo = call {
    call foo
};
// _foo is Anything

Fusselwurm avatar Sep 02 '18 19:09 Fusselwurm

Now I see. So, we take the last statement of call to take its return type. It happens that the Linter currently takes ; to denote when we want to return Nothing instead of Anything (wrongly). As a consequence, it made _foo to be Nothing.

LordGolias avatar Sep 02 '18 19:09 LordGolias