Re: What is the difference between enum and shared immutable?

2020-10-29 Thread Ali Çehreli via Digitalmars-d-learn
On 10/29/20 10:16 AM, H. S. Teoh wrote: > Module-level immutable can be initialized either as part of the > declaration: > >immutable int x = 1; To add, the expression can be a call to a function that need not return immutable, as long as it is pure, which can be inferred by the compiler

Re: What is the difference between enum and shared immutable?

2020-10-29 Thread Ali Çehreli via Digitalmars-d-learn
On 10/28/20 3:07 PM, H. S. Teoh wrote: A shared immutable is initialized at compile-time, To prevent a misunderstanding, immutable can be initialized at run time as well. On the other hand, immutable initialized at compile time was surprising to me when I learned it recently: import std;

Re: What is the difference between enum and shared immutable?

2020-10-29 Thread Ali Çehreli via Digitalmars-d-learn
On 10/28/20 5:55 PM, matheus wrote: On Wednesday, 28 October 2020 at 22:07:06 UTC, H. S. Teoh wrote: ... (This is why it's a bad idea to use enum with an array literal, because every time it's referenced you get a new copy of the array.) ... Could you please give an example (Snippet) about

Re: Print int[string] sorted by Value

2020-10-28 Thread Ali Çehreli via Digitalmars-d-learn
On 10/28/20 9:30 AM, Paul wrote: > On Wednesday, 28 October 2020 at 15:40:23 UTC, aberba wrote: >> Have you tries .values() function? dictionary.values.sort() > > Thanks aberba. Yes, that was my first attempt! > > If my terminology is correct that gives me a "range" of sorted VALUES. No, both

Re: Unexpected behaviour using remove on char[]

2020-10-25 Thread Ali Çehreli via Digitalmars-d-learn
On 10/25/20 12:29 PM, IGotD- wrote: What I discovered is that remove doesn't really remove size number of bytes but also removed entire multibyte characters and consider that one step. The result was of course that I got out of bounds exceptions as it went past the end. This is the infamous

Re: Template pattern delegate?

2020-10-25 Thread Ali Çehreli via Digitalmars-d-learn
On 10/25/20 4:30 AM, frame wrote: Is there a possibility to write templated code / custom trait pattern with usage like a delegate? I have a try-catch block with different types and don't want to repeat myself in every method again. It's always the same, just what's tried changes, eg.:

Re: toStringz lifetime

2020-10-25 Thread Ali Çehreli via Digitalmars-d-learn
On 10/25/20 3:19 AM, rikki cattermole wrote: On 25/10/2020 11:03 PM, Ali Çehreli wrote: Does the GC see that local variable 'name' that is on the C side? What I don't know is whether the GC is aware only of the stack frames of D functions or the entire thread, which would include the C

toStringz lifetime

2020-10-25 Thread Ali Çehreli via Digitalmars-d-learn
toStringz documentation is clear on why, when, and how to extend the lifetime of a D string: https://dlang.org/phobos/std_string.html#.toStringz Assume foo is a D library function that passes a "string" result to e.g. C: extern(C) void foo(ref const(char) * name) { name =

Re: Two ways of receiving arrays on the C ABI

2020-10-20 Thread Ali Çehreli via Digitalmars-d-learn
On 10/19/20 6:28 PM, Nicholas Wilson wrote:> On Tuesday, 20 October 2020 at 00:16:48 UTC, Ali Çehreli wrote: >> On the D side, both of the following extern(C) functions take the same >> arguments. > > https://github.com/dlang/dmd/pull/8120 > > there are issues with structs. Not sure about

Two ways of receiving arrays on the C ABI

2020-10-19 Thread Ali Çehreli via Digitalmars-d-learn
On the D side, both of the following extern(C) functions take the same arguments. 1) func1 takes .length and .ptr implicitly as parts of a D array: extern(C) void func1(int[] arr) { assert(arr.equal(3.iota)); } 2) func2 takes .length and .ptr separately and then makes a slice explicitly:

Re: Forward referencing functions in D

2020-10-17 Thread Ali Çehreli via Digitalmars-d-learn
On 10/17/20 8:28 AM, NonNull wrote: On Friday, 16 October 2020 at 21:28:18 UTC, Steven Schveighoffer wrote: Inner functions have benefits: 1. They are only accessible inside the function. Which means you only have to worry about correctness while INSIDE that function. 2. inner functions have

Re: why do i need an extern(C): here?

2020-10-16 Thread Ali Çehreli via Digitalmars-d-learn
On 10/16/20 4:12 PM, WhatMeWorry wrote: > Isn't dlopen() for Linux More like dlopen() is for Posix, which means it should be available on Windows as well. However, as I've discovered, a D shared library cannot be loaded with dlopen() because the main program and the shared library would do

Re: How can I convert Hexadecimal to RGB Color and vice-versa?

2020-10-16 Thread Ali Çehreli via Digitalmars-d-learn
On 10/16/20 1:10 PM, Marcone wrote: How can I convert Hexadecimal to RGB Color and vice-versa? On 10/16/20 1:10 PM, Marcone wrote: > How can I convert Hexadecimal to RGB Color and vice-versa? Do you mean from a string like "#123456" to the values of red, green, and blue? I am having more

