You are viewing an older version of the site. Click here to view
the latest version of this page. (This may be a dead link, if so, try the root page of the docs
here.)
Strict mode places the compiler in an much more restrictive mode, where backwards compatibility is not as much of a
concern, and where things that would have been compiler warnings previously will be turned into compiler errors, and
generally causes the compiler to be more pedantic.
{{Warning|text=Note, putting your files in strict mode may require more work when upgrading, as your scripts may no
longer compile. Future additions to strict mode will be implemented, and if your scripts do not conform, you will
have to upgrade them immediately.}}
Conforming to strict mode has its advantages, however. Errors that would have gone unnoticed are highlighted much more
quickly. Strict mode enforces better programming practices and clearer and cleaner code, which the compiler has
the opportunity to do more optimizations on. Strict mode will be expanded
in the future, and those expansions will be listed below, so that you can begin to write conformant code, and it will
not break once strict mode expands to include those options. However, unlisted modes may be added in the future with no
notice, however, they will then be added to this documentation.
One guarantee is that code that is written to conform to strict mode will always run the same in non-strict mode.
Strict mode can be enabled per file with the appropriate [[File_Options|file option]] or globally with a config setting.
It can also be set as a runtime setting, but this will only benefit files that have not been compiled yet (includes,
eval'd code, etc). To enable this runtime setting, use:
Copy Code
== No Bare Strings ==
A bare string is a string without quotes.
Copy Code
Using bare strings runs the risk that an identifier (such as a keyword) will be introduced in the future, which has
the same name as the bare string. In this case, the compiler will use the functionality of the identifier, rather
than the string, and your code could suddenly change without you knowing, causing hard to diagnose bugs. Thus, in strict
mode, bare strings are not allowed, you must quote all strings.
== No Auto Concatenation ==
Auto concatenation is when two objects with no operator between them are taken to be concatenated, with a space added
between.
Copy Code
While less code to write, this has the unfortunate side effect that the whole script must be wrapped in a concatenation
block, which means that for each line of code, the concatenation effect must also run, even in cases where the
concatenation doesn't make sense. This adds overhead to the code for no extra value in 99% of cases, and so strict
mode disallows this feature, requiring explicit concatenation operators, at the benefit of decreased code runtime.
Note that even in non-strict mode, 100% proper use of semicolons will cause the code to run in the performant mode,
however, missing a semicolon will not cause any sort of warning, and so is difficult to enforce.
== Statement semicolons ==
Semicolons are used to denote end of statements. In a future strict mode update, they will be required, though
the specification for them is not fully complete. Where used, they currently do create statements, and this works in
non-strict mode as well.
See the article on [[Statements]] for a fuller treatment of statements.
Strict mode simply requires this on every statement, which can help ensure you as a programmer have the correct habit,
so that the possibly ambiguous cases are less likely to happen.
Some languages solve this same problem with newlines, instead of semicolons. However, MethodScript has a design
principal which states that whitespace should never be used as code.
== Object typing ==
(not implemented yet)
In strict mode, all newly defined variables (and procedure definitions) must be typed. For instance:
Copy Code
Adding type safety to your code is a well documented advantage, as it allows the compiler to more easily and quickly
detect invalid code, as you cannot in the future assign a value of a different type to a variable that was defined with
another type. In non-strict mode, types are still allowed, and have the same behavior, but untyped values are assumed
to have the type ''auto''. This type is still allowed in strict mode, but the declaration must be explicit.
Copy Code
Please see the page on [[Cross_Casting|cross casting]] for a further discussion on the auto keyword.
== Compiler Warnings ==
Compiler warnings, such as deprecations and others are normally warned about, but compilation continues. In strict mode,
these warnings instead trigger a compile error, and must be fixed immediately. However, in combination with the
suppressWarnings file option, individual warnings can be ignored, and these will trigger neither a compiler warning or
a compiler error.
set_runtime_setting('system.strict_mode.enabled', true);

1 {{function|set_runtime_setting}}('system.strict_mode.enabled', {{keyword|true}});
string s = bareString;
string s = "notABareString";
string s = 'alsoNotABareString';

1 {{object|string}} s = bareString;
2 {{object|string}} s = "notABareString";
3 {{object|string}} s = 'alsoNotABareString';
msg('this' 'is' 'concatenated'); // auto concatenation
msg('this' . ' is' . ' also' . ' concatenated'); // explicit concatenation

1 {{function|msg}}('this' 'is' 'concatenated'); // auto concatenation
2
3 {{function|msg}}('this' . ' is' . ' also' . ' concatenated'); // explicit concatenation
string @s;
string @a = 'a';
int @i = 5;
int proc _myproc(string @m) {
return 5;
}

1 {{object|string}} @s;
2 {{object|string}} @a = 'a';
3
4 {{object|int}} @i = 5;
5
6 {{object|int}} {{keyword|proc}} {{function|_myproc}}({{object|string}} @m) {
7 {{keyword|return}} 5;
8 }
auto @x = 'string';
@x = 5;
@x = array();

1 {{keyword|auto}} @x = 'string';
2 @x = 5;
3 @x = {{function|array}}();
Find a bug in this page? Edit this page yourself, then submit a pull request.