Re: opEquals unsafe? Please tell me this isnt true...
On Tuesday, 25 November 2014 at 03:42:50 UTC, Eric wrote: I'm finding it really hard to write robust classes in D due to all the problems with Object. I wish Object didn't have any methods. One thing I do is kinda act as if it didn't and make my own interfaces instead.
Re: opEquals unsafe? Please tell me this isnt true...
On Tuesday, 25 November 2014 at 02:48:43 UTC, Jonathan M Davis via Digitalmars-d-learn wrote: On Monday, November 24, 2014 22:12:08 Eric via Digitalmars-d-learn wrote: @safe class Y { } @safe class X { } @safe class Z { int x; this() { if (typeid(X) == typeid(Y)) x = 1; // Compile Error else x = 2; } } void main() { new Z; } // test.d(19): Error: safe function 'test.Z.this' // cannot call system function 'object.opEquals' Isn't this analagous to saying that the "instanceof" operator in java endangers the GC? Is it correct to replace '==' with 'is'? It's not that it's inherently unsafe. The problem is a combination of the fact that stuff in druntime that pre-existed @safe hasn't been made @safe yet (particularly, stuff in TypeInfo) and the fact that Object shouldn't even have opEquals, opCmp, toHash, or toString on it, because that restricts which attributes can be used ( https://issues.dlang.org/show_bug.cgi?id=9769 ), though I think that with @safe, we can work around that (unlike with const). However, for whatever reason, TypeInfo's opEquals function hasn't been marked with @safe or @trusted, so it's considered @system. That will need to be fixed, but I don't know if there are any implementation issues preventing it. It _looks_ like it could probably be marked @trusted, but I haven't actually dug into it in detail. In any case, you should be able to just mark the constructor as @trusted for now to work around the issue, and at some point in the future opEqualso or TypeInfo should be @trusted or @safe. - Jonathan M Davis Thanks for reminding me about @trusted. I'm finding it really hard to write robust classes in D due to all the problems with Object. -Eric
Re: opEquals unsafe? Please tell me this isnt true...
On Monday, November 24, 2014 22:12:08 Eric via Digitalmars-d-learn wrote: > > @safe > class Y { } > > @safe > class X { } > > @safe > class Z > { > int x; > > this() > { > if (typeid(X) == typeid(Y)) x = 1; // Compile Error > else x = 2; > } > } > > void main() { new Z; } > > // test.d(19): Error: safe function 'test.Z.this' > // cannot call system function 'object.opEquals' > > Isn't this analagous to saying that the "instanceof" operator > in java endangers the GC? > > Is it correct to replace '==' with 'is'? It's not that it's inherently unsafe. The problem is a combination of the fact that stuff in druntime that pre-existed @safe hasn't been made @safe yet (particularly, stuff in TypeInfo) and the fact that Object shouldn't even have opEquals, opCmp, toHash, or toString on it, because that restricts which attributes can be used ( https://issues.dlang.org/show_bug.cgi?id=9769 ), though I think that with @safe, we can work around that (unlike with const). However, for whatever reason, TypeInfo's opEquals function hasn't been marked with @safe or @trusted, so it's considered @system. That will need to be fixed, but I don't know if there are any implementation issues preventing it. It _looks_ like it could probably be marked @trusted, but I haven't actually dug into it in detail. In any case, you should be able to just mark the constructor as @trusted for now to work around the issue, and at some point in the future opEqualso or TypeInfo should be @trusted or @safe. - Jonathan M Davis
Re: DerelictOrg and SDL_Mixer
On Monday, 24 November 2014 at 23:27:58 UTC, Jack wrote: It's a common error but did you load the Mixer and SDL libraries? DerelictSDL2Mixer.load(); DerelictSDL2.load(); And some code or output from gdb would be most helpful. Oh.. I didn't know about the DerelictSDL2Mixer.load()! I thought the initialization of sdl_mixer was done with DerelictSDL2.load() Thanks very much!! I'll try this tonight! (And yes, I know it's better to post some code but I forgot to bring the source code..)
Re: DerelictOrg and SDL_Mixer
On Monday, 24 November 2014 at 21:19:40 UTC, torea wrote: Hi all, I'm playing a bit with D and SDL using DerelictOrg on debian 6. I've installed SDL2, SDL_mixer2 and the latest packages DerelictUtil and DerelictSDL2. I can display some stuffs and interact with the keys. Now I'm trying to add music and sounds but each time I try to access a function from SDL_mixer ( I tried with Mix_openAudio and Mix_LoadWav ), I get a segmentation fault. These functions are defined in the mixer.d file in the DerelictSDL2 package but I guess I miss something concerning the link to the real sdl_mixer lib file. Is there a specific way to install the sdl_mixer package with DerelictSDL2? It's a common error but did you load the Mixer and SDL libraries? DerelictSDL2Mixer.load(); DerelictSDL2.load(); And some code or output from gdb would be most helpful.
A nice D coding pattern
In some D programs I'm using this coding pattern: struct Foo { // Instance fields here. @disable this(); this(in string[] data) pure @safe in { // Many pre-conditions here. } out(result) { // Some post-conditions here. } body { // ... } Nullable!(string[][]) doIt() pure { //... } // Various other methods here... } void main() { // Created at compile-time. enum something = "".Foo; // Something much larger is done at run-time. immutable const result = something.doIt; } The structure is created at compile-time using data known at compile-time (here a string). This struct has a constructor that runs at compile-time that has many pre-conditions that avoid wrong input data at compile-time. The largest part of the computation is done at run-time calling one or more struct methods. And the @disable this() assures that a struct is correctly initialized by the constructor. This pattern has significant advantages regarding code reliability. You can see an example of this pattern that I've used here: http://rosettacode.org/wiki/Solve_a_Hopido_puzzle#D Bye, bearophile
Something between a scalar and an enum
An enumeration contains a small number of distinct elements: enum Colors { red, green, yellow, brown } While an integral numeric value encodes a scalar: uint x; x = 120; x++; A sufficiently common pattern in my code is to have something intermediate: that has a small group of special values, plus many values identified just by a number. alias Unit = uint; enum : Uint { EmptyUnit = 0, BrokenUnit = Unit.max - 1, MissingUnit = Unit.max } This allows me to denote some special units, plus count the units starting from 1 up to millions or more. It's unfortunate the D type system doesn't offer me much good to express this idiom, but I am not sure how much a type system can help here even in principle. Bye, bearophile
opEquals unsafe? Please tell me this isnt true...
@safe class Y { } @safe class X { } @safe class Z { int x; this() { if (typeid(X) == typeid(Y)) x = 1; // Compile Error else x = 2; } } void main() { new Z; } // test.d(19): Error: safe function 'test.Z.this' // cannot call system function 'object.opEquals' Isn't this analagous to saying that the "instanceof" operator in java endangers the GC? Is it correct to replace '==' with 'is'? -Eric
DerelictOrg and SDL_Mixer
Hi all, I'm playing a bit with D and SDL using DerelictOrg on debian 6. I've installed SDL2, SDL_mixer2 and the latest packages DerelictUtil and DerelictSDL2. I can display some stuffs and interact with the keys. Now I'm trying to add music and sounds but each time I try to access a function from SDL_mixer ( I tried with Mix_openAudio and Mix_LoadWav ), I get a segmentation fault. These functions are defined in the mixer.d file in the DerelictSDL2 package but I guess I miss something concerning the link to the real sdl_mixer lib file. Is there a specific way to install the sdl_mixer package with DerelictSDL2?
Re: Sum informations in file....
Thanks! But is there any way to do it with std.algorithm? Can be skipOver used for it?
Re: Sum informations in file....
On Monday, 24 November 2014 at 20:23:57 UTC, Suliman wrote: thanks! But how I can skip first line? My varian: auto lines = "foo.txt".File .byLine .filter!(f=>f[0] != f[0]); With 'drop' from std.range: auto lines = "foo.txt".File .byLine .drop(1) .filter!(f=>f[0] != f[0]);
Re: Sum informations in file....
thanks! But how I can skip first line? My varian: auto lines = "foo.txt".File .byLine .filter!(f=>f[0] != f[0]);
Re: Sum informations in file....
On Mon, 24 Nov 2014 11:41:43 -0800 Ali Çehreli via Digitalmars-d-learn wrote: > On 11/24/2014 11:30 AM, ketmar via Digitalmars-d-learn wrote: > > > File.byLine returns *output* *range*. > > Although the range is used for outputting, it is still an InputRange. :) ah, yes, my bad. i'm always tend to mess with "input", "output", "client", "server" and such. knowing that i checked three times if i wrote the correct range direction and... failed it. signature.asc Description: PGP signature
Re: Sum informations in file....
On 11/24/2014 11:30 AM, ketmar via Digitalmars-d-learn wrote: > File.byLine returns *output* *range*. Although the range is used for outputting, it is still an InputRange. :) Ali
Re: Sum informations in file....
On 11/24/2014 11:04 AM, Suliman wrote: > I can't understand why foreach loop produce every line by line, while > it's fuctional analog print lines on one string: > > foreach(f; file.byLine()) > { > writeln(f); f is a char[] and writeln prints all strings as their contents i.e. "first string" is printed as first string > } > > auto file = File("foo.txt","r"); > file > .byLine() > .writeln; In that case the first three lines make a range object. By default, writeln prints ranges as if they are arrays. For this example, the range is a range of strings, so it prints it as [ "first string" ] writefln gives us more power: auto content = File("foo.txt","r").byLine(); writefln("%-(%s\n%)", content); Per-element formatting is specified within %( and %). So, each element would be printed with "%s\n" format. Notes: 1) The dash in %-( means "do not print the double-quotes for strings" 2) Everything after %s in the per-element formatting is taken as element delimiter and writefln does not print the delimiters are not printed for the last element. One may need to use %| to specify the actual delimiters. Here is the spec: http://dlang.org/phobos/std_format.html#.formattedWrite Here is my rewording: http://ddili.org/ders/d.en/formatted_output.html Ali
Re: Sum informations in file....
On Mon, 24 Nov 2014 19:04:34 + Suliman via Digitalmars-d-learn wrote: > I can't understand why foreach loop produce every line by line, > while it's fuctional analog print lines on one string: the two samples are not the same, they doing completely different things. File.byLine returns *output* *range*. what `foreach` does is processing this range element by element, while `writeln(range)` outputs the whole range. the second code means: auto lines = file.byLine(); writeln(lines); for `writeln` output range is a kind of array, so it outputs it as an array. signature.asc Description: PGP signature
Re: Sum informations in file....
I can't understand why foreach loop produce every line by line, while it's fuctional analog print lines on one string: foreach(f; file.byLine()) { writeln(f); } auto file = File("foo.txt","r"); file .byLine() .writeln; file content: - first sring second string - Output: D:\code\JSONServer\source>app.exe ["first sring", "second string"] expected: first sring second string
Re: projections in D
On Monday, 24 November 2014 at 15:44:02 UTC, Ramon wrote: What is the difference between lazy and eager ranges? (I guess, the lazy one has not yet queried the elements) The lazy returns a range that once iterated gives the results one at a time (so the function allocates no heap memory). The eager version creates an array of the results in heap memory. Bye, bearophile
Re: projections in D
What is the difference between lazy and eager ranges? (I guess, the lazy one has not yet queried the elements)
Re: projections in D
Ramon: example in C#: class ProjectionWanted { public int field1 { get; set; } public int field2 { get; set; } public int field3 { get; set; } } void Foo(List list) { var list_projected = list.Select(l => new { l.field1, l.field2 }); // list_projected elements now contain only field1 and field2 } Here I have defined ProjectionWanted as a struct. //-- import std.stdio, std.algorithm, std.typecons; struct ProjectionWanted { public int a, b, c; } auto foo(ProjectionWanted[] seq) pure nothrow @safe @nogc { return seq.map!(p => tuple(p.a, p.b)); } void main() { [ProjectionWanted(1, 2, 3), ProjectionWanted(4, 5, 6)] .foo .writeln; } //-- Note that foo() returns a lazy range. If you need an eager one you can append an ".array": return seq.map!(p => tuple(p.a, p.b)).array; And you have to import std.array too. Bye, bearophile
projections in D
what range/algorithm allows me to make projections from one sequence to another? that is, in C#, this is the Select method (http://msdn.microsoft.com/en-us/library/vstudio/bb548891%28v=vs.100%29.aspx) example in C#: class ProjectionWanted { public int field1 { get; set; } public int field2 { get; set; } public int field3 { get; set; } } void Foo(List list) { var list_projected = list.Select(l => new { l.field1, l.field2 }); // list_projected elements now contain only field1 and field2 } So how would I make it in D? First yet, does D supports creating anonymous type for the projection? Or maybe I need to declare the class/struct that I want to be projected?
Re: @property method needs ()
Thanks a lot for the info. Kind regards André On Monday, 24 November 2014 at 09:26:16 UTC, Marc Schütz wrote: On Monday, 24 November 2014 at 08:35:08 UTC, ketmar via Digitalmars-d-learn wrote: a known thing. not sure if this is a known *bug* (seems that almost nobody cares). compiler is obviously wrong here (and i believe that it's wrong to accept `()` for properties at all), but i don't know if this will ever be fixed. There's hope that this will get fixed in the near future: https://github.com/D-Programming-Language/dmd/pull/2305
Re: print yyyy-mm-dd
On Monday, November 24, 2014 10:38:55 Suliman via Digitalmars-d-learn wrote: > And could anybody explain me how the cast is work. How to > understand which types to which may be casted? Look at the documentation for opCast. That's how the casting is done. But what it comes down to is SysTime -> DateTime, Date, or TimeOfDay DateTime -> Date or TimeOfDay Those are the conversions which lose information, so they can be done via casts, whereas the conversions that would increase the amount of information in the type require using a constructor, because you have to use information from multiple objects to be able to create the new object. e.g. auto dt = DateTime(date, tod); The primary reason to cast a SysTime to a Date prior to calling toISOExtString on it is to strip off the time portion, but slicing the result as uri suggested works too (though it's probably a tad less efficient due to the time spent creating the portion of the string that isn't even used). - Jonathan M Davis
Re: print yyyy-mm-dd
On Monday, November 24, 2014 10:33:16 Suliman via Digitalmars-d-learn wrote: > Is there any way to set separator? For example I want use '/' or > ':'? No. toISOString and toISOExtString support the ISO and Extended ISO formats from the ISO standard, and std.datetime does not currently support custom date/time string formatting (though it's on the todo list). The only non-standard format that it currently suports is Boost's "simple" time via toSimpleString, and it probably shouldn't even be there, since it just confuses things without adding any real, useful functionality. Custom date/time string formatting is what's really needed for non-standard representations, especially because everyone has a different way that they want to represent the time. However, most code really should use toISOExtString, since it's standard and reasonably readable (e.g. 2014-11-24T02:53:12 as opposed to the non-extended ISO format of 20141124T025312). - Jonathan M Davis
Re: print yyyy-mm-dd
And could anybody explain me how the cast is work. How to understand which types to which may be casted?
Re: print yyyy-mm-dd
Is there any way to set separator? For example I want use '/' or ':'?
Re: @property method needs ()
On Monday, 24 November 2014 at 08:35:08 UTC, ketmar via Digitalmars-d-learn wrote: On Mon, 24 Nov 2014 06:56:08 + Andre via Digitalmars-d-learn wrote: Hi, in following example the @property method needs the () otherwise compiler error for row 24 is thrown. I cannot judge, whether the compiler behaves correct or not. Kind regards André --- alias fnError = void delegate(string s); interface IfSession { @property fnError addError(); } class Session: IfSession { private fnError _addError; @property fnError addError() { return _addError; } } void main() { auto session = new Session(); session._addError = delegate(s){}; session.addError()("test"); // Works session.addError("test"); // Does not work } a known thing. not sure if this is a known *bug* (seems that almost nobody cares). compiler is obviously wrong here (and i believe that it's wrong to accept `()` for properties at all), but i don't know if this will ever be fixed. There's hope that this will get fixed in the near future: https://github.com/D-Programming-Language/dmd/pull/2305
Re: @property method needs ()
On Mon, 24 Nov 2014 06:56:08 + Andre via Digitalmars-d-learn wrote: > Hi, > > in following example the @property method needs the () > otherwise compiler error for row 24 is thrown. > I cannot judge, whether the compiler behaves correct or not. > > Kind regards > André > > --- > > alias fnError = void delegate(string s); > > interface IfSession > { > @property fnError addError(); > } > > class Session: IfSession > { > private fnError _addError; > > @property fnError addError() > { > return _addError; > } > } > > void main() > { > auto session = new Session(); > session._addError = delegate(s){}; > > session.addError()("test"); // Works > session.addError("test"); // Does not work > } a known thing. not sure if this is a known *bug* (seems that almost nobody cares). compiler is obviously wrong here (and i believe that it's wrong to accept `()` for properties at all), but i don't know if this will ever be fixed. signature.asc Description: PGP signature