Re: Forward referencing functions in D

2020-10-16 Thread Ali Çehreli via Digitalmars-d-learn
On 10/16/20 1:47 PM, wilcro wrote: > would > there be any reason to avoid placing the majority of code for a program > outside of the main function? Keeping scopes of symbols as small as possible is a general guideline in D and elsewhere but I wouldn't crowd my main() function with details of

Re: Packing of Struct Fields

2020-10-16 Thread Ali Çehreli via Digitalmars-d-learn
On 10/16/20 1:32 PM, Per Nordlöw wrote: Why is `T.sizeof` 12 instead of 8 when `U.sizeof` is 8 in the following example? struct S {     int i;     bool b; } struct T {     S s;     char c; } struct U {     int i;     bool b;     char c; } ? I have a function that dumps member

Re: Struct field destructor not called when exception is thrown in the main struct destructor

2020-10-16 Thread Ali Çehreli via Digitalmars-d-learn
On 10/16/20 9:05 AM, Steven Schveighoffer wrote: > The destruction of members is outside the destructor's purview. It can't > turn the destruction off, so it should logically be considered part of > an enclosing function. Thank you. Makes sense. Ali

Re: why do i need an extern(C): here?

2020-10-16 Thread Ali Çehreli via Digitalmars-d-learn
On 10/15/20 2:42 PM, Ali Çehreli wrote: > I've recently done the same by calling dlopen() and dlsym() > directly. Runtime.loadLibrary documentation says "If the library > contains a D runtime it will be integrated with the current runtime." > That would explain why my program seg-faults for my

Re: Struct field destructor not called when exception is thrown in the main struct destructor

2020-10-16 Thread Ali Çehreli via Digitalmars-d-learn
On 10/16/20 6:12 AM, tchaloupka wrote: > struct Foo { > Bar bar; > bool err; > > ~this() { > // scope(failure) destroy(bar); // < this fixes the Bar > destructor call > enforce(!err, "Test err"); Well, that check means "cannot continue", which means the compiler

Re: why do i need an extern(C): here?

2020-10-15 Thread Ali Çehreli via Digitalmars-d-learn
On 10/15/20 2:29 PM, WhatMeWorry wrote: > name wrangling? Name mangling. :) I don't know the answer but I can hijack your thread. > import core.runtime; > auto mydll = Runtime.loadLibrary("mydll.dll"); Interesting. I've recently done the same by calling dlopen() and dlsym()

Re: malloc(s)[0..s] vs cast(T)malloc(s)

2020-10-14 Thread Ali Çehreli via Digitalmars-d-learn
On 10/14/20 1:15 PM, Jack wrote: >> auto x = malloc(s)[0..s]; > https://wiki.dlang.org/Memory_Management#Explicit_Class_Instance_Allocation Note that 'x' is passed to emplace() at that link and emplace() requires a slice. That's why the a slice is made from the pointer returned by

Re: Passing pointer to extern(C++) templated function

2020-10-13 Thread Ali Çehreli via Digitalmars-d-learn
On 10/13/20 4:11 PM, James Blachly wrote: On 10/13/20 5:23 AM, Jamie wrote: Building with: g++ -c a.cpp dmd main.d a.o Throws the error: /usr/bin/ld: main.o: in function `_Dmain': main.d:(.text._Dmain[_Dmain]+0x31): undefined reference to `void func3(int*, int*)' /usr/bin/ld:

Re: List of exceptions?

2020-10-12 Thread Ali Çehreli via Digitalmars-d-learn
On 10/12/20 2:11 AM, Dominikus Dittes Scherkl wrote: > - Throw exceptions only if you have a plan what to do with them if you > catch them. My thinking is different: Throw exceptions if you can't accomplish a task. My code is filled with enforce() and assert() checks, which do throw

Re: Renaming Flag!"" in API

2020-10-12 Thread Ali Çehreli via Digitalmars-d-learn
It's amazing how things come together before each conference. Flag appears among my slides for an upcoming conference as well! :) But I don't think there is any solution to your problem. On 10/12/20 3:24 AM, FreeSlave wrote: > Later I realize that 'myflagname' is a bad name and I want to

Re: Range format specifiers in other languages?

2020-10-11 Thread Ali Çehreli via Digitalmars-d-learn
On 10/11/20 5:44 PM, Max Haughton wrote: > Possibly worth showing off (especially given that some people at first > don't even know the templated format string exists) This feature is already among my slides for an upcoming conference. ;) Ali

Range format specifiers in other languages?

2020-10-11 Thread Ali Çehreli via Digitalmars-d-learn
I find D's %( and %) range format specifiers very useful: import std.stdio; import std.range; void main() { 5.iota.writefln!"%(%s, %)"; // Prints 0, 1, 2, 3, 4 } Are there similar features in other languages? Thank you, Ali

Re: List of exceptions?

2020-10-10 Thread Ali Çehreli via Digitalmars-d-learn
On 10/10/20 12:51 PM, DMon wrote: > I will copy that down. > > The idea for specific exceptions came from the online docs and > Programing in D, 39.2 The try-catch statemet > > try > { // the code block that is being executed, where an // exception may be > thrown > } > catch (an_exception_type)

Re: List of exceptions?

2020-10-10 Thread Ali Çehreli via Digitalmars-d-learn
On 10/10/20 9:16 AM, DMon wrote: > catch (Exception e) // implicit (any exception) > catch (ConvException f) // explicit (conversion only) > > Or is that not correct? I think in class hierarchies, "more general" and "more specific" are better terms. :) The answer is, catch by the most

Re: List of exceptions?

2020-10-10 Thread Ali Çehreli via Digitalmars-d-learn
On 10/10/20 8:46 AM, DMon wrote: On Saturday, 10 October 2020 at 14:56:31 UTC, Ali Çehreli wrote: On 10/10/20 5:12 AM, DMon wrote: Is there a list of a list of the exceptions or what can be used with catch? Only Throwable and classes that are derived from it can be thrown and caught. Ali

Re: List of exceptions?

2020-10-10 Thread Ali Çehreli via Digitalmars-d-learn
On 10/10/20 5:12 AM, DMon wrote: Is there a list of a list of the exceptions or what can be used with catch? I'm thinking that I missed it and there is something easier than breaking old code, scouring the site, or hypnotic regression. Only Throwable and classes that are derived from it can

Re: Efficient sort function allowing own test and swap function as parameter

2020-10-06 Thread Ali Çehreli via Digitalmars-d-learn
On 10/6/20 3:18 PM, Alaindevos wrote: I have a large table consisting of two columns.One with words.Another with frequencies. I want to sort them efficiently according to the names or frequency. For this I need an efficient sort function where I can plugin my proper test of order, and proper

Re: It is possible to substract 5 from 3 unsigned integer

2020-10-06 Thread Ali Çehreli via Digitalmars-d-learn
On 10/6/20 5:24 AM, Alaindevos wrote: Is that the expected behavior of the programmer? Opinions can differ. Feel free to elaborate. The following is even more "expected". ;) Subtract zero from -1 and you get size_t.max. void main() { int[] arr; int i = -1; auto u = (i - arr.length);

Re: Deprecation in traits

2020-09-29 Thread Ali Çehreli via Digitalmars-d-learn
On 9/29/20 10:08 AM, Frak wrote: Hi folks, I've this: /Users/frak/dlang/ldc-1.23.0/bin/../import/std/traits.d(3711): Deprecation: function `std.typecons.Nullable!long.Nullable.get_` is deprecated - Implicit conversion with `alias Nullable.get this` will be removed after 2.096. Please use

Re: Memory management

2020-09-29 Thread Ali Çehreli via Digitalmars-d-learn
On 9/29/20 3:57 AM, novice3 wrote:> Naive newbie question: > > Can we have (in theory) in D lang memory management like V lang? > > Quote: > https://github.com/vlang/v/blob/master/doc/docs.md#memory-management > > "V doesn't use garbage collection or reference counting. The compiler > cleans

Re: How does alias exactly work

2020-09-28 Thread Ali Çehreli via Digitalmars-d-learn
On 9/28/20 6:46 PM, Ruby The Roobster wrote: I thought alias could work like this with classes: That would work with template parameters: alias A = Foo!(3, "hello"); alias test = MyClass(3,"H",9.1); //Assume the constructor parameters for MyClass are (int,string,double). Can anybody fix

Re: How to hide a function return type in order to wrap several functions into an associated array?

2020-09-27 Thread Ali Çehreli via Digitalmars-d-learn
On 9/27/20 11:54 AM, tastyminerals wrote: > I have a collection of functions that all have the same input, a string. > The output however is different and depending on what the function does > it can be ulong, double or bool. The following approach overcomes the different return type issue by

Re: A scheduled control signal with fibers?

2020-09-27 Thread Ali Çehreli via Digitalmars-d-learn
On 9/27/20 6:33 AM, Ferhat Kurtulmuş wrote: > On Sunday, 27 September 2020 at 12:05:13 UTC, Ferhat Kurtulmuş wrote: >> On Sunday, 27 September 2020 at 10:40:25 UTC, Ali Çehreli wrote: >>> On 9/27/20 3:06 AM, Ferhat Kurtulmuş wrote: > > Oh, It will work fine if I imitate my time-consuming image

Re: A scheduled control signal with fibers?

2020-09-27 Thread Ali Çehreli via Digitalmars-d-learn
On 9/27/20 3:06 AM, Ferhat Kurtulmuş wrote: > __gshared DList!Entry queue; > __gshared bool shouldRun = true; Have you considered passing messages with std.concurrency.send() and std.concurrency.receive() and friends? You wouldn't need 'queue' because all of your threads already have mail

Re: Timeout around function call

2020-09-23 Thread Ali Çehreli via Digitalmars-d-learn
On 9/23/20 1:19 PM, Imperatorn wrote: > No. You should not share anything. Personally I would just send a > message to request termination or use the solution provided with timeout. std.concurrency does not allow "mutable thread-local data"; so one needs to cast to shared (assuming copying is

Re: Array of Algebraic argument syntax

2020-09-22 Thread Ali Çehreli via Digitalmars-d-learn
On 9/22/20 2:53 PM, Kasra Sadeghi wrote: On Tuesday, 22 September 2020 at 21:36:48 UTC, Ali Çehreli wrote: ... alias Value = Algebraic!(int, double, string, None); ... void main() {   printValue([Value(4.5), Value("hello"), Value(42)]); } Thanks! Wish there was a less redundant syntax for the

Re: Timeout around function call

2020-09-22 Thread Ali Çehreli via Digitalmars-d-learn
On 9/22/20 2:32 AM, drathier wrote:> What's the obvious way to put a timeout around a function call? I'm > thinking a 5 or 30 second timeout, and I'm expecting it to pretty much > never time out. I would start a thread and use receiveTimeout(): import std.concurrency; import std.stdio; import

Re: Array of Algebraic argument syntax

2020-09-22 Thread Ali Çehreli via Digitalmars-d-learn
On 9/22/20 2:30 PM, Kasra Sadeghi wrote: Hi everyone! What's the syntax for passing an array of Algebraics? definition:  class None {}  class Value = Algebraic!(int, double, string, None); That should be 'alias' instead of 'class': import std.variant; import std.stdio; class None {}

Re: QuickSort on ranges

2020-09-12 Thread Ali Çehreli via Digitalmars-d-learn
On 9/12/20 11:25 AM, jerome wrote: > > import std.stdio : writeln; > import std.algorithm.sorting; > > pure void quickSort(T) (T[] r) > { >if (r.length > 1) >{ > size_t p = pivotPartition(r, r.length-1); //r[$-1] is swapped to r[p] > >

Re: how to do this meta-programming? print the address of random element's address of a variable length of arrays?

2020-09-11 Thread Ali Çehreli via Digitalmars-d-learn
On 9/11/20 6:44 PM, mw wrote:> e.g. > > int[] a = new int[la]; > int[] b = new int[lb]; > int[] c = new int[lc]; > int[] d = new int[ld]; > > > the func I want to write, e.g. for 2 arrays (instantiation) is like this: > > void print_random_elem_addr(int[] x, int[] y) { >auto i =

Reducing .init effect of a struct that has large static array members

2020-09-10 Thread Ali Çehreli via Digitalmars-d-learn
[tldr; I have come up with a way of removing all undesired effects of large static array struct members. See conclusion at the bottom.] Continuing the discussion at https://forum.dlang.org/thread/rdk3m2$725$1...@digitalmars.com and understanding kinke's comments there better... And this is

Re: how stdin stream works?

2020-08-20 Thread Ali Çehreli via Digitalmars-d-learn
On 8/19/20 11:46 AM, Flade wrote: Try instead getting a line via readln, and then trying to read that into your expected input. -Steve Thanks Steve! I will get the input a string then as you said and then I'll try to convert it! Thanks a lot, have a nice day! In some cases clearerr() and

Re: Disjoint slices of an array as reference

2020-08-20 Thread Ali Çehreli via Digitalmars-d-learn
On 8/19/20 9:11 PM, data pulverizer wrote: On Thursday, 20 August 2020 at 03:47:15 UTC, Paul Backus wrote: double[][] y; y ~= x[0..5]; Thanks. I might go for a design like this: ``` struct View(T){   T* data;   long[2][] ranges; } ``` The ranges are were the slices are stored and T* (maybe

Re: Disjoint slices of an array as reference

2020-08-20 Thread Ali Çehreli via Digitalmars-d-learn
On 8/19/20 7:40 PM, data pulverizer wrote: > An array in D is either two pointers or one pointer and a length (I > don't know which) It is the length, followed by the pointer, equivalent of the following struct: struct A { size_t length_; void * ptr; size_t length() { return

Re: Factory pattern for classes

2020-08-10 Thread Ali Çehreli via Digitalmars-d-learn
On 8/10/20 8:38 AM, lexxn wrote: Btw is it possible to pass a property to the constructor, if I've one declared, in the factory? I'm talking about this piece cast(A)Object.factory("deneme.A") I think you mean "parameter". No, Object.factory creates the object with its default constructor.

Re: generating random numbers

2020-08-10 Thread Ali Çehreli via Digitalmars-d-learn
On 8/9/20 10:51 PM, Andy Balba wrote: generating random numbers using https://dlang.org/library/std/random/uniform01.html I find the example given in this section totally incomprehensible ... Can any help me answer two simple questions: How to generate a random floating number in range [0,1) ?

Re: Factory pattern for classes

2020-08-09 Thread Ali Çehreli via Digitalmars-d-learn
On 8/9/20 7:27 AM, lexxn wrote: > I assume that the correct syntax for the getClassById is > Object getClassById(uint id) { > if (id == 0) { > return new A(); > } else if (id == 1) { > return new B(); > } else { > return new C(); > } > } > or maybe

Re: Non-recursive maxSizeOf

2020-08-06 Thread Ali Çehreli via Digitalmars-d-learn
On 8/6/20 4:44 AM, Per Nordlöw wrote: On Thursday, 6 August 2020 at 01:13:28 UTC, Ali Çehreli wrote: Boring in D. :p template maxSizeOf(T...) {   enum maxSizeOf = compute();   auto compute() {     size_t result;     static foreach (t; T) {   if (t.sizeof > result) {     result =

Re: Non-recursive maxSizeOf

2020-08-05 Thread Ali Çehreli via Digitalmars-d-learn
On 8/5/20 5:58 PM, Per Nordlöw wrote: Is it possible to implement template maxSizeOf(T...) {     static if (T.length == 1)     enum size_t maxSizeOf = T[0].sizeof;     else     {     enum size_t firstSize = T[0].sizeof;     enum size_t maxSizeRest = maxSizeOf!(T[1 .. $]);

Re: Question about UDAs

2020-08-03 Thread Ali Çehreli via Digitalmars-d-learn
On 8/2/20 8:00 PM, Cecil Ward wrote: > Ali Çehreli’s book mentions them briefly with an example > but that doesn’t seem to qualify as a realistic use-case. The XML example I chose there qualifies as serialization like H. S. Teoh mentions. UDAs on user-defined type members are for marking them

Re: 2-D array initialization

2020-08-02 Thread Ali Çehreli via Digitalmars-d-learn
On 8/1/20 7:00 PM, Andy Balba wrote: >> >> ubyte[3][4] c = [ [5, 5, 5], [15, 15,15], [25, 25,25], [35, >> 35,35] ]; > Although not detailed in my original question, in my actual app > I have array ubyte [1000][3] Big which consists of research data I > obtained, > and from which I want to

Re: 2-D array initialization

2020-08-01 Thread Ali Çehreli via Digitalmars-d-learn
On 8/1/20 12:57 PM, Andy Balba wrote: > On Saturday, 1 August 2020 at 00:08:33 UTC, MoonlightSentinel wrote: >> On Friday, 31 July 2020 at 23:42:45 UTC, Andy Balba wrote: >>> How does one initialize c in D ? >> >> ubyte[3][4] c = [ [5, 5, 5], [15, 15,15], [25, 25,25], [35, 35,35] ]; > I'm a D

Re: dynamic array .length vs .reserve - what's the difference?

2020-07-30 Thread Ali Çehreli via Digitalmars-d-learn
On 7/30/20 4:42 PM, wjoe wrote: > So .capacity can't be assigned a value like length to reserve the RAM ? Yes, a read-only property... >> auto a = b; >> b = b[0 .. $-1]; >> b ~= someT; >> >> If that last line is done in-place, then it overwrites a[$-1]. > > So this is a case of sharing being

Re: dynamic array .length vs .reserve - what's the difference?

2020-07-30 Thread Ali Çehreli via Digitalmars-d-learn
On 7/30/20 8:58 AM, wjoe wrote:     b.reserve(n);     b.length = n; There may be something that I don't know but I think assigning to the .length property alone should be the same as reserving and then assigning. reserve is supposed to make sure no memory will be allocated as

Re: is using predSwitch the only way to create to create recursive functions in D ?

2020-07-29 Thread Ali Çehreli via Digitalmars-d-learn
On 7/29/20 3:13 PM, Andy Balba wrote: ,, Not at all. The wording in the documentation is misleading. Recursive functions are as trivial as they are: int foo(uint i) { if (i == 0) { return 42; } return foo(i - 1); } void main() { assert(foo(7) == 42); } Ali

Re: Using D within a rust codebase

2020-07-27 Thread Ali Çehreli via Digitalmars-d-learn
On 7/27/20 4:43 AM, Ali Çehreli wrote: On 7/27/20 3:50 AM, Jacob Carlborg wrote:> On 2020-07-27 03:03, Paul > The D runtime needs to be initialized first [1]. Then it should be > terminated as well [2]. > > [1] https://dlang.org/phobos/core_runtime.html#.rt_init [...] pragma

Re: Using D within a rust codebase

2020-07-27 Thread Ali Çehreli via Digitalmars-d-learn
On 7/27/20 3:50 AM, Jacob Carlborg wrote:> On 2020-07-27 03:03, Paul Backus wrote: > >> extern(C) void hello() >> { >> import std.stdio: writeln; >> writeln("Hello from D!"); >> } > > The D runtime needs to be initialized first [1]. Then it should be > terminated as well [2]. > > [1]

Re: miscellaneous array questions...

2020-07-21 Thread Ali Çehreli via Digitalmars-d-learn
On 7/20/20 8:16 PM, a...@a.com wrote: >> 3) Lastly, In the following code snippet, is arrayA and arrayB both >> allocated on the stack? arrayA is allocated on thread-local storage and lives as long as the program is active. I guess a final interaction with it can be in a 'static ~this()' or a

Re: How can I make executeShell ask for Admin Elevation?

2020-07-12 Thread Ali Çehreli via Digitalmars-d-learn
On 7/11/20 7:10 PM, Marcone wrote: I don't want start program with admin elevation, but ask user for admin permission when some function is called. Here is a hacky solution that attempts the command and fails back to asking the password. It should work on POSIX systems. (Tested on Linux.)

Re: What's the point of static arrays ?

2020-07-10 Thread Ali Çehreli via Digitalmars-d-learn
On 7/10/20 8:03 AM, wjoe wrote: > What I'm saying is even if this allocation is slow let's say 5ms, but it > only happens once, that wouldn't matter to overall performance at all. Yes, you are correct and there are dynamic arrays that are allocated once in many programs. I haven't read the

Re: What's the point of static arrays ?

2020-07-09 Thread Ali Çehreli via Digitalmars-d-learn
On 7/9/20 5:12 AM, wjoe wrote: Considering the many downsides why would I ever want to choose a static over a dynamic array ? In addition to what others said, dynamic arrays can be more expensive both in space and time. Time: Dynamic array elements are accessed through an extra pointer

Re: Template function specialization doesn't work

2020-07-07 Thread Ali Çehreli via Digitalmars-d-learn
On 7/7/20 12:53 PM, IGotD- wrote: ubyte[3] ar = [ 1, 2, 3 ]; ubyte[] arSlice = ar; overloadedFunction(arSlice); The first function will be used. Shouldn't the template argument (T : T[]) make the compiler pick the second one? There is also template constraints which may be useful: import

Re: opApply and attributes

2020-07-07 Thread Ali Çehreli via Digitalmars-d-learn
On 7/6/20 5:20 PM, solidstate1991 wrote:> See implementation of data structure here: > https://github.com/ZILtoid1991/collections-d/blob/master/source/collections/treemap.d#L565 > > > If I try to compile this code, it'll fail, limiting it's usecase: > > @safe pure unittest { > alias

Re: I need an Easy example to understand Alias This

2020-07-06 Thread Ali Çehreli via Digitalmars-d-learn
On 7/6/20 5:44 PM, Marcone wrote: On Tuesday, 7 July 2020 at 00:42:40 UTC, Ali Çehreli wrote: On 7/6/20 5:35 PM, Marcone wrote: Hi, I study Dlang for one year, and I can't understand alias this. I need an Easy example to understand Alias This. Is the following example useful?  

Re: I need an Easy example to understand Alias This

2020-07-06 Thread Ali Çehreli via Digitalmars-d-learn
On 7/6/20 5:35 PM, Marcone wrote: Hi, I study Dlang for one year, and I can't understand alias this. I need an Easy example to understand Alias This. Is the following example useful? http://ddili.org/ders/d.en/alias_this.html Ali

Re: Generating struct .init at run time?

2020-07-02 Thread Ali Çehreli via Digitalmars-d-learn
On 7/2/20 10:51 AM, kinke wrote: On Thursday, 2 July 2020 at 16:51:52 UTC, kinke wrote: `= void` for members doesn't work and, I dare say, not work anytime soon if ever. I've quickly checked; `= void` for members has initialize-with-zeros semantics too, so with LDC, it's equivalent to `= 0`

Re: Generating struct .init at run time?

2020-07-02 Thread Ali Çehreli via Digitalmars-d-learn
On 7/2/20 3:37 AM, kinke wrote: > On Thursday, 2 July 2020 at 07:51:29 UTC, Ali Çehreli wrote: >> Of course, the solution is to define members with '= void' > > Since when? https://issues.dlang.org/show_bug.cgi?id=11331 and your > https://issues.dlang.org/show_bug.cgi?id=16956 are still open.

Re: Generating struct .init at run time?

2020-07-02 Thread Ali Çehreli via Digitalmars-d-learn
On 7/2/20 2:37 AM, IGotD- wrote: > what on earth are those extra 800MB? I'm losing my mind. :) Of course it's just 8M. Too many digits for me to handle. :p > Also, this an obvious optimization that can be implemented, that the > program do an initialization loop instead of putting it in the

Generating struct .init at run time?

2020-07-02 Thread Ali Çehreli via Digitalmars-d-learn
Normally, struct .init values are known at compile time. Unfortunately, they add to binary size: enum elementCount = 1024 * 1024; struct S { double[elementCount] a; } void main() { S s; assert(typeid(S).initializer.length == double.sizeof * elementCount);

Re: How to implement Canceleable spawn() from parent

2020-07-01 Thread Ali Çehreli via Digitalmars-d-learn
On 7/1/20 2:41 AM, aberba wrote: On Tuesday, 30 June 2020 at 14:43:40 UTC, Steven Schveighoffer wrote: On 6/30/20 10:15 AM, Simen Kjærås wrote: [...] My thinking is I don't want regular consumers using the package to think about the technicality of thread_joinAll() at all. Thinking about

Re: How to implement Canceleable spawn() from parent

2020-06-29 Thread Ali Çehreli via Digitalmars-d-learn
On 6/29/20 4:34 PM, aberba wrote: > So with this, without the Thread.sleep() to block main from exiting, the > spawned thread will terminate immediately. You can call core.thread.thread_joinAll at the end of main. Another way would be to wait for a worker's exit by looking for LinkTerminated

Re: How to implement Canceleable spawn() from parent

2020-06-28 Thread Ali Çehreli via Digitalmars-d-learn
On 6/28/20 4:08 PM, aberba wrote: So I checked receiveTimeout() when I was looking for what I could use. I wish there was an example in the docs. https://dlang.org/library/std/concurrency/receive_timeout.html I have an example of it:

Re: foreach iterator with closure

2020-06-28 Thread Ali Çehreli via Digitalmars-d-learn
On 6/28/20 9:07 AM, Denis wrote: > * foreach is the actual iterator, Yes. foreach is "lowered" to the following equivalent: for ( ; !range.empty; range.popFront()) { // Use range.front here } A struct can support foreach iteration through its opCall() member function as well.

Re: foreach iterator with closure

2020-06-27 Thread Ali Çehreli via Digitalmars-d-learn
On 6/27/20 8:19 PM, Denis wrote: > Is it possible to write an iterator It is arguable whether D's ranges are iterators but if nouns are useful, we call them ranges. :) (Iterators can be written in D as well and then it would really be confusing.) >struct letters { > string str; >

Re: Passing iterators into functions

2020-06-25 Thread Ali Çehreli via Digitalmars-d-learn
Collection elements are accessed by ranges in D. Although both iterators and ranges fundamentally do the same thing (access elements). More accurately, ranges correspond to a pair iterators. On 6/24/20 8:35 PM, repr-man wrote: > auto func(R)(R r, size_t width) > if(isRandomAccessRange!R) > {

Re: how to skip the next (n) item & continue from (n+1) with a range ? e.g. in File(fn).byLine();

2020-06-22 Thread Ali Çehreli via Digitalmars-d-learn
On 6/22/20 2:37 PM, mw wrote: > On Monday, 22 June 2020 at 20:58:58 UTC, Ali Çehreli wrote: > >> Others have other explanations for this but my understanding is about >> exception safety: If it changed internal state and returned the front >> object, you would no be able to make a function like

Re: how to skip the next (n) item & continue from (n+1) with a range ? e.g. in File(fn).byLine();

2020-06-22 Thread Ali Çehreli via Digitalmars-d-learn
On 6/22/20 1:46 PM, mw wrote: > so `front` is peek, and `popFront` is the pop action whose return type > is `void`, why we need two *separate* calls instead of just let > `popFront` return T Others have other explanations for this but my understanding is about exception safety: If it changed

Re: GtkD code review - How to update a progressbar using data sharing concurrency

2020-06-22 Thread Ali Çehreli via Digitalmars-d-learn
On 6/21/20 5:52 AM, adnan338 wrote: I am trying to figure out how to prevent this data race. I still like the std.concurrency method I used here: https://forum.dlang.org/post/rkitcprqvslexgqaf...@forum.dlang.org The only difference is that your individual progresses are from 0% to 100%.

Re: Some questions about strings

2020-06-21 Thread Ali Çehreli via Digitalmars-d-learn
On 6/21/20 8:17 PM, Denis wrote:> I have a few questions about how strings are stored. > > - First, is there any difference between string, wstring and dstring? string is char[] wstring is wchar[] dstring is dchar[] char is 1 byte: UTF-8 code unit wchar is 2 bytes: UTF-16 code unit dchar is 4

Re: GtkD code review - How to update a progressbar using data sharing concurrency

2020-06-21 Thread Ali Çehreli via Digitalmars-d-learn
On 6/20/20 9:30 AM, adnan338 wrote: > Hello, I need a code review on my strategy I don't know gtkd so I did not compile the code and I did not review the code very carefully. However, I don't think you need to 'synchronized' the whole parallel loop. Since there is only one thread that

Re: "if not" condition check (for data validation)

2020-06-18 Thread Ali Çehreli via Digitalmars-d-learn
On 6/18/20 5:13 AM, Denis wrote: > Templates offer a clean syntax Here is an earlier experiment of nested templates, which may be useful in this case. This is unrelated to your problem but the syntax can be pretty readable with templates: // If there are template arguments, then the result

Re: "if not" condition check (for data validation)

2020-06-17 Thread Ali Çehreli via Digitalmars-d-learn
On 6/17/20 4:46 PM, Denis wrote:> Is there a cleaner way to implement an "if not" condition check? >if ( configfile.isFile && configfile.extension == ".conf", message ) { } >else if (isConfigFile(name)) { // ... } else { // ... } The following is suitable in many

Re: How to create Multi Producer-Single Consumer concurrency

2020-06-16 Thread Ali Çehreli via Digitalmars-d-learn
On 6/12/20 3:02 PM, adnan338 wrote: > So there are multiple "download finished" message producers, and one > consumer of those messages. Furthermore, that producer has a callback > that triggers an UI object. That's almost exactly what I do in some of my programs. I use std.concurrency and the

Re: Initializing an associative array of struct

2020-06-14 Thread Ali Çehreli via Digitalmars-d-learn
On 6/14/20 7:43 AM, Denis wrote:> @Kagamin: > > On Sunday, 14 June 2020 at 07:16:18 UTC, Kagamin wrote: >> parameters[param]=Parameter(); > > I did not realize that you can use a type on the RHS of an assignment, Note that it's not just the type but with parenthesis after it. For example, Foo()

Re: final switch problem

2020-06-13 Thread Ali Çehreli via Digitalmars-d-learn
On 6/13/20 9:22 AM, John Chapman wrote: Hmm, compiling with -release makes it work. Not a huge issue, I'll just avoid final switches in debug mode until it's fixed. Thanks. Apparently, it's a known issue: https://issues.dlang.org/show_bug.cgi?id=19548 Ali

Re: Metaprogramming with D

2020-06-09 Thread Ali Çehreli via Digitalmars-d-learn
On 6/8/20 7:50 AM, ag0aep6g wrote: https://ddili.org/ders/d.en/literals.html#ix_literals.q%7B%7D Thank you. I am biased but I like my :) index of the book, where all such syntax items appear: https://ddili.org/ders/d.en/ix.html Ali

Re: Metaprogramming with D

2020-06-06 Thread Ali Çehreli via Digitalmars-d-learn
On 6/6/20 5:03 PM, FunkyD wrote:> On Saturday, 6 June 2020 at 09:57:36 UTC, Jan Hönig wrote: > D is pretty good for meta-programming. For certain other things it is > terrible. I am glad I don't know enough about other technologies to feel that way. > String mixins simply mix in D code. It

Re: writeln Function while reading a Text File is printing appending text "before text" and "after text" at the same position

2020-06-05 Thread Ali Çehreli via Digitalmars-d-learn
On 6/3/20 1:43 PM, BoQsc wrote: Chomp sounds kind of funny hahaha. Also consider strip, stripLeft, and stripRight. (Not because they may be funny but because they are useful as well. :) ) Ali

Re: Making alias of a struct field needs "this".

2020-06-02 Thread Ali Çehreli via Digitalmars-d-learn
On 6/2/20 1:56 AM, realhet wrote: > struct A{ >struct B{ int c; } >B b; > >auto f(){ > alias d = b.c; The spec explicitly says it's not legal: "Aliases cannot be used for expressions" (Item 10): https://dlang.org/spec/declaration.html#alias I use nested functions for such

Re: How to efficiently resolve Associative Arrays not being sorted?

2020-06-02 Thread Ali Çehreli via Digitalmars-d-learn
On 6/2/20 12:32 AM, BoQsc wrote: > I want to read a file, put it into an array, make some search and > replace on the content and output the modified text. How large is the data? If it fits into memory, just read the whole thing, update it, sort the keys, and then output like this: import

Re: how to achieve C's Token Pasting (##) Operator to generate variable name in D?

2020-05-31 Thread Ali Çehreli via Digitalmars-d-learn
On 5/31/20 1:00 PM, mw wrote:> On Sunday, 31 May 2020 at 09:37:24 UTC, Ali Çehreli wrote: > One question: in Sebastiaan's solution opDispatch is performed at > run-time Templates don't exist at run time. They are used for generating code at compile time and that's it. opDispatch is a

Re: how to achieve C's Token Pasting (##) Operator to generate variable name in D?

2020-05-31 Thread Ali Çehreli via Digitalmars-d-learn
On 5/31/20 2:26 AM, Ali Çehreli wrote: Unfortunately, I could not reach the following cleaner syntax with a mixin template:   mixin RW!int.x; Ok, I solved that too with a very convoluted "eponymous mixin template opDispatch." :) struct RW(T) { template opDispatch(string name) {

Re: how to achieve C's Token Pasting (##) Operator to generate variable name in D?

2020-05-31 Thread Ali Çehreli via Digitalmars-d-learn
On 5/30/20 11:28 PM, mw wrote: On Sunday, 31 May 2020 at 00:46:09 UTC, Paul Backus wrote: You can simplify this considerably using a mixin template [1]: --- mixin template RW(T, string name) {     private T var;     public T get() { return var; }     public typeof(this) set(T val) { var = val;

Re: Overload function template for rectangular array

2020-05-25 Thread Ali Çehreli via Digitalmars-d-learn
On 5/25/20 1:20 AM, John Chapman wrote: void foo(T)(T[] a) {} void foo(T)(T[][] a) {} auto ra = new int[][](5, 5); ra.foo(); // matches both import std.traits; void foo(T)(T[] a) if (!isArray!T) {} void foo(T)(T[] a) if (isArray!T) {} Or you can take T as parameter and check ElementType!T:

Re: alias this and initialisation

2020-05-25 Thread Ali Çehreli via Digitalmars-d-learn
On 5/24/20 6:35 PM, Danni Coy wrote:> can anybody tell me why > > struct S > { > int x; > alias x this; > } > > void test() > { > S s; > s = 8; // this works > S s = 8 // but this does not? > } alias this is for implicit conversion, which requires an object to convert

Re: How to flatten N-dimensional array?

2020-05-24 Thread Ali Çehreli via Digitalmars-d-learn
On 5/24/20 2:37 AM, Pavel Shkadzko wrote: On Saturday, 23 May 2020 at 19:59:30 UTC, Ali Çehreli wrote: On 5/23/20 11:15 AM, Pavel Shkadzko wrote:> I have tried to implement a simple flatten function for multidimensional [...] Thank you, I was lacking practical examples for templates with

Re: Asserting that a base constructor is always called

2020-05-23 Thread Ali Çehreli via Digitalmars-d-learn
On 5/23/20 3:04 PM, Tim wrote: I have a base class GameObject: /// Base class of most objects in the game class GameObject{     this(){     world[layer] = this;     }     abstract void update(){}     void draw(){} } I want to make sure that whenever a class inherits from this, the

  1   2   3   4   5   6   7   8   9   10   >