Re: How can i find my LAN IP Address using std.socket?
On Tuesday, 4 February 2014 at 13:02:26 UTC, TheFlyingFiddle wrote: I'm trying to find my own ip address using std.socket with little success. How would i go about doing this? (It should be a AddressFamily.INET socket) Sadly, the standard library doesn't seem to offer network interface enumeration. However, if you don't mind delving into platform-specific C APIs that are exposed by Phobos but not mentioned in the docs ([issue 5872](https://issues.dlang.org/show_bug.cgi?id=5872)), it can be done. The code I found here was enough to get me started: https://github.com/Kripth/my-ip/blob/356e02f0/src/myip/private_.d The key to a linux implementation was `getifaddrs()`, which can be found in core.sys.linux.ifaddrs.
Re: std.string.assumeUTF() silently casting mutable to immutable?
On Wednesday, 14 February 2024 at 10:57:42 UTC, RazvanN wrote: This has already been fixed, you just need to use -preview=fixImmutableConv. This was put behind a preview flag as it introduces a breaking change. I just tried that flag on run.dlang.org, and although it fixes the case I posted earlier, it doesn't fix this one: ```d string test(const(ubyte)[] arr) { import std.string; return arr.assumeUTF; } ``` Shouldn't this be rejected as well?
Re: std.string.assumeUTF() silently casting mutable to immutable?
On Tuesday, 13 February 2024 at 14:05:03 UTC, Johan wrote: On Tuesday, 13 February 2024 at 08:10:20 UTC, Jonathan M Davis wrote: So, there's definitely a bug here, but it's a dmd bug. Its checks for whether it can safely change the constness of the return type apparently aren't sophisticated enough to catch this case. This is a pretty severe bug. Thanks, gents. Reported on the tracker: https://issues.dlang.org/show_bug.cgi?id=24394
std.string.assumeUTF() silently casting mutable to immutable?
I may have found a bug in assumeUTF(), but being new to D, I'm not sure. The description: Assume the given array of integers arr is a well-formed UTF string and return it typed as a UTF string. ubyte becomes char, ushort becomes wchar and uint becomes dchar. Type qualifiers are preserved. The declaration: ```d auto assumeUTF(T)(T[] arr) if (staticIndexOf!(immutable T, immutable ubyte, immutable ushort, immutable uint) != -1) ``` Shouldn't that precondition's `immutable T` be simply `T`? As it stands, I can do this with no complaints from the compiler... ```d string test(ubyte[] arr) { import std.string; return arr.assumeUTF; } ``` ...and accidentally end up with a "string" pointing at mutable data. Am I missing something?
Getting a Type from TypeInfo / Getting Variant Type
I'm using std.variant.Variant to hold a value of unknown type (not a string, could be a numeric type or a container holding multiple numeric types). I'm trying to retrieve this value with .get!T but I need the type to do that... .type gives me TypeInfo, but that's not a type so I'm not sure how to get the type without checking the TypeInfo against all possible types (which would be a massive pain).
Re: Variadic Mixin/Template classes?
On Saturday, 25 November 2017 at 10:08:36 UTC, vit wrote: On Saturday, 25 November 2017 at 09:52:01 UTC, Chirs Forest wrote: [...] import std.meta : staticMap; class Bar(T) { T bar; } class Foo(Ts...){ staticMap!(Bar, Ts) bars; this(){ static foreach(i, alias T; Ts) bars[i] = new Bar!T; } } void main(){ auto foo = new Foo!(string, int, string, ubyte[2]); foo.bars[0].bar = "hello"; foo.bars[1].bar = 23; foo.bars[2].bar = "hello"; foo.bars[3].bar[0] = 88; foo.bars[3].bar[1] = 99; auto foo2 = new Foo!(ubyte, string); foo2.bars[0].bar = 9; foo2.bars[1].bar = "world"; } thankyou!
Variadic Mixin/Template classes?
I'd like to make a class that takes multiple template types (1 - several) which can hold an array/tuple of a second class that are instantiated with those types. class Bar(T) { T bar; } class Foo(T[]){ // not sure how to take variadic types here? Bar!(?)[] bars; //not sure how I'd define an array/tuple of multiple types this(){ foreach(int i, b; bars) b = new Bar!T[i]; } } void main(){ auto foo = new Foo!(string, int, string, ubyte[2]); foo.bars[0].bar = "hello"; foo.bars[1].bar = 23; foo.bars[2].bar = "hello"; foo.bars[3].bar[0] = 88; foo.bars[3].bar[1] = 99; auto foo2 = new Foo!(ubyte, string); foo.bars[0].bar = 9; foo.bars[1].bar = "world"; }
Turn a float into a value between 0 and 1 (inclusive)?
I'm interpolating some values and I need to make an (elapsed_time/duration) value a float between 0 and 1 (inclusive of 0 and 1). The elapsed_time might be more than the duration, and in some cases might be 0 or less. What's the most efficient way to cap out of bounds values to 0 and 1? I can do a check and cap them manually, but if I'm doing a lot of these operations I'd like to pick the least resource intensive way. Also, if I wanted out of bounds values to wrap (2.5 becomes 0.5) how would I get that value and also have 1.0 not give me 0.0?
Re: Class instance becoming null after calling bindings to C code???
On Wednesday, 15 November 2017 at 13:30:14 UTC, Adam D. Ruppe wrote: On Wednesday, 15 November 2017 at 13:24:02 UTC, Chirs Forest wrote: class Audio { Audio a; writeln(); // 281478 is usually wrong. That's the address of the reference, not of the actual object. You might want `cast(void*) a` instead to print the address of the object itself. writeln(); // null Though why it would ever say null is weird. Maybe one of the lengths you pass to a C function is wrong and it is writing too far and killing one of the hidden thread local pointer variables. The problem isn't that it's printing null, the problem is that I'm getting an access violation at the points where writeln will return null. writeln(cast(void*)a) gives me slightly different results: It'll start off as null, print an address after the class has been created, and then print a different address after calling load(). I'm not passing any lengths... I'm passing a string, but the function doesn't take a string length (I'm using toStrings to make the cstring and I've tried adding \0 at the end of it too, but it doesn't help). The function takes a struct for options but is meant to take null if you're not doing anything special. Replacing that with a pointer to an instance of that struct will give me an access violation calling writeln(cast(void*)a) after load() (that struct takes the size of itself as its first member and that's set properly). I'm not really sure what fmod is doing after I call it. Even calling the function to control volume messes with the class address... it might be messing with more than just that, so if it's possible for fmod to be overflowing into the context that called it, that's probably it. Is there anything I can do about it or should i be contacting the fmod guys, or the derelict binding guys?
Class instance becoming null after calling bindings to C code???
I'm using the derelict fmod bindings to handle sounds in my application and I'm running into a weird bug... If I put calls to fmod in a function inside a class, upon calling those functions the instance of that class will be null, or otherwise changed. I obviously get an access violation if I try to do anything with that instance of the class while it's null. class Audio { FMOD_SOUND* audio; FMOD_SYSTEM* fmod; this(){ //initialization code } void load(string path){ FMOD_System_CreateSound(fmod, path.toStringz, 0, null, ); } } Audio a; void main(){ writeln(); // 281478 a = new Audio(); writeln(); // 281478 a.load("audio.ogg"); writeln(); // null ... //some time later writeln(); // 281478 } It happens even when I rearrange example code to be inside a class https://github.com/Extrawurst/DerelictFmod/blob/v4.1.0/source/app.d The calls work and don't return an error code. I can play music as long as I load and play within the same function call. Just the surrounding class reference gets messed up? And if I check later in the program the instance will return to its former value. I'm using Windows 7 x64. Just updated to the latest DMD version. I'm compiling as an x86 Windows application. Got the most recent bindings and dlls. I don't understand what's making this happen... any help would be very much appreciated.
Why do I have to cast arguments from int to byte?
I keep having to make casts like the following and it's really rubbing me the wrong way: void foo(T)(T bar){...} byte bar = 9; foo!byte(bar + 1); //Error: function foo!byte.foo (byte bar) is not callable using argument types (int) foo!byte(cast(byte)(bar + 1)); It wouldn't be so bad if I didn't have to use the word cast before each cast, bust since I have to specify both the word cast and the cast type and then wrap both the cast type and the value in brackets... it just explodes my code into multiple lines of unreadable mess. void foo(T)(T bar, T bar2, T bar3){...} byte foobar = 12; foo!byte(foobar + 1, foobar + 22, foobar + 333); vs. foo!byte(cast(byte)(foobar + 1), cast(byte)(foobar + 22), cast(byte)(foobar + 333)); Why?
Double ended arrays?
I have some data that I want to store in a dynamic 2d array... I'd like to be able to add elements to the front of the array and access those elements with negative integers as well as add numbers to the back that I'd acess normally with positive integers. Is this something I can do, or do I have to build a container to handle what I want?