Re: Is it possible to do this with a template?
On Friday, 17 December 2021 at 07:52:18 UTC, rempas wrote: I want to use an expression and put it in place inside the `if` parentheses. The expression is: `is(typeof(val) == type)`. I want to use a template called "is_same" that will take the value and a type to place them to the respective places. I have tried the following but it doesn't seem to work: ``` mixin template is_same(val, type) { is(typeof(val) == type) } void main() { int val = 10; static if (is_same!(val, int)) {} } ``` When trying to compile, I'm taking the following error message: ``` Error: declaration expected, not `is` ``` Is this a limitation of templates in D or is there a way to bypass this? It isn't really about limitation of templates. You're trying to use mixin template and it's main purpose is to inject declarations. If you want to replace `is expression` with template you could use something like this: ```d bool is_same(alias value, T)() { return is(typeof(value) == T); } void main() { int value = 10; static if (is_same!(value, int)) { writeln("it is true!"); } else { writeln("it is false!"); } } ``` Personally, I don't see any benefit with replacing that kind of `is expressions` with templates. Perhaps I'm missing something :)
Re: Counting number of regular expressions matches
On Friday, 9 April 2021 at 15:01:58 UTC, Alain De Vos wrote: I've got, import std.regex: regex,matchAll; ... string regfiltertext="\\b"~entryfilter.getText()~"\\b"; auto reg = regex(regfiltertext); auto result = name.strip("_").matchAll(reg); int t=0; foreach (c; result) t+=1; This make t the number of regular expressions matches. Is there a better way to have the number of matches ? `matchAll` returns `RegexMatch` which is a ForwardRange, so it should be possible to get it's length using `walkLength`. ```d auto r = regex(`([a-z])a`); auto result = "banana".matchAll(r); writeln(result); // [["ba", "b"], ["na", "n"], ["na", "n"]] writeln(result.walkLength); // 3 ``` I'm not familiar with using matches with `std.regex`, but I hope this solves your problem.
Re: fold on empty range
On Wednesday, 17 February 2021 at 11:38:45 UTC, Rumbu wrote: On Wednesday, 17 February 2021 at 10:15:10 UTC, Mitacha wrote: it'll use empty string as first element in range. BTW perheps you could use `joinner` instead of this `fold` to join values with ",". Thanks for that. I thought to joiner too, but it doesn't work. I need fold to take a list of strings and concatenate them. Basically I read comma separated keywords from various sources and i want to iterate through all of them. If you know other method without the involved allocation of fold... .map!(a => a.hit.stripLeft("[").strip("]")) //"k1,k2", "k3,k4" ... .fold!((a, b) => a ~ "," ~ b)("") //"k1,k2,k3,k4,..." .splitter(',') //"k1", "k2", "k3", "k4", ..., .map!(a => a.stripLeft("\" '").strip("\" '")) .filter!(a => a.length && !a.any!(b => b == ' ' || b == '\\' || b == '/' || b == ':')) .array .sort .uniq; If you replace `fold` and `splitter` with this, then it doesn't allocate: ``` auto fn() @nogc { return only("k1,k2", "k3,k4") .map!(x => x.splitter(",")) .joiner; } void main() { auto range = fn(); range.writeln; } ```
Re: fold on empty range
On Wednesday, 17 February 2021 at 09:21:47 UTC, Rumbu wrote: In the expression below: return matchAll(content, keywordsPattern) .map!(a => a.hit.stripLeft("[").strip("]")) .fold!((a, b) => a ~ "," ~ b) .splitter(',') .map!(a => a.stripLeft("\" ").strip("\" ")) .filter!(a => !a.any!(b => b == ' ' || b == '\\' || b == '/' || b == ':')) .array .sort .uniq; fold is throwing an exception if the result of the previous map is empty. Is there any way to express something to convince fold to return the empty range received from map? Of course, I know I can test for empty in a separate expression, but I'd like to keep my expression flow as it is. I think you can try using `fold` with seed value: ``` .map!(a => a.hit.stripLeft("[").strip("]")) .fold!((a, b) => a ~ "," ~ b)("") .splitter(',') ``` it'll use empty string as first element in range. BTW perheps you could use `joinner` instead of this `fold` to join values with ",".
Re: How to specify which parameters a function that is received as an argument should receive?
On Tuesday, 13 October 2020 at 09:02:04 UTC, Marcone wrote: How to specify which parameters a function that is received as an argument should receive? Example: import std; void myfun(int n){ writeln(n); } void test(lazy void delegate() fun) // how specify that "fun" may receive int ? { fun(int); } test({myfun;}); You need to change signature of your function to: ``` void test(lazy void delegate(int) fun) /// ^ this is all you need :D ``` And then pass some value to `fun` Working example: https://run.dlang.io/is/B9bbVl
Re: Range checked assignment
On Tuesday, 8 September 2020 at 14:18:14 UTC, Cecil Ward wrote: What I would like to do is (in pseudo-code) : declare_var my_var : int range 0..7; // i.e. 0 <= val <= 7; my_var = 6; // ok my_var = 8; // bang ! static assert fail or assert fail at runtime my_var = 6; my_var += 2; // bang ! value 8 is > 7 So every assignment is range-checked at either compile-time if at all possible or else at runtime. This includes things like += and initialisers of course, not just straight assignment. I assumed I would have to create a struct type definition and handle various operators. How many will I have to handle? I would of course make it a template so I can reuse this otherwise horribly repetitive code. I believe you could use Checked (https://dlang.org/library/std/experimental/checkedint.html) with custom hook or roll your own type with appropriate operator overloading(https://dlang.org/spec/operatoroverloading.html). Code for this won't be that bad, thanks to string mixins. Just mixin("lhs" ~ op ~ "rhs") and Bob's your uncle :).
Re: Uploading coverage to Codecov doesn't work
On Saturday, 11 July 2020 at 09:43:39 UTC, Basile B. wrote: On Wednesday, 8 July 2020 at 15:55:58 UTC, Mitacha wrote: [...] It's broken for me too, on gitlab,... --- GitLab CI detected. project root: . --> token set from env Yaml found at: ./.codecov.yml ==> Running gcov in . (disable via -X gcov) ==> Python coveragepy not found ==> Searching for coverage reports in: + . --> No coverage report found. Please visit http://docs.codecov.io/docs/supported-languages --- That used to work perfectly. Note that it's broken for DMD test suite too. I filed an issue on codecov community forum https://community.codecov.io/t/uploading-d-lang-coverage-doesnt-work/1740 So hopefully, someone will look at this.
Uploading coverage to Codecov doesn't work
Hello there, I've been trying to setup bitbucket pipelines to submit coverage to codecov, but with no luck. I use `dub run -b unittest-cov` and it generates .lst files correctly, then `bash <(curl -s https://codecov.io/bash) -t $CODECOV_TOKEN` is called, but all I get is: ``` ==> Bitbucket detected. project root: . Yaml not found, that's ok! Learn more at http://docs.codecov.io/docs/codecov-yaml ==> Running gcov in . (disable via -X gcov) ==> Python coveragepy not found ==> Searching for coverage reports in: + . --> No coverage report found. Please visit http://docs.codecov.io/docs/supported-languages ``` No reports were uploaded. The thing I'm concerned about is "--> No coverage report found.". I checked and token is supplied. I ran same commands locally and get same result. Is there some magic configuration in yaml file necessary, to make that work?
How to copy const object?
I've a const struct object and I'd like to make a mutable copy of it. Struct definition contains string and an array of structs. ``` struct A { string a; B[] b; } struct B { string a; string b; } ``` As far as I can tell copy constructor isn't generated for struct `A` because it contains an array. Correct? Is there an idiomatic way to create copy of a const object?
Re: How to iterate over range two items at a time
On Monday, 17 February 2020 at 05:04:02 UTC, Adnan wrote: What is the equivalent of Rust's chunks_exact()[1] method in D? I want to iterate over a spitted string two chunks at a time. [1] https://doc.rust-lang.org/beta/std/primitive.slice.html#method.chunks_exact It sounds similar to `slide` https://dlang.org/phobos/std_range.html#slide
Re: Type Inference and Try Blocks
On Monday, 20 January 2020 at 23:16:07 UTC, Henry Claesson wrote: This isn't a D-specific "problem", but there may be D-specific solutions. I have a function `doSomething()` that returns a Voldemort type, and this same function also throws. So, there's this: try { auto foo = doSomething(); } catch (AnException e) { // Do stuff } The problem that I'm encountering is that I'd like, assuming no exception was thrown, to use foo outside the `try` (or `finally`) block to avoid nesting as any operations on `foo` from that point onward may also throw. Are there any constructs that act as alternatives to try/catch/finally so that I can do this? (This issue could very well stem from poor design and not being familiar with programming using exceptions. So feel free to ignore.) Thanks You could try using `ifThrown` from `std.exception`. It lets you turn statement based exception handling into expression based one. So, if there is some "default" value in your case you could return that. auto foo = doSomething().ifThrown!AnException("defaultValue");
Re: ddox build locally failed
On Saturday, 26 October 2019 at 16:39:19 UTC, berni44 wrote: I tried to build ddox documentation locally with the command (in dlang.org): make -j3 -f posix.mak apidocs-prerelease After a while I get the message "Linking..." followed by a call of dmd with lots of parameters, among them -L-lssl and -L-lcrypto. This ends in: /usr/bin/ld: cannot find -lssl /usr/bin/ld: cannot find -lcrypto I'm using debian stable. There are probably some packages missing or maybe a path needs to be set. Any ideas? It looks like your missing dev dependencies for ssl. Try sudo apt-get install libssl-dev And build again
Using output-range overloads of SysTime.toISO{Ext}String with formatting code
On Monday, 8 July 2019 at 12:53:18 UTC, Digital Mars wrote: 08.07.2019 13:38, Joseph Rushton Wakeling пишет: [...] Sorry that my answer wasn't thoughtful. I guess that there is no way to have `writeln` automatically use the output range overload instead of allocating one. You need somehow to provide the output range to `toISOExtString` explicitly because `writeln` outputs the return of `toISOExtString` and have no ability to use specific overload. That is compiler calls `toISOExtString` and then passes its return to `writeln`. Probably library solution isn't possible in this case. Workaround is using own wrapper to provide output range to `toISOExtString`. I've managed to make it work using 'alias this' and wrapper struct. https://run.dlang.io/is/3SMEFZ It's not an elegant solution, there could be a better way to do this.
Few questions about staticMap
Hi everyone, I checked, just out of curiosity, what is staticMap's implementation. It's implemented using recursive, this made me think if there is way to use static foreach instead. I came out with following solution: https://run.dlang.io/is/qvgJaw I checked time it took compiler to compile std and my version for 7 parameters and there was no difference. The only difference I found was number of template instantiations: 1 for my code and 9 for std version. Are there any benefits to implementing staticMap use recursive template?