Import template in phobos

2015-08-01 Thread vit via Digitalmars-d-learn
Exist in phobos something like Import template? public import std.traits; template Import(alias Module){ mixin(import ~ moduleName!Module ~ ;); } class C; struct Test{ Import!(std.typecons).Rebindable!C test;//symbols }

template instantiation

2016-05-19 Thread vit via Digitalmars-d-learn
///Hello, i'm trying to implement d compiler and i've problems with some template corner cases. class Type{} class Foo(T : const T){ alias X = T; } alias Bar(T : Foo!Ts, Ts...) = Ts[0]; pragma(msg, "Foo1: ", Foo!(immutable Type).X);//Foo1: Type pragma(msg, "Foo2: ",

Re: Is there a way to make a class variable visible but constant to outsiders, but changeable (mutable) to the class itself?

2016-05-21 Thread vit via Digitalmars-d-learn
On Saturday, 21 May 2016 at 17:32:47 UTC, dan wrote: Is it possible to have a class which has a variable which can be seen from the outside, but which can only be modified from the inside? Something like: class C { int my_var = 3; // semi_const?? void do_something() { my_var = 4; } }

Re: binary expression...

2016-05-21 Thread vit via Digitalmars-d-learn
On Saturday, 21 May 2016 at 18:10:55 UTC, captain_fid wrote: Please forgive if asked before. My google skills seemed to fail me and didn't see any result from search. My problem is simple (though not my understanding LOL). struct D { int value; bool opEquals()(bool value) const {

Re: Lazily evaluated property pointing to read only object

2016-09-27 Thread vit via Digitalmars-d-learn
On Saturday, 24 September 2016 at 11:51:56 UTC, Basile B. wrote: On Saturday, 24 September 2016 at 10:59:50 UTC, mikey wrote: On Saturday, 24 September 2016 at 10:16:34 UTC, Basile B. wrote: You don't need to cast, from "mutable" to "const" is implicit:

Re: Overloading funtion templates.

2017-06-29 Thread vit via Digitalmars-d-learn
On Thursday, 29 June 2017 at 06:40:04 UTC, Balagopal Komarath wrote: On Wednesday, 28 June 2017 at 12:19:31 UTC, vit wrote: auto foo(alias F, T)(T x) { return x.foo(); } With this definition foo!((x) => x+1)(3); doesn't work. Is there a way to solve this? You don´t need overload

Re: Accessing part of a struct in an easy way

2017-07-03 Thread vit via Digitalmars-d-learn
On Monday, 3 July 2017 at 13:53:45 UTC, Paolo Invernizzi wrote: I've struct like that: struct Foo { int a_1; float a_2; string a_3; string b_1; double b_2; } I would like to transparently access that like: foo.a.first foo.b.second = "baz"; with an helper like: auto a(...) { ... }

Re: Overloading funtion templates.

2017-06-28 Thread vit via Digitalmars-d-learn
On Wednesday, 28 June 2017 at 11:49:57 UTC, Balagopal Komarath wrote: Shouldn't the compiler be able to resolve foo!g(3) to the first template foo? import std.stdio; import std.algorithm; import std.range; auto foo(F, T)(T x) { return x.foo(F); } auto foo(F, T)(T x, F f) { return

difference between x = Nullable.init and x.nullify

2017-06-02 Thread vit via Digitalmars-d-learn
Hello, What's the difference between x = Nullable!Test.init and x.nullify? class Test{} void test()pure nothrow{ Nullable!Test x; x = Nullable!Test.init; //OK x.nullify; //Error: function 'std.typecons.Nullable!(Test).Nullable.nullify!().nullify' is not nothrow

Re: difference between x = Nullable.init and x.nullify

2017-06-03 Thread vit via Digitalmars-d-learn
On Saturday, 3 June 2017 at 06:19:29 UTC, Jonathan M Davis wrote: [...] thanks

Re: difference between x = Nullable.init and x.nullify

2017-06-04 Thread vit via Digitalmars-d-learn
On Sunday, 4 June 2017 at 09:04:14 UTC, Jonathan M Davis wrote: [...] Why Nullable!T call destroy for reference types?

Re: Variadic Mixin/Template classes?

2017-11-25 Thread vit via Digitalmars-d-learn
On Saturday, 25 November 2017 at 09:52:01 UTC, Chirs Forest wrote: I'd like to make a class that takes multiple template types (1 - several) which can hold an array/tuple of a second class that are instantiated with those types. class Bar(T) { T bar; } class Foo(T[]){ // not sure how to

ptr wrapper with dip1000

2017-12-19 Thread vit via Digitalmars-d-learn
How to manualy declare constructor for struct Ptr which work like Ptr.create? struct Ptr{ int* ptr; static Ptr create(scope return int* ptr)@safe{ Ptr x; x.ptr = ptr; return x; } /++ This doesn't work: this(scope return int* ptr)scope @safe{

Re: minElement on array of const objects

2017-11-13 Thread vit via Digitalmars-d-learn
On Monday, 13 November 2017 at 12:15:26 UTC, Nathan S. wrote: Unqual!Element seed = r.front; alias MapType = Unqual!(typeof(mapFun(CommonElement.init))); This looks like a job for std.typecons.Rebindable!(const A) instead of Unqual!(const A) which is used currently. I am surprised that

Re: return ref this -dip1000

2017-12-11 Thread vit via Digitalmars-d-learn
On Monday, 11 December 2017 at 20:44:06 UTC, Eugene Wissner wrote: On Monday, 11 December 2017 at 20:40:09 UTC, vit wrote: This code doesn't compile with -dip1000: struct Foo{ int foo; ref int bar(){ return foo; } } Error: returning `this.foo` escapes a reference to

return ref this -dip1000

2017-12-11 Thread vit via Digitalmars-d-learn
This code doesn't compile with -dip1000: struct Foo{ int foo; ref int bar(){ return foo; } } Error: returning `this.foo` escapes a reference to parameter `this`, perhaps annotate with `return` How can be annotated this parameter with 'return ref' ?

template specialization and Rebindable

2017-12-03 Thread vit via Digitalmars-d-learn
Why is template param T != B but T == Rebindable!(const(B)) when specialization is T : const(A)? abstract class A{} class B : A{} string foo(T : const(A))(T x){ return T.stringof; } void main(){ import std.typecons : Rebindable; import std.stdio : writeln; Rebindable!(const

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 ) {

Re: Is Nullable supposed to provide Optional semantics?

2017-12-29 Thread vit via Digitalmars-d-learn
On Friday, 29 December 2017 at 21:43:25 UTC, Chris Paulson-Ellis wrote: On Friday, 29 December 2017 at 21:34:27 UTC, vit wrote: use: n = Nullable!Object.init; //doesn't call destroy instead of: n.nullify(); Only nullify() can make isNull return true again. I need that semantic.

Re: Is Nullable supposed to provide Optional semantics?

2017-12-29 Thread vit via Digitalmars-d-learn
On Friday, 29 December 2017 at 20:52:51 UTC, Chris Paulson-Ellis wrote: I've been bitten by trying to use Nullable(T) on class types. Minimal example... [...] use: n = Nullable!Object.init; //doesn't call destroy instead of: n.nullify();

@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

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

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

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

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?

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

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;

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

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

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

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

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 ? : _file.print(text); text.print(); } static void log(bool newline

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: Patterns to avoid GC with capturing closures?

2018-08-26 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: 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

Re: Solving the impossible?

2018-08-29 Thread vit via Digitalmars-d-learn
On Wednesday, 29 August 2018 at 19:56:31 UTC, Everlast wrote: On Tuesday, 28 August 2018 at 22:01:45 UTC, Paul Backus wrote: [...] One of the things that makes a good language is it's internal syntactic consistency. This makes learning a language easier and also makes remembering it easier.

Re: Can't print enum values

2018-08-30 Thread vit via Digitalmars-d-learn
On Thursday, 30 August 2018 at 10:41:58 UTC, Andrey wrote: Hello, This code doesn't print enum values: import std.meta; import std.traits; import std.stdio; enum MyEnum : string { First = "F_i_r_s_t", Second = "S_e_c_o_n_d" } alias QW(alias arg) =

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: 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];

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

-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

Re: Can't print enum values

2018-08-30 Thread vit via Digitalmars-d-learn
On Thursday, 30 August 2018 at 11:34:36 UTC, Andrey wrote: On Thursday, 30 August 2018 at 11:09:40 UTC, vit wrote: [...] I want to create a reusable template for this purpose. Why I can't use "staticMap" so that compiler it self would do this: [...] Just wrap some some symbol "args" with

Re: Pass lambda into template

2018-09-03 Thread vit via Digitalmars-d-learn
On Monday, 3 September 2018 at 09:09:44 UTC, Andrey wrote: Hello, Here is a code with comments: https://run.dlang.io/is/BNl2Up. I don't understand how to pass lambda into template. I get an error: onlineapp.d(18): Error: template instance `qwerty!((i) => "arg" ~ i.to!string ~ "[0] == '?'",

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: 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:

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 = } ///Error: cannot take address of `scope` local `x` in

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

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

@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

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

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?

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"); }

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{

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 ++ } }

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,

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

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

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 =

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() {

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:

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: 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

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

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{

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

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: Not allowed to globally overload operators?

2021-07-20 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).

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:

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: 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

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",

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;

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{

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

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

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

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

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.):

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

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

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:

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-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

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

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 == "+") {

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 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

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

  1   2   >