Re: Can assumeSafeAppend() grab more and more capacity?
On 06/07/2017 12:12 AM, Ali Çehreli wrote: On 06/06/2017 12:13 PM, Jesse Phillips wrote: > On Monday, 5 June 2017 at 23:17:46 UTC, Ali Çehreli wrote: >> auto a = [ 1, 2, 3, 4 ]; >> auto b = a; [...] The only issue remaining for me is the part that you've quoted: Jesse Phillips didn't quote the spec. I guess you mean me. For reference, the spec quote again [1]: "one may use the .capacity property to determine how many elements can be appended to the array without reallocating." If we can trust capacity per spec, like we would trust after calling reserve(), then a and b in my code above are counter examples where both a and b have capacity initially but one of them will lose its capacity as soon as the other one gains an element. `reserve` works the same. `reserve`d capacity is still capacity and it can get snapped away by another slice. void main() { import std.stdio; int[] foo; writeln(foo.capacity); /* 0 */ foo.reserve(10); writeln(foo.capacity); /* 15 */ int[] bar = foo; bar ~= 1; writeln(foo.capacity); /* 0 -- bar took the capacity */ writeln(bar.capacity); /* 15 */ } You understand the spec to say that because `foo.capacity` is 15 at one point, you should then be able to put 15 elements into `foo` without relocation. And what `bar` does in the meantime shouldn't matter. I don't think the spec is supposed to make that strong a guarantee, but I see how it can be interpreted that way. Maybe it should be reworded/amended to describe the actual behavior more precisely. [1] http://dlang.org/spec/arrays.html#resize
Re: Can assumeSafeAppend() grab more and more capacity?
On 06/06/2017 12:13 PM, Jesse Phillips wrote: > On Monday, 5 June 2017 at 23:17:46 UTC, Ali Çehreli wrote: >> auto a = [ 1, 2, 3, 4 ]; >> auto b = a; >> >> Both of those slices have non-zero capacity yet one of them will be >> the lucky one to grab it. Such semantic issues make me unhappy. :-/ >> >> Ali > > You have to remember that slices don't own their memory. So while > capacity show a guaranteed reserved memory, it is reserved for the > dynamic array the slice has a window into. > > Remove probably shouldn't try to reclaim capacity, while it is > destructive for any other slice, it shouldn't make string appending also > destructive. > > untested: > > auto a = [ 1, 2, 3, 4 ]; > auto b = a[$-1, $]; > a.remove(2); > assert(b == [4]); > a ~= 6; > assert(b == [4]); Agreed. The only issue remaining for me is the part that you've quoted: If we can trust capacity per spec, like we would trust after calling reserve(), then a and b in my code above are counter examples where both a and b have capacity initially but one of them will lose its capacity as soon as the other one gains an element. Although I like the fact that the current semantics are more efficient (because capacity is given lazily), they conflict with the other part of the spec. Ali
Re: Can assumeSafeAppend() grab more and more capacity?
On Monday, 5 June 2017 at 23:17:46 UTC, Ali Çehreli wrote: auto a = [ 1, 2, 3, 4 ]; auto b = a; Both of those slices have non-zero capacity yet one of them will be the lucky one to grab it. Such semantic issues make me unhappy. :-/ Ali You have to remember that slices don't own their memory. So while capacity show a guaranteed reserved memory, it is reserved for the dynamic array the slice has a window into. Remove probably shouldn't try to reclaim capacity, while it is destructive for any other slice, it shouldn't make string appending also destructive. untested: auto a = [ 1, 2, 3, 4 ]; auto b = a[$-1, $]; a.remove(2); assert(b == [4]); a ~= 6; assert(b == [4]);
Re: Use template functions within mixin
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
Re: gdc and shared objects
On 06/06/2017 05:07 PM, Russel Winder via Digitalmars-d-learn wrote: I hope I am just missing an option as everything seems to be there fore this to work. You are missing the `-shared-libphobos` option. -- Mike Wey
Re: Avast virus warning?
On Monday, 5 June 2017 at 16:31:04 UTC, Anonymouse wrote: I just sent a pre-compiled .exe of my project to a friend, and his Avast anti-virus promptly quarantined it and sent it off for analysis. I tried sending him a Hello World[1] with the same results. Is this something common for d programs? Anything I can do to work around it from my end? [1]: http://www.mediafire.com/file/fc51qz141r3ns6r/helloworld.exe https://forum.avast.com/index.php?topic=203573.0 It's not that people do bad things with D. It's that dmd generates code that doesn't look like anything it has seen before.
Re: Use template functions within mixin
On Tuesday, June 06, 2017 15:00:50 Timoses via Digitalmars-d-learn wrote: > Hey there, > > I'm wondering how I can use a template function within my mixin: > > ``` > ubyte[] value = x[33, 3a,3f, d4]; > foreach (type; TypeTuple!("int", "unsigned int", > "byte")) > { > mixin(`if (value.length == type.sizeof) > { > ubyte[type.sizeof] raw = > value[0..$]; > auto fValue = > raw.littleEndianToNative!(type); > displayinfo(fValue); > } > break; > `); > } > ``` > > > Error: template std.bitmanip.littleEndianToNative cannot deduce > function from argument types !("int")(ubyte[8]), candidates are: > [..]\src\phobos\std\bitmanip.d(2560,3): > std.bitmanip.littleEndianToNative(T, uint n)(ubyte[n] val) if > (canSwapEndianness!T && n == T.sizeof) > > ``` > `raw.littleEndianToNative!` ~ type ~ `;` > ``` > > neither works.. > > Any way to accomplish this? Well, for starters, I see no reason to even use mixins here. Just do foreach(type; TypeTuple!(int, uint, byte)) { } and write the code normally. Also, unsigned int isn't a type in D. It's uint. And with what you have, mixing in "type.sizeof" is just going to result in getting size of the type string. You'd need to do something like "ubyte[" ~ type ~ "].sizeof] raw =" if you wanted to have the value of type mixed in. Also, FYI, TypeTuple was renamed to AliasSeq, so that's what's normally used now (though TypeTuple wasn't deprecated because of the amount of existing code that uses it, so it will still work). In any case, there's really no reason to be using string mixins here. TypeTuple/AliasSeq and foreach will work with the actual types. - Jonathan M Davis
Re: Use template functions within mixin
On Tuesday, 6 June 2017 at 15:00:50 UTC, Timoses wrote: Hey there, I'm wondering how I can use a template function within my mixin: ``` ubyte[] value = x[33, 3a,3f, d4]; foreach (type; TypeTuple!("int", "unsigned int", "byte")) { mixin(`if (value.length == type.sizeof) { ubyte[type.sizeof] raw = value[0..$]; auto fValue = raw.littleEndianToNative!(type); displayinfo(fValue); } break; `); } ``` Error: template std.bitmanip.littleEndianToNative cannot deduce function from argument types !("int")(ubyte[8]), candidates are: [..]\src\phobos\std\bitmanip.d(2560,3): std.bitmanip.littleEndianToNative(T, uint n)(ubyte[n] val) if (canSwapEndianness!T && n == T.sizeof) ``` `raw.littleEndianToNative!` ~ type ~ `;` ``` Did you also put the ` ~ type ~ ` on the 2 other cases where you use the variable type? mixin(`if (value.length == ` ~ type ~ `.sizeof) { ubyte[` ~ type ~ `.sizeof] raw = value[0..$]; auto fValue = raw.littleEndianToNative!(` ~ type ~ `); displayinfo(fValue); } break; `);
gdc and shared objects
using gdc (Debian 6.3.0-18) 6.3.0 20170516 I find: gdc -I. -fPIC -c -o code.o code.d gdc -o libanswer.so -shared code.o leads to problems because the linker tries to link against: /usr/lib/gcc/x86_64-linux-gnu/6/libgdruntime.a instead of against one of: /usr/lib/gcc/x86_64-linux-gnu/6/libgdruntime.so /usr/lib/x86_64-linux-gnu/libgdruntime.so.68.0.2 /usr/lib/x86_64-linux-gnu/libgdruntime.so.68 I hope I am just missing an option as everything seems to be there fore this to work. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part
Use template functions within mixin
Hey there, I'm wondering how I can use a template function within my mixin: ``` ubyte[] value = x[33, 3a,3f, d4]; foreach (type; TypeTuple!("int", "unsigned int", "byte")) { mixin(`if (value.length == type.sizeof) { ubyte[type.sizeof] raw = value[0..$]; auto fValue = raw.littleEndianToNative!(type); displayinfo(fValue); } break; `); } ``` Error: template std.bitmanip.littleEndianToNative cannot deduce function from argument types !("int")(ubyte[8]), candidates are: [..]\src\phobos\std\bitmanip.d(2560,3): std.bitmanip.littleEndianToNative(T, uint n)(ubyte[n] val) if (canSwapEndianness!T && n == T.sizeof) ``` `raw.littleEndianToNative!` ~ type ~ `;` ``` neither works.. Any way to accomplish this?