Re: How to create delegates with an independent scope?

2022-03-30 Thread vit via Digitalmars-d-learn
On Wednesday, 30 March 2022 at 12:46:07 UTC, Vijay Nayar wrote: Consider the following code example: ```d import std.stdio; void main() { alias DelegateT = string delegate(); // An array of delegates, each has their own scope. DelegateT[] funcs; foreach (i; ["ham", "cheese"]) { //

Re: How to create delegates with an independent scope?

2022-03-30 Thread vit via Digitalmars-d-learn
On Wednesday, 30 March 2022 at 12:56:39 UTC, Vijay Nayar wrote: On Wednesday, 30 March 2022 at 12:53:10 UTC, vit wrote: use two delegates :) ```d (){ // Deliberately create a copy to keep in delegate scope. string myStr = i.dup; // The

Re: Can std.variant be used with std.container.rbtree?

2022-04-02 Thread vit via Digitalmars-d-learn
On Friday, 1 April 2022 at 22:22:21 UTC, Vijay Nayar wrote: Consider the following program: ```d void main() { import std.stdio; import std.container.rbtree; import std.variant; [...] Variant can contain any type => variant cannot assume attributes like pure nothrow @safe @nogc o

Re: Can std.variant be used with std.container.rbtree?

2022-04-02 Thread vit via Digitalmars-d-learn
On Saturday, 2 April 2022 at 14:49:15 UTC, Vijay Nayar wrote: On Saturday, 2 April 2022 at 14:35:10 UTC, Vijay Nayar wrote: The `tryMatch` method fails to compile, and instead I get the following error: ```d /dlang/dmd/linux/bin64/../../src/phobos/std/sumtype.d(2004): Error: static assert: "`

scope variable `b` assigned to `a` with longer lifetime (-dip1000)

2022-04-09 Thread vit via Digitalmars-d-learn
Hello, Why doesn't this code compile? ```d static struct Foo{ void *ptr; void proxySwap1(scope ref typeof(this) rhs)scope pure nothrow @trusted @nogc{ auto tmp = this.ptr; this.ptr = rhs.ptr; rhs.ptr = tmp; } void proxySwap2()(scope ref typeof(this) rh

Re: Create an associative array with function pointers as the value

2022-04-20 Thread vit via Digitalmars-d-learn
On Wednesday, 20 April 2022 at 10:42:59 UTC, rempas wrote: I'm trying to create an associative array where the keys will be a "string" type and the values will be function pointers. I'm using a custom type is called "file_struct" and for anyone that wants to try specifically with this type, the

Re: Reference counting example

2022-04-26 Thread vit via Digitalmars-d-learn
On Tuesday, 26 April 2022 at 23:33:28 UTC, cc wrote: On Tuesday, 26 April 2022 at 22:16:01 UTC, cc wrote: Test application: I should point out that all this stuff with saving refcounted things to arrays and so on is extremely untested and experimental🙄 One problem I'm seeing is the inabili

generic function instance without call

2022-04-27 Thread vit via Digitalmars-d-learn
Hi, is it possible to get address of generic function instance for specified arguments without calling the function? Example: ```d auto foo(alias fn, Args...)(auto ref Args args){ ///return function/delegate type of `fn` for arguments `args` } void main(){ long x; auto fn = foo

Re: generic function instance without call

2022-04-27 Thread vit via Digitalmars-d-learn
Fix: ```d auto fn = foo!((a, ref b) => true)(new int(42), x); ```

Re: generic function instance without call

2022-04-27 Thread vit via Digitalmars-d-learn
On Wednesday, 27 April 2022 at 16:03:32 UTC, user1234 wrote: On Wednesday, 27 April 2022 at 15:23:26 UTC, vit wrote: Hi, is it possible to get address of generic function instance for specified arguments without calling the function? Example: ```d auto foo(alias fn, Args...)(auto ref Args a

Re: Help with DynamicArray of Emsi Containers

2022-05-01 Thread vit via Digitalmars-d-learn
On Saturday, 30 April 2022 at 20:22:43 UTC, Christian Köstlin wrote: I am struggling with initializing an Emsi Containers DynamicArray in a nice way. Some background information of my usecase: I experimenting in porting some old 3d engine code of mine from c++ to dlang. In the engine I want t

Re: Why isn't my dynamic array method doesn't work for this type?

2022-05-05 Thread vit via Digitalmars-d-learn
On Thursday, 5 May 2022 at 10:40:44 UTC, rempas wrote: I have created a structure that is a actually an array that allocates memory and growths. It is a template and it has worked with a couple of types that I have tried with. It doesn't work with one tho and I cannot understand why. I will li

Re: Why isn't my dynamic array method doesn't work for this type?

2022-05-05 Thread vit via Digitalmars-d-learn
On Thursday, 5 May 2022 at 11:49:29 UTC, vit wrote: On Thursday, 5 May 2022 at 10:40:44 UTC, rempas wrote: I have created a structure that is a actually an array that allocates memory and growths. It is a template and it has worked with a couple of types that I have tried with. It doesn't work

Re: How to unpack template parameters,Or how to forward parameters?

2022-05-05 Thread vit via Digitalmars-d-learn
On Friday, 6 May 2022 at 00:41:18 UTC, zjh wrote: Hello everyone,I have following function: ```d import core.stdc.stdio; void f(int i,int j){ printf("%i,%i",i,j); } ``` I want to forward `int[N]` to `(int i,int j)` etc. How can I write a forwarding function? ```d void ff(alias g,int...I)(in

dip1000 return scope dmd v 2.100

2022-05-06 Thread vit via Digitalmars-d-learn
Hello, new dmd (2.100) has return/scope changes. It look like examples at page https://dlang.org/spec/function.html#ref-return-scope-parameters are no longer relevant. What difference are between `return scope`, `scope return` and `return`? Why `void* ptr` in struct change effect of `scope r

Re: dip1000 return scope dmd v 2.100

2022-05-07 Thread vit via Digitalmars-d-learn
On Friday, 6 May 2022 at 17:17:01 UTC, Dennis wrote: On Friday, 6 May 2022 at 09:24:06 UTC, vit wrote: [...] They were recently updated to match the implementation in 2.100. [...] `return scope` means pointer members (such `this.ptr`, `C.ptr`) may not escape the function, unless they are

Re: Virtual methods on stack objects

2022-05-12 Thread vit via Digitalmars-d-learn
On Wednesday, 11 May 2022 at 20:53:21 UTC, Marvin Hannott wrote: On Wednesday, 11 May 2022 at 20:23:07 UTC, Ali Çehreli wrote: On 5/11/22 13:06, Marvin Hannott wrote: > I appreciate the answer, don't much like the "solutions". Me neither. :) > It's not so much about copying Great! > scoped

Re: Template shenannigans with multiple datatypes

2022-05-13 Thread vit via Digitalmars-d-learn
On Friday, 13 May 2022 at 06:43:39 UTC, Chris Katko wrote: I have an intrinsicGraph(T) class that is given a pointer to a T dataSource and automatically polls that variable every frame to add it to the graph, whether it's a float, double, integer, and maybe bool. [...] I dont understand fir

Re: Template shenannigans with multiple datatypes

2022-05-13 Thread vit via Digitalmars-d-learn
On Friday, 13 May 2022 at 07:32:16 UTC, Chris Katko wrote: On Friday, 13 May 2022 at 07:05:36 UTC, vit wrote: On Friday, 13 May 2022 at 06:43:39 UTC, Chris Katko wrote: I have an intrinsicGraph(T) class that is given a pointer to a T dataSource and automatically polls that variable every frame

Re: Template shenannigans with multiple datatypes

2022-05-13 Thread vit via Digitalmars-d-learn
On Friday, 13 May 2022 at 11:58:15 UTC, zjh wrote: On Friday, 13 May 2022 at 08:28:56 UTC, vit wrote: ... ```d ... this(DataSources dataSources){ this.dataSources = dataSources; } ... return new MultiGraph!(staticMap!(PointerTarget, Ts))(ts);//ts ``` How is `ts` convert to `DataSources`

decimal type in d

2022-05-15 Thread vit via Digitalmars-d-learn
Hello, I want read decimal type from sql db, do some arithmetic operations inside D program and write it back to DB. Result need to be close to result as if this operations was performed in sql DB. Something like C# decimal. Exists this kind of library ind D? (ideally `pure @safe @nogc nothrow`

Re: decimal type in d

2022-05-17 Thread vit via Digitalmars-d-learn
On Monday, 16 May 2022 at 09:59:41 UTC, bauss wrote: On Monday, 16 May 2022 at 09:46:57 UTC, IGotD- wrote: On Sunday, 15 May 2022 at 13:26:30 UTC, vit wrote: [...] This also something I wondered, it should be standard in the D library. Implementing it can be done straight forward with exist

Re: Odd construct idea. Splitting arguments inside a parameter list.

2022-05-23 Thread vit via Digitalmars-d-learn
On Monday, 23 May 2022 at 08:34:21 UTC, Chris Katko wrote: D struct pair { float x,y; } [...] This work too: ```d myFunction(taco, p.tupleof, burrito); ```

Re: mixin template

2022-05-23 Thread vit via Digitalmars-d-learn
On Monday, 23 May 2022 at 15:14:53 UTC, Vindex wrote: I have this code: ``` import std.array, std.exception, std.stdio; mixin template RealizeException() { this(string msg, string file = __FILE__, size_t line = __LINE__) { super(msg, file, line); } } class WrongUsage : Except

Inferred attributes errors in template function.

2022-05-27 Thread vit via Digitalmars-d-learn
Hello, I have this problem: ```d static int i; void bar(T)(){ static if(is(T == int)) (()@system => 1)(); static if(is(T == float)) i = 42; } void foo(T)(){ bar!T(); } void main()@safe pure{ foo!long(); foo!float(); //Error: `pure` function `D main`

Re: "only" vs "[]"

2022-08-08 Thread vit via Digitalmars-d-learn
On Monday, 8 August 2022 at 11:35:48 UTC, pascal111 wrote: The output of next code is the same to extent that we feel that there's no difference between "only" and "[]", so what "only" added here?: '''D [1,2,3].writeln; only(1,2,3).writeln; ''' output: [1, 2, 3] [1, 2, 3] `only(1,2,3)` do

Re: Why can I call a function with mismatched parameter type?

2020-12-11 Thread vit via Digitalmars-d-learn
On Friday, 11 December 2020 at 11:25:22 UTC, Andrey Zherikov wrote: Here is the example: alias f1 = (string ) {}; f1(int .init); alias f2 = (string s) {}; f2(int .init); alias f3 = (int ) {}; f3(string.init); alias f4 = (int i ) {}; f4(string.init); "f1" case compiles

Re: struct constructor with rvalue param of same struct type

2021-01-19 Thread vit via Digitalmars-d-learn
On Tuesday, 19 January 2021 at 06:49:06 UTC, Kyle Ingraham wrote: I’m sorry that this isn’t a solution to your problem but your code caught my attention. What is your snippet supposed to do? This is more specific example: https://run.dlang.io/is/W7rd2u import std.traits : CopyTypeQualifiers;

Re: GC.addRange in pure function

2021-02-10 Thread vit via Digitalmars-d-learn
On Wednesday, 10 February 2021 at 12:17:43 UTC, rm wrote: On 09/02/2021 5:05, frame wrote: On Sunday, 7 February 2021 at 14:13:18 UTC, vitamin wrote: Why using 'new' is allowed in pure functions but calling GC.addRange or GC.removeRange isn't allowed? Does 'new' violate the 'pure' paradigm? P

cannot take address of scope local in safe function

2021-06-13 Thread vit via Digitalmars-d-learn
Why I can take address of Foo variable but not Bar? ```d //-dip1000 struct Foo{ private double d; } struct Bar{ private void* ptr; } void main()@safe{ ///this is OK: { scope Foo x; scope ptr = &x; } ///Error: cannot take address of `scope` local `x` i

Re: cannot take address of scope local in safe function

2021-06-13 Thread vit via Digitalmars-d-learn
On Sunday, 13 June 2021 at 17:18:46 UTC, ag0aep6g wrote: On Sunday, 13 June 2021 at 16:27:18 UTC, vit wrote: Why I can take address of Foo variable but not Bar? ```d //-dip1000 struct Foo{ private double d; } struct Bar{ private void* ptr; } void main()@safe{ ///this is OK:

@trusted methods

2021-06-18 Thread vit via Digitalmars-d-learn
Are asserts enough to make method @trusted or is something like throw exception or return error code necessary? How it work in phobos? Example: ```d struct Slice(T){ private T[] data; this(T[] data){ this.data = data; } inout(T)[] opSlice()inout scope return pu

String front, back return code point/unit

2021-06-23 Thread vit via Digitalmars-d-learn
Hello, I am implementing @nogc string struct similar to c++ std::basic_string (link https://code.dlang.org/packages/basic_string). C++ string has methods front, back and pop_back returning/deleting code units. D strings has functions (std.range) front, back and popBack returning/deleting co

Re: String front, back return code point/unit

2021-06-23 Thread vit via Digitalmars-d-learn
On Wednesday, 23 June 2021 at 15:48:57 UTC, Tejas wrote: On Wednesday, 23 June 2021 at 15:31:04 UTC, vit wrote: Hello, I am implementing @nogc string struct similar to c++ std::basic_string (link https://code.dlang.org/packages/basic_string). C++ string has methods front, back and pop_back

Re: Template arg deduction

2021-07-07 Thread vit via Digitalmars-d-learn
On Wednesday, 7 July 2021 at 19:14:52 UTC, Kevin Bailey wrote: I'm trying to use some fairly simple template argument deduction, but maybe I'm not getting the syntax correct. C++ doesn't event blink at something like this, but D is giving me: temptest.d(18): Error: template temptest.func canno

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread vit via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 12:49:58 UTC, Tejas wrote: On Wednesday, 14 July 2021 at 12:35:07 UTC, wjoe wrote: On Wednesday, 14 July 2021 at 11:31:36 UTC, Tejas wrote: ``` {auto a = i[1] , ++i[1] , a} //note the , not the ;``` Sorry I can't provide something even more concrete. Yes I saw

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread vit via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 13:16:49 UTC, Tejas wrote: On Wednesday, 14 July 2021 at 13:09:56 UTC, vit wrote: On Wednesday, 14 July 2021 at 12:49:58 UTC, Tejas wrote: [...] From doc: https://dlang.org/spec/operatoroverloading.html Postincrement e++ and Postdecrement e-- Operators These are

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread vit via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 15:01:45 UTC, wjoe wrote: On Wednesday, 14 July 2021 at 14:39:03 UTC, vit wrote: On Wednesday, 14 July 2021 at 13:16:49 UTC, Tejas wrote: [...] This work: ```d import std.stdio; struct abc{ int[100] a; ref int opIndex(int index)return{ retur

Re: opIndexUnary post in-/decrement how to ?

2021-07-14 Thread vit via Digitalmars-d-learn
On Wednesday, 14 July 2021 at 15:19:03 UTC, vit wrote: ```d import std.stdio; struct abc{ int[100] a; struct Proxy{ abc* ptr; const int index; int opUnary(string op : "++")(){ return ++ptr.a[index]; //add missing ++ } } Pr

Re: Not allowed to globally overload operators?

2021-07-19 Thread vit via Digitalmars-d-learn
On Tuesday, 20 July 2021 at 06:20:34 UTC, Tejas wrote: ... Initially, I was trying to create the spaceship operator of C++, but we aren't allowed to create new operators ... D has spaceship operator: opCmp (https://dlang.org/spec/operatoroverloading.html#compare).

betterC shared static ctor

2021-07-21 Thread vit via Digitalmars-d-learn
Is it possible to call all shared static ctors in betterC? ```d //-betterC static immutable int i; shared static this(){ i = 42; } extern(C) void main(){ assert(i != 42); } ```

Re: betterC shared static ctor

2021-07-21 Thread vit via Digitalmars-d-learn
On Wednesday, 21 July 2021 at 08:28:22 UTC, Mike Parker wrote: On Wednesday, 21 July 2021 at 08:11:06 UTC, vit wrote: Is it possible to call all shared static ctors in betterC? ```d //-betterC static immutable int i; shared static this(){ i = 42; } extern(C) void main(){ assert(i

Re: Destructors can't be @nogc?

2021-07-24 Thread vit via Digitalmars-d-learn
On Friday, 23 July 2021 at 20:24:02 UTC, Jim wrote: Hello, I've been playing with D and trying to understand how to work with @nogc. I must be doing something wrong, because even though I tagged the destructor for my class `@nogc`, I'm getting the following error: `.\min.d(27): Error: "@nogc"

Re: cast to pure function stop work after upgrade

2021-08-02 Thread vit via Digitalmars-d-learn
On Monday, 2 August 2021 at 11:28:46 UTC, Tejas wrote: On Monday, 14 June 2021 at 13:31:51 UTC, baby_tiger wrote: [...] It seems to not work at runtime either. Maybe they've made this behaviour illegal now? Hopefully someone answers. ```d import std.traits; import core.stdc.stdarg;

Re: cast to pure function stop work after upgrade

2021-08-02 Thread vit via Digitalmars-d-learn
On Monday, 14 June 2021 at 13:31:51 UTC, baby_tiger wrote: this used work for me, after upgrade I get this error. how to fix it ? import std.traits; enum LogLevel : ubyte { INFO = 0, WARN, ERROR, F

compare types of functions.

2021-08-02 Thread vit via Digitalmars-d-learn
Hello, Why this doesn't work: ```d template DestructorType(T){ alias Get(T) = T; alias DestructorType = Get!(typeof((void*){ T tmp; })); } struct Foo{ ~this()@safe{} } ``` ```d void main(){ //Error: static assert: `is(void function(void*) pure nothrow @nogc

input range with non copyable element

2021-08-08 Thread vit via Digitalmars-d-learn
Hello, is there reason why elements of input range must be copyable? For example this example works and copy ctor is never called: ```d import std.algorithm : map; import std.range; struct Foo{ int i; this(scope ref typeof(this) rhs)pure nothrow @safe @nogc{ //this.i = rhs

Re: input range with non copyable element

2021-08-09 Thread vit via Digitalmars-d-learn
On Monday, 9 August 2021 at 02:47:40 UTC, Mathias LANG wrote: On Sunday, 8 August 2021 at 18:36:02 UTC, vit wrote: Hello, is there reason why elements of input range must be copyable? By design, not that I can think of. But it is assumed all over the place, unfortunately. You can make your `f

Re: How to call destructor before free without dropping @nogc?

2021-08-19 Thread vit via Digitalmars-d-learn
On Thursday, 19 August 2021 at 08:25:23 UTC, Bienlein wrote: On Thursday, 19 August 2021 at 07:30:38 UTC, Bienlein wrote: Hello, I allocate some instance of class C manually and then free the memory again: class C { int num; ~this() { writeln("~this"); }

template ctor overload Segmentation fault

2021-12-12 Thread vit via Digitalmars-d-learn
Hello, why does this code fail to compile? ```d struct Foo(T){ this(Rhs, this This)(scope Rhs rhs){ } this(ref scope typeof(this) rhs){ } } struct Bar{ Foo!int foo; } void main(){ } ``` error: Segmentation fault (core dumped)

Re: template ctor overload Segmentation fault

2021-12-12 Thread vit via Digitalmars-d-learn
On Sunday, 12 December 2021 at 18:32:28 UTC, Imperatorn wrote: On Sunday, 12 December 2021 at 11:57:43 UTC, vit wrote: Hello, why does this code fail to compile? ```d struct Foo(T){ this(Rhs, this This)(scope Rhs rhs){ } this(ref scope typeof(this) rhs){ } } struct Bar{

Re: template ctor overload Segmentation fault

2021-12-14 Thread vit via Digitalmars-d-learn
On Tuesday, 14 December 2021 at 14:40:00 UTC, RazvanN wrote: On Sunday, 12 December 2021 at 11:57:43 UTC, vit wrote: Hello, why does this code fail to compile? ```d struct Foo(T){ this(Rhs, this This)(scope Rhs rhs){ } this(ref scope typeof(this) rhs){ } } struct Bar{

dynamic array + copy ctor

2021-12-19 Thread vit via Digitalmars-d-learn
Hello, Why is copy ctor in this example not called? ```d import std.stdio; struct Foo { int i; this(int i){ this.i = i; writeln("init: ", i); } this(ref typeof(this) rhs){ this.i = rhs.i; writeln("copy: ", i); } ~this() { writeln(

Re: dynamic array + copy ctor

2021-12-20 Thread vit via Digitalmars-d-learn
On Sunday, 19 December 2021 at 23:21:00 UTC, Stanislav Blinov wrote: On Sunday, 19 December 2021 at 22:29:21 UTC, vit wrote: Hello, Why is copy ctor in this example not called? Because D runtime isn't properly married to copy constructors yet. I.e. it's a bug, a variant of this one: https://

Re: How to properly use variadic templates (functions)?

2021-12-21 Thread vit via Digitalmars-d-learn
On Tuesday, 21 December 2021 at 08:26:17 UTC, rempas wrote: On Tuesday, 21 December 2021 at 08:11:39 UTC, Anonymouse wrote: I'm not certain I understand, but won't `foreach (i, a; args) { /* ... */ }` in his example do that? As in, if you necessarily must index `args` instead of using a for

opCast + dtor error

2021-12-28 Thread vit via Digitalmars-d-learn
Hi, why this code doesn't compile? ```d struct Foo{ bool opCast(T : bool)()const{ assert(0); } ~this(){} } struct Bar{ const Foo foo; } void main(){ } ``` Error: template instance `opCast!(Foo)` does not match template declaration `opCast(T : bool)()`

Re: opCast + dtor error

2021-12-29 Thread vit via Digitalmars-d-learn
On Wednesday, 29 December 2021 at 01:00:53 UTC, Tejas wrote: On Tuesday, 28 December 2021 at 18:27:36 UTC, vit wrote: [...] Since a destructor ignores `const`, I think adding the `~this` to Foo manually is somehow making the compiler have to actually cast away const, which it is doing via `c

Re: mixin does not work as expected

2022-01-05 Thread vit via Digitalmars-d-learn
On Wednesday, 5 January 2022 at 08:40:15 UTC, rempas wrote: I'm trying to use mixins and enums to "expand" code in place but the results are not what I expected and I'm getting an weird error. I have created the smallest possible example to reproduce the error and it is the following: [...]

Re: mixin does not work as expected

2022-01-05 Thread vit via Digitalmars-d-learn
On Wednesday, 5 January 2022 at 08:40:15 UTC, rempas wrote: I'm trying to use mixins and enums to "expand" code in place but the results are not what I expected and I'm getting an weird error. I have created the smallest possible example to reproduce the error and it is the following: [...]

Re: mixin does not work as expected

2022-01-05 Thread vit via Digitalmars-d-learn
On Wednesday, 5 January 2022 at 09:17:54 UTC, rempas wrote: On Wednesday, 5 January 2022 at 09:02:53 UTC, vit wrote: Try this: ```d pragma(msg, type_check!("static if", "i8", "true", "5", "4", "10", "5")); ``` Result: ```d static if(is_same!(num, i8)) { mixin(base_digit!("5", "4", "10",

Re: A slice consisting of non-consecutive elements of an array?

2022-01-11 Thread vit via Digitalmars-d-learn
On Wednesday, 12 January 2022 at 05:27:08 UTC, forkit wrote: I am familiar with the concept of a slice in D. However, a slice is a consecutive slice, is in not? (e.g) [4..$-1] I would like a slice (or a view, or whatever name you wanna call it), of particular elements within an array that ma

Re: A slice consisting of non-consecutive elements of an array?

2022-01-11 Thread vit via Digitalmars-d-learn
On Wednesday, 12 January 2022 at 06:43:40 UTC, forkit wrote: On Wednesday, 12 January 2022 at 06:16:49 UTC, vit wrote: Yes std.algorithm : filter. ```d import std.stdio : writeln; import std.algorithm : filter; void main()@safe{ auto a = ["one", "one", "two", "one", "two", "one",

Re: A slice consisting of non-consecutive elements of an array?

2022-01-11 Thread vit via Digitalmars-d-learn
On Wednesday, 12 January 2022 at 06:58:47 UTC, vit wrote: On Wednesday, 12 January 2022 at 06:43:40 UTC, forkit wrote: On Wednesday, 12 January 2022 at 06:16:49 UTC, vit wrote: Yes std.algorithm : filter. ```d import std.stdio : writeln; import std.algorithm : filter; void main()@safe{

error: rvalue constructor and a copy constructor

2022-01-12 Thread vit via Digitalmars-d-learn
Hello, I have this code: ```d import core.lifetime : move, forward; import std.stdio : writeln; struct Foo{ int i; static auto make(int i){ Foo tmp; tmp.i = i; return move(tmp); } this(scope const ref typeof(this) rhs){ this.i = rhs.i; w

Re: error: rvalue constructor and a copy constructor

2022-01-12 Thread vit via Digitalmars-d-learn
On Wednesday, 12 January 2022 at 08:04:19 UTC, vit wrote: Hello, I have this code: ```d [...] run.dlang.io has old version of dmd-beta/dmd-nightly with bug

Re: alias and __VERSION__ condition doesn't play well

2022-01-17 Thread vit via Digitalmars-d-learn
On Tuesday, 18 January 2022 at 04:42:45 UTC, frame wrote: At the very top of my module I have this declaration: ```d static if (__VERSION__ >= 2098) { alias Foo = TypeA; } else { alias Foo = TypeB; } ``` No problem inside the module itself but this doesn't work when imported from anoth

Re: alias and __VERSION__ condition doesn't play well

2022-01-18 Thread vit via Digitalmars-d-learn
On Tuesday, 18 January 2022 at 12:05:38 UTC, Paul Backus wrote: On Tuesday, 18 January 2022 at 04:42:45 UTC, frame wrote: At the very top of my module I have this declaration: ```d static if (__VERSION__ >= 2098) { alias Foo = TypeA; } else { alias Foo = TypeB; } ``` No problem inside

Re: alias and __VERSION__ condition doesn't play well

2022-01-18 Thread vit via Digitalmars-d-learn
On Wednesday, 19 January 2022 at 04:15:54 UTC, Steven Schveighoffer wrote: On 1/18/22 7:19 AM, vit wrote: On Tuesday, 18 January 2022 at 12:05:38 UTC, Paul Backus wrote: On Tuesday, 18 January 2022 at 04:42:45 UTC, frame wrote: At the very top of my module I have this declaration: ```d static

Re: Function prototype overloading does not work ?

2022-01-19 Thread vit via Digitalmars-d-learn
On Wednesday, 19 January 2022 at 08:47:27 UTC, Enjoys Math wrote: ``` module expr; import dots; import operator; import equation; import var; import var_expr; import zz_const; class Expr { public: void opBinary(string op)(string s) const { static if (op == "+") { Expr

forward tuple arg to local variable + dtor

2022-01-22 Thread vit via Digitalmars-d-learn
Hello, Why is tuple variable `params` immediately destructed after its construction? Why is `check(-2)` after `dtor(2)`? Code: ```d import std.stdio : writeln; import core.lifetime : forward, move; struct Foo{ int i; this(int i){ this.i = i; writeln("ctor(", i, ")

Re: forward tuple arg to local variable + dtor

2022-01-22 Thread vit via Digitalmars-d-learn
On Saturday, 22 January 2022 at 14:23:32 UTC, Adam Ruppe wrote: You can't forward to a local variable. Local variables will be a copy of the tuple. forward only actually works if sent *directly* to another function call. There's a bunch of things in D that only work in function parameter list

Re: forward tuple arg to local variable + dtor

2022-01-22 Thread vit via Digitalmars-d-learn
On Saturday, 22 January 2022 at 17:23:12 UTC, Ali Çehreli wrote: On 1/22/22 07:17, vit wrote: > Why local variable of type tuple call destructors immediately after > initialization? I don't even understand where the local variable comes from. If you want a pair of Foo objects, I would use std.

Re: forward tuple arg to local variable + dtor

2022-01-22 Thread vit via Digitalmars-d-learn
On Saturday, 22 January 2022 at 19:01:09 UTC, Stanislav Blinov wrote: On Saturday, 22 January 2022 at 18:00:58 UTC, vit wrote: [...] Take by value and make a copy without forwarding: ```d import std.typecons : Tuple; import std.meta : allSatisfy; [...] Thanks, second options is what I nee

Re: passing a variadic parameter to randomSample

2022-01-25 Thread vit via Digitalmars-d-learn
On Tuesday, 25 January 2022 at 09:48:25 UTC, forkit wrote: so I'm trying to write (or rather learn how to write) a 'variadic template function', that returns just one of its variadic parameter, randomly chosen. But can't get my head around the problem here :-( .. Error: template `std.random.r

Re: opCast in class prevents destroy

2022-03-01 Thread vit via Digitalmars-d-learn
On Tuesday, 1 March 2022 at 08:16:13 UTC, Mike Parker wrote: On Tuesday, 1 March 2022 at 07:16:11 UTC, bauss wrote: Right now if you want to add an additional cast then you have to implement ALL the default behaviors and then add your custom cast. It's two template functions like the OP used

Re: Cannot Call Super-Class Overloaded Function If Class has Function Override?

2022-03-01 Thread vit via Digitalmars-d-learn
On Tuesday, 1 March 2022 at 08:40:12 UTC, Vijay Nayar wrote: I've randomly encountered a strange error, and I cannot find any explanation for it in the official documentation of syntax. Essentially, a class cannot call function overload in a super-class if the class itself contains an override

Re: Cannot Call Super-Class Overloaded Function If Class has Function Override?

2022-03-01 Thread vit via Digitalmars-d-learn
On Tuesday, 1 March 2022 at 08:40:12 UTC, Vijay Nayar wrote: I've randomly encountered a strange error, and I cannot find any explanation for it in the official documentation of syntax. [...] Here is more info (3.): https://docarchives.dlang.io/v2.078.0/spec/function.html#function-inheritanc

error forward references if scope

2022-03-12 Thread vit via Digitalmars-d-learn
Hello, why this code compile without problems: ```d struct Query{ public const SharedPtr!Builder builder; } interface Builder{ void build(ref Query query); } struct SharedPtr(T){ enum touch_T = __traits(hasMember, T, "touch"); //... } void main(){ } ``` but if `ref Query q

Re: How to make a generic function to take a class or struct by reference?

2022-03-27 Thread vit via Digitalmars-d-learn
On Sunday, 27 March 2022 at 16:27:44 UTC, JN wrote: I would like to have only one definition of getX if possible, because they both are doing the same thing. I can't remove the ref one, because without a ref it will pass the struct as a temporary and compiler won't like that. ```d import std.

-dip1000 @safe scope wrapper

2018-06-28 Thread vit via Digitalmars-d-learn
Hello, Is it possible to create scope wrapper initialized by non default constructor with scope parameter? something like this: struct Wrapper{ int* p; static Wrapper create(scope return int* p)@safe{ Wrapper w; w.p = p; return w; } /++ This doesn't wor

Re: how to create an array of scoped objects ?

2018-07-03 Thread vit via Digitalmars-d-learn
On Tuesday, 3 July 2018 at 02:13:21 UTC, Flaze07 wrote: e.g A is a class that emits output during destruction { auto a = scoped!A(); } how do I contain it in a container, in the Array struct ? { auto a = scoped!A(); Array!( typeof( a ) ) arr; foreach( i ; 0..3 ) { arr.in

Re: Using C++ with D / returning a templated type from C++

2018-07-04 Thread vit via Digitalmars-d-learn
On Wednesday, 4 July 2018 at 17:32:49 UTC, Robert M. Münch wrote: I have the following C++ code and want to give the D/C++ integration a new try: template class Array {...} class myClass {...} typedef Array myClassArray; myClassArray classA::getArray() noexcept {

Re: ranges.chunks and map! does not work

2018-07-05 Thread vit via Digitalmars-d-learn
On Thursday, 5 July 2018 at 09:47:32 UTC, Andre Pany wrote: Hi, the purpose of this code is to generate CSV based on 3 double arrays. I wonder why map cannot directly use the result of the chunks function. import std.experimental.all; void main() { double[] timestamps = [1.1]; doubl

Re: Safe to cast to immutable and return?

2018-07-05 Thread vit via Digitalmars-d-learn
On Thursday, 5 July 2018 at 11:15:03 UTC, Timoses wrote: Is this safe? class A {} immutable(A) getA() { A a; // .. do stuff with a // not leaking a to any functions // is this safe return cast(immutable A)a; } What if A is replaced with A[] or A[int]? If it's not safe,

Re: UFCS confusion

2018-07-16 Thread vit via Digitalmars-d-learn
On Friday, 13 July 2018 at 11:37:09 UTC, Michael wrote: On Friday, 13 July 2018 at 11:17:32 UTC, Radu wrote: On Friday, 13 July 2018 at 11:12:47 UTC, Michael wrote: On Friday, 13 July 2018 at 10:52:54 UTC, Radu wrote: On Friday, 13 July 2018 at 10:21:54 UTC, Michael wrote: [...] Do you try

@nogc closures

2018-08-05 Thread vit via Digitalmars-d-learn
It's possible create something like this without errors? void main()@nogc{ //Error: function `app.main` is `@nogc` // yet allocates closures with the GC import std.experimental.all; const int j = 2; int i = 0; const int[3] tmp = [1, 2, 3]; tmp[]

Re: @nogc closures

2018-08-05 Thread vit via Digitalmars-d-learn
On Sunday, 5 August 2018 at 10:57:32 UTC, Steven Schveighoffer wrote: On 8/5/18 5:20 AM, vit wrote: It's possible create something like this without errors? void main()@nogc{   //Error: function `app.main` is `@nogc`     //  yet allocates closures with the GC     import std.ex

GC and void[N] in struct

2018-08-06 Thread vit via Digitalmars-d-learn
Hello, I have this struct: struct S{ uint kind; void[N] data_; } Instances of struct S are allocated by standard GC new and S.data_ can contain pointers/ranges to GC allocated data. If is GC disabled then program run fine. But when is GC enabled then it fail randomly. If the defini

Re: GC and void[N] in struct

2018-08-06 Thread vit via Digitalmars-d-learn
On Monday, 6 August 2018 at 18:28:11 UTC, Steven Schveighoffer wrote: On 8/6/18 2:22 PM, vit wrote: Hello, I have this struct: struct S{     uint kind;     void[N] data_; define "N" } Instances of struct S are allocated by standard GC new and S.data_ can contain pointers/ranges to GC a

Re: GC and void[N] in struct

2018-08-06 Thread vit via Digitalmars-d-learn
On Monday, 6 August 2018 at 19:17:58 UTC, nkm1 wrote: On Monday, 6 August 2018 at 18:22:24 UTC, vit wrote: Hello, I have this struct: struct S{ uint kind; void[N] data_; } Instances of struct S are allocated by standard GC new and S.data_ can contain pointers/ranges to GC allocated d

Re: GC and void[N] in struct

2018-08-06 Thread vit via Digitalmars-d-learn
On Monday, 6 August 2018 at 19:56:03 UTC, Steven Schveighoffer wrote: BTW, is there a reason you aren't just using Algebraic? https://dlang.org/phobos/std_variant.html#.Algebraic -Steve primarily visit for Algebraic isn't pure, @nogc, @safe, nothrow.

Re: scope variable values assigned to non-scope this.placeholder

2018-08-06 Thread vit via Digitalmars-d-learn
On Monday, 6 August 2018 at 20:29:29 UTC, Alexandru Ermicioi wrote: Hi Dlang community! I've been playing with dip1000 and scope storage class and stumbled upon a strange error that I can't to understand yet. Here is minimized version of code that generates the error: [...] Parameter value

Re: GC and void[N] in struct

2018-08-06 Thread vit via Digitalmars-d-learn
On Monday, 6 August 2018 at 21:23:36 UTC, Paul Backus wrote: On Monday, 6 August 2018 at 20:22:36 UTC, vit wrote: On Monday, 6 August 2018 at 19:56:03 UTC, Steven Schveighoffer wrote: BTW, is there a reason you aren't just using Algebraic? https://dlang.org/phobos/std_variant.html#.Algebraic

float/double to string (pure nothrow @nogc)

2018-08-08 Thread vit via Digitalmars-d-learn
Hello, is in phobos some function which convert float/double to string and is pure @nogc and nothrow?

Re: float/double to string (pure nothrow @nogc)

2018-08-08 Thread vit via Digitalmars-d-learn
On Wednesday, 8 August 2018 at 17:40:11 UTC, ketmar wrote: vit wrote: Hello, is in phobos some function which convert float/double to string and is pure @nogc and nothrow? i don't think that you can make it `pure`, but you certainly can make it `nothrow`, `@nogc` and ctfe-able. it's dangerou

Re: Concat enum of strings into one string

2018-08-14 Thread vit via Digitalmars-d-learn
On Tuesday, 14 August 2018 at 14:37:33 UTC, Andrey wrote: On Tuesday, 14 August 2018 at 14:07:23 UTC, Timoses wrote: Here's one version: template StringEnumValues(alias Enum) { import std.traits : EnumMembers; string[] StringEnumValues() { string[] enumValues; static

Re: Make function alias

2018-08-20 Thread vit via Digitalmars-d-learn
On Monday, 20 August 2018 at 13:22:02 UTC, Andrey wrote: On Monday, 20 August 2018 at 13:14:14 UTC, Andrey wrote: Mistake... this is: static void log(bool newline = true)(string text) { alias print(T...) = newline ? &writeln : &write; _file.print(text); text.print(); } static void lo

Re: Patterns to avoid GC with capturing closures?

2018-08-25 Thread vit via Digitalmars-d-learn
On Friday, 24 August 2018 at 15:18:13 UTC, Peter Alexander wrote: Consider this code, which is used as an example only: auto scaleAll(int[] xs, int m) { return xs.map!(x => m * x); } As m is captured, the delegate for map will rightly allocate the closure in the GC heap. In C++, you would

Re: Is there a simple way to check if value is null for every case?

2018-08-27 Thread vit via Digitalmars-d-learn
On Monday, 27 August 2018 at 12:54:59 UTC, SG wrote: On Monday, 27 August 2018 at 03:21:04 UTC, rikki cattermole wrote: Templates make it the easiest way, since common patterns, like arrays, classes and pointers have the exact same null check syntax. I see. That code is only for classes. C#

Re: Create constraint for each parameter in template arg pack

2018-08-28 Thread vit via Digitalmars-d-learn
On Tuesday, 28 August 2018 at 12:28:19 UTC, Andrey wrote: Hello, Let we have two variadic templates: template Qwerty(Values...) {} template Qaz(alias type, Data...) {} Now I want to add a constraint to "Qwerty" so that each type in "Values" pack must be a "Qaz" template. I don't care about v

  1   2   >