On Tuesday, 6 June 2017 at 18:08:52 UTC, Ali Çehreli wrote:
Just import modules at local scopes. Here is something that
works:
void displayinfo(T)(T v) {
import std.stdio : writefln;
writefln("%08x", v);
}
void foo() {
import std.meta : AliasSeq;
enum value = cast(ubyte[])x"33 3a 3f d4";
foreach (type; AliasSeq!(int, uint, byte)) {
static if (value.length == type.sizeof) {
import std.bitmanip : littleEndianToNative;
pragma(msg, "working with " ~ type.stringof);
ubyte[type.sizeof] raw = value;
auto fValue = raw.littleEndianToNative!type;
displayinfo(fValue);
break;
}
}
}
void main() {
foo();
}
Ali
Sorry, it was probably a bad example.
The value ubyte[] array read at run-time.
The way of Patrick Schluter works!
```
void displayinfo(T)(T ff) {
writeln(ff);
}
switch (varType)
{
import std.meta;
foreach (type; AliasSeq!("int", "uint", "byte"))
{
pragma(msg, type);
mixin(`case `~type~`.stringof:
if (value.length == `~type~`.sizeof)
{
ubyte[`~type~`.sizeof] raw =
value[0..$];
auto fValue =
raw.littleEndianToNative!(`~type~`);
displayinfo(fValue);
}
break;`);
}
```
I'm trying to detect whether the type is `uint`, but the
following does not seem to work:
```
static if (is (type == uint))
{
pragma(msg, "isi uint");
assert(0);
}
```
The reason is that I'd like to have the cases:
- `case "uint":`
- `case "unsigned int":`
for the uint loop turn.