Skip to content

Commit c66d14f

Browse files
committed
chore: documentation
1 parent c2c63b2 commit c66d14f

File tree

1 file changed

+82
-3
lines changed

1 file changed

+82
-3
lines changed

README.md

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Also if you're using this library for a project, consider adding it (or asking m
3333
- [nogc strings](#nogc-strings)
3434
- [EmmyLua Annotations (IDE autocomplete)](#emmylua-annotations-ide-autocomplete)
3535
- [Nullable support](#nullable-support)
36+
- [Tuple support](#tuple-support)
37+
- [Tuple parsing behaviour](#tuple-parsing-behaviour)
3638
- [Projects](#projects)
3739
- [Contributing](#contributing)
3840

@@ -270,12 +272,14 @@ lua.doString(`
270272

271273
### Returning multiple values (statically)
272274

273-
A way of returning multiple values in a statically typed way, is to use `LuaMultiReturn` as your return value:
275+
A way of returning multiple values in a statically typed way, is to use `std.typecons.Tuple` as your return value:
274276

275277
```d
276-
LuaMultiReturn!(int, string, bool) multiReturn()
278+
import std.typecons : tuple
279+
280+
auto multiReturn()
277281
{
278-
return typeof(return)(20, "40", true);
282+
return tuple(20, "40", true);
279283
}
280284
281285
auto lua = new LuaState(null);
@@ -597,6 +601,81 @@ Then simply `require("api.lua")` in your lua code, et voila (hopefully).
597601

598602
Lumars natively supports Phobos' `Nullable` type.
599603

604+
If the `Nullable` is null: A Lua `nil` is used.
605+
606+
If the `Nullable` isn't null: The underlying value is used.
607+
608+
Most of the code should transparently support `Nullable`.
609+
610+
## Tuple Support
611+
612+
Lumars natively supports Phobos' `Tuple` type.
613+
614+
You can use this to easily return multiple values from a function in D.
615+
616+
You can also use this to easily parse multiple Lua values into a single D value.
617+
618+
Currently `Nullable` isn't supported within tuples very well.
619+
620+
Currently partial parsing (i.e. the tuple expects 2 values but only 1 is available) of tuples isn't supported, and will generate an exception, outside of the special case described below.
621+
622+
### Tuple Parsing behaviour
623+
624+
Normally Lumars will try to parse the top `n` (where `n` is the number of values in the tuple) values from the stack into the tuple, and if that fails either due to type mismatches, incorrect number of values, etc. then an exception is generated.
625+
626+
If:
627+
628+
* Lumars fails to parse `n` values from the top of the stack into a Tuple
629+
630+
* And the failure occurs for the 0th value
631+
632+
* And the 0th value is a table
633+
634+
* And the tuple has named fields (e.g. `Tuple!(int, "foo", int, "bar")`)
635+
636+
* Then Lumars will attempt to parse the 0th value into the tuple as if it were a struct.
637+
638+
```d
639+
import std.typecons : Tuple;
640+
641+
Tuple!(int, string, bool) multiReturn()
642+
{
643+
return typeof(return)(20, "40", true);
644+
}
645+
646+
auto lua = new LuaState(null);
647+
lua.register!multiReturn("multiReturn");
648+
lua.doString(`
649+
local i, s, b = multiReturn()
650+
assert(i == 20)
651+
assert(s == "40")
652+
assert(b)
653+
`);
654+
655+
alias Employee = Tuple!(int, "ID", string, "Name", bool, "FullTime");
656+
657+
lua.doString(`
658+
function employee1()
659+
return 15305, "Domain", true
660+
end
661+
662+
function employee2()
663+
return { ID = 15605, Name = "Range", FullTime = false }
664+
end
665+
`);
666+
667+
auto f = lua.globalTable.get!LuaFunc("employee1").bind!(Employee);
668+
auto g = lua.globalTable.get!LuaFunc("employee2").bind!(Employee);
669+
auto employee1 = f();
670+
auto employee2 = g();
671+
assert(employee1.ID == 15305);
672+
assert(employee1.Name == "Domain");
673+
assert(employee1.FullTime);
674+
assert(employee2.ID == 15605);
675+
assert(employee2.Name == "Range");
676+
assert(!employee2.FullTime);
677+
```
678+
600679
# Projects
601680

602681
- [Inochi Session](https://github.com/Inochi2D/inochi-session): A tool used to do live streaming with Inochi2D puppets, uses Lumars for plugins and expression bindings.

0 commit comments

Comments
 (0)