TheGameCreators / AGK-Studio

3 stars 1 forks source link

Would like an #option setting to require variables to be explicitly typed. #887

Open adambiser opened 3 years ago

adambiser commented 3 years ago

This compiles without error:

#option_explicit
global myvariable

Expected result: a compilation error saying "myvariable has not been defined". Using global inside a function also allows the variable to circumvent #option_explicit.

Calling myvariable = "asdf" afterward will cause an error because the variable is actually being typed to an integer.

adambiser commented 3 years ago

This also happens with "local" and type declarations

#option_explicit
type testtype
    x
endtype

This should raise an error, but doesn't.

PaulSJ commented 3 years ago

Both of those count as being defined. Using myvariable = "asdf" by itself without the global definition would trigger the error. Missing off the type (integer, float, etc) gives it the default type but it is still being defined.

adambiser commented 3 years ago

Even with #option_explicit used?

Considering global myvariable an integer is implicit, not explicit.

PaulSJ commented 3 years ago

The type may be implicit but the definition of the variable is explicit.

adambiser commented 3 years ago

Not requiring a type works for languages like VB6 that have a Variant type since myvariable = 1 and myvariable = "hello" both work for Dim myvariable. (Admittedly terrible practice, since Variant is slow.)

Was just hoping to avoid hair-pulling errors like this:

#option_explicit
global playerx
inc playerx, 0.25

Then can this request be changed to "please add #option_strict to act like #option_explicit, but also force the explicit declaration of variable types"?

(I realize that "Option Strict" in .NET also prevents passing floats to integer parameters, but whatever.)

PaulSJ commented 3 years ago

It's possible the feature could be expanded

adambiser commented 3 years ago

Thanks, Paul.