Re: template/mixin magic for to! auto inferring type from variable
On Friday, 2 February 2024 at 23:25:37 UTC, Chris Katko wrote: The auto solution won't work for a struct however which I'm using: ```D struct procTable{ //contains all the fields inside a file I'm parsing uint time; int priority; string name; // etc } ``` Maybe you can use `typeof` in that case? ```d procTable pt; pt.time = to!(typeof(pt.time))(data[1]); // etc ``` ...although I guess then you're repeating the field name, which isn't great either. You could avoid the repetition by wrapping the above pattern up in a helper function, though: ```d void convertAssign(Dest, Value)(ref Dest dest, Value value) { import std.conv; dest = to!Dest(value); } void main() { string[3] data = ["100", "-5", "foobar"]; uint time; int priority; string name; time.convertAssign(data[0]); priority.convertAssign(data[1]); name.convertAssign(data[2]); assert(time == 100); assert(priority == -5); assert(name == "foobar"); } ```
Re: template/mixin magic for to! auto inferring type from variable
On Friday, 2 February 2024 at 21:01:53 UTC, Paul Backus wrote: No, D only does bottom-up type inference, not top down. If you want to avoid repeating the type, use `auto` on the left side: ```d auto time = to!uint(data[1]); auto priority = to!int(data[2]); ``` Okay thanks. It finally clicked what bottom-up/top-down type interference is. The auto solution won't work for a struct however which I'm using: ```D struct procTable{ //contains all the fields inside a file I'm parsing uint time; int priority; string name; // etc } ```
Re: template/mixin magic for to! auto inferring type from variable
On Friday, 2 February 2024 at 07:43:09 UTC, Chris Katko wrote: Is there some way to do: ```D string[3] data; //strings from some file input, some are ints, uints, etc. auto into!(T)(T value){return to!???(value); } // ??? uint time = into!(data[1]); // We already know this is uint int priority = into!(data[2]); ``` instead of: ```D uint time = to!uint(data[1]); // specifying type twice int priority = to!int(data[2]) ``` No, D only does bottom-up type inference, not top down. If you want to avoid repeating the type, use `auto` on the left side: ```d auto time = to!uint(data[1]); auto priority = to!int(data[2]); ```
Re: Scripting with Variant from std.variant: parameter passing
On Friday, 2 February 2024 at 20:28:50 UTC, Carl Sturtivant wrote: On Friday, 2 February 2024 at 19:22:22 UTC, Steven Schveighoffer wrote: ```d // shim auto foo(Args...)(Args args) if (!allSatisfy!(isVariant, Args)) { mixin("return foo(", argsAsVariants(args.length), ");"); } ``` Thanks for this idea. I'll work on it. Another variation on the same theme: ```d void foo(Variant x, Variant y) { import std.stdio: writeln; writeln("x = ", x); writeln("y = ", y); } /// map over a variadic argument list template mapArgs(alias fun) { auto mapArgs(Args...)(auto ref Args args) { import std.typecons: tuple; import core.lifetime: forward; import std.meta: Map = staticMap; auto ref mapArg(alias arg)() { return fun(forward!arg); } return tuple(Map!(mapArg, args)); } } import std.variant: Variant; import std.meta: allSatisfy; enum isVariant(T) = is(T == Variant); auto foo(Args...)(Args args) if (!allSatisfy!(isVariant, Args)) { return .foo(mapArgs!Variant(args).expand); } void main() { foo(123, 456); foo("hello", "world"); } ```
Re: Scripting with Variant from std.variant: parameter passing
On Friday, 2 February 2024 at 19:22:22 UTC, Steven Schveighoffer wrote: ```d void foo(Variant x, Variant y) { ... } import std.meta : allSatisfy; enum isVariant(T) = is(T == Variant); // this is going to suck at CTFE but... string argsAsVariants(size_t count) { import std.format; import std.range; import std.alglorithm; import std.array; return iota(count).map!(i => format("Variant(args[%s])", i).join(","); } // shim auto foo(Args...)(Args args) if (!allSatisfy!(isVariant, Args)) { mixin("return foo(", argsAsVariants(args.length), ");"); } ``` Thanks for this idea. I'll work on it. -Steve
Re: Scripting with Variant from std.variant: parameter passing
On Friday, 2 February 2024 at 08:22:42 UTC, Carl Sturtivant wrote: It seems I cannot pass e.g. an int argument to a Variant function parameter. What's the simplest way to work around this restriction? You'd have to implement the function that accepts the parameters and wraps in a Variant. This is the best I can come up with, which should be copy/pasteable to other shims: ```d void foo(Variant x, Variant y) { ... } import std.meta : allSatisfy; enum isVariant(T) = is(T == Variant); // this is going to suck at CTFE but... string argsAsVariants(size_t count) { import std.format; import std.range; import std.alglorithm; import std.array; return iota(count).map!(i => format("Variant(args[%s])", i).join(","); } // shim auto foo(Args...)(Args args) if (!allSatisfy!(isVariant, Args)) { mixin("return foo(", argsAsVariants(args.length), ");"); } ``` -Steve
profiling vibe
I can't seem to be able to use `--profile` with vibe: ```shell dub init -t vibe.d dub build --build=profile ../../.dub/packages/vibe-core/2.7.3/vibe-core/source/vibe/internal/async.d-mixin-119(142,3): Warning: statement is not reachable ../../.dub/packages/vibe-core/2.7.3/vibe-core/source/vibe/internal/async.d-mixin-119(143,3): Warning: statement is not reachable ../../.dub/packages/vibe-core/2.7.3/vibe-core/source/vibe/internal/async.d-mixin-119(142,3): Warning: statement is not reachable ../../.dub/packages/vibe-core/2.7.3/vibe-core/source/vibe/internal/async.d-mixin-119(143,3): Warning: statement is not reachable ../../.dub/packages/vibe-core/2.7.3/vibe-core/source/vibe/internal/async.d-mixin-119(142,3): Warning: statement is not reachable ../../.dub/packages/vibe-core/2.7.3/vibe-core/source/vibe/internal/async.d-mixin-119(143,3): Warning: statement is not reachable ../../.dub/packages/vibe-core/2.7.3/vibe-core/source/vibe/internal/async.d-mixin-119(142,3): Warning: statement is not reachable ../../.dub/packages/vibe-core/2.7.3/vibe-core/source/vibe/internal/async.d-mixin-119(143,3): Warning: statement is not reachable ../../.dub/packages/vibe-core/2.7.3/vibe-core/source/vibe/internal/async.d-mixin-119(142,3): Warning: statement is not reachable ../../.dub/packages/vibe-core/2.7.3/vibe-core/source/vibe/internal/async.d-mixin-119(143,3): Warning: statement is not reachable ../../.dub/packages/vibe-core/2.7.3/vibe-core/source/vibe/internal/async.d-mixin-119(142,3): Warning: statement is not reachable ../../.dub/packages/vibe-core/2.7.3/vibe-core/source/vibe/internal/async.d-mixin-119(143,3): Warning: statement is not reachable ../../.dub/packages/vibe-core/2.7.3/vibe-core/source/vibe/internal/async.d-mixin-119(142,3): Warning: statement is not reachable ../../.dub/packages/vibe-core/2.7.3/vibe-core/source/vibe/internal/async.d-mixin-119(143,3): Warning: statement is not reachable ../../.dub/packages/vibe-core/2.7.3/vibe-core/source/vibe/internal/async.d-mixin-119(142,3): Warning: statement is not reachable ../../.dub/packages/vibe-core/2.7.3/vibe-core/source/vibe/internal/async.d-mixin-119(143,3): Warning: statement is not reachable ../../.dub/packages/vibe-core/2.7.3/vibe-core/source/vibe/internal/async.d-mixin-119(142,3): Warning: statement is not reachable ../../.dub/packages/vibe-core/2.7.3/vibe-core/source/vibe/internal/async.d-mixin-119(143,3): Warning: statement is not reachable Error: warnings are treated as errors Use -wi if you wish to treat warnings only as informational. ``` I've tried using `buildRequirements "allowWarnings"`, but still the same. Jordan
Re: sokol-d: Static Struct
On Saturday, 30 December 2023 at 19:27:08 UTC, Matheus Catarino wrote: Hi everyone. Currently I'm working on D binding for sokol project (truly a dual bindgen [sokol-tools, sokol-header]) which could be merged into the upstream project. Up to now, my "ideal" configuration has been to run all the ported examples (from other supported bindings) on Drt or betterC provided @safe has DIP1000 enabled (not obligatory). Except for the "clear" example (no have structs) works, the rest of the examples have partial execution, without 2D/3D animations, like the original ported examples. e.g.: https://github.com/kassane/sokol-d/blob/main/src/examples/sgl_context.d Finally fixed!! Works in, linux/macos/windows/wasm32(need emsdk) I was having trouble with the floating/double init (nan) and solved this issue by giving default values to structs fields. ![](https://github.com/kassane/sokol-d/assets/6756180/198f1b6b-26c0-4dfc-b372-533df1bc77d3)
Re: Scripting with Variant from std.variant: parameter passing
On Friday, 2 February 2024 at 08:22:42 UTC, Carl Sturtivant wrote: It seems I cannot pass e.g. an int argument to a Variant function parameter. What's the simplest way to work around this restriction? The easiest thing would be to actually pass it a `Variant` with `someFunction(Variant(myInt))`. The more-involved thing would be to write a template constrained to non-`Variants` that does the above for you. ```d auto someFunction(T)(T t) if (!is(T : Variant)) { return someFunction(Variant(t)); } auto someFunction(Variant v) { // ... } void main() { someFunction(42); someFunction("hello"); someFunction(3.14f); someFunction(true); someFunction(Variant(9001)); } ```
Re: Scripting with Variant from std.variant: parameter passing
On Friday, 2 February 2024 at 08:22:42 UTC, Carl Sturtivant wrote: It seems I cannot pass e.g. an int argument to a Variant function parameter. What's the simplest way to work around this restriction? Just tell the compiler clearly what you want. ```d import std; void f(Variant x) { writeln(x); } void main() { f( Variant(42)); f( Variant(2.5) ); f( Variant("Hi!") ); } ```
Scripting with Variant from std.variant: parameter passing
It seems I cannot pass e.g. an int argument to a Variant function parameter. What's the simplest way to work around this restriction?