You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
274
276
275
277
```d
276
-
LuaMultiReturn!(int, string, bool) multiReturn()
278
+
import std.typecons : tuple
279
+
280
+
auto multiReturn()
277
281
{
278
-
return typeof(return)(20, "40", true);
282
+
return tuple(20, "40", true);
279
283
}
280
284
281
285
auto lua = new LuaState(null);
@@ -597,6 +601,81 @@ Then simply `require("api.lua")` in your lua code, et voila (hopefully).
597
601
598
602
Lumars natively supports Phobos' `Nullable` type.
599
603
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
+
600
679
# Projects
601
680
602
681
-[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