New vs length on dymamic array

2020-11-09 Thread Andrey via Digitalmars-d-learn
Hello, Are here any differences in creation of dynamic array with known size? auto array = new wchar[](111); and wchar[] array; array.length = 111;

Re: Call method of object variable

2020-10-16 Thread Andrey via Digitalmars-d-learn
Thank you!

Call method of object variable

2020-10-16 Thread Andrey via Digitalmars-d-learn
Hi, I have got: struct Qaz { wstring read() {return null;} wstring hear() {return "";} } void main() { // ... static if(some_condition) alias method = Qaz.hear; else alias method = Qaz.read; // ... Qaz qaz; qaz.method(); // ??? } How to call alias

Re: Count template parameters of method

2020-10-12 Thread Andrey via Digitalmars-d-learn
And what about: void test() {} and void text(alias qqq)() {} ?

Count template parameters of method

2020-10-11 Thread Andrey via Digitalmars-d-learn
Hello, How to count a number of parameters in uninitialized template method? For example: struct Test { void abc(int a, bool status, string text)() {} { The method "Test.abc" has three template paramenters. I know that "TemplateArgsOf" exists but it is used only for INITIALIZED

Re: Sum string lengths

2020-05-14 Thread Andrey via Digitalmars-d-learn
Thanks everyone.

Sum string lengths

2020-05-13 Thread Andrey via Digitalmars-d-learn
Hi, I want to sum lengths of all strings in array: auto data = ["qwerty", "az", ""]; Fold and reduce doesn't work: auto result = data.fold!`a + b.length`(0U); gives error: static assert: "Incompatible function/seed/element: binaryFun/uint/string" How to do it in one line?

Re: declaration of inner function is already defined

2020-05-13 Thread Andrey via Digitalmars-d-learn
On Wednesday, 13 May 2020 at 12:58:11 UTC, Adam D. Ruppe wrote: On Wednesday, 13 May 2020 at 12:45:06 UTC, Andrey wrote: Why this works: It's just defined that way. Local functions follow local variable rules - must be declared before use and names not allowed to overload each other.

declaration of inner function is already defined

2020-05-13 Thread Andrey via Digitalmars-d-learn
Hi, Why this works: void setBases(string type)(ref int data, string base, string[] syllables) { } void setBases(string type, T)(ref int data, const ref T source) { } void main() { int q = 6; setBases!"tt"(q, "qwerty", ["tg", "jj"]); setBases!"tt"(q, q); } and this doesn't

Call of C function breaks memoty layout

2020-01-24 Thread Andrey via Digitalmars-d-learn
Hello, I'm trying to bind C library for Tcl/Tk in D code. There is a function called "Tcl_CreateInterp()" which I declared as extent(C). When I call this function then layout of memory become broken - one of my global wstring variables loses it's value. I don't know why it is happens. If I

Re: CTFE and assoc array

2020-01-19 Thread Andrey via Digitalmars-d-learn
On Saturday, 18 January 2020 at 21:44:35 UTC, Boris Carvajal wrote: I read that thread. But: Deprecation: initialization of immutable variable from static this is deprecated. Use shared static this instead. And we get? No CTFE with static immutable AA?

CTFE and assoc array

2020-01-18 Thread Andrey via Digitalmars-d-learn
Hello, Why this doesn't work? import std; struct Qwezzz { shared static this() { qaz = qazMap; } enum qazMap = ["rrr": "vv", "hty": "4ft6"]; static immutable string[string] qaz; } void main() { enum sorted = Qwezzz.qaz.keys.sort(); } The variable "qaz"

Re: undefined symbol: _D3std7variant...

2019-10-22 Thread Andrey via Digitalmars-d-learn
On Tuesday, 22 October 2019 at 12:57:45 UTC, Daniel Kozak wrote: Have you try to clean all caches? Try to remove .dub folder I removed .dub folder but this error appears again.

undefined symbol: _D3std7variant...

2019-10-22 Thread Andrey via Digitalmars-d-learn
Hello, During compilation on linking stage I get strange errors (LDC): lld-link: error: undefined symbol: _D3std7variant__T8VariantNVmi56TSQBf8typecons__T5TupleTAyuTSQCgQCf__TQCaVmi32TSQCzQBu__TQBoTAQBmTQBqZQCbTQnTQCbZQDrZQCqTQBcTQCrZQEh4typeMxFNbNdNeZC8TypeInfo referenced by

Re: contains method on immutable sorted array

2019-10-22 Thread Andrey via Digitalmars-d-learn
On Monday, 21 October 2019 at 20:44:29 UTC, Nicholas Wilson wrote: works, so I guess contains doesn't work with immutable? If you can do some more research into this and confirm it then, please file a bug report. As I understand - yes. It doesn't work with immutable object. Also I see the

contains method on immutable sorted array

2019-10-21 Thread Andrey via Digitalmars-d-learn
Hello, I have got a global constant immutable array: immutable globalvalues = sort(cast(wstring[])["й", "ц", "ук", "н"]); Somewhere in program I want to check an existance: globalvalues.contains("ук"w).writeln; But get an error: Error: template std.range.SortedRange!(wstring[], "a <

Re: Import sources from parent project

2019-10-20 Thread Andrey via Digitalmars-d-learn
On Saturday, 19 October 2019 at 18:54:28 UTC, Andre Pany wrote: In dub.json of your child apps you need to add a dependency to parentapp. I added via "dependencies" parameter and after got this error: "Detected dependency cycle".

Import sources from parent project

2019-10-19 Thread Andrey via Digitalmars-d-learn
Hi, I have got this structure of my project: parentapp dub.json source common.d childapp1 dub.json source app.d somefile.d childapp2 dub.json source app.d The "childapp1" and "childapp2" are standanole

Problem with aliasing member function

2019-08-18 Thread Andrey via Digitalmars-d-learn
Hello, I can't compile this piece of code: struct Object { void run(wstring ending, uint index)(int number) { } } void tester(alias callback, T)(int number, T object = null) { static if(is(T == typeof(null))) alias handler = callback; else auto handler(wstring ending, uint

How to get name of my application (project)

2019-08-03 Thread Andrey via Digitalmars-d-learn
Hello, how to get name of my application (project) that we write in dub.json? Is there any compile-time constant like __MODULE__?

Re: Memory allocation failed in CT

2019-07-13 Thread Andrey via Digitalmars-d-learn
On Tuesday, 9 July 2019 at 19:04:53 UTC, Max Haughton wrote: Is this a 64 or 32 bit compiler? Also could you post the source code if possible? You could try "--DRT-gcopt=profile:1" druntime flag to see if the compiler is running out of memory for real Thanks for help. I solved my issue by

Re: Memory allocation failed in CT

2019-07-09 Thread Andrey via Digitalmars-d-learn
On Tuesday, 9 July 2019 at 17:59:24 UTC, Max Haughton wrote: On Tuesday, 9 July 2019 at 17:48:52 UTC, Andrey wrote: Hello, I have got a problem with compile-time calulations. I have some code generator that should create some long string of code during CT and after generation I mixin it. If I

Re: Memory allocation failed in CT

2019-07-09 Thread Andrey via Digitalmars-d-learn
On Tuesday, 9 July 2019 at 17:59:24 UTC, Max Haughton wrote: On Tuesday, 9 July 2019 at 17:48:52 UTC, Andrey wrote: I in addition wrote "buffer.reserve(10 * 1014 * 1024);" and it also doesn't help.

Memory allocation failed in CT

2019-07-09 Thread Andrey via Digitalmars-d-learn
Hello, I have got a problem with compile-time calulations. I have some code generator that should create some long string of code during CT and after generation I mixin it. If I run it normally - in run time - then there is no error and I get expected output - string with size ~ 3.5 MB. If I

Mixin mangled name

2019-07-01 Thread Andrey via Digitalmars-d-learn
Hello, Is it possible to mixin in code a mangled name of some entity so that compiler didn't emit undefined symbol error? For example mangled function name or template parameter?

Mixin mangled name

2019-07-01 Thread Andrey via Digitalmars-d-learn
Hello, Is it possible to mixin in code a mangled name of some entity so that compiler didn't emit undefined symbol error? For example mangled function name or template parameter?

Re: Alias overload of function

2019-06-09 Thread Andrey via Digitalmars-d-learn
On Sunday, 9 June 2019 at 10:42:12 UTC, Basile-z wrote: On Sunday, 9 June 2019 at 10:22:36 UTC, Andrey wrote: Hello, I have got 2 functions: void myFunc(string name, int a)(wstring value) {} void myFunc(string name, int a)() {} I want to make an alias (for example for second function

Alias overload of function

2019-06-09 Thread Andrey via Digitalmars-d-learn
Hello, I have got 2 functions: void myFunc(string name, int a)(wstring value) {} void myFunc(string name, int a)() {} I want to make an alias (for example for second function without argument): alias myAlias(int a) = myFunc!("Name", a); but compiler says: ... matches more than one

Re: Extract code of function

2019-05-26 Thread Andrey via Digitalmars-d-learn
On Sunday, 26 May 2019 at 18:21:23 UTC, Dennis wrote: On Sunday, 26 May 2019 at 18:14:23 UTC, Jacob Carlborg wrote: No, that's not possible. Some hacky solutions are possible by importing a source file as a string and parsing it manually. dglsl actually extracts D function code to put into

Extract code of function

2019-05-26 Thread Andrey via Digitalmars-d-learn
Hello, Is it possible to extract code of some function into string variable using CT reflextion? For example: int test(bool flag) { return flag ? 100 : getRandom(); } enum string code = GetFunctionCode!test; // "return flag ? 100 : getRandom();"

What compiles faster?

2019-05-19 Thread Andrey via Digitalmars-d-learn
Hello, Let we have got 3 template functions: void func1(int a)() {} void func2(int a, string b)() {} void func3(int a, string b, bool c)() {} As we see, the first function accepts 1 template argument, the second - 2 and the third - 3. What compiles faster: 1. When a program has got 100

CTFE sort of tuples

2019-05-01 Thread Andrey via Digitalmars-d-learn
Hello, I have got this code: alias Group = Tuple!(int[], "data", int, "key"); void main() { enum group = [ Group([1,2,3,4], 1), Group([5,3], 1), Group([4,5,4, 8, 9, 4], 1), Group([2,3,4], 1), ]; enum result = group.sort!"a.data < b.data"().array();

incomplete mixin expression

2019-04-24 Thread Andrey via Digitalmars-d-learn
enum Qaz : wstring { One = "один" } template Qwerty(Values...) { enum text = "Values[%d]"; enum args = iota(Values.length).map!(value => format!text(value)).join(','); pragma(msg, args); alias Qwerty = Alias!(mixin("AliasSeq!(" ~ args ~ ");")); } void main() {

Re: Check if function argument can be handled in CT

2019-04-24 Thread Andrey via Digitalmars-d-learn
On Wednesday, 24 April 2019 at 08:28:06 UTC, Basile.B wrote: On Wednesday, 24 April 2019 at 07:53:47 UTC, Andrey wrote: I know about this template. Unfortunally, it doesn't work inside functions. void test(string arg1, string arg2) { enum isKnown1 = is(typeof((){enum v = arg1;}));

Check if function argument can be handled in CT

2019-04-24 Thread Andrey via Digitalmars-d-learn
Hi all, Do you know can we detect that some argument of a function can be handled in CT? For example: int value1 = 10; someFunction(value1); int value2 = getValueFromUserInput(); someFunction(value2); void someFunction(int arg) { static if(argCanBeHandledInCT(arg)) { // perform

Make partial alias of template function

2019-04-23 Thread Andrey via Digitalmars-d-learn
Hello, I want to make partial alias of template function "format": void qaz(alias tmp, Values...)() { alias message = format!tmp; // ... enum v = message(Values); } void main() { qaz!("test %s!", "Qwerty"); } But I get this: Error: static assert:

Inherit enum members

2019-04-21 Thread Andrey via Digitalmars-d-learn
Hello, I have got 2 enums. How to inherit one enum from another? enum Key : string { K1 = "qwerty", K2 = "asdfgh" } enum ExtendedKey : Key { E1 = "q1", E2 = "w2", E3 = "e3" } Result: onlineapp.d(27): Error: cannot implicitly convert expression "q1" of type string to Key

Re: How to mixin finction name?

2019-04-14 Thread Andrey via Digitalmars-d-learn
On Sunday, 14 April 2019 at 11:44:16 UTC, Boris Carvajal wrote: On Sunday, 14 April 2019 at 10:07:30 UTC, Andrey wrote: Create some function in loop and use it. But I don't know how to mixin names? import std.stdio; void main() { enum letters = ['A', 'B', 'C']; static foreach(ch;

How to mixin finction name?

2019-04-14 Thread Andrey via Digitalmars-d-learn
Hi, I want to do something like this: void main() { enum letters = ['A', 'B', 'C']; static foreach(ch; letter) { void mixin("print" ~ ch)(uint i) { writeln(ch, " - ", i); } } printB(6); } Create some function in loop and use it.

Access outer member of struct from inner struct

2019-04-02 Thread Andrey via Digitalmars-d-learn
Hello, In this example how can I access the members "read" and "q" of struct Outer from Inner struct? struct Outer { ulong q = 1; Inner inner; void read(ulong value) { q += value; } void run() { q.writeln; read(5); } struct

Error: this.__lambda2 has no value

2019-04-01 Thread Andrey via Digitalmars-d-learn
Hello, My code: import std.string : join; enum Key : string { First = "qwerty", Last = "zaqy" } struct Qaz { enum text(alias Values) = Values.map!(value => "bool has" ~ value ~ " = false;").join(); } //enum text(alias Values) = Values.map!(value => "bool has" ~ value ~ " =

Pass template parameter into q{} string

2019-04-01 Thread Andrey via Digitalmars-d-learn
Hello, enum Key : string { First = "qwerty", Last = "zaqy" } void main() { enum decl(alias values1) = q{ static foreach(value; values1) mixin("bool " ~ value ~ " = false;"); }; enum qqq = [Key.First, Key.Last]; mixin(decl!qqq); } I

Wrong initialization of variables

2019-04-01 Thread Andrey via Digitalmars-d-learn
Hello, Simple code: import std.stdio; mixin template DeclFlag(alias values) { static foreach(value; values) { mixin("bool has" ~ value ~ " = false;"); } } enum Key : string { First = "qwerty", Last = "zaqy" } void main() { enum data = [Key.First, Key.Last];

Re: How to decode UTF-8 text?

2019-03-29 Thread Andrey via Digitalmars-d-learn
On Wednesday, 27 March 2019 at 19:16:21 UTC, kdevel wrote: On Wednesday, 27 March 2019 at 13:39:07 UTC, Andrey wrote: Thank you!

How to decode UTF-8 text?

2019-03-27 Thread Andrey via Digitalmars-d-learn
Hello, I have got some text with UTF-8. For example this part: Παράλληλη αναζήτηση How to decode it to get this result? Παράλληλη αναζήτηση I have tried functions like "decode", "byUTF", "to!wchar"... but no success. Input string is correct - checked it with

Build AA from two simple array

2019-03-23 Thread Andrey via Digitalmars-d-learn
Hello, I have got 2 simple arrays with the same length: int[] values = [1, 2, 3, 4, 5]; char[] keys = ['a', 'b', 'c', 'd', 'e']; auto result = buildAA(keys, values); // [a: 1, b: 2, c: 3, d: 4, e: 5] I want to build AA "result" using "values" and "keys". How to do it?

How to break from parallel foreach?

2019-02-26 Thread Andrey via Digitalmars-d-learn
Hello, How to break from parallel foreach? More general question - how to control such loop?

Re: Creating fixed array on stack

2019-01-12 Thread Andrey via Digitalmars-d-learn
On Friday, 11 January 2019 at 15:23:08 UTC, Dgame wrote: On Friday, 11 January 2019 at 14:46:36 UTC, Andrey wrote: Hi, In C++ you can create a fixed array on stack: int count = getCount(); int myarray[count]; In D the "count" is part of type and must be known at CT but in example it is RT.

Creating fixed array on stack

2019-01-11 Thread Andrey via Digitalmars-d-learn
Hi, In C++ you can create a fixed array on stack: int count = getCount(); int myarray[count]; In D the "count" is part of type and must be known at CT but in example it is RT. How to do such thing in D? Without using of heap.

Filter AA at compile time

2018-12-26 Thread Andrey via Digitalmars-d-learn
Hi, I want to filter AA at compile time and do this: void main() { // datamap is some AA enum qaz = "qq"; enum types = datamap.byKeyValue.filter!(pair => qaz.isGood(pair)).assocArray(); types.writeln; } But compiler says: Error: _aaRange cannot be interpreted at compile

Mixin operator 'if' directly

2018-12-19 Thread Andrey via Digitalmars-d-learn
Hi, Here is a template mixin: mixin template create(alias input, uint index, alias data) { if(input.length < index) return; // ... some code } When I try to compile it, I get: Error: declaration expected, not if Is it possible to mixin operator 'if' directly inside my template

chunkBy array at compile time

2018-12-19 Thread Andrey via Digitalmars-d-learn
Hi, I have got this code: import std.array : array; import std.algorithm.mutation; import std.algorithm.iteration; import std.stdio; void main() { string input = "sieviaghp"; enum data = ["emo", "emoze", "emow", "emuo", "evuo", "ete", "ie", "vuo", "sie", "w"]; enum index = 3;

Re: Reverse and sort array elements

2018-12-18 Thread Andrey via Digitalmars-d-learn
On Tuesday, 18 December 2018 at 12:32:35 UTC, angel wrote: On Tuesday, 18 December 2018 at 12:07:37 UTC, Andrey wrote: Thank you everybody. Here was another problem that local variable 'array' shadows function 'array()' from std.array.

Reverse and sort array elements

2018-12-18 Thread Andrey via Digitalmars-d-learn
Hi, Have array: enum array = ["qwerty", "a", "baz"]; Need to reverse and sort array elements to get this result: [a, ytrewq, zab] Did this: enum result = array.map!(value => value.retro()).sort(); Got: Error: template std.algorithm.sorting.sort cannot deduce function from argument types

Initialize static array without explicit length

2018-12-03 Thread Andrey via Digitalmars-d-learn
Hi, I want to create a static array and immediately init it with values: uint[x] data = [1,3,10,44,0,5000]; I don't want to set the length of it explicitly (x in square brackets). I want that compiler itself counted number of values (in example it is 6). What should be a right

Re: How to pass -J switch to compiler via DUB?

2018-11-30 Thread Andrey via Digitalmars-d-learn
On Friday, 30 November 2018 at 11:21:04 UTC, fghost wrote: On Friday, 30 November 2018 at 11:05:26 UTC, Andrey wrote: Hi, How to pass -J switch to compiler via DUB? I want to import some text file at compile time: string data = import("vertex.glsl"); In dub.json: "dflags": [

How to pass -J switch to compiler via DUB?

2018-11-30 Thread Andrey via Digitalmars-d-learn
Hi, How to pass -J switch to compiler via DUB? I want to import some text file at compile time: string data = import("vertex.glsl"); In dub.json: "dflags": [ "-J=vertex.glsl" ] The file itself is located on the same level as "dub.json".

Re: Cannot build project due to Derelict package

2018-11-26 Thread Andrey via Digitalmars-d-learn
On Monday, 26 November 2018 at 12:11:03 UTC, Alex wrote: On Monday, 26 November 2018 at 11:57:40 UTC, Andrey wrote: Hello, I try to build my project using command "dub build" but I can\t because there is an error: Fetching derelict-util 3.0.0-beta.2 (getting selected version)... SSL connect

Cannot build project due to Derelict package

2018-11-26 Thread Andrey via Digitalmars-d-learn
Hello, I try to build my project using command "dub build" but I can\t because there is an error: Fetching derelict-util 3.0.0-beta.2 (getting selected version)... SSL connect error on handle 1F19AC0 And this happens every time... As I understand dub can't find package. In dub.json I have

Pass lambda into template

2018-09-03 Thread Andrey via Digitalmars-d-learn
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] == '?'", "||")` cannot use local __lambda1 as parameter to non-global

Re: Can't print enum values

2018-08-31 Thread Andrey via Digitalmars-d-learn
On Friday, 31 August 2018 at 12:21:48 UTC, aliak wrote: auto ToUnderlyingType(alias a)() { return cast(OriginalType!(typeof(a)))a; } void print(T...)(T args) { writeln(staticMap!(ToUnderlyingType, args)); } Oohhh. So easy! Killed 2 days - and templates and mixins tried... And the

Re: Can't print enum values

2018-08-31 Thread Andrey via Digitalmars-d-learn
On Thursday, 30 August 2018 at 12:04:26 UTC, vit wrote: 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

Re: Can't print enum values

2018-08-30 Thread Andrey via Digitalmars-d-learn
On Thursday, 30 August 2018 at 11:09:40 UTC, vit wrote: args are runtime arguments. import std.experimental.all; enum MyEnum : string { First = "F_i_r_s_t", Second = "S_e_c_o_n_d" } ///alias QW(alias arg) = Alias!(cast(OriginalType!(typeof(arg)))arg); auto QW(T)(const auto ref T x){

Re: Static foreach internal variable

2018-08-30 Thread Andrey via Digitalmars-d-learn
On Thursday, 30 August 2018 at 09:49:15 UTC, drug wrote: 30.08.2018 11:19, Andrey пишет: Thanks everybody. Works!

Can't print enum values

2018-08-30 Thread Andrey via Digitalmars-d-learn
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) = Alias!(cast(OriginalType!(typeof(arg)))arg); void print(T...)(T args) {

Static foreach internal variable

2018-08-30 Thread Andrey via Digitalmars-d-learn
Hello, is it possible to declare an internal variable in "static foreach" and on each iteration assign something to it? Example: static foreach(arg; SomeAliasSeq) { internal = arg[0].converted;// a shortcut for expression "arg[0].converted" static if(internal.length == 0) { ... }

Re: Create constraint for each parameter in template arg pack

2018-08-28 Thread Andrey via Digitalmars-d-learn
On Tuesday, 28 August 2018 at 13:05:15 UTC, bauss wrote: I'm not sure if there is a better way, but isInstanceOf (std.traits) seems to work with a static foreach and a static if. template Qwerty(Values...) { static foreach (value; Values) { static if (!isInstanceOf!(Qaz,

Create constraint for each parameter in template arg pack

2018-08-28 Thread Andrey via Digitalmars-d-learn
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 values of "type" or "Data" in "Qaz". How to do it in D?

Re: How to mixin repeated text?

2018-08-27 Thread Andrey via Digitalmars-d-learn
On Monday, 27 August 2018 at 11:56:08 UTC, Kamil Koczurek wrote: Mixins seem to be an overkill here. Maybe something like this would suffice: data[index + 1 .. index + 5].map!(k => k[0]).array == "" Here there is dynamic code, with memory allocs. I found solution:

How to mixin repeated text?

2018-08-27 Thread Andrey via Digitalmars-d-learn
Hello again, I have this part of code: ... if(index + 3 >= data.length || data[index + 1][0] == '&' || data[index + 2][0] == '&' || data[index + 3][0] == '&' || data[index + 4][0] == '&') { writeln("Some text..."); } I don't want to write manually these four "or" conditions because in

How to pass alias as template parameter?

2018-08-27 Thread Andrey via Digitalmars-d-learn
Hello, This code doesn't compile: --- import std.meta; import std.stdio; enum Option : string { First = "-first" , Second = "-second", Qwerty = "-qwerty" } void handler(Option option)(string[] args, ref ushort

Get max elemenr over RegexMatch

2018-08-24 Thread Andrey via Digitalmars-d-learn
Hello, This code produces an error: auto matches = content.matchAll(pattern); auto max = matches.maxElement!"a => a.back.to!uint"(); I have a RegexMatch array like: [["text1234", "1234"], ["zxs432fff", "432"], ["text000_", "000"]] Max element here is 1234. I apply map function "a =>

Test if variable has void value

2018-08-22 Thread Andrey via Digitalmars-d-learn
Hello, How to test if variable has void value? string text = void; if(text == void) { writeln("Is void"); } Tried this: if(is(text == void)) but doesn't work.

How to map elements of a tuple?

2018-08-22 Thread Andrey via Digitalmars-d-learn
Hello, Is there a template/function/mixin... in the library that I can use to map elements of a tuple? object.foo(Mapper!myMapFunction(1, bool, "Qwerty", EnumedColor.Red)); where "Mapper" is this mapper and "myMapFunction" is a template function that I want to apply to each member in

[Unit tests] Mocking D objects

2018-08-22 Thread Andrey via Digitalmars-d-learn
Hello, I know that D has build-in unit tests. If so, what mechanism D provides for mocking objects? For example: struct WebParser { // ... int download(string path) { SomeHttpClient client(path); auto result = client.request(path, 10, "Qwerty"); // ...

Re: Coreect way to create delegate for struct method.

2018-08-22 Thread Andrey via Digitalmars-d-learn
On Tuesday, 21 August 2018 at 22:52:31 UTC, Alex wrote: Maybe, like this: Thank you but here you use heap to create ab object. I want only on stack. I know that one can do this: test_handler.ptr = null; and in place of call this: handler.ptr = cast(void*) but it is ugly... Hmm, any

Coreect way to create delegate for struct method.

2018-08-21 Thread Andrey via Digitalmars-d-learn
Hello, This is a code: import std.stdio; struct Test { static Test opCall() { Test test; test.handler = return test; } void one() const { writeln("In handler: Address = ", , "; Text = ", text); } void execute() { text = "Inited!";

Cast to original type each argument in template pack

2018-08-21 Thread Andrey via Digitalmars-d-learn
Hello, I have a function: string format(string pattern, T...)(T value) { auto writer = appender!string(); writer.formattedWrite!pattern(convertToUnderlyingType(value)); //Tuple!T(value).expand.to!(OriginalType!T) return writer.data; } The "value" in this function can be any type

Re: Deduce type of struct in function arguments

2018-08-21 Thread Andrey via Digitalmars-d-learn
On Monday, 20 August 2018 at 17:45:25 UTC, Seb wrote: ... yet. Though you can vote for this DIP and show your support there: https://github.com/dlang/DIPs/pull/71 It even comes with an implementation in DMD already: https://github.com/dlang/dmd/pull/8460 How and where to vote?

Re: Make function alias

2018-08-20 Thread Andrey via Digitalmars-d-learn
On Monday, 20 August 2018 at 13:35:07 UTC, ag0aep6g wrote: On 08/20/2018 03:14 PM, Andrey wrote: Thanks everybody for your answers.

Re: Make function alias

2018-08-20 Thread Andrey via Digitalmars-d-learn
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(); }

Make function alias

2018-08-20 Thread Andrey via Digitalmars-d-learn
Hello, I want to make an alias to function "std.stdio.writeln" and "std.stdio.write" and use it like: static void log(bool newline = true)(string text) { alias print(T...) = newline ? : _file.print(); text.print(); } Unfortunately, it doesn't work... Also tried with "enum print

Re: Deduce type of struct in function arguments

2018-08-20 Thread Andrey via Digitalmars-d-learn
On Monday, 20 August 2018 at 11:38:39 UTC, Paul Backus wrote: Create an overload of foo that takes two arguments and combines them into a `Data` struct internally: void foo(int a, string text) { Data data = {a, text}; foo(data); } Hmm, not very good solution. In C++ you can not to

Re: ushort + ushort = int?

2018-08-20 Thread Andrey via Digitalmars-d-learn
On Monday, 20 August 2018 at 09:56:13 UTC, Jonathan M Davis wrote: It's a combination of keeping the C semantics (in general, C code is valid D code with the same semantics, or it won't compile) and the fact that D requires casts for narrowing conversions. When you add two shorts in C/C++, it

Deduce type of struct in function arguments

2018-08-20 Thread Andrey via Digitalmars-d-learn
Hello, I have a function and a struct: void foo(ref Data data) { ... } struct Data { int a; string text; } How to pass struct into function without naming its type? This doesn't work: foo({1234, "Hello!"});

Re: ushort + ushort = int?

2018-08-20 Thread Andrey via Digitalmars-d-learn
On Monday, 20 August 2018 at 08:49:00 UTC, rikki cattermole wrote: Yes. On x86 int's will be faster just an FYI so it does make sense to use them for computation. Inconveniently always use casts. Why in D one decided to do in such way?

Re: ushort + ushort = int?

2018-08-20 Thread Andrey via Digitalmars-d-learn
On Monday, 20 August 2018 at 08:42:20 UTC, rikki cattermole wrote: It's called integer promotion and it originates from C. And yes C++ does have such support in some variant (I really don't feel like comparing the two). And I should do? Always use "cast" operator when I operate not with

ushort + ushort = int?

2018-08-20 Thread Andrey via Digitalmars-d-learn
Hello, Here is a code that you can execute using online compiler https://run.dlang.io/: import std.stdio; void main() { ushort first = 5; ushort second = 1000; ushort result = first + second; writeln(result); } I hae this error: onlineapp.d(7): Error: cannot implicitly convert

Set optional function parameter

2018-08-17 Thread Andrey via Digitalmars-d-learn
Hello, In D there is a nice function: auto Tuple!(int,"status",string,"output") executeShell ( scope const(char)[] command, const(string[string]) env = cast(const(string[string]))null, Config config = cast(Config)0, ulong maxOutput = 18446744073709551615LU, scope const(char)[] workDir =

Convert output of map() to array of strings

2018-08-15 Thread Andrey via Digitalmars-d-learn
Hello, I have the following code: string[] list; string text; // ... enum pattern = ctRegex!`^[0-9]+$`; list = text.split('\n').map!(line => line.matchFirst(pattern).hit); Compiler says that it can't convert result of map function to string[]... What I want: 1. Split some text into lines

Re: Concat enum of strings into one string

2018-08-15 Thread Andrey via Digitalmars-d-learn
On Tuesday, 14 August 2018 at 16:03:05 UTC, vit wrote: import std.traits : EnumMembers; import std.string : join; import std.algorithm : map; pragma(msg, [EnumMembers!Type].map!(x => cast(string)x).join(" ")); Thank you! Jonathan M Davis, I understood.

Re: Concat enum of strings into one string

2018-08-14 Thread Andrey via Digitalmars-d-learn
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 foreach (member; EnumMembers!Enum) enumValues

Re: Concat enum of strings into one string

2018-08-14 Thread Andrey via Digitalmars-d-learn
On Tuesday, 14 August 2018 at 13:45:48 UTC, Mike Franklin wrote: I think you just need to use the concatenation operator `~`. enum Type : string { One = "Q1", Two = "W2", Three = "R3" } enum concatenation = Type.One ~ " " ~ Type.Two ~ " " ~ Type.Three; void main() { import

Concat enum of strings into one string

2018-08-14 Thread Andrey via Digitalmars-d-learn
Hello, I have a enum: enum Type : string { One = "Q1", Two = "W2", Three = "R3" } I want to concat it in compile-time: enum result = doConcat!Type(); And get this result: writeln(result); // output: "Q1 W2 R3" Delimiter here is space symbol. How do do it?

Re: Importing struct

2018-08-14 Thread Andrey via Digitalmars-d-learn
On Monday, 13 August 2018 at 14:30:36 UTC, Timoses wrote: On Monday, 13 August 2018 at 14:16:47 UTC, Mike Parker wrote: On Monday, 13 August 2018 at 13:09:24 UTC, Andrey wrote: [...] The convention is to use lowercase for the module name: module myclass; struct MyClass {} Using lowercase

Re: How to declare static compile-time assoc array inside struct?

2018-08-13 Thread Andrey via Digitalmars-d-learn
On Monday, 13 August 2018 at 11:53:06 UTC, rikki cattermole wrote: You must use a module constructor to initialize it. Tried this: static this() { Test.DESCRIPTION = [Test.Type.One: "One!", Test.Type.Two: "It's Two...", Test.Type.Three: "And... Three!"]; } struct Test { // ... } I

Re: Importing struct

2018-08-13 Thread Andrey via Digitalmars-d-learn
On Monday, 13 August 2018 at 13:05:28 UTC, evilrat wrote: however the best option is simply avoid naming anything with same name as module. Hmm, I thought that name of class should match name of file... And how to name a file that contains only one class/struct? Like in my case. What usually

Importing struct

2018-08-13 Thread Andrey via Digitalmars-d-learn
Hello, This is my test project: source/app.d source/MyClass.d app.d: import std.stdio; import MyClass; void main(string[] args) { MyClass.MyClass.parse(args); // I want just MyClass.parse(args); }

Re: How to declare static compile-time assoc array inside struct?

2018-08-13 Thread Andrey via Digitalmars-d-learn
On Monday, 13 August 2018 at 11:53:06 UTC, rikki cattermole wrote: Unsupported. AA's don't go between CT and RT. You must use a module constructor to initialize it. Will be supported in future?

How to declare static compile-time assoc array inside struct?

2018-08-13 Thread Andrey via Digitalmars-d-learn
Hello, I need to declare a static compile-time assoc array inside struct: struct Test { enum Type : ubyte { One, Two, Three } static immutable string[Type] DESCRIPTION = [Type.One: "One!", Type.Two: "It's Two...",

  1   2   >