53 lines
1.5 KiB
Markdown
53 lines
1.5 KiB
Markdown
---
|
|
weight: 100
|
|
title: "MV_INLINE"
|
|
description: "Make preprocessor macros"
|
|
icon: "code_blocks"
|
|
date: "2025-05-01T18:47:58-05:00"
|
|
lastmod: "2025-05-01T18:47:58-05:00"
|
|
---
|
|
|
|
`declare function MV_INLINE<A..., R...>(inlineFunction: (A...) -> R...): (A...) -> R...`
|
|
|
|
will mark the passed function to be inlined where used.
|
|
|
|
Some limitations apply:
|
|
- The passed function cannot be variadic (i.e. no ... usage)
|
|
- The passed function cannot tailcall or otherwise recursively call itself.
|
|
- You must use this macro in it's specific form, i.e. `local <var> = MV_INLINE(function() ... end)`
|
|
|
|
Note that this pass acts more like a preprocessor that replaces the function calls with the results of the function (if any), and can have strange effects in some cases.
|
|
|
|
For example, when an inlined function is called, its function body is inserted right before the statement the call is in. This can lead to some unexpected behavior when used in `or` or `if` expressions:
|
|
|
|
```lua
|
|
local called = false
|
|
local testFunc = MV_INLINE(function()
|
|
called = true
|
|
return 'hi'
|
|
end)
|
|
|
|
local foo = 'bar' or testFunc()
|
|
print(foo, called)
|
|
```
|
|
|
|
Will print `bar true`, where under normal circumstances testFunc should not be executed. This is because the `or` expression is evaluated after the inserted body.
|
|
|
|
## Valid Usage
|
|
|
|
```lua
|
|
local testFunc = MV_INLINE(function(start, endn)
|
|
local total = 0
|
|
for i = start, endn do
|
|
total = total + i
|
|
end
|
|
return total
|
|
end)
|
|
|
|
local testFunc2 = MV_INLINE(function()
|
|
return testFunc(1, 10), testFunc(11, 20), testFunc(1, 15)
|
|
end)
|
|
|
|
print(testFunc2())
|
|
```
|