2021-05-19, 11:25 AM
2021-05-17: Passing Normal Functions As Callbacks
Functions that most people think take inline functions can also take public functions with using callback (or using public):
This does need an explicit type (iiiis) because the types of publics can?t be automatically determined in the same way as for inlines.? With an inline the type is checked and if the parameters don?t match a warning is given.? However, with a public, you specify the type manually, so if it is wrong there should be no warning (you basically promise the compiler it is called his way).? Fortunately, this code uses addressof underneath, which generates code to check the call is correct - so if the specified type is wrong here the compiler actually gives an error, thus re-introducing lovely type-checking.
Because the call uses addressof, you can extend this to another type of function - bare functions. These are sometimes called stocks, but they are not stocks - that keyword is only used in some situations (mainly libraries):
This has three advantages:
Functions that most people think take inline functions can also take public functions with using callback (or using public):
Quote:
public Response(playerid, dialogid, response, listitem, string:inputtext[])
{
}
main()
{
____Dialog_ShowCallback(playerid, using callback Response<iiiis>, DIALOG_STYLE_INPUT, "Title", "Contents", "OK");
}
This does need an explicit type (iiiis) because the types of publics can?t be automatically determined in the same way as for inlines.? With an inline the type is checked and if the parameters don?t match a warning is given.? However, with a public, you specify the type manually, so if it is wrong there should be no warning (you basically promise the compiler it is called his way).? Fortunately, this code uses addressof underneath, which generates code to check the call is correct - so if the specified type is wrong here the compiler actually gives an error, thus re-introducing lovely type-checking.
Because the call uses addressof, you can extend this to another type of function - bare functions. These are sometimes called stocks, but they are not stocks - that keyword is only used in some situations (mainly libraries):
Quote:
Response(playerid, dialogid, response, listitem, string:inputtext[])
{
}
main()
{
____Dialog_ShowCallback(playerid, using function Response<iiiis>, DIALOG_STYLE_INPUT, "Title", "Contents", "OK");
}
This has three advantages:
- Reduced scope.? You can declare the functions as static if you need.
- Reduced headers.? The Response function is no longer placed in the publics section of the AMX header.
- No forward.? Some people really really hate writing this keyword, now you don?t need to.