-
Notifications
You must be signed in to change notification settings - Fork 48
Types
This is a page about supported type definitions in GMEdit.
These are used for smart completion and linter type checks.
Most of the Feather-compliant type names are also supported.
Casts to and from any type.
If type of something is not known, it is assumed to be any
.
Cannot be cast from or to.
Can be used to mark variables that should never be touched or as a function return value (or, well, lack thereof)
Includes reals and integers.
Currently an alias for number
, for convenience.
Boolean values (true
/false
).
Boolean and comparison operators all produce bool
return values.
You can disable implicit bool
<-> number conversion in linter preferences.
String values and literals.
The built-in undefined
value is this type.
array
is an array of anything (array_create(n)
or empty []
give you one).
array<T>
is an array with specific type inside.
You can cast array<int>
to array
and vice versa, but not array<int>
to array<string>
, for example.
You can also use T[]
in place of array<T>
for convenience.
Any object/instance. Handy for type constraints!
Any struct-based value.
Any resource - sprites, objects, etc.
Either of two or more types - for example, number|string
.
Parentheses are convenient for grouping - e.g. you can use (A|B|C)[]
to declare an array that contains values of types A, B, or C.
You can cast values of any of types to either-type, but casting from either-type to a specific type requires use of as
(or cast
).
Can also be used either<A, B...>
.
Typed signatures for built-in data structures.
ds_list<string>
is a list with strings in it,
and ds_map<string, number>
is a map
where keys are strings and values are numbers.
A function with a matching signature.
If a function returns nothing, returnType
can be void
.
For functions with trailing arguments, use rest<type>
.
Examples:
var _string_length:function<string, number> = string_length;
var _min:function<rest<number>, number> = min;
var _show_message:function<any, void> = show_message;
Same as above, but for constructors.
A constructor is a function, but a function is not necessarily a constructor.
returnType
can be used to accept only constructors inheriting from a specific type.
An array with a specific key/index type!
This is good for cases when the key can be cast to an integer (e.g. a resource ID) but doesn't implicitly cast to it as far as type checking goes, e.g.
var spriteData:ckarray<sprite, int> = ...;
var a = spriteData[spr_somme]; // OK
var b = spriteData[obj_some]; // shows a warning
var c = spriteData[0]; // also shows a warning
Same as above, but for structs (with struct[$ key]
access).
Specified type or undefined
.
T
can be cast to T?
, but casting T?
back to value requires as
- e.g. var v:?int;
.. i = v as int
.
A map with specific key-value pairs.
GMEdit uses this for different kinds of async_load
maps,
but you can also use these yourself if you want:
var _map:specified_map<i:int, s:string>;
_map = json_decode(@'{"i":1,"s":"hi!"}');
var i:int, s:string;
i = _map[?"i"];
i = _map[?"s"]; // will warn
s = _map[?"i"]; // will warn
s = _map[?"s"];
var x = _map[?"other key"]; // will warn
A reference to a type.
If you have a @hint
-based type, for example,
globalvar Some; Some = function(a, b) constructor {
// ...
}
/// @hint new Some(a, b)
/// @hint Some.staticFunc(a, b)
referencing Some
directly will have it typed as type<Some>
.
That is, a fixed-size array with per-index types.
Since arrays use less memory than structs, they can be beneficial for small sets of data.
Examples:
var v2:tuple<number, string> = [1, "hi!"];
v2[0] = ""; // will warn
v2[2] = 0; // also will warn
var v3:tuple<x:number, y:number, z:number> = [1, 2, 3]; // with field labels
If the last type is rest<something>
, the tuple will accept trailing values.
Automatic tuple for enum items.
This complements type magic.
Types for individual enum items can be defined as following:
enum v_enum_tuple {
an_int, /// @is {int}
a_string, /// @is {string}
sizeof
}
A struct containing field(s) from a type.
This is used for instance_create_depth
, for example.
Turns type parameters into argument types for rest
.
For example, if you do
/// @template {function} T
/// @param {T} func
/// @param {params_of<T>} ...args
function my_func() {}
then my_func(string_repeat, "", "")
will show a warning because
string_repeat
's second argument is a number, not a string.
Like above, but omits the last type parameter -
good for function<>
specifically (where the last parameter is the return type).
Special cases that were added for built-in functions but maybe you can use these too.
If your function has an argument typed as buffer_type
, setting a subsequent argument's or return's type to buffer_auto_type
will dynamically change it based on what type was passed in - e.g. int
for buffer_s32
or string
for buffer_string
.
/// @param {buffer} buf
/// @param {buffer_type} type
/// @param {buffer_auto_type} value
function my_buffer_write(buf, type, value) { ... }
// my_buffer_write(buf, buffer_s32, "hi") would show a warning
/// @param {buffer} buf
/// @param {buffer_type} type
/// @returns {buffer_auto_type} value
function my_buffer_read(buf, type) { ... }
// x = my_buffer_read(buf, buffer_string) would show a warning
GMEdit uses this for a lot of built-in buffer_*
functions
Used for the method()
function,
self
for method_auto_func
argument will be what you've used
for method_auto_self
argument (which should precede it).
- Smart auto-completion
- Types
- JSDoc tags (incl. additional ones)
- @hint tag (mostly 2.3)
- `vals: $v1 $v2` (template strings)
- #args (pre-2.3 named arguments)
- ??= (for pre-GM2022 optional arguments)
- ?? ?. ?[ (pre-GM2022 null-conditional operators)
- #lambda (pre-2.3 function literals)
- => (2.3+ function shorthands)
- #import (namespaces and aliases)
- v:Type (local variable types)
- #mfunc (macros with arguments)
- #gmcr (coroutines)