Re: What's equivalent to C#'s select?
On Sunday, 14 January 2018 at 22:07:22 UTC, Marc wrote: thanks, can i use it at compile time as well? enum isMutableString(string field) = is(typeof(__traits(getMember, >C, field)) == string); static foreach(field; [FieldNameTuple!C].filter!(f => isMutableString!(f))) { writeln(field); } You're mixing compile-time and run-time logic here in a way that D doesn't allow. In particular, isMutableString requires the passed string to be a compile-time constant, and filter works on run-time values. There are a few different ways to resolve this. First, std.meta has the Filter template, which behaves much in the same way as std.algorithm.filter, but with compile-time tuples: static foreach (field; Filter!(isMutableString, FieldNameTuple!C)) { writeln(field); } The other option is to rewrite isMutableString to work with run-time values: bool isMutableString(string field) { switch (field) { foreach (cField; FieldNameTuple!C) { case cField: return is(typeof(__traits(getMember, C, cField)) == string); } default: return false; } } static foreach(field; [FieldNameTuple!C].filter!(f => isMutableString(f))) { writeln(field); } Both of these give the same output, and should be what you want. -- Simen
Re: String Type Usage. String vs DString vs WString
On Monday, 15 January 2018 at 02:05:32 UTC, Chris P wrote: Is usage of one type over the others encouraged? I would use string (UTF-8) throughout the program, but there seems to be no style guideline for this. Keep in mind two gotchas: D's foreach and D's ranges will autodecode and silently iterate over dchar, not char, even when the input is string, not dstring. (It's also possible to explicitly decode strings, see std.utf and std.uni.) If you call into the Windows API, some functions require extra care if everything in your program is UTF-8. But I still agree with the approach to keep everything as string in your program, and then wrap the Windows API calls, as the UTF-8 Everywhere manifesto suggests: http://utf8everywhere.org/ -- Simon
Re: String Type Usage. String vs DString vs WString
On Monday, January 15, 2018 03:14:02 Tony via Digitalmars-d-learn wrote: > On Monday, 15 January 2018 at 02:09:25 UTC, rikki cattermole > > wrote: > > Unicode has three main variants, UTF-8, UTF-16 and UTF-32. > > The size of a code point is 1, 2 or 4 bytes. > > I think to be technically correct, 1 (UTF-8), 2 (UTF-16) or 4 > (UTF-32) bytes are referred to as "code units" and the size of a > code point varies in UTF-8 and UTF-16. Yes, for UTF-8, a code unit is 8 bits, and there can be up to 6 of them (IIRC) in a code point. For UTF-16, a code unit is 16 bits, and there are either 1 or 2 code units per code point. For UTF-32, a code unit is 32 bits, and there is always 1 code unit per code point. For better or worse (mostly worse), ranges then treat all strings as ranges of code points and decode them to code points such that get a range of dchar (which means fun things like isRandomAccessRange!string and hasLength!string are false). As I understand it, each code point is then something which can be physically printed, but either way, it's not necessarily a full character. Multiple code points can then be combined to make a grapheme cluster (which then corresponds to what we'd normally consider a full character - e.g. a letter and an accent can each be a code point which are then combined to create an accented character). std.uni provides the functionality for operating on graphemes. And std.utf.byCodeUnit can be used to treat strings as ranges of code units instead of code points (and a fair bit of Phobos takes the solution of specializing range-based code for strings to avoid the auto-decoding). All in all, the whole thing is annoyingly complicated, though at least D is much more explicit about it than most languages, and I suspect that your average D programmer is better educated about Unicode than your average programmer. And having to figure out why the heck strings and wstrings act so bizarrely as ranges does have the positive side effect of putting it even more in your face than it would be otherwise, making it that much more likely that folks are going to learn about Unicode - though I still think that we'd be better off if we could ever figure out how to treat all strings as ranges of code units without breaking everything in the process. :| - Jonathan M Davis
Re: SegFault with HibernateD
On Saturday, 13 January 2018 at 06:18:43 UTC, Ali Çehreli wrote: On 01/12/2018 06:50 PM, Venkat wrote: > Sorry about all these posts. Wish there were an edit button. That's ok. :) These are actually newsgroups (see NNTP protocol). Newsgroups don't have any edit functionality. The "forum" is just a web interface to newsgroups. Ali Oh! ok, that makes sense. TY.
Re: How do I get the value cache()?
On Monday, 15 January 2018 at 02:54:16 UTC, Marc wrote: let's assume I have class C { static string foo() { writeln("got called!"); // } } then I want to cache foo at some point: import std.algorithm; auto v = cache(c.foo); That doesn't do what I think you think it does. http://dpldocs.info/experimental-docs/std.algorithm.iteration.cache.html Notice it is from the `iteration` module.. the cache function caches results of an iteration, not results of a function. It actually returns an object that caches the individual characters of that string, rather than the string! The memoize function is closer to what you want: http://dpldocs.info/experimental-docs/std.functional.memoize.2.html Though tbh, I think you should just simply do: string s = c.foo; // go ahead and just use s now here's why I'm confused more often than I should: I geeeting to D's way to do thing and the auto keyword even in documentation confused me a bit as I'm used to C++/C# world where the struct/class returned is explicity so I just navigate to aggregate type's documentation page. The tricky thing is a lot of them create a new type based on its arguments, so there would be nothing to navigate to - it all depends on what you pass it. Though, you'll notice with memoize, it returns `ReturnType!fun`, that is, the same return type fun (which you passed to it) had.
Re: String Type Usage. String vs DString vs WString
On Monday, 15 January 2018 at 02:09:25 UTC, rikki cattermole wrote: Unicode has three main variants, UTF-8, UTF-16 and UTF-32. The size of a code point is 1, 2 or 4 bytes. I think to be technically correct, 1 (UTF-8), 2 (UTF-16) or 4 (UTF-32) bytes are referred to as "code units" and the size of a code point varies in UTF-8 and UTF-16.
How do I get the value cache()?
let's assume I have class C { static string foo() { writeln("got called!"); // } } then I want to cache foo at some point: import std.algorithm; auto v = cache(c.foo); I call do: for(int i = 0; i <10; i++) { writeln(v); } then it'll print "got called" only once, which is what I want but something obvious is how do I get the returned value as a string? here's why I'm confused more often than I should: I geeeting to D's way to do thing and the auto keyword even in documentation confused me a bit as I'm used to C++/C# world where the struct/class returned is explicity so I just navigate to aggregate type's documentation page.
Re: String Type Usage. String vs DString vs WString
On Monday, January 15, 2018 02:22:09 Chris P via Digitalmars-d-learn wrote: > On Monday, 15 January 2018 at 02:15:55 UTC, Nicholas Wilson wrote: > > On Monday, 15 January 2018 at 02:05:32 UTC, Chris P wrote: > >> [...] > >> > > string == immutable( char)[], char == utf8 > > > > wstring == immutable(wchar)[], char == utf16 > > dstring == immutable(dchar)[], char == utf32 > > > > Unless you are dealing with windows, in which case you way need > > to consider using wstring, there is very little reason to use > > anything but string. > > > > N.B. when you iterate over a string there are a number of > > different "flavours" (for want of a better term) you can > > iterate over, bytes, unicode codepoints and graphemes ( I'm > > possible forgetting some). have a look in std.uni and related > > modules. Iteration in Phobos defaults to coepoints I think. > > > > TLDR use string. > > Thank you (and rikki) for replying. Actually, I am using Windows > (Doh!) but I now understand. Cheers! Even with Windows, there usually isn't any reason to use wstring. The only reason that wstring might be more desirable on Windows is that you need UTF-16 when dealing with the Windows API calls, and that's normally only going to come up if you're not writing platform-independent code. The common stuff such as file access is already wrap by Phobos (e.g. in std.file and std.stdio), so most programs, don't need to worry about the Windows API calls. And even if you do, the best practice generally is to use string everywhere in your code and then only convert to a zero-terminated wchar* when making the Windows API calls (either by actually allocating a zero-terminated wchar* or using a static array with the appropriate wchar set to 0, depending on the context). If you have to do a ton with Windows API calls, at some point, it arguably becomes better to just keep them as wstrings to avoid the conversions, but even then, because strings in D aren't zero-terminated, and the C API calls usually require them to be, you're often forced to copy the string to pass it to a Windows API call anyway, in which case, you lose most of the benefit of keeping stuff around in wstrings instead of just using strings everywhere. If you do need to worry about call a Windows API call, then check out toUTFz in std.utf, since it will allow you to easily convert to zero-terminated strings of any character type (std.string.toStringz handles zero-terminated strings as well, but just for string). - Jonathan M Davis
Re: Using Postgres connection functions
On Saturday, 13 January 2018 at 17:58:14 UTC, Joe wrote: On Saturday, 13 January 2018 at 10:10:41 UTC, Jacob Carlborg wrote: There's a native D library, ddb [1], for connecting to Postgres. Then you don't have to worry about null-terminated strings. There are several D libraries that I would consider "native": derelict-pq, dpq, dpq2 and ddb. The latter perhaps has the distinction that it doesn't use libpq, but rather implements the Postgres FE/BE protocol. That's a bit *too* native for my taste. It means the library maintainer has to keep up with changes to the internal protocol, which although published, the Postgres group doesn't have to maintain compatibility from version to version. For example, they haven't dropped the PQsetdbLogin function even though the PQconnectdb and PQconnectdbParams functions are obviously preferred. OTOH, there used to be an AsciiRow message format in the protocol, that was dropped, unceremoniously (not even mentioned in the release notes). If you are after a good way to use Postgres in a real-world application, I highly recommend ddbc[1] (which also supports other backends). There are a lot of D Postgres bindings out there, and all of them are about 70% completed, but nobody really bothered to make one finished and really good (and well maintained) binding. DDBC is really close to being complete, and contains a few convenience features that make it nice to use in an application. It also is used by Hibernated[2] in case you want an ORM for your app at some point. Both libraries aren't up to tools like SQLAlchemy & Co. from other programming languages, but they are decent. For simple cases, dpq2 & Co. might work well enough as well. In any case, please don't start another Postgres library and consider contributing to one of the existing ones, so that we maybe have one really awesome, 100% complete library at some point. If, on the other hand, your goal is to learn about the low-level Postgres interface and not just to have a Postgres interface for an application you develop, by all means, play with it :-) Cheers, Matthias [1]: https://github.com/buggins/ddbc [2]: https://github.com/buggins/hibernated
Re: String Type Usage. String vs DString vs WString
On Monday, 15 January 2018 at 02:15:55 UTC, Nicholas Wilson wrote: On Monday, 15 January 2018 at 02:05:32 UTC, Chris P wrote: [...] string == immutable( char)[], char == utf8 wstring == immutable(wchar)[], char == utf16 dstring == immutable(dchar)[], char == utf32 Unless you are dealing with windows, in which case you way need to consider using wstring, there is very little reason to use anything but string. N.B. when you iterate over a string there are a number of different "flavours" (for want of a better term) you can iterate over, bytes, unicode codepoints and graphemes ( I'm possible forgetting some). have a look in std.uni and related modules. Iteration in Phobos defaults to coepoints I think. TLDR use string. Thank you (and rikki) for replying. Actually, I am using Windows (Doh!) but I now understand. Cheers!
Re: String Type Usage. String vs DString vs WString
On Monday, 15 January 2018 at 02:05:32 UTC, Chris P wrote: Hello, I'm extremely new to D and have a quick question regarding common practice when using strings. Is usage of one type over the others encouraged? When using 'string' it appears there is a length mismatch between the string length and the char array if large Unicode characters are used. So I figured I'd ask. Thanks in advance, Chris P - Tampa string == immutable( char)[], char == utf8 wstring == immutable(wchar)[], char == utf16 dstring == immutable(dchar)[], char == utf32 Unless you are dealing with windows, in which case you way need to consider using wstring, there is very little reason to use anything but string. N.B. when you iterate over a string there are a number of different "flavours" (for want of a better term) you can iterate over, bytes, unicode codepoints and graphemes ( I'm possible forgetting some). have a look in std.uni and related modules. Iteration in Phobos defaults to coepoints I think. TLDR use string.
String Type Usage. String vs DString vs WString
Hello, I'm extremely new to D and have a quick question regarding common practice when using strings. Is usage of one type over the others encouraged? When using 'string' it appears there is a length mismatch between the string length and the char array if large Unicode characters are used. So I figured I'd ask. Thanks in advance, Chris P - Tampa
Re: String Type Usage. String vs DString vs WString
On 15/01/2018 2:05 AM, Chris P wrote: Hello, I'm extremely new to D and have a quick question regarding common practice when using strings. Is usage of one type over the others encouraged? When using 'string' it appears there is a length mismatch between the string length and the char array if large Unicode characters are used. So I figured I'd ask. Thanks in advance, Chris P - Tampa D's strings are Unicode. Unicode has three main variants, UTF-8, UTF-16 and UTF-32. The size of a code point is 1, 2 or 4 bytes. But here is the thing, what is displayed (a character) could be multiple code points and these can be combined to form a grapheme. So yes, there will be length mismatches between them :)
Re: Range over a container r-value with disabled postblit
On 1/14/18 4:59 PM, Nordlöw wrote: On Sunday, 14 January 2018 at 21:57:37 UTC, Nordlöw wrote: Note that __trait(isLvalue, this) cannot be used to detect whether `this` is an l-value or an r-value, which I find strange. Shall be __traits(isRef, this) That would be difficult because lval/rval is known on he callee side. -- Andrei
Re: What's equivalent to C#'s select?
On Sunday, 14 January 2018 at 21:59:26 UTC, Seb wrote: On Sunday, 14 January 2018 at 21:21:52 UTC, Marc wrote: give a list, how can I select only the elements of a range according to a condition give by a lamba function? something like this: auto l = myList.select(e => e.id < 300); it would return a range. Similar to C#'s select: https://msdn.microsoft.com/en-us/library/bb548891(v=vs.110).aspx Shameless self-plug - you might like this (incomplete) comparison between LINQ and D ranges: https://github.com/wilzbach/linq Sounds pretty interesting, I'll give a try! Thanks
Re: What's equivalent to C#'s select?
On Sunday, 14 January 2018 at 21:38:39 UTC, drug wrote: 15.01.2018 00:21, Marc пишет: give a list, how can I select only the elements of a range according to a condition give by a lamba function? something like this: auto l = myList.select(e => e.id < 300); it would return a range. Similar to C#'s select: https://msdn.microsoft.com/en-us/library/bb548891(v=vs.110).aspx import std.algorithm : filter; auto l = myList.filter!(e => e.id < 300); thanks, can i use it at compile time as well? enum isMutableString(string field) = is(typeof(__traits(getMember, >C, field)) == string); static foreach(field; [FieldNameTuple!C].filter!(f => isMutableString!(f))) { writeln(field); } give error: Error: variable f cannot be read at compile time
Re: Range over a container r-value with disabled postblit
On Sunday, 14 January 2018 at 21:57:37 UTC, Nordlöw wrote: Note that __trait(isLvalue, this) cannot be used to detect whether `this` is an l-value or an r-value, which I find strange. Shall be __traits(isRef, this)
Re: What's equivalent to C#'s select?
On Sunday, 14 January 2018 at 21:21:52 UTC, Marc wrote: give a list, how can I select only the elements of a range according to a condition give by a lamba function? something like this: auto l = myList.select(e => e.id < 300); it would return a range. Similar to C#'s select: https://msdn.microsoft.com/en-us/library/bb548891(v=vs.110).aspx Shameless self-plug - you might like this (incomplete) comparison between LINQ and D ranges: https://github.com/wilzbach/linq
Re: Range over a container r-value with disabled postblit
On Sunday, 14 January 2018 at 14:04:46 UTC, kinke wrote: That sounds reasonable. For something like `foreach (e; makeRange().wrapRangeByRef())`, referencing the makeRange() struct rvalue by pointer in the wrapped range won't work, as the underlying range lifetime ends with the foreach range expression, while the wrapped range's lifetime extends to the end of the foreach loop. So making the wrapped range take ownership of a range rvalue by moving it into a member (of ByRvalueElement) makes sense IMO. Lvalues aren't moved implicitly by the compiler, so referencing them by pointer () is safe. Thanks for confirmation, kinke. Further, if we add the new trait(s) __trait(isLvalue, symbol) __trait(isRvalue, symbol) or perhaps simply just __trait(isRvalue, symbol) that can be applied with symbol being `this` to make this static code branching work inside member functions such as opSlice aswell. What do you think of such an addition? Note that __trait(isLvalue, this) cannot be used to detect whether `this` is an l-value or an r-value, which I find strange.
Re: What's equivalent to C#'s select?
15.01.2018 00:21, Marc пишет: give a list, how can I select only the elements of a range according to a condition give by a lamba function? something like this: auto l = myList.select(e => e.id < 300); it would return a range. Similar to C#'s select: https://msdn.microsoft.com/en-us/library/bb548891(v=vs.110).aspx import std.algorithm : filter; auto l = myList.filter!(e => e.id < 300);
What's equivalent to C#'s select?
give a list, how can I select only the elements of a range according to a condition give by a lamba function? something like this: auto l = myList.select(e => e.id < 300); it would return a range. Similar to C#'s select: https://msdn.microsoft.com/en-us/library/bb548891(v=vs.110).aspx
Re: How do I get class member type?
On Sunday, 14 January 2018 at 19:08:44 UTC, Marc wrote: On Sunday, 14 January 2018 at 18:18:50 UTC, Marc wrote: I didn't find how using traits I could get a class member type? I need to test if give class member is not immutable, I find isMutable but not how get a type from give class member to pass to it. for clarify, I want all this at compile time, imaginary code example: static foreach(field; FieldNameTuple!C) { static if(isFunction!(__traits(getMember, C, field)) && isMutable(typeof(__traits(getMember, C, field { // do something } } I solved with this: enum isMutableString(string field) = is(typeof(__traits(getMember, Field, field)) == string);
Re: How do I get class member type?
On Sunday, 14 January 2018 at 18:18:50 UTC, Marc wrote: I didn't find how using traits I could get a class member type? I need to test if give class member is not immutable, I find isMutable but not how get a type from give class member to pass to it. for clarify, I want all this at compile time, imaginary code example: static foreach(field; FieldNameTuple!C) { static if(isFunction!(__traits(getMember, C, field)) && isMutable(typeof(__traits(getMember, C, field { // do something } }
Re: variable template question
On Sunday, 14 January 2018 at 16:23:18 UTC, kdevel wrote: Why does this compile while both of the commented lines give a compile error. The code boils down to this: struct decimal32 { this(int x) {} } immutable decimal32 c = 3; /* works */ void main () { immutable decimal32 i = 1; /* error */ } I think this is CTFE being unexpectedly smart. If you add the `pure` attribute to the constructor, then the `i` line works as well. That's because a strongly pure constructor is guaranteed to return a unique object, and a unique object can be converted implicitly to other mutability levels. The `pure` attribute is needed for `i`, because here the compiler only looks at the function attributes to determine purity. No `pure` attribute -> function is regarded as impure. But for `c`, the constructor goes through CTFE, and CTFE doesn't care all that much about the `pure` attribute. Instead, CTFE just tries to evaluate the function and aborts when it encounters an action that would be impure.
How do I get class member type?
I didn't find how using traits I could get a class member type? I need to test if give class member is not immutable, I find isMutable but not how get a type from give class member to pass to it.
Re: Problem with function taking variable number of arguments with -m64
So is this a bug, or am I misunderstanding something?
variable template question
vartmpl.d ``` import std.stdio : writeln; import decimal : decimal32; template F(T) { immutable T c = 3; } void foo (T) () { immutable T t = 1; } void main () { // immutable decimal32 i = 1; // Error: none of the overloads of '__ctor' are callable using a immutable object // foo!decimal32; // Error: none of the overloads of '__ctor' are callable us ing a immutable object, candidates are: alias c = F!decimal32.c; c.writeln; writeln (typeof (c).stringof); } ``` $ dmd -g vartmpl.d decimal.git/libdecimal.a $ ./vartmpl 3 immutable(Decimal!32) Why does this compile while both of the commented lines give a compile error. decimal ist http://rumbu13.github.io/decimal/
Re: Range over a container r-value with disabled postblit
On Sunday, 14 January 2018 at 01:38:17 UTC, Nordlöw wrote: My current proposal for a solution is to make `byElement` a free unary function byElement(auto ref X x) which statically checks via static if (__traits(isRef, x)) whether the `X`-instance is passed as either an - l-value, where my current solution works (ByLvalueElement), or - r-value, in which the X-instance instead is moved into range (ByRvalueElement). That sounds reasonable. For something like `foreach (e; makeRange().wrapRangeByRef())`, referencing the makeRange() struct rvalue by pointer in the wrapped range won't work, as the underlying range lifetime ends with the foreach range expression, while the wrapped range's lifetime extends to the end of the foreach loop. So making the wrapped range take ownership of a range rvalue by moving it into a member (of ByRvalueElement) makes sense IMO. Lvalues aren't moved implicitly by the compiler, so referencing them by pointer () is safe.
Re: function template specialization question D vs. C++
On Sunday, 14 January 2018 at 02:24:52 UTC, Adam D. Ruppe wrote: On Sunday, 14 January 2018 at 02:14:50 UTC, Jonathan M Davis wrote: If you're using template constraints rather than template specializations, then you can't have any unconstrained templates. Not true: see the tip of the week here http://arsdnet.net/this-week-in-d/2016-sep-04.html Thanks for that hint (self specialization). cppcompat.d ``` import std.stdio; void foo (T) () { writeln ("(1) ", __PRETTY_FUNCTION__); } void foo (T: T) () if (is (T == float)) { writeln ("(2) ", __PRETTY_FUNCTION__); } void foo (T: T) () if (is (T == double)) { writeln ("(3) ", __PRETTY_FUNCTION__); } void main () { foo!int; foo!float; foo!double; foo!real; foo!string; } ``` $ ./cppcomat (1) void cppcompat.foo!int.foo() (2) void cppcompat.foo!float.foo() (3) void cppcompat.foo!double.foo() (1) void cppcompat.foo!real.foo() (1) void cppcompat.foo!string.foo()
Re: Where can get the Number Convert module?Thanks.
On Sunday, 14 January 2018 at 03:49:05 UTC, Jonathan M Davis wrote: I get the result "1000" from byte[] byteData =[0,0,0,8]; Thank you very much. Good to hear. On a side note, I would point out that you almost certainly want to be using ubyte and not byte. byte is signed, whereas ubyte is unsigned. So, if you want to represent bytes of memory rather than an integral value between 1 and 127, then you want ubyte. - Jonathan M Davis Thank you.