Re: Using filter with std.container.Array.
On Thursday, 23 March 2017 at 03:02:54 UTC, Jonathan M Davis wrote: On Thursday, March 23, 2017 02:53:40 Soulsbane via Digitalmars-d-learn wrote: [...] find just iterates to the first element that matches. It doesn't affect the range beyond that. It works basically the same way that find would work with iterators in that it iterates until it finds the element you're looking for. However, since ranges then refer to more than one element at a time, the rest of the range beyond that element is still there. [...] Thanks a lot for the explanation. It's much clearer to me now! You've been a big help.
Re: Delay allocating class instance in stack.
On Wednesday, 22 March 2017 at 13:19:32 UTC, Nicholas Wilson wrote: On Wednesday, 22 March 2017 at 08:57:34 UTC, ANtlord wrote: You still have the buffer (the class has to go somewhere!), but it is implicit (you can't refer to it directly only through the class reference) and so is the destructor call, as opposed to the emplace + explicit buffer combo. In the latter case the class destructor will not be called automatically so you must do it yourself with `destroy`. My bad. It appears I need to clarify. I mean the case that I create object without delaying. I mean simple construction. scope myObj = MyClass(1); Answering own question: In this case object is allocated using stack (according documention https://wiki.dlang.org/Memory_Management#Allocating_Class_Instances_On_The_Stack). Function `destroy` used for cleanup object from heap (according documention https://wiki.dlang.org/Memory_Management#Explicit_Class_Instance_Allocation).
Re: Using filter with std.container.Array.
On Thursday, March 23, 2017 02:53:40 Soulsbane via Digitalmars-d-learn wrote: > Thanks for the reply Jonathan! Yes, I was trying to find all the > ids that match but couldn't get find to work. So I think I have > missed something somewhere. > > As a quick example: > import std.stdio; > import std.algorithm; > import std.container; > > struct Foo > { >string name; >size_t id; > } > > Array!Foo foo_; > > void main(string[] arguments) > { > Foo first; > first.id = 200; > > Foo second; > second.id = 100; > > Foo third; > third.id = 345; > > Foo fourth; > fourth.id = 100; > > foo_.insert(first); > foo_.insert(second); > foo_.insert(third); > foo_.insert(fourth); > > auto filterIt = filter!((Foo data) => data.id == 100)(foo_[]); > auto foundIt = find!((Foo data) => data.id == 100)(foo_[]); > writeln(filterIt); > writeln(foundIt); > } > > Will print: > [Foo("", 100), Foo("", 100)] > [Foo("", 100), Foo("", 345), Foo("", 100)] > > I only want the ids that match 100. Looking at find's > documentation it looks like it's returning exactly as it should > but not the way I want. No 345 id. > > Thanks for the help. I have this code working just fine using > foreach and have been trying to learn this different way of doing > things lately and my brain hurts :). Thanks again. find just iterates to the first element that matches. It doesn't affect the range beyond that. It works basically the same way that find would work with iterators in that it iterates until it finds the element you're looking for. However, since ranges then refer to more than one element at a time, the rest of the range beyond that element is still there. filter, on the other hand, creates a lazy range which skips the elements in the original range which do not match the predicate. It doesn't actually do anything until you iterate over it. So, if you're looking specifically for _all_ of the elements in a range which match a predicate and no other elements from that range (as it sounds like you're doing), then filter is the correct choice. If, on the other hand, you just want to find the first element that matches and then do who-knows-what with the rest of the range, then find would be appropriate. Note that because filter is lazy, it results in a new range which wraps the original, whereas find returns the original range with elements popped off. - Jonathan M Davis
Re: Using filter with std.container.Array.
On Wednesday, 22 March 2017 at 07:30:48 UTC, Jonathan M Davis wrote: On Wednesday, March 22, 2017 07:06:47 Soulsbane via Digitalmars-d-learn wrote: Example code: struct Foo { string name; size_t id; } Array!Foo foo_; I get errors when I try to use filter like this: auto found = filter!((Foo data, size_t id) => data.id == id)(foo_[], 100); I get this error source/app.d(15,62): Error: template std.algorithm.iteration.filter!(function (Foo data, ulong id) => data.id == id).filter cannot deduce function from argument types !()(RangeT!(Array!(Foo)), int), candidates are: /usr/include/dmd/phobos/std/algorithm/iteration.d(1089,10): std.algorithm.iteration.filter!(function (Foo data, ulong id) => data.id == id).filter(Range)(Range range) if (isInputRange!(Unqual!Range)) I can't figure out what I'm doing wrong. Thanks! filter takes a unary predicate that gets called on each element in a range. It's not going to work with a function that takes two arguments, and filter itself isn't going to take two arguments. You could do something like auto result = filter!(a => a.id == 100)(foo_[]); but you can't pass multiple arguments to filter. Also, if you're looking to find an element, then find would make more sense than filter, since filter is going to give you a lazy range with every element that matches the predicate, whereas find is just going to iterate the range until it finds the element (or is empty) and then returns the range. But I don't know whether calling the variable found was just the name you came up with or whether you're really trying to do a find operation rather than filter. - Jonathan M Davis Thanks for the reply Jonathan! Yes, I was trying to find all the ids that match but couldn't get find to work. So I think I have missed something somewhere. As a quick example: import std.stdio; import std.algorithm; import std.container; struct Foo { string name; size_t id; } Array!Foo foo_; void main(string[] arguments) { Foo first; first.id = 200; Foo second; second.id = 100; Foo third; third.id = 345; Foo fourth; fourth.id = 100; foo_.insert(first); foo_.insert(second); foo_.insert(third); foo_.insert(fourth); auto filterIt = filter!((Foo data) => data.id == 100)(foo_[]); auto foundIt = find!((Foo data) => data.id == 100)(foo_[]); writeln(filterIt); writeln(foundIt); } Will print: [Foo("", 100), Foo("", 100)] [Foo("", 100), Foo("", 345), Foo("", 100)] I only want the ids that match 100. Looking at find's documentation it looks like it's returning exactly as it should but not the way I want. No 345 id. Thanks for the help. I have this code working just fine using foreach and have been trying to learn this different way of doing things lately and my brain hurts :). Thanks again.
Re: bug in foreach continue
On 03/22/2017 05:39 PM, H. S. Teoh via Digitalmars-d-learn wrote: > the article is turning out to be quite a bit longer I said "book"; didn't I? :) Ali
Re: bug in foreach continue
On Wed, Mar 22, 2017 at 11:12:06PM +, Jesse Phillips via Digitalmars-d-learn wrote: > On Friday, 17 March 2017 at 19:05:20 UTC, H. S. Teoh wrote: > > There are actually (at least) TWO distinct phases of compilation > > that are conventionally labelled "compile time": > > > > 1) Template expansion / AST manipulation, and: > > > > 2) CTFE (compile-time function evaluation). > > This was an awesome explanation, please do put it on the wiki or as a > pull request for the website. I'm still working on that. :-) But the article is turning out to be quite a bit longer than I anticipated -- there is just so much I want to say! The draft is on the wiki but I don't really want to post the link just yet because it still needs lots of editing before it's ready for public consumption. T -- Dogs have owners ... cats have staff. -- Krista Casada
Re: bug in foreach continue
On Friday, 17 March 2017 at 19:05:20 UTC, H. S. Teoh wrote: There are actually (at least) TWO distinct phases of compilation that are conventionally labelled "compile time": 1) Template expansion / AST manipulation, and: 2) CTFE (compile-time function evaluation). This was an awesome explanation, please do put it on the wiki or as a pull request for the website.
Re: how to test if member of instance exists/defined?
On Wednesday, 22 March 2017 at 21:04:48 UTC, StarGrazer wrote: On Wednesday, 22 March 2017 at 21:02:41 UTC, StarGrazer wrote: On Wednesday, 22 March 2017 at 20:53:17 UTC, crimaniak wrote: On Wednesday, 22 March 2017 at 20:35:27 UTC, StarGrazer wrote: I've tried compiles but I guess that only checks if the code has valid syntax, not if it actually will compile in context. https://dlang.org/spec/traits.html#hasMember I tried that but D complains static if (hasMember!(S, "x")) return; else s.x = 3; D says that x doesn't exist. It's not that it doesn't work but the code still trying to be compiled by the compiler, when I don't want it to. nevermind, I was being stupid! ;)
Re: how to test if member of instance exists/defined?
On Wednesday, 22 March 2017 at 21:02:41 UTC, StarGrazer wrote: On Wednesday, 22 March 2017 at 20:53:17 UTC, crimaniak wrote: On Wednesday, 22 March 2017 at 20:35:27 UTC, StarGrazer wrote: I've tried compiles but I guess that only checks if the code has valid syntax, not if it actually will compile in context. https://dlang.org/spec/traits.html#hasMember I tried that but D complains static if (hasMember!(S, "x")) return; else s.x = 3; D says that x doesn't exist. It's not that it doesn't work but the code still trying to be compiled by the compiler, when I don't want it to.
Re: how to test if member of instance exists/defined?
On Wednesday, 22 March 2017 at 20:53:17 UTC, crimaniak wrote: On Wednesday, 22 March 2017 at 20:35:27 UTC, StarGrazer wrote: I've tried compiles but I guess that only checks if the code has valid syntax, not if it actually will compile in context. https://dlang.org/spec/traits.html#hasMember I tried that but D complains static if (hasMember!(S, "x")) return; else s.x = 3; D says that x doesn't exist.
Re: template alias parameter vs type parameter.
On Wednesday, 22 March 2017 at 18:12:31 UTC, Yuxuan Shui wrote: Hi, Is there any difference, when a type is passed into an alias parameter vs into a type parameter? alias parameters fail with builtin types, ex. 'int'. hopefully this will be corrected one day.
Re: how to test if member of instance exists/defined?
On Wed, Mar 22, 2017 at 08:35:27PM +, StarGrazer via Digitalmars-d-learn wrote: > I've tried compiles but I guess that only checks if the code has valid > syntax, not if it actually will compile in context. I'm not sure what you mean by "member of instance", but if you mean whether some given type T, presumably an aggregate like a struct, has some member x, here's how to do it: Code: struct StructA { int x; } struct StructB { int y; } template CheckMembers(T) { static if (is(typeof(T.init.x))) pragma(msg, T.stringof ~ " has member named x"); else pragma(msg, T.stringof ~ " doesn't have a member named x"); } alias dummy1 = CheckMembers!StructA; alias dummy2 = CheckMembers!StructB; Compiler output: StructA has member named x StructB doesn't have a member named x The key is to use is(typeof(...)) as the check. The idea being that if the member doesn't exist, then the compiler won't be able to find a type for the member, so it will not have a valid type and is(...) will return false. Whereas if the member does exist, then it will have some valid type (and it doesn't matter what that type is) and is(...) will return true. Generally, using is(typeof(...)) is preferable to using __traits(compiles, ...) where possible. T -- If Java had true garbage collection, most programs would delete themselves upon execution. -- Robert Sewell
Re: how to test if member of instance exists/defined?
On Wednesday, 22 March 2017 at 20:35:27 UTC, StarGrazer wrote: I've tried compiles but I guess that only checks if the code has valid syntax, not if it actually will compile in context. https://dlang.org/spec/traits.html#hasMember
how to test if member of instance exists/defined?
I've tried compiles but I guess that only checks if the code has valid syntax, not if it actually will compile in context.
Re: Derelict SDL segfaulting on ubuntu?
On Wednesday, 22 March 2017 at 12:31:34 UTC, Mike Parker wrote: On Wednesday, 22 March 2017 at 12:20:27 UTC, Mike Parker wrote: have at the bottom of the page the version they were added. But it's easier to look at the source for DerelictSDL2. In order to support the SharedLibVersion, the loader implements functions like this one: And I've just discovered a bug in one of those functions, which may actually be the cause of your sefgaults when using SharedLibVersion. Please try with version=2.1.3 and see if it works for you now. I did the update, but to no avail. When I get the time, I'll look through the list and report back.
template alias parameter vs type parameter.
Hi, Is there any difference, when a type is passed into an alias parameter vs into a type parameter?
Re: Weird template error in Phobos (after editing) that I can't quite get. Compiler bug?
On Wednesday, 22 March 2017 at 14:06:56 UTC, Atila Neves wrote: isInputRange looks like this: template isInputRange(R) { enum bool isInputRange = is(typeof( (inout int = 0) { R r = R.init; // can define a range object if (r.empty) {} // can test for empty r.popFront; // can invoke popFront() auto h = r.front; // can get the front of the range })); } [...] Got the same error when I changed it to: enum isInputRange(R) = is(typeof({...})); Which might explain why it's still inside an explicit template declaration. Or not, this whole thing is weird to me. Atila
Weird template error in Phobos (after editing) that I can't quite get. Compiler bug?
isInputRange looks like this: template isInputRange(R) { enum bool isInputRange = is(typeof( (inout int = 0) { R r = R.init; // can define a range object if (r.empty) {} // can test for empty r.popFront; // can invoke popFront() auto h = r.front; // can get the front of the range })); } If I change the `enum bool` line to `enum bool isInputRange = true && is(typeof(`, all is fine. If instead I: enum foo = true; enum bool isInputRange = foo && is(typeof( Then: std/range/primitives.d(352): Error: static assert "Cannot put a char[] into a Appender!string." std/format.d(1877):instantiated from here: put!(Appender!string, char[]) std/format.d(1784):instantiated from here: formatUnsigned!(Appender!string, ulong, char) std/format.d(1755):instantiated from here: formatIntegral!(Appender!string, ulong, char) std/format.d(3778):... (3 instantiations, -v to show) ... std/typecons.d(421):instantiated from here: format!(char, ulong, ulong) std/encoding.d(3468):instantiated from here: Tuple!(BOM, "schema", ubyte[], "sequence") Surely they should be identical? Obviously I was trying to do something else with an enum, but this is the reduced sample. Atila
Re: GDC options
On Wednesday, 22 March 2017 at 05:47:59 UTC, Sebastien Alaiwan wrote: On Monday, 13 March 2017 at 11:06:53 UTC, Russel Winder wrote: It is a shame that dmd and ldc do not just use the standard GCC option set. Totally agreed. Moreover, funny stuff like "dmd -of" (instead of standard "-o ") breaks automatic Msys path conversion hack (the code translates Unix paths from the command line to Windows paths before the invocation of a non-msys program), which makes it impossible to use dmd under Msys without wrapping it first. pkg-config also is a real pain to use with dmd (the pkg-config's output needs to be post-processed so it has the form "-L-lstuff" instead of "-lstuff"). This is an issue, because it makes it very hard to use write portable makefiles for programs containing D code. Too bad, because the D code is actually platform-independent, and there's been a lot of work in Phobos to make it easy to write such code. D was designed to be binary compatible with the C ABI ; however, having a compiler whose command-line behaves so different from gcc makes it harder to actually work with existing C libs. This is actually the main reason why I almost exclusively use gdc: to have one Makefile, for all platforms, allowing native and cross-compilation with no platform-specific special cases. This is why most of my work in Meson to get D supported is adding weird hacks to translate compiler flags between GNU <-> non-GNU <-> DMD. It sucks quite badly, and every now and then I hit a weird corner case where things break. For example: https://github.com/mesonbuild/meson/commit/d9cabe9f0ca6fb06808c1d5cf5206a7c5158517e Would be amazing if all D compilers would support the GCC flags, like Clang does for C - that would help in removing a lot of hacks (and making Makefiles which work with all compilers). The reason for not using a dmd wrapper is that one might want to use flags specific to a certain compiler.
Re: Delay allocating class instance in stack.
On Wednesday, 22 March 2017 at 08:57:34 UTC, ANtlord wrote: On Wednesday, 22 March 2017 at 06:47:26 UTC, Ali Çehreli wrote: On 03/21/2017 09:57 PM, ANtlord wrote: > Thank you for clarification. But I have one more question. Do I have to > use destroy for deallocating object from stack? Yes because what is going out of scope are two things: - A buffer - A MyClass reference Oh I got it. I have to use `destroy` in this case. If I use `scope` I haven't buffer and MyClass reference then I can don't use `destroy`. Do I understand correctly? You still have the buffer (the class has to go somewhere!), but it is implicit (you can't refer to it directly only through the class reference) and so is the destructor call, as opposed to the emplace + explicit buffer combo. In the latter case the class destructor will not be called automatically so you must do it yourself with `destroy`.
Re: Derelict SDL segfaulting on ubuntu?
On Wednesday, 22 March 2017 at 12:20:27 UTC, Mike Parker wrote: have at the bottom of the page the version they were added. But it's easier to look at the source for DerelictSDL2. In order to support the SharedLibVersion, the loader implements functions like this one: And I've just discovered a bug in one of those functions, which may actually be the cause of your sefgaults when using SharedLibVersion. Please try with version=2.1.3 and see if it works for you now.
Re: Derelict SDL segfaulting on ubuntu?
On Wednesday, 22 March 2017 at 10:32:44 UTC, Robly18 wrote: Well, I have a call to SDL_GetVersion in my code, to check exactly what you're saying. And it spits out "2.0.5", so I don't think that is the problem. I believe I do have it installed: I rm'd the old lib files and used make install on SDL. Same thing for SDL_Image. OK. If it says 2.0.5, then you do have it installed. And I'm out of ideas until I try it out myself. I'm also not sure, how can I tell if a function is from SDL 2.0.5 or 2.0.4? I tried to look up the changelogs, but couldn't find anything compact enough that I can look through it, yet that would tell me what functions are new. Some of the functions listed on the SDL Wiki, like this one [1], have at the bottom of the page the version they were added. But it's easier to look at the source for DerelictSDL2. In order to support the SharedLibVersion, the loader implements functions like this one: ``` private ShouldThrow allowSDL_2_0_0(string symbolName) { switch(symbolName) { // Functions added in 2.0.1 case "SDL_free": break; case "SDL_SetAssertionHandler": break; case "SDL_GetAssertionReport": break; case "SDL_ResetAssertionReport": break; case "SDL_GetSystemRAM": break; case "SDL_UpdateYUVTexture": break; case "SDL_GL_GetDrawableSize": break; case "SDL_GetBasePath": break; case "SDL_GetPrefPath": break; default: return allowSDL_2_0_1(symbolName); } return ShouldThrow.No; } ``` So you can look starting from [2] to see all of the functions added in 2.0.1 and higher. [1] http://wiki.libsdl.org/SDL_GetWindowBordersSize?highlight=%28%5CbCategoryVideo%5Cb%29%7C%28CategoryEnum%29%7C%28CategoryStruct%29 [2] https://github.com/DerelictOrg/DerelictSDL2/blob/master/source/derelict/sdl2/sdl.d#L566
Re: Derelict SDL segfaulting on ubuntu?
On Wednesday, 22 March 2017 at 03:44:17 UTC, Mike Parker wrote: So given what I know so far, my guess is you're using functions from 2.0.5 with a 2.0.4 version of the library. You can call SDL_GetVersion [1] to verify. In that case, you need to use DerelictSDL2 2.1.x and do no use SharedLibVersion -- just let it load the default. Well, I have a call to SDL_GetVersion in my code, to check exactly what you're saying. And it spits out "2.0.5", so I don't think that is the problem. I believe I do have it installed: I rm'd the old lib files and used make install on SDL. Same thing for SDL_Image. I'm also not sure, how can I tell if a function is from SDL 2.0.5 or 2.0.4? I tried to look up the changelogs, but couldn't find anything compact enough that I can look through it, yet that would tell me what functions are new. TIA.
Re: Delay allocating class instance in stack.
On Wednesday, 22 March 2017 at 06:47:26 UTC, Ali Çehreli wrote: On 03/21/2017 09:57 PM, ANtlord wrote: > Thank you for clarification. But I have one more question. Do I have to > use destroy for deallocating object from stack? Yes because what is going out of scope are two things: - A buffer - A MyClass reference Oh I got it. I have to use `destroy` in this case. If I use `scope` I haven't buffer and MyClass reference then I can don't use `destroy`. Do I understand correctly?
Re: Using filter with std.container.Array.
On Wednesday, March 22, 2017 07:06:47 Soulsbane via Digitalmars-d-learn wrote: > Example code: > struct Foo > { >string name; >size_t id; > } > > Array!Foo foo_; > > I get errors when I try to use filter like this: > > auto found = filter!((Foo data, size_t id) => data.id == > id)(foo_[], 100); > > I get this error > source/app.d(15,62): Error: template > std.algorithm.iteration.filter!(function (Foo data, ulong id) => > data.id == id).filter cannot deduce function from argument types > !()(RangeT!(Array!(Foo)), int), candidates are: > /usr/include/dmd/phobos/std/algorithm/iteration.d(1089,10): > std.algorithm.iteration.filter!(function (Foo data, ulong id) => > data.id == id).filter(Range)(Range range) if > (isInputRange!(Unqual!Range)) > > I can't figure out what I'm doing wrong. Thanks! filter takes a unary predicate that gets called on each element in a range. It's not going to work with a function that takes two arguments, and filter itself isn't going to take two arguments. You could do something like auto result = filter!(a => a.id == 100)(foo_[]); but you can't pass multiple arguments to filter. Also, if you're looking to find an element, then find would make more sense than filter, since filter is going to give you a lazy range with every element that matches the predicate, whereas find is just going to iterate the range until it finds the element (or is empty) and then returns the range. But I don't know whether calling the variable found was just the name you came up with or whether you're really trying to do a find operation rather than filter. - Jonathan M Davis
Using filter with std.container.Array.
Example code: struct Foo { string name; size_t id; } Array!Foo foo_; I get errors when I try to use filter like this: auto found = filter!((Foo data, size_t id) => data.id == id)(foo_[], 100); I get this error source/app.d(15,62): Error: template std.algorithm.iteration.filter!(function (Foo data, ulong id) => data.id == id).filter cannot deduce function from argument types !()(RangeT!(Array!(Foo)), int), candidates are: /usr/include/dmd/phobos/std/algorithm/iteration.d(1089,10): std.algorithm.iteration.filter!(function (Foo data, ulong id) => data.id == id).filter(Range)(Range range) if (isInputRange!(Unqual!Range)) I can't figure out what I'm doing wrong. Thanks!