Re: Can the order in associative array change when keys are not midified?
On Thursday, 1 January 2015 at 18:58:04 UTC, Andrej Mitrovic via Digitalmars-d-learn wrote: On 1/1/15, Tobias Pankrath via Digitalmars-d-learn wrote: You could implement an OrderedMap!(Key, Value) via RedBlackTree!(Tuple!(Key, Value), (a,b) => a[0] < b[0]). We could add this as an alias into Phobos or perhaps as just a documentation line on the website. V good point. This is one of those small frictions that cumulatively raises the cost of adoption for newcomers who are not by nature hackers but want to get stuff done, and yet could be valuable members of the D community over time. For whatever reason, there is some resistance for many people to ask for help in a forum and it is easy to be overwhelmed and give up. Modern people don't have much tolerance for pain, even though that's an essential part of learning and being a developer. And compare python where you have OrderedDict and a search will find many examples of how to use it. I agree with comments about readability of library functions, but maybe also worth extending hints and tips for common patterns / code fragments. What is the protocol for contributing to the wiki? Can one just edit it ?
Re: Scoped external function declaration
On Thu, 01 Jan 2015 17:51:45 + novice2 via Digitalmars-d-learn wrote: > I want to use external or C function. > It used only one time from one D function. > I want do declare C function inside D function. > I don't want to declare C function in global scope. > > Is my wish correct? > Reduced code: > > > > extern (C) int getch(); > void main() { >getch(); > } > //compiled OK > > > > void main() { >extern (C) int getch(); >getch(); > } > //Error 42: Symbol Undefined __D4test4mainFZ5getchUZi nope, you can't do this. actually, there is `pragma(mangle)`, but it's not working for nested declarations. but you can use it to rename your import: pragma(mangle, "getch") extern (C) int external_getch(); void main() { external_getch(); } this will allow you to avoid name conflicts in your D code, yet still link to external `getch` function. signature.asc Description: PGP signature
Re: Can the order in associative array change when keys are not midified?
On Thu, 01 Jan 2015 12:32:33 + Idan Arye via Digitalmars-d-learn wrote: > If I have an associative array and I only modify it's values, > without changing the keys, can I assume that the order won't > change? please, don't: this is implementation-specific. druntime code can change (for example, by tracking access frequency and regrouping frequently accesed keys into clusters for better cache utilising), and your finely crafted code will start to fail mysteriously. it is safe to assume that after ANY change in AA *everything* is changed there. you'd better augment AA with some "change buffer" or use different data structure for your task. signature.asc Description: PGP signature
Re: Initialization of nested struct fields
On Friday, 2 January 2015 at 00:08:02 UTC, anonymous wrote: Apparently dmd thinks that the result of f must be a nested struct. I.e. it needs a context pointer. And I guess hell would break loose if you'd use a nested struct with a null context pointer. At least when the context pointer is actually used, unlike here. Ah, I see. So the problem is that the nested struct doesn't really have a sensible default value, meaning you must initialize it explicitly in the constructor. Thanks for the clarification.
Re: Initialization of nested struct fields
On Thursday, 1 January 2015 at 23:06:30 UTC, Peter Alexander wrote: Can someone please explain this behaviour? I find it totally bizarre. auto f(T)(T x) { struct S { T y; this(int) { } } return S(0); } void main() { f(f(0)); } Error: constructor f376.f!(S).f.S.this field y must be initialized in constructor, because it is nested struct Why must y be initialized in the constructor? It isn't const. Why isn't it default initialized? Is this explained anywhere in the docs? I can't see anything in the nested struct section, or in any constructor section. A simplification of your code that helped me understand what's going on: auto f() { struct S1 { this(int) { } } return S1(); } struct S2 { typeof(f()) y; /* Error: field y must be initialized in constructor, because it is nested struct */ this(int) { } } Apparently dmd thinks that the result of f must be a nested struct. I.e. it needs a context pointer. And I guess hell would break loose if you'd use a nested struct with a null context pointer. At least when the context pointer is actually used, unlike here. If the struct needed to be nested, the compiler would maybe do the right thing here: preventing null/garbage dereferencing. As it is, it should maybe see that S1 doesn't need a context pointer. You can explicitly mark the struct as not-nested by making it "static".
Re: How to create instance of class that get data from 2 another instance?
On Thursday, 1 January 2015 at 22:15:52 UTC, Suliman wrote: I have instance of class, that need data from 2 another classes. So I decided that I need to create it only after I get the data from both instance. auto mysql = new MySQL(parseconfig, seismoDownload); // the problem is here! (LOC 170) mysql need settings form parseconfig, and link from instance of seismoDownload class. But I am getting error: undefined identifier parseconfig, did you mean class p arseConfig? What I am doing wrong? http://www.everfall.com/paste/id.php?iwh4qdcqv6zi Well, there's no `parseconfig` there. Do you expect the `parseconfig` from line 30 or line 193 to be available in line 170?
Initialization of nested struct fields
Can someone please explain this behaviour? I find it totally bizarre. auto f(T)(T x) { struct S { T y; this(int) { } } return S(0); } void main() { f(f(0)); } Error: constructor f376.f!(S).f.S.this field y must be initialized in constructor, because it is nested struct Why must y be initialized in the constructor? It isn't const. Why isn't it default initialized? Is this explained anywhere in the docs? I can't see anything in the nested struct section, or in any constructor section.
Re: Is it possible to use an UDA to generate a struct inside a class ?
On Thursday, 1 January 2015 at 21:15:27 UTC, Ali Çehreli wrote: On 01/01/2015 09:35 AM, Basile Burg wrote: > On Tuesday, 30 December 2014 at 19:18:41 UTC, Basile Burg wrote: > an ICE (every > compiler crash is an ICE right ?), Yes, the compiler should never crash but produce an error message. Please report it preferably with a reduced code sample: The report is filed. I was not sure of its validity or if it could be a dup. - You realize, all of those foreach'es are processed at compile-time for code generation. There will be no 'o' at run time; so its address cannot be used. Instead, Can I get the *relative offset* of a particular member at compile-time ? Then at run-time I could easily define the delegate, eg: // the AA or some arrays filled at compile-time ptrdiff_t[string] gettersOffset; ptrdiff_t[string] settersOffset; // a delegate, set at run-time, for example in this(). myDelegate.funcptr = gettersOffset["propIdentifier"]; myDelegate.ptr = cast(void*) this; If so then the problem is solved...even if other problems could appends, for example if the methods are final, if they are inlined... Actually I'd be surprised that nobody has already designed something similar for properties (kind of "published" attribute as defined in Pascal-like languages.)
How to create instance of class that get data from 2 another instance?
I have instance of class, that need data from 2 another classes. So I decided that I need to create it only after I get the data from both instance. auto mysql = new MySQL(parseconfig, seismoDownload); // the problem is here! (LOC 170) mysql need settings form parseconfig, and link from instance of seismoDownload class. But I am getting error: undefined identifier parseconfig, did you mean class p arseConfig? What I am doing wrong? http://www.everfall.com/paste/id.php?iwh4qdcqv6zi
Re: "function ... is not callable using argument types"
On 01/01/2015 05:09 AM, Suliman wrote: But why variant: static const int PGSQL_UNITTEST_PORT = 5432; do not require of implicit convert to!short() at connection string? Walter Bright explains the reasons in his "Value Range Propagation" article: http://www.drdobbs.com/tools/value-range-propagation/229300211 Ali
Re: Is it possible to use an UDA to generate a struct inside a class ?
On 01/01/2015 09:35 AM, Basile Burg wrote: > On Tuesday, 30 December 2014 at 19:18:41 UTC, Basile Burg wrote: > an ICE (every > compiler crash is an ICE right ?), Yes, the compiler should never crash but produce an error message. Please report it preferably with a reduced code sample: https://issues.dlang.org/ > not tested on trunk: It has the same problem on git head. > > http://dpaste.dzfl.pl/70ab707b21e4 > > e.g I try to get the getter delegate in order to set a new > izPropDescriptor in an AA. foreach(m; __traits(allMembers, tp)) { foreach(o; __traits(getOverloads, tp, m)) { foreach(attr; __traits(getAttributes, o)) { static if (is(attr == get)) { writeln(attr.stringof, " < ", m); alias PT = ReturnType!o; //auto ICE = &o; //comment = no crash You realize, all of those foreach'es are processed at compile-time for code generation. There will be no 'o' at run time; so its address cannot be used. The compiler problem is probably related to that. } else if (is(attr == set)) { You probably want that to be 'else static if'. Otherwise, when the previous 'static if' fails the condition, there will be an 'if' statement inserted, which would get executed at run time. void main(string args[]){ Compiling with the -w compiler switch produces the following warning: Warning: instead of C-style syntax, use D-style syntax 'string[] args' Ali
Re: "function ... is not callable using argument types"
Suliman: But why variant: static const int PGSQL_UNITTEST_PORT = 5432; do not require of implicit convert to!short() at connection string? Because value range analysis now propagates the range even across expressions if they are const. It's a recent improvement to make the D compile a bit less stupid. Bye, bearophile
Re: Scoped external function declaration
On Thursday, 1 January 2015 at 17:51:46 UTC, novice2 wrote: I want to use external or C function. It used only one time from one D function. I want do declare C function inside D function. I don't want to declare C function in global scope. Is my wish correct? Reduced code: extern (C) int getch(); void main() { getch(); } //compiled OK void main() { extern (C) int getch(); getch(); } //Error 42: Symbol Undefined __D4test4mainFZ5getchUZi you can use local import cfun.d: module cfun; extern (C) int getc(); main.d: module main; void main() { import cfun; getc(); }
Re: Fastest Way to Append Multiple Elements to an Array
On Wednesday, 31 December 2014 at 00:36:36 UTC, Damian wrote: Append and Prepend would be very useful additions to Phobos it's quite surprising there not already there? In any case would love to see some pull requests :) Do we really need Append and Prepend (along with append and prepend) here? Doesn't [cC]hain already fulfill the needs of a lazy range in this case inplace of Append? /Per
Re: Can the order in associative array change when keys are not midified?
On 1/1/15, Tobias Pankrath via Digitalmars-d-learn wrote: > You could implement an OrderedMap!(Key, Value) via > RedBlackTree!(Tuple!(Key, Value), (a,b) => a[0] < b[0]). We could add this as an alias into Phobos or perhaps as just a documentation line on the website.
Re: Can the order in associative array change when keys are not midified?
On Thursday, 1 January 2015 at 18:08:52 UTC, Andrej Mitrovic via Digitalmars-d-learn wrote: On 1/1/15, H. S. Teoh via Digitalmars-d-learn wrote: If you need consistent ordering of values, you probably want a different data structure, like an ordered map This one works nicely on D1, I'd imagine the D2 port works just the same: https://github.com/SiegeLord/Tango-D2/blob/d2port/tango/util/container/SortedMap.d You could implement an OrderedMap!(Key, Value) via RedBlackTree!(Tuple!(Key, Value), (a,b) => a[0] < b[0]).
Re: Call of rmdir in destructor causes InvalidMemoryOperationError
Am Thu, 01 Jan 2015 16:07:06 + schrieb "thedeemon" : > Solution in this case: call rmdir not from destructor. Thanks (also to Daniel Kozak) for the information. I moved the functionality to an explicitly called method. Best regards, Timo
Re: Can the order in associative array change when keys are not midified?
On 1/1/15, H. S. Teoh via Digitalmars-d-learn wrote: > If you need consistent ordering of values, you probably want a different > data structure, like an ordered map This one works nicely on D1, I'd imagine the D2 port works just the same: https://github.com/SiegeLord/Tango-D2/blob/d2port/tango/util/container/SortedMap.d
Scoped external function declaration
I want to use external or C function. It used only one time from one D function. I want do declare C function inside D function. I don't want to declare C function in global scope. Is my wish correct? Reduced code: extern (C) int getch(); void main() { getch(); } //compiled OK void main() { extern (C) int getch(); getch(); } //Error 42: Symbol Undefined __D4test4mainFZ5getchUZi
Re: Is it possible to use an UDA to generate a struct inside a class ?
On Tuesday, 30 December 2014 at 19:18:41 UTC, Basile Burg wrote: On Tuesday, 30 December 2014 at 19:05:23 UTC, Ali Çehreli wrote: On 12/30/2014 09:42 AM, Basile Burg wrote: Ok, thx. I see the trick: mixin (propertyInjections!C); Introspection inside each class. I don't find this solution ugly btw. I think there is no other way to do this. i've chosen another way to do that but it unleashes an ICE (every compiler crash is an ICE right ?), not tested on trunk: http://dpaste.dzfl.pl/70ab707b21e4 e.g I try to get the getter delegate in order to set a new izPropDescriptor in an AA.
Re: Can the order in associative array change when keys are not midified?
On Thu, Jan 01, 2015 at 04:17:39PM +, Idan Arye via Digitalmars-d-learn wrote: [...] > My use case is that I have a large AA where the values are numbers and > the keys are strings, and I need to send it over network again and > again. The values constantly change but the keys should remain the > same(after an initial initialization process), so I don't want to > always have to send the keys, which compose the larger part of the > AA's size. If the order is fixed as long as the keys are fixed I can > cache the keys order and send only the values(without having to sort > them each time). It's risky to rely on the order of values returned by an AA. It's implementation-dependent. Although currently we don't see any reason for ever changing the order, that doesn't guarantee that a future implementation with a new, innovative lookup algorithm, won't change it, since the spec says that AA's are inherently unordered. If you need consistent ordering of values, you probably want a different data structure, like an ordered map, instead of an AA. Alternatively, you can implement your own wrapper around the built-in AA's that keeps track of insertion order, something like this: struct MyAA(K,V) { static struct WrappedValue { WrappedValue* next; V value; alias value this; } private WrappedValue[K] impl; WrappedValue* first; void opIndexAssign(V value, K key) { auto val = WrappedValue(value); val.next = first; impl[key] = val; first = &impl[key]; } ref V opIndex(K key) { return impl[key].value; } @property auto byValue() { static struct Range { WrappedValue* current; @property bool empty() { return current is null; } @property auto front() { return current.value; } void popFront() { current = current.next; } } return Range(first); } ... // other AA methods here } Some care would have to be taken care of if you need to support deleting AA keys (you have to relink stuff, so potentially you want a doubly-linked list instead of a singly-linked list here). But basically, something along these lines should give you an AA-like container that guarantees iteration order no matter what the underlying AA implementation is. T -- "No, John. I want formats that are actually useful, rather than over-featured megaliths that address all questions by piling on ridiculous internal links in forms which are hideously over-complex." -- Simon St. Laurent on xml-dev
Re: Can the order in associative array change when keys are not midified?
On 1/1/15, Peter Alexander via Digitalmars-d-learn wrote: > The order is unspecified, but an iteration must iterate in *some* > order. The question (if I've understood it correctly), is whether > that order of iteration changes when the keys aren't changed. Hmm yeah, that definitely wasn't ever specified. But remember that there is also a .rehash() method. It's a bit tricky to work with AAs for sure.. > The spec doesn't say anything about this, although I would expect > in practice that the order will not change. > > I've added a bug to track this omission from the spec: > https://issues.dlang.org/show_bug.cgi?id=13923 Thanks.
Re: Can the order in associative array change when keys are not midified?
On Thursday, 1 January 2015 at 13:39:57 UTC, Peter Alexander wrote: On Thursday, 1 January 2015 at 13:13:10 UTC, Andrej Mitrovic via Digitalmars-d-learn wrote: On 1/1/15, Idan Arye via Digitalmars-d-learn wrote: If I have an associative array and I only modify it's values, without changing the keys, can I assume that the order won't change? Associative arrays are not ordered at all. See the first note here: http://dlang.org/hash-map.html The order is unspecified, but an iteration must iterate in *some* order. The question (if I've understood it correctly), is whether that order of iteration changes when the keys aren't changed. The spec doesn't say anything about this, although I would expect in practice that the order will not change. I've added a bug to track this omission from the spec: https://issues.dlang.org/show_bug.cgi?id=13923 That's right. My use case is that I have a large AA where the values are numbers and the keys are strings, and I need to send it over network again and again. The values constantly change but the keys should remain the same(after an initial initialization process), so I don't want to always have to send the keys, which compose the larger part of the AA's size. If the order is fixed as long as the keys are fixed I can cache the keys order and send only the values(without having to sort them each time).
Re: Call of rmdir in destructor causes InvalidMemoryOperationError
On Thursday, 1 January 2015 at 15:14:41 UTC, Timo Gransch wrote: Hi, I have a class which unzips an archive into a temporary directory below the system temp folder. I want to delete this temporary directory in the class's destructor, but when I call rmdir there, I get an core.exception.InvalidMemoryOperationError@(0) Destructors are usually called by GC, during a GC cycle, so allocating and deallocating memory is not allowed there. If some function allocates, reallocates or deallocates, it will cause this very error. This means you shouldn't use any functions in a destructor that are not @nogc. Solution in this case: call rmdir not from destructor.
Re: Call of rmdir in destructor causes InvalidMemoryOperationError
Timo Gransch via Digitalmars-d-learn píše v Čt 01. 01. 2015 v 16:14 +0100: > Hi, > > I have a class which unzips an archive into a temporary directory below the > system temp folder. I want to delete this temporary directory in the class's > destructor, but when I call rmdir there, I get an > > core.exception.InvalidMemoryOperationError@(0) > > The effect is not limited to this special case. Whenever I call rmdir in the > destructor, no matter for which directory, I get the same error. > > Environment: DMD v2.066.1 (from D-Apt) on Ubuntu 14.10 (32 and 64 bit). > > Sample code: > > // --- > module main; > > import std.stdio; > import std.file; > import std.path; > > class RmdirTest > { > private string sTempDir; > > this(string sInstanceName) > { > writeln("Constructor called"); > sTempDir=tempDir() ~ dirSeparator ~ sInstanceName; > mkdir (sTempDir); > } > > ~this() > { > writeln("Destructor called"); > > if (sTempDir !is null) > { > rmdir(sTempDir); > } > } > } > > void main(string[] args) > { > RmdirTest rmDirTest=new RmdirTest("123"); > } > // --- > > Console output is: > > Constructor called > Destructor called > core.exception.InvalidMemoryOperationError@(0) > > The directory "/tmp/123" is created, but not deleted. > > When I change the line "(sTempDir !is null)" to "((sTempDir !is null) && > (exists(sTempDir)) && (isDir(sTempDir)))", the exception is thrown already on > this line, so obviously the problem also applies to other file functions > like exists and isDir. > > Is there any solution for this? > > Thanks and best regards, > Timo You shoud not use destructor for this operation. In D there is no guarantee that class destructor will be called. So you can probablly use struct instead of class or add some method and called it explicitly when needed
Call of rmdir in destructor causes InvalidMemoryOperationError
Hi, I have a class which unzips an archive into a temporary directory below the system temp folder. I want to delete this temporary directory in the class's destructor, but when I call rmdir there, I get an core.exception.InvalidMemoryOperationError@(0) The effect is not limited to this special case. Whenever I call rmdir in the destructor, no matter for which directory, I get the same error. Environment: DMD v2.066.1 (from D-Apt) on Ubuntu 14.10 (32 and 64 bit). Sample code: // --- module main; import std.stdio; import std.file; import std.path; class RmdirTest { private string sTempDir; this(string sInstanceName) { writeln("Constructor called"); sTempDir=tempDir() ~ dirSeparator ~ sInstanceName; mkdir (sTempDir); } ~this() { writeln("Destructor called"); if (sTempDir !is null) { rmdir(sTempDir); } } } void main(string[] args) { RmdirTest rmDirTest=new RmdirTest("123"); } // --- Console output is: Constructor called Destructor called core.exception.InvalidMemoryOperationError@(0) The directory "/tmp/123" is created, but not deleted. When I change the line "(sTempDir !is null)" to "((sTempDir !is null) && (exists(sTempDir)) && (isDir(sTempDir)))", the exception is thrown already on this line, so obviously the problem also applies to other file functions like exists and isDir. Is there any solution for this? Thanks and best regards, Timo
Re: Passing string literals to C
On Wednesday, 31 December 2014 at 11:19:36 UTC, Laeeth Isharc wrote: Argh - no way to edit. What's best practice here? D strings are not null-terminated. === cpling.c char* cpling(char *s) { s[0]='!'; return s; } === dcaller.d extern(C) char* cpling(char* s); void callC() { writefln("%s",fromStringz(cpling("hello\0"))); } or void callC() { writefln("%s",fromStringz(cpling(toStringz("hello"; } === am I missing a better way to do this? To call a C function you can either use string literals which are always null terminated or use std.string.toStringz (when using string variables) to add the null and return a char*. To convert from char* (from a C function return value) to a D string use std.conv.to!(string).
Re: Can the order in associative array change when keys are not midified?
On Thursday, 1 January 2015 at 13:13:10 UTC, Andrej Mitrovic via Digitalmars-d-learn wrote: On 1/1/15, Idan Arye via Digitalmars-d-learn wrote: If I have an associative array and I only modify it's values, without changing the keys, can I assume that the order won't change? Associative arrays are not ordered at all. See the first note here: http://dlang.org/hash-map.html The order is unspecified, but an iteration must iterate in *some* order. The question (if I've understood it correctly), is whether that order of iteration changes when the keys aren't changed. The spec doesn't say anything about this, although I would expect in practice that the order will not change. I've added a bug to track this omission from the spec: https://issues.dlang.org/show_bug.cgi?id=13923
Re: "function ... is not callable using argument types"
On Thursday, 1 January 2015 at 13:09:21 UTC, Suliman wrote: But why variant: static const int PGSQL_UNITTEST_PORT = 5432; do not require of implicit convert to!short() at connection string? As I said the compiler infers that 5432 is between short.min and short.max. Try it with something out of this range. BTW: If you just want to have a global constant, I'd use enum or immutable: enum PGSQL_UNITTEST_PORT = 5432; immutable PGSQL_UNITTEST_PORT = 5432;
Re: Can the order in associative array change when keys are not midified?
On 1/1/15, Idan Arye via Digitalmars-d-learn wrote: > If I have an associative array and I only modify it's values, > without changing the keys, can I assume that the order won't > change? Associative arrays are not ordered at all. See the first note here: http://dlang.org/hash-map.html
Re: "function ... is not callable using argument types"
But why variant: static const int PGSQL_UNITTEST_PORT = 5432; do not require of implicit convert to!short() at connection string?
Re: "function ... is not callable using argument types"
So it's look like that it can accept strings and ints without problem. And I really can't understand why it's accept only "static const string" constructions... int does not implicitly convert to short. It does in the hardcoded version, because the compiler can prove that the value is between short.min and short.max. Use to!short() to convert it to short.
Can the order in associative array change when keys are not midified?
If I have an associative array and I only modify it's values, without changing the keys, can I assume that the order won't change?
"function ... is not callable using argument types"
I need to pass some config to ddbc driver. When I use static const all work ok. static const string PGSQL_UNITTEST_HOST = "localhost"; static const intPGSQL_UNITTEST_PORT = 5432; static const string PGSQL_UNITTEST_USER = "postgres"; static const string PGSQL_UNITTEST_PASSWORD = "Infinity8"; static const string PGSQL_UNITTEST_DB = "test2"; But using static const mean that settings will be hardcoded. But I need to read it from config file. So I need so simply declared it as: this(parseConfig parseconfig) { string PGSQL_UNITTEST_HOST = parseconfig.dbhost; intPGSQL_UNITTEST_PORT = parseconfig.dbport; string PGSQL_UNITTEST_USER = parseconfig.dbuser; string PGSQL_UNITTEST_PASSWORD = parseconfig.dbpass; string PGSQL_UNITTEST_DB = parseconfig.dbname; ... } But when I do it like above code stop compile and I am getting error: source\app.d(218): Error: function ddbc.drivers.pgsqlddbc.PGSQLDriver.generateUrl (string host, ushort port, string dbname) is not callable using argument types (string, int, string) I looked at driver source code and have found next code: class PGSQLDriver : Driver { // helper function public static string generateUrl(string host, ushort port, string dbname) { return "postgresql://" ~ host ~ ":" ~ to!string(port) ~ "/" ~ dbname; } public static string[string] setUserAndPassword(string username, string password) { string[string] params; params["user"] = username; params["password"] = password; params["ssl"] = "true"; return params; } So it's look like that it can accept strings and ints without problem. And I really can't understand why it's accept only "static const string" constructions...