Skip to content
Vadym Diachenko edited this page Dec 13, 2024 · 2 revisions

This is a page about supported type definitions in GMEdit.

These are used for smart completion and linter type checks.

Basics

Most of the Feather-compliant type names are also supported.

any

Casts to and from any type.

If type of something is not known, it is assumed to be any.

void

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)

number

Includes reals and integers.

int

Currently an alias for number, for convenience.

bool

Boolean values (true/false).

Boolean and comparison operators all produce bool return values.

You can disable implicit bool <-> number conversion in linter preferences.

string

String values and literals.

undefined

The built-in undefined value is this type.

array, array<T>, T[]

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.

object

Any object/instance. Handy for type constraints!

struct

Any struct-based value.

asset

Any resource - sprites, objects, etc.

A|B, (A|B)

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...>.

ds_map<K, V>, ds_list<T>, ds_grid<T>, etc.

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.

function<argType1, argType2, ..., returnType>

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;

constructor<argType1, argType2, ..., returnType>

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.

GMEdit conveniences

ckarray<K, V>, CustomKeyArray<K, V>

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

ckstruct<K, V> or CustomKeyStruct<K, V>

Same as above, but for structs (with struct[$ key] access).

T? or Null<T>

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.

specified_map<...field:type, defaultType>

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

type<T>

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>.

[A, B] or tuple<A, B>

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.

tuple<enum_name> or enum_name

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
}

any_fields_of<T>

A struct containing field(s) from a type.

This is used for instance_create_depth, for example.

params_of<T>

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.

params_of_nl<T>

Like above, but omits the last type parameter - good for function<> specifically (where the last parameter is the return type).

Situational

Special cases that were added for built-in functions but maybe you can use these too.

buffer_auto_type: a special type for buffer-related functions.

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

method_auto_self<T> and method_auto_func<T>

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).

Better workflow:

Syntax extensions:

  • `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)

Customization:

User-created:

Other:

Clone this wiki locally