Re: adding delegate to opApply

2019-09-02 Thread Paul Backus via Digitalmars-d-learn
On Monday, 2 September 2019 at 12:43:31 UTC, berni wrote: I need to pass the delegate add_new_way somehow to opApply. Here I managed this, by adding this delegate to the remove-delegate and safe it away for further use. This works, because if remove is never called, add_new_way is not used

Re: Input/Output multiple values from function

2019-09-01 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 1 September 2019 at 20:42:28 UTC, Jabari Zakiya wrote: It still won't compile, with this error. Error: AliasSeq!(modpg, res_0, restwins, resinvrs) is not an lvalue and cannot be modified Here's a gist of the code. Top functions in code with issues are genPgParameters and

Re: Is removing elements of AA in foreach loop safe?

2019-08-30 Thread Paul Backus via Digitalmars-d-learn
On Friday, 30 August 2019 at 13:43:54 UTC, XavierAP wrote: On Thursday, 29 August 2019 at 10:11:58 UTC, berni wrote: Iterating of some structure and removing elements thereby is always errorprone and should be avoided. But: In case of AA, I've got the feeling, that it might be safe: foreach

Re: Sort Associative Array by Key

2019-08-25 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 25 August 2019 at 19:03:10 UTC, JN wrote: I think normal lambdas are better than these string ones: foo.byPair.array.sort!((a, b) => a[0] < b[0]).map!(a => a[1]).writeln; You can also use names instead of numeric indices: foo.byPair.array.sort!((a, b) => a.key < b.key).map!(a =>

Re: How to removes an variable topmost const. like remove_const in C++.

2019-08-22 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 22 August 2019 at 18:50:59 UTC, lili wrote: Hi: I write a template function is below, that need to remove const topmost if has. How to do this? https://dlang.org/phobos/std_traits.html#Unqual

Re: advise about safety of using move in an opAssign with __traits(isRef

2019-08-17 Thread Paul Backus via Digitalmars-d-learn
On Friday, 16 August 2019 at 08:07:28 UTC, aliak wrote: Hi, I'm trying to fix a use-case where you have a wrapper template type (it's an optional) and the wrapping type has @disable this(this). And having this scenario work: struct S { @disable this(this); } Optional!S fun() {...}

Re: advise about safety of using move in an opAssign with __traits(isRef

2019-08-16 Thread Paul Backus via Digitalmars-d-learn
On Friday, 16 August 2019 at 08:07:28 UTC, aliak wrote: Hi, I'm trying to fix a use-case where you have a wrapper template type (it's an optional) and the wrapping type has @disable this(this). And having this scenario work: [...] 2) use isRef inside opAssign like this: void opAssign(auto

Re: Local static class fields

2019-08-12 Thread Paul Backus via Digitalmars-d-learn
On Monday, 12 August 2019 at 22:48:43 UTC, Bert wrote: Making a field static is effectively a global variable to the class. I have a recursive class structure(think of a graph or tree) and I need to keep a global state for it, but this state actually needs to be different for each tree

Re: Private variables accessible from outside class

2019-08-08 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 8 August 2019 at 15:51:45 UTC, Drobet wrote: I'm having a weird issue, where after defining my classes variables as private, they can still be modified and looked at from the outside. That leads to this code compiling with no issues. [...] My question is if this is intended

Re: Question about ubyte x overflow, any safe way?

2019-08-04 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 4 August 2019 at 18:22:30 UTC, matheus wrote: On Sunday, 4 August 2019 at 18:15:30 UTC, Max Haughton wrote: What do you want to do? If you just want to count to 255 then use a foreach This was just an example, what I'd like in this code is either: Get an error (exception) when

Re: Help me decide D or C

2019-07-31 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 31 July 2019 at 18:38:02 UTC, Alexandre wrote: Hi everyone, I would like an honest opinion. I have a beginner level (able to do very small programs) in a few languages such as python, go, C, guile(scheme) and common lisp. I want to pick a language and go deep with it and focus

Re: How to check that import module will succeed?

2019-07-26 Thread Paul Backus via Digitalmars-d-learn
On Friday, 26 July 2019 at 03:42:58 UTC, Andrey Zherikov wrote: Is there a way to check whether some module, say "foo", is available for import before doing "import foo"? I want to create a function that imports module if it's available or does something else otherwise. So I think the code

Re: Any way to move in @disabled this(this) type in to a wrapping template?

2019-07-25 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 25 July 2019 at 21:58:06 UTC, aliak wrote: Haha. Ironic. Thanks, again :) Though, if you use auto ref, and you check if it's mutable and not copyable and then move, then that means you could potentially be applying move to an object on behalf of the clients auto a =

Re: Any way to move in @disabled this(this) type in to a wrapping template?

2019-07-25 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 25 July 2019 at 20:38:59 UTC, aliak wrote: On Thursday, 25 July 2019 at 19:35:36 UTC, aliak wrote: Basically, can template W be made to handle an S that can't be copied? import std; static struct S { int i; @disable this(this); this(int i) { this.i = i; } } [...]

Re: dip1000, perhaps annotate with return, and vibe-d

2019-07-24 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 24 July 2019 at 12:54:51 UTC, aliak wrote: Trying to get dip1000 flag in use. I have this error: Error: returning Optional(null, false).this(value) escapes a reference to parameter value, perhaps annotate with return in this function: public auto some(T)(auto ref T value) {

Re: Why is Throwable.TraceInfo.toString not @safe?

2019-07-21 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 21 July 2019 at 18:03:33 UTC, Johannes Loher wrote: I'd like to log stacktraces of caught exceptions in an @safe manner. However, Throwable.TraceInfo.toString is not @safe (or @trusted), so this is not possible. Why is it not @safe? Can it be @trusted? Thanks for your help!

Re: How would I know if AppVayer is parsing my yaml file?

2019-07-17 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 17 July 2019 at 17:03:16 UTC, Mike Brockus wrote: If you never seen Meson before then pick up a camera and take a picture: 樂  https://mesonbuild.com/ Quick question. I started integrating ci/cd best practices to my Meson projects and I was wondering how would I know if AppVayer

Re: Manipulating alias sequences

2019-07-15 Thread Paul Backus via Digitalmars-d-learn
On Monday, 15 July 2019 at 13:40:29 UTC, Ben Ogles wrote: Now I want to extend it so that a caller can specify the values of only some of the parameters. I tried using a static foreach instead of the staticMap function. But I can't modify AliasSeq values. alias args = AliasSeq!(0, 0); static

Re: Is there a way to slice non-array type in @safe?

2019-07-11 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 11 July 2019 at 19:44:51 UTC, Stefanos Baziotis wrote: On Thursday, 11 July 2019 at 19:37:38 UTC, Nathan S. wrote: If you know that what you're doing cannot result in memory corruption but the compiler cannot automatically infer @safe, it is appropriate to use @trusted. (For

Re: Is there a way to slice non-array type in @safe?

2019-07-11 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 11 July 2019 at 16:31:58 UTC, Stefanos Baziotis wrote: I searched the forum but did not find something. I want to do this: int foo(T)(ref T s1, ref T s2) { const byte[] s1b = (cast(const(byte)*))[0 .. T.sizeof]; const byte[] s2b = (cast(const(byte)*))[0 .. T.sizeof]; }

Re: How to build a package as application ?

2019-07-02 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 2 July 2019 at 08:50:36 UTC, guiguidu60 wrote: I have a hello world program build with DUB on Linux, but the program as marked as "application/x-sharedlib" and not "application/x-executable" (in properties of the file): so, I can't able to execute the program from the file explorer

Re: Changing Environment Paths using D language's Phobos Library

2019-06-29 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 29 June 2019 at 09:50:12 UTC, BoQsc wrote: The correct syntax is: environment.opIndexAssign("Some Random Value Here", "variableName"); And not this one: environment.opIndexAssign("variableName", "Some Random Value Here"); Note that opIndexAssign is an operator overload [1], so

Re: Setting default values for Main function's args Array

2019-06-27 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 27 June 2019 at 17:05:05 UTC, Vaidas wrote: Is it possible to set the default values for the Main function's arguments? It seems that I'm getting Range error. import std.stdio : writeln; void main(string[] args = ["asdsfasdf", "asdklfajsdk", "asdfasdfasd"]){ writeln("",

Re: Setting default values for Main function's args Array

2019-06-27 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 27 June 2019 at 17:20:37 UTC, Paul Backus wrote: void main(string[] args) { string[] defaultArgs = ["my", "default", "arguments"]; if (args.length == 0) { args = defaultArgs; } // Process args... } Correction: you should check for `args.length == 1`, since

Re: Options for unit testing in D?

2019-06-22 Thread Paul Backus via Digitalmars-d-learn
On Friday, 21 June 2019 at 22:35:55 UTC, Mike Brockus wrote: On Friday, 21 June 2019 at 17:52:43 UTC, Mike Wey wrote: On 21-06-2019 06:08, Mike Brockus wrote: [...] If you are using the D unittests in your source you can recompile the same source with `d_unittest: true`, the

Why can't 'scope' be inferred here?

2019-06-21 Thread Paul Backus via Digitalmars-d-learn
The following code fails to compile with -dip1000: struct Inner(T) { T value; this(ref T value) { this.value = value; } } struct Outer(T) { Inner!T inner; void opAssign(ref T rhs) { inner = Inner!T(rhs); } } @safe void main() { int x; int* p = Outer!(int*) o; o

Re: Elegant way to test if members of array A are present in array B?

2019-06-12 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 12 June 2019 at 06:57:55 UTC, Robert M. Münch wrote: If you use an associative array or a set, it's O(n) time and O(n) space. I don't see how this is the case. The AA itself has some overhead too. So, the checking loop is O(n) but the AA lookups not. Hash table insertion and

Re: Elegant way to test if members of array A are present in array B?

2019-06-11 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 11 June 2019 at 17:12:17 UTC, Robert M. Münch wrote: Is there a simple and elegant way to do this? Or is just using a foreach(...) with canFind() the best way? It's a space/time tradeoff. foreach with canFind is O(n^2) time and O(1) space. If you use an associative array or a set,

Re: What does ! Stand for in map! and filter! function calls?

2019-06-02 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 2 June 2019 at 16:39:57 UTC, Rnd wrote: Is it not possible in the language to have template map function also to called as map(x=>... ? This will reduce complexity which will attract more people to this language. Easy languages have great mass appeal as has been shown with

Re: What does ! Stand for in map! and filter! function calls?

2019-06-02 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 2 June 2019 at 15:48:54 UTC, Rnd wrote: I have recently started using Dlang, hence this basic question. Thanks for your insight. map and filter are templates in D, and !(...) is D's syntax for passing arguments to templates: map!(x => x*x)([1, 2, 3, 4, 5]) ^ ^ |

Re: hasElaborateCopyConstructor bug?

2019-06-02 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 2 June 2019 at 14:44:47 UTC, H. S. Teoh wrote: On Sun, Jun 02, 2019 at 02:32:16PM +, Paul Backus via Digitalmars-d-learn wrote: [...] If std.v2 ever materializes, we'll have an opportunity to fix papercuts like this. Until then, my preferred workaround is to use a renaming

Re: Very simple null reference escape

2019-06-02 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 2 June 2019 at 07:55:27 UTC, Amex wrote: A.B If A is null, crash. A?.B : writeln("HAHA"); No crash, ignored, equivalent to if (A is null) writeln("HAHA"); else A.B; The "optional" package on dub [1] has a .dispatch method that does this: auto d = some(A()); //

Re: hasElaborateCopyConstructor bug?

2019-06-02 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 2 June 2019 at 06:59:02 UTC, Jonathan M Davis wrote: Almost certainly, hasElaborateCopyConstructor should be updated to test for both postblit constructors and copy constructors, since its purpose is to test for whether the type has a user-defined copying function [...] Whether

Re: hasElaborateCopyConstructor bug?

2019-06-01 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 1 June 2019 at 23:29:08 UTC, SrMordred wrote: On Saturday, 1 June 2019 at 21:39:33 UTC, SImen Kjærås wrote: On Saturday, 1 June 2019 at 21:05:32 UTC, SrMordred wrote: hasElaborateCopyConstructor checks if the type defines a postblit[0]. Yes, I know this. But since dmd 2.086

Re: Calling copyctor manually

2019-06-01 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 1 June 2019 at 02:27:36 UTC, SrMordred wrote: Its possible to call copyctor manually without calling dtor? ex, what i did before: struct T{ ~this(){ writeln("DTOR"); } this(this){ writeln("POSTBLIT"); } } T a; T b; memcpy(,,T.sizeof); a.__postblit; /* output: POSTBLIT DTOR

Re: Linked List iterating over and inserting an element around (before/after) the current position.

2019-05-19 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 19 May 2019 at 22:20:48 UTC, Josh wrote: Thank you, that helps big time. This is just more curiosity, but do you happen to know why I have to use DList.linearRemove() instead of DList.remove()? import std.stdio; import std.container.dlist; import std.algorithm; import std.range;

Re: Linked List iterating over and inserting an element around (before/after) the current position.

2019-05-19 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 19 May 2019 at 20:55:28 UTC, Josh wrote: Just started looking at D this weekend, coming from a C++/Java/Go/Rust background and it's really not going well. Trying to write something to play with the language and need a linked list, looking in std.container you have a single or

Re: Pass field as a template parameter

2019-05-08 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 8 May 2019 at 18:01:53 UTC, Ben Jones wrote: I'm trying to write a template function like the below... is it possible without using string mixins? void fun( alias(?) field, alias p1, alias p2)() { if(p1.field) use(p2.field); } called with something like static

Re: srand time error uint/long

2019-04-29 Thread Paul Backus via Digitalmars-d-learn
On Monday, 29 April 2019 at 14:36:49 UTC, number wrote: And how to use the d libs instead? is this the way to go? ``` Random rnd = Random(Clock.currTime().second); uniform01(rnd); //(or whatever) ``` https://dlang.org/phobos/std_random.html#unpredictableSeed

Re: Memory management by interfacing C/C++

2019-04-28 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 28 April 2019 at 23:10:24 UTC, Ferhat Kurtulmuş wrote: You are right. I am rewriting the things using mallocs, and will use core.stdc.stdlib.free on d side. I am not sure if I can use core.stdc.stdlib.free to destroy arrays allocated with new op. core.stdc.stdlib.free is (as the

Re: Memory management by interfacing C/C++

2019-04-27 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 27 April 2019 at 22:25:58 UTC, Ferhat Kurtulmuş wrote: Hi, I am wrapping some C++ code for my personal project (opencvd), and I am creating so many array pointers at cpp side and containing them in structs. I want to learn if I am leaking memory like crazy, although I am not

Re: const of AliasSeq is silently ignored

2019-04-09 Thread Paul Backus via Digitalmars-d-learn
On Monday, 8 April 2019 at 19:56:50 UTC, Yuxuan Shui wrote: In this example: const(AliasSeq!(int, int)) a; pragma(msg, typeof(a)); // (int, int) This kind of make sense, since AliasSeq is not a "single" type. But silently dropping const seems bad, the compiler should probably report

Re: Overloads not returning appropriate info. [Field reflunkory]

2019-04-07 Thread Paul Backus via Digitalmars-d-learn
On 4/6/19 11:47 PM, Alex wrote: > What you need to tell me is why using .stringof is bad. You have simply > conjured up a rule and are stating it but not giving any reason why it > is not a good idea to follow when, in fact, not following can be shown > to be beneficial. I'm not Adam, but I've

Re: Error: template instance `Reflect!(type)` cannot use local `type` as parameter to non-global template `Reflect(Ts...)()`

2019-04-07 Thread Paul Backus via Digitalmars-d-learn
On 4/7/19 1:30 AM, Nicholas Wilson wrote: > On Sunday, 7 April 2019 at 05:24:38 UTC, Alex wrote: >> Error: template instance `Reflect!(type)` cannot use local `type` as >> parameter to non-global template `Reflect(Ts...)()` >> >> mixin(`import `~moduleName!(T)~`;`);    >> mixin(`alias X =

Re: What's the correct way to interface with a intptr_t?

2019-04-07 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 7 April 2019 at 12:19:10 UTC, bauss wrote: On Saturday, 6 April 2019 at 20:16:06 UTC, Paul Backus wrote: On Saturday, 6 April 2019 at 19:31:15 UTC, Robert M. Münch wrote: I have a C interface that uses a parameter of type intptr_t. Wondering if size_t is the correct D equivalent?

Re: What's the correct way to interface with a intptr_t?

2019-04-06 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 6 April 2019 at 19:31:15 UTC, Robert M. Münch wrote: I have a C interface that uses a parameter of type intptr_t. Wondering if size_t is the correct D equivalent? The correct equivalent is `intptr_t` from `core.stdc.stdint`.

Re: Using opOpAssign, cannot assign sequence

2019-04-05 Thread Paul Backus via Digitalmars-d-learn
On Friday, 5 April 2019 at 13:59:27 UTC, Alex wrote: class X(T) void opOpAssign(string op)(T d) If T has more than length of one then x += We can work around this but it seems to me that we should be able to get it to work in some way x += Alias!(a,b,c) fails to package it up

Re: Simultaneously assigning to all values in a tuple

2019-03-30 Thread Paul Backus via Digitalmars-d-learn
On 3/27/19 8:29 PM, Jamie wrote: > Is it possible to assign to all values in a tuple at once if they are > the same type? > I.e. > > Tuple!(double, "x", double, "y") t; > t[] = 1.0; > foreach (ref member; t.expand) { member = 1.0; }

Re: Why is creating of if Expressions not allowed?

2019-03-24 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 24 March 2019 at 16:18:49 UTC, sighoya wrote: Why auto GenIf()() { return mixin("if(true) { return true;} else {return false;}"); } public bool testFunction2() { GenIf!(); } gives me: onlineapp.d-mixin-3(3): Error: expression expected, not if onlineapp.d(8): Error:

Re: Can't make inout work.

2019-03-17 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 17 March 2019 at 10:49:03 UTC, aliak wrote: Ah! Thanks! So next problem with that: import std.stdio; struct S(T) { T value; } auto make(T)(inout auto ref T val) { return inout(S!T)(val); } void main() { writeln(make("hello") == S!string("hello")); } Error: Error:

Re: Can't make inout work.

2019-03-16 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 16 March 2019 at 11:55:56 UTC, spir wrote: I think (but may be wrong) that you don't need inout here, since a plain 'ref' will (and does) work. This is accepted by me (I added vars to make the code clearer to myself): struct S(T) { T value = T.init; } auto ref make(T)(ref T

Re: Block statements and memory management

2019-03-15 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 16 March 2019 at 03:47:43 UTC, Murilo wrote: Does anyone know if when I create a variable inside a scope as in {int a = 10;} it disappears complete from the memory when the scope finishes? Or does it remain in some part of the memory? I am thinking of using scopes to make

Re: Can't make inout work.

2019-03-15 Thread Paul Backus via Digitalmars-d-learn
On Friday, 15 March 2019 at 23:57:15 UTC, aliak wrote: Anyone knows how to make this work? You need an explicit `inout` on the return value of `make`: auto ref make(T)(inout auto ref T value) { return inout(S!T)(value); }

Re: Why this template code does not compile?

2019-03-13 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 13 March 2019 at 17:38:02 UTC, Victor Porton wrote: Why this template code does not compile? /// module runnable; import std.meta; import std.typecons; template FieldInfo(argT, string argName) { template FieldInfo(Nullable!argT argDefault = Nullable!argT()) { } }

Re: Array of byLineCopy ranges behaves as they are byLine

2019-03-11 Thread Paul Backus via Digitalmars-d-learn
On Monday, 11 March 2019 at 17:33:31 UTC, HaraldZealot wrote: Ah yes, I forget about laziness of `map`. BTW, I have found other solution, which is more fit to my initial intention. ```d ReturnType!(std.stdio.File.byLineCopy!(char, immutable(char)))[] ranges; foreach(filename; args[1 .. $]) {

Re: this is null

2019-03-09 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 9 March 2019 at 19:18:38 UTC, ANtlord wrote: Hello everyone! I've encountered the problem which I already encountered before. Unfortunately, I had no time in the previous time to report and to talk about it. So I decided to play making my own "malloc" function in pure D (betterC)

Re: Compile Time Fun Time

2019-02-25 Thread Paul Backus via Digitalmars-d-learn
On Monday, 25 February 2019 at 06:51:20 UTC, Yevano wrote: I am writing a domain specific language of sorts in D for the lambda calculus. One of my requirements is that I should be able to generate expressions like this: new Abstraction(v1, M) like this: L!(x => M) A word of caution: this

Re: How to call other variadic function with the same arguments?

2019-02-24 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 24 February 2019 at 13:09:15 UTC, Victor Porton wrote: Let f be a variadic function: Result f(...); How to implement variadic function g which calls f with the same arguments as one it receives? Result g(...) { // ... } If you must use C-style variadic arguments, then your

Re: isSame/TemplateOf bug?

2019-02-19 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 19 February 2019 at 22:43:25 UTC, SrMordred wrote: import std.traits; import std.stdio; struct Test(T) { this(T)( auto ref T value ) { writeln( TemplateOf!(typeof(value)).stringof); writeln( __traits(isSame, TemplateOf!(typeof(value)), Test) );

Re: Generators in D

2019-02-19 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 19 February 2019 at 18:37:22 UTC, Matthew_Blake wrote: Hi there, is the concept of generators (like in Python, using yield) available in D? If so, where can I find information? Regards Generally speaking, things that are generators in Python are implemented as ranges [1] in D.

Re: How to use dproto for removing and element from a repeated field

2019-02-06 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 6 February 2019 at 10:07:06 UTC, Sudhi wrote: Hi, I have been using dproto to work with protocol buffers. I have below proto structure message Person { required string name = 1; required string id = 2; repeated string emailId = 3; } Where a person can have multiple

Re: static arrays at runtime with templates ?

2019-02-05 Thread Paul Backus via Digitalmars-d-learn
On Monday, 4 February 2019 at 22:48:10 UTC, XavierAP wrote: However one of the possibilities of D is the ability to generate and execute quite some code at compile time by means of meta-programming; and I guess here's where staticArray() may end up being useful enough to have merited

Re: static arrays at runtime with templates ?

2019-02-03 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 3 February 2019 at 16:33:48 UTC, Emil wrote: I tried this on a whim and it appears to work: auto static_array(T, size_t data_size)() { T[data_size] data; return data; } This is actually already in the standard library: https://dlang.org/phobos/std_array.html#staticArray

Re: Converting a type to a char

2019-01-25 Thread Paul Backus via Digitalmars-d-learn
On Friday, 25 January 2019 at 15:53:10 UTC, Thomas Gregory wrote: I would like to check that a char (unknown at compile time) matches a particular type but I would like to do so without if statements or a hash map as I would like it to be as fast as possible. Ideally I would choose something

Re: Alternative to Interfaces

2019-01-25 Thread Paul Backus via Digitalmars-d-learn
On Friday, 25 January 2019 at 19:34:02 UTC, Sebastien Alaiwan wrote: On Saturday, 19 January 2019 at 09:24:21 UTC, Kagamin wrote: On Friday, 18 January 2019 at 18:48:46 UTC, Jonathan M Davis wrote: Yes, but some D features will use the GC They would like to allocate, but they don't know nor

Re: Am I missing with ref in this code?

2019-01-24 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 24 January 2019 at 16:04:06 UTC, Suliman wrote: Error: static assert: "Cannot convert arguments '(MyUrl)' to function arguments '(MyUrl*)'." You've forgotten to change the call site to pass a pointer. However, it turns out that even if you do that, vibe will not allow you to

Re: Am I missing with ref in this code?

2019-01-24 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 24 January 2019 at 15:28:19 UTC, Suliman wrote: I am doing very small link-checker. Here is' code https://run.dlang.io/is/p8whrA I am expecting that on line: writefln("url: %s, status: %s", url.url, url.status); I will print link and it's status. But I am getting only: url:

Re: Modulo that 'wraps' the number?

2019-01-20 Thread Paul Backus via Digitalmars-d-learn
On Monday, 21 January 2019 at 04:52:53 UTC, NaN wrote: On Sunday, 20 January 2019 at 18:51:54 UTC, Steven Schveighoffer wrote: On 1/20/19 1:28 PM, faissaloo wrote: In Python -1%3 == 2 however in D -1%3 == -1 Is there a standard library function or something that gives me the Python version of

Re: API Documentation

2019-01-20 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 20 January 2019 at 16:51:23 UTC, kdevel wrote: Online but not on a local installation I frequently land on pages under https://dlang.org/library/ (API Documentation). What is the reason for this duplicate docs? The API documentation looks much cleaner and better structured.

Re: Getting template parameters by its name

2019-01-10 Thread Paul Backus via Digitalmars-d-learn
On Friday, 11 January 2019 at 04:59:50 UTC, Yui Hosaka wrote: I want to do something like this: template S(T) { } void main() { pragma(msg, S!(int).T); // Error: no property `T` for type `void` } You can get the arguments of a template instance as an AliasSeq using

Re: changing the output of `std.stdio.write'

2019-01-03 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 3 January 2019 at 17:59:28 UTC, Manfred Nowak wrote: According to this tutorial https://wiki.dlang.org/Defining_custom_print_format_specifiers it seems easy to change the format of the output for `std.stdio.writef'. But why is there no example for changing the output when there

Re: static foreach direct use of variables

2019-01-01 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 1 January 2019 at 21:14:09 UTC, Michelle Long wrote: auto foo(S s) { static foreach(k, p; [s, this]) for(int i = 0; i < p.length; i++) ... } The idea is to provide single for loop structure for each of the variables(in this case s and

Re: typeof function literals which define the types of its parameters but do not give their parameters names

2018-12-26 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 27 December 2018 at 03:52:52 UTC, Johannes Loher wrote: Hey all, I am a bit confused about the inferred types of function literals which do not name their parameters (something like `(int) {}`). The confusion arises from the fact that the inferred type sometimes is `void` (might

Re: Qualified class opEquals()

2018-12-24 Thread Paul Backus via Digitalmars-d-learn
On Monday, 24 December 2018 at 22:58:03 UTC, Per Nordlöw wrote: Is it in the following code possible to make the statement assert(car1 == car2); in the function testEqual() compile in a `@safe pure nothrow @nogc context`? No, because equality comparison between classes lowers to

Re: Can you move a disabled this(this) struct in to a container type if it's an rvalue?

2018-12-13 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 13 December 2018 at 13:17:05 UTC, aliak wrote: Ah. Is there any case where you would not want to do that when you have a T value as parameter? And, what if it's "this()(auto ref T value)"? Then moving could be dangerous if the parameter was passed as a ref. Or maybe it just

Re: Can you move a disabled this struct in to a container type if it's an rvalue?

2018-12-12 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 12 December 2018 at 20:05:18 UTC, aliak wrote: Ie: struct S { @disable this(); this(int i) {} } struct Container(T) { T value; this(auto ref T value) { this.value = value; } } void main() { auto a = Container!S(S(3)); // can't do this. } The

Re: is(ElementType!(char[2]) == dchar - why?

2018-12-11 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 11 December 2018 at 17:51:56 UTC, Denis Feklushkin wrote: import std.stdio; import std.range.primitives; void main() { writeln( typeid(ElementType!(char[2])) ); static assert(is(ElementType!(char[2]) == dchar)); // why? } ? https://run.dlang.io/is/Q74yHm

Re: cannot use local f as parameter to non-global template

2018-12-10 Thread Paul Backus via Digitalmars-d-learn
On Monday, 10 December 2018 at 16:15:36 UTC, aliak wrote: Ah, that's a good way of breaking it down. But ok, so then the other version would be lowered to: template match(handlers...) { template match(T) { auto match(T holder) { return handlers[0](holder); }

Re: dmd -unittest works poorly with executables

2018-12-08 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 8 December 2018 at 20:16:09 UTC, Andrew Pennebaker wrote: I think it's lame to have to use magical code like `version(unittest) {} else` to guard our main functions, when we run unit tests. Could D go ahead and do the right thing, automatically shadowing our main functions when

Re: cannot use local f as parameter to non-global template

2018-12-08 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 8 December 2018 at 09:57:29 UTC, aliak wrote: This compiles fine. However, if I change the match template to: template match(handlers...) { auto match(alias f)(Holder!f holder) { return handlers[0](holder); } } Notice the template parameter of the eponymous match

Re: public imports

2018-12-05 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 5 December 2018 at 21:13:29 UTC, Sjoerd Nijboer wrote: A small question. Is it intended behaviour that public imports inside function calls fail with the message "Error: found public instead of statement", or is it an underdocumented feature? void foo() { public import bar;

Re: compile time sequence through dub config or commandline.

2018-12-02 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 2 December 2018 at 14:50:05 UTC, Sjoerd Nijboer wrote: The idea being that bar can be packed in a library and the program that includes this library can decide what parameters will be added to foo whitout any runtime overhead and ugly looking syntax. The normal way to do this

Re: interrupting a function

2018-11-17 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 17 November 2018 at 06:19:25 UTC, Alex wrote: It can happen, that the input params are correct, however, the calculation lasts too long. Is there a way, to measure the execution time of foo (á la benchmark) and then, if some execution time limit is exceeded to interrupt the

Re: Creating InputRanges from strings, files etc.

2018-11-08 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 8 November 2018 at 13:58:55 UTC, Vinay Sajip wrote: Excuse my ignorance, but from looking at the documentation on std.range and a quick skim of the guides mentioned there near the top, I can't see what the simple way is of creating an InputRange!(ubyte) from strings, files etc. I

Re: Need help with calling a list of functions

2018-11-03 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 4 November 2018 at 01:17:01 UTC, Luigi wrote: I need to call a function that can create a function from an array of functions and calls them in reverse order. I am learning D any help would be import std.stdio; import std.algorithm; import std.array : array; import std.range;

Re: anyway to set a const object after the fact?

2018-10-29 Thread Paul Backus via Digitalmars-d-learn
On Monday, 29 October 2018 at 21:50:32 UTC, aliak wrote: Hi, so if you have this piece of code: struct C { void f() { string[] others; const string[] restArgs; foreach (i, arg; args) { if (isValidArg(arg)) { restArgs = args[i + 1 .. $]; break; }

Re: Built-in array opSliceAssign

2018-10-25 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 25 October 2018 at 12:25:37 UTC, Eduard Staniloiu wrote: As I wrote in the comments above, I was expecting `a[] = b[]` to iterate the slices and assign the elements of b into a. What really happens is a memcpy: as you can see from godblot [0], this gets lowered to a call to

Re: lazy variables

2018-10-18 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 18 October 2018 at 14:08:11 UTC, aliak wrote: On Wednesday, 17 October 2018 at 23:34:55 UTC, Paul Backus wrote: auto x = () { // do some heavy stuff }; if (condition) { func(x().y); // heavy stuff evaluated here } That would do heavy stuff everytime i wanted to get y

Re: lazy variables

2018-10-17 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 17 October 2018 at 07:32:37 UTC, aliak wrote: lazy S x = () { // do some heavy stuff }(); if (condition) { func(x.y); // heavy stuff evaluated here } auto x = () { // do some heavy stuff }; if (condition) { func(x().y); // heavy stuff evaluated here } If you want

Re: betterC generate dynamic array throw Error: TypeInfo cannot be used with -betterC

2018-10-17 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 17 October 2018 at 07:01:21 UTC, test wrote: test1: module test1; import test2; enum X = getR(1,3); void main(string[] args){} test2: module test2; struct R { int i; } R[] getR(int a, int b){ R[] r; r ~=

Re: Using . notation abstractly

2018-10-12 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 10 October 2018 at 22:56:14 UTC, James Japherson wrote: The point of all this is because D does not allow nesting of enums which allows for nice use of . to separate hiearchies: enum A { enum B { X, } } A.B.X, rather than having to have one large flat enum and do

Re: Copy parameters from function passed as an alias

2018-10-10 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 10 October 2018 at 12:10:06 UTC, bauss wrote: On Wednesday, 10 October 2018 at 08:16:11 UTC, Kagamin wrote: How do you want to use parameter names of an arbitrary function? What I want to do is pass a function to a template and that template creates a function with the same

Re: Error: variable 'xyz' has scoped destruction, cannot build closure

2018-10-05 Thread Paul Backus via Digitalmars-d-learn
On Friday, 5 October 2018 at 06:56:49 UTC, Nicholas Wilson wrote: On Friday, 5 October 2018 at 06:44:08 UTC, Nicholas Wilson wrote: Alas is does not because each does not accept additional argument other than the range. Shouldn't be hard to fix though.

Re: Compile time sequences

2018-10-03 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 3 October 2018 at 07:57:07 UTC, drug wrote: According to https://dlang.org/articles/ctarguments.html compile time sequences "...allow a programmer to operate on types, symbols and values..." Phobos has `isType`/`isTypeTuple` traits, also `isExpressions` where expression may

Re: std.process.execute without capturing stderr?

2018-09-20 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 20 September 2018 at 07:24:52 UTC, berni wrote: I need to execute a program and capture stdout, which I hoped std.process.execute would do. But unfortunatly this command also captures stderr, which I need to be ignored. When looking at the implementation of std.process.execute I

Re: Cannot use UFCS in lambda?

2018-09-16 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 16 September 2018 at 10:55:43 UTC, berni wrote: The problem is more general: you can only use top-level symbols in UFCS. You can use an identity alias template to bypass this: https://blog.thecybershadow.net/2015/04/28/the-amazing-template-that-does-nothing/ (search for UFCS in the

Re: array to string functional?

2018-09-15 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 15 September 2018 at 20:04:36 UTC, berni wrote: Anotherone I'm not getting to work: From some output with newlines I want to discard all lines, that start with a # and select part of the other lines with a regex. (I know the regex r".*" is quite useless, but it will be replaced by

Re: array to string functional?

2018-09-15 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 15 September 2018 at 05:39:48 UTC, berni wrote: Oh, thanks. What I didn't know about was join. Now I wonder how I could have found out about it, without asking here? Even yet I know it's name I cannot find it, nighter in the language documentation nor in the library documentation.

Re: array to string functional?

2018-09-14 Thread Paul Backus via Digitalmars-d-learn
On Friday, 14 September 2018 at 20:43:45 UTC, SrMordred wrote: What you want is std.range.chunks auto a = [1,0,1,1,1,0,1,0,1,1,1,1,0]; a.map!(to!string) .join("") .chunks(4) .map!(to!string) //don´t know why the chunks are not already strings at this point ;/

Re: Variadic template with template arguments in pairs

2018-09-12 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 12 September 2018 at 15:12:16 UTC, Anonymouse wrote: void doByPair(Args...)(Args args) if (Args.length) { foreach (pair; args.pairwise) { static assert(is(typeof(pair[0]) == string)); static assert(isPointer!(pair[1])); assert(pair[1] !is null);

<    3   4   5   6   7   8   9   >