Re: passing a variadic parameter to randomSample

2022-01-26 Thread Ali Çehreli via Digitalmars-d-learn
On 1/26/22 07:44, Ali Çehreli wrote: > the instantiations could be too many for R... I am still wrong there. It is inconceivable to instantiate the following template for the same type (e.g. string) from "too many" places in a program: auto RandomChoice(R...)(R r) { // ... } There may be

Re: passing a variadic parameter to randomSample

2022-01-26 Thread Ali Çehreli via Digitalmars-d-learn
On 1/26/22 02:20, Stanislav Blinov wrote: > On Tuesday, 25 January 2022 at 22:07:43 UTC, Ali Çehreli wrote: >> On 1/25/22 13:55, forkit wrote: >> >> > auto RandomChoice(R...)(R r) >> >> Watch out though: The compiler will compile a different function per >> set of values. For example, there will

Re: gdc or ldc for faster programs?

2022-01-26 Thread Ali Çehreli via Digitalmars-d-learn
On 1/26/22 04:06, Johan wrote: > The stdlib makes a huge difference in performance. > Ali's program uses string manipulation, Yes, on the surface, I thought my inner loop had just / and % but of course there is that formattedWrite. I will change the code to use sprintf into a static buffer

Re: gdc or ldc for faster programs?

2022-01-25 Thread Ali Çehreli via Digitalmars-d-learn
On 1/25/22 16:15, Johan wrote: > On Tuesday, 25 January 2022 at 19:52:17 UTC, Ali Çehreli wrote: >> >> I am using compilers installed by Manjaro Linux's package system: >> >> ldc: LDC - the LLVM D compiler (1.28.0): >> based on DMD v2.098.0 and LLVM 13.0.0 >> >> gdc: dc (GCC) 11.1.0 >> >> dmd:

Re: gdc or ldc for faster programs?

2022-01-25 Thread Ali Çehreli via Digitalmars-d-learn
On 1/25/22 14:33, H. S. Teoh wrote: > This is very interesting Fascinating code generation and investigation! :) Ali

Re: passing a variadic parameter to randomSample

2022-01-25 Thread Ali Çehreli via Digitalmars-d-learn
On 1/25/22 13:55, forkit wrote: > auto RandomChoice(R...)(R r) Watch out though: The compiler will compile a different function per set of values. For example, there will be separate RandomChoice instances for ("hello") vs. ("world"). D has a simple variadic parameter syntax as well: auto

Re: gdc or ldc for faster programs?

2022-01-25 Thread Ali Çehreli via Digitalmars-d-learn
On 1/25/22 12:42, H. S. Teoh wrote: >> For a test run for 2 million numbers: >> >> ldc: ~0.95 seconds >> gdc: ~0.79 seconds >> dmd: ~1.77 seconds > > For measurements under 1 second, I'm skeptical of the accuracy, because > there could be all kinds of background noise, CPU interrupts and stuff >

Re: gdc or ldc for faster programs?

2022-01-25 Thread Ali Çehreli via Digitalmars-d-learn
On 1/25/22 12:59, Daniel N wrote: Maybe you can try --ffast-math on ldc. Did not make a difference. Ali

Re: gdc or ldc for faster programs?

2022-01-25 Thread Ali Çehreli via Digitalmars-d-learn
On 1/25/22 12:01, Johan wrote: Have you tried `--enable-cross-module-inlining` with LDC? Tried now. Makes no difference that I can sense, likely because there is only one module anyway. :) (But I guess it works over Phobos modules too.) Ali

Re: gdc or ldc for faster programs?

2022-01-25 Thread Ali Çehreli via Digitalmars-d-learn
On 1/25/22 11:52, Ali Çehreli wrote: > a program I wrote about spelling-out parts of a number Here is the program as a single module: module spellout.spellout; // This program was written as a code kata to spell out // certain parts of integers as in "1 million 2 thousand // 42". Note that

gdc or ldc for faster programs?

2022-01-25 Thread Ali Çehreli via Digitalmars-d-learn
Sorry for being vague and not giving the code here but a program I wrote about spelling-out parts of a number (in Turkish) as in "1 milyon 42" runs much faster with gdc. The program integer-divides the number in a loop to find quotients and adds the word next to it. One obvious optimization

Re: From the D Blog: The Binary Language of Moisture Vaporators

2022-01-24 Thread Ali Çehreli via Digitalmars-d-announce
On 1/24/22 13:57, Moth wrote: >> Reddit: >> https://www.reddit.com/r/programming/comments/sbn7n6/the_binary_language_of_moisture_vaporators/ > first: how exactly does assembly output relate to moisture vaporators? Someone answered that question on the ycombinator thread. They included

Re: dynamic format specifier possible?

2022-01-23 Thread Ali Çehreli via Digitalmars-d-learn
On 1/23/22 13:59, forkit wrote: I would like to calculate the width of the format specifier dynamically, at runtime. You use an asterisk and provide the width as an argument. This one uses the length of the name of the program: import std; void main(string[] args) { int val = 999000;

Re: map question

2022-01-22 Thread Ali Çehreli via Digitalmars-d-learn
On 1/22/22 11:32, forkit wrote: > trying to make sense of the below: The generate() solution shown by Stanislav Blinov is suitable here. > auto rnd = Random(unpredictableSeed); Somebody else mentioned this before but none of the programs we've seen so far seemed to need a special random

Re: forward tuple arg to local variable + dtor

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

Re: number ranges

2022-01-21 Thread Ali Çehreli via Digitalmars-d-learn
On 1/21/22 08:58, Salih Dincer wrote: > ```d > auto inclusiveRange(T)(T f, T l, T s = cast(T)0) > in(!isBoolean!T) { 'in' contracts are checked at runtime. The one above does not make sense because you already disallow compilation for 'bool' below. You could add a template constraint there:

Re: -debug question

2022-01-20 Thread Ali Çehreli via Digitalmars-d-learn
On 1/20/22 18:07, forkit wrote: I have a line of code, that I do NOT want executed when -debug is passed in. enforce(!exists(fname), "Oop! That file already exists!"); Is this even possible? (with using -version ..) The following should do it: debug {} else { foo(); } Ali

Re: automate tuple creation

2022-01-20 Thread Ali Çehreli via Digitalmars-d-learn
On 1/20/22 17:35, forkit wrote: > module test; > @safe Does that make just the following definition @safe or the entire module @safe? Trying... Yes, I am right. To make the module safe, use the following syntax: @safe: > idArray.reserve(recordsNeeded); [...] > idArray ~=

Re: automate tuple creation

2022-01-20 Thread Ali Çehreli via Digitalmars-d-learn
On 1/20/22 15:10, Ali Çehreli wrote: > void foo(const int[]) {} // Idiomatic As H. S. Teoh would add at this point, that is not idiomatic but the following are (with different meanings): void foo(const(int)[]) {} // Idiomatic void foo(const(int[])) {} // Idiomatic > void

Re: automate tuple creation

2022-01-20 Thread Ali Çehreli via Digitalmars-d-learn
On 1/20/22 15:01, forkit wrote: > On Thursday, 20 January 2022 at 22:31:17 UTC, Steven Schveighoffer wrote: >> >> Because it would allow altering const data. >> > > I'm not sure I understand. At what point in this function is valuesArray > modified, and thus preventing it being passed in with

Re: number ranges

2022-01-20 Thread Ali Çehreli via Digitalmars-d-learn
On 1/19/22 21:24, Salih Dincer wrote: > ```d >size_t length() inout { > //return last_ - first_ + 1 - empty_;/* > auto len = 1 + last_ - first_; > return cast(size_t)len;//*/ >} > ``` Good catch but we can't ignore '- empty_'. Otherwise an empty range will return 1. >

Re: automate tuple creation

2022-01-19 Thread Ali Çehreli via Digitalmars-d-learn
On 1/19/22 15:21, H. S. Teoh wrote: > On Wed, Jan 19, 2022 at 02:33:02PM -0800, Ali Çehreli via Digitalmars-d-learn wrote: > [...] >> // Returning a dynamically allocated array looks expensive >> // here. Why not use a struct or std.typecons.Tuple instead? > >

Re: automate tuple creation

2022-01-19 Thread Ali Çehreli via Digitalmars-d-learn
On 1/19/22 14:33, Ali Çehreli wrote: > Random rnd; > > shared static this() { >rnd = Random(unpredictableSeed); > } But that's a mistake: If rnd is thread-local like that, it should be initialized in a 'static this' (not 'shared static this'). Otherwise, only the main thread's 'rnd' would

Re: automate tuple creation

2022-01-19 Thread Ali Çehreli via Digitalmars-d-learn
On 1/19/22 13:59, forkit wrote: > void createBoolMatrix(ref uint[][] m) > { > auto rnd = Random(unpredictableSeed); That works but would be unnecessarily slow and be against the idea of random number generators. The usual approach is, once you have a randomized sequence, you just

Re: number ranges

2022-01-19 Thread Ali Çehreli via Digitalmars-d-learn
On 1/19/22 04:51, Salih Dincer wrote: > Is it okay to swap places instead of throwing an error? I would be happier if my potential mistake is caught instead of the library doing something on its own. > Let's also > implement BidirectionalRange, if okay. I had started experimenting with that

Re: why there is a [] at the end of assocArray

2022-01-19 Thread Ali Çehreli via Digitalmars-d-learn
On 1/19/22 06:06, michaelbi wrote: On Wednesday, 19 January 2022 at 13:21:32 UTC, Stanislav Blinov wrote: On Wednesday, 19 January 2022 at 13:15:35 UTC, michaelbi wrote:     foreach(line; > File("input.txt").byLine.map!(a=>a.idup).array.transposed) so why there is a [] at the end of

Re: number ranges

2022-01-18 Thread Ali Çehreli via Digitalmars-d-learn
On 1/18/22 14:08, forkit wrote: > never use number ranges.. not ever! ;-) > > (except in combination with iota) Indeed, the following is an elegant but slow (tested with dmd) implementation with Phobos: auto range(T)(T a, T b) in (a <= b) { import std.range : chain, iota, only; return

Re: number ranges

2022-01-18 Thread Ali Çehreli via Digitalmars-d-learn
On 1/18/22 12:43, forkit wrote: > wrapper function... > > > auto range(T:T)(T a, T b) > { > import std.range : iota; > return iota(a, (b+1)); > } Needs a little more work to be correct. The following produces and empty range. ;) range(uint.min, uint.max) Also, is it important

Re: Dynamic array ot not

2022-01-16 Thread Ali Çehreli via Digitalmars-d-learn
On 1/16/22 17:51, H. S. Teoh wrote: >> In practice, malloc'ed memory is cleared e.g. by memset(). Or, there is >> calloc() which returns memory filled with zeros. > > Correction: malloc() is not guaranteed to clear the allocated memory. Agreed. What I meant is, the programmer ordinarily clears

Re: Dynamic array ot not

2022-01-16 Thread Ali Çehreli via Digitalmars-d-learn
On 1/16/22 15:14, forkit wrote: > But -profile=gc .. says no. It's not. > > int[][] mArr = [[1, 2], [3, 4], [5, 6], [7, 8]]; // GC allocation occurs. > int[][] mArr = iota(1, 9).chunks(2).map!array.array; // No GC allocation > occurs. Definitely a -profile=gc bug. Here are the existing ones:

Re: Dynamic array ot not

2022-01-16 Thread Ali Çehreli via Digitalmars-d-learn
On 1/16/22 14:43, forkit wrote: > Well, it's fair to say, that 'range-based programming' is kinda new to me. I hope it will be useful to you. :) > int[][] mArr2 = [[1, 2], [3, 4], [5, 6], [7, 8]]; // GC allocation > > But it turns out: > > int[][] mArr = iota(1, 9).chunks(2).map!array.array;

Re: Throw stack trace from program kill

2022-01-16 Thread Ali Çehreli via Digitalmars-d-learn
On 1/16/22 07:15, Hipreme wrote: > Is there some way to throw a stack trace when killing the program from > the CTRL+C from the terminal? I am interested in how to add Ctrl+C support for a D program as well. > It would help a lot into debugging occasional infinity loops One way of achieving

Re: Dynamic array ot not

2022-01-16 Thread Ali Çehreli via Digitalmars-d-learn
On 1/16/22 07:32, Salih Dincer wrote: > On Sunday, 16 January 2022 at 11:43:40 UTC, Ali Çehreli wrote: >> >> void main() { >> enum count = 7; >> >> // Allocate some memory >> void* rawData = malloc(int.sizeof * count); In practice, malloc'ed memory is cleared e.g. by memset(). Or, there is

Re: Dynamic array ot not

2022-01-16 Thread Ali Çehreli via Digitalmars-d-learn
On 1/16/22 01:43, forkit wrote: > On Sunday, 16 January 2022 at 04:58:21 UTC, Ali Çehreli wrote: >> >> I have a problem with calling type[] a dynamic array because it is a >> slice, which may be providing access to the elements of a dynamic array. >> > > Yes. A more useful way of describing []

Re: Dynamic array ot not

2022-01-15 Thread Ali Çehreli via Digitalmars-d-learn
On 1/15/22 20:09, forkit wrote: > so at this link: https://dlang.org/spec/arrays.html > > it indicates an array of type[] is of type 'Dynamic array'. I have a problem with calling type[] a dynamic array because it is a slice, which may be providing access to the elements of a dynamic array.

Re: All Community Discord channels are now being bridged to Matrix

2022-01-15 Thread Ali Çehreli via Digitalmars-d-announce
On 1/15/22 16:53, Paul Backus wrote: there is a Matrix client for emacs: I am not surprised at all. :) Matrix sounds very promising: https://matrix.org/ Ali

Re: All Community Discord channels are now being bridged to Matrix

2022-01-15 Thread Ali Çehreli via Digitalmars-d-announce
On 1/15/22 15:49, rikki cattermole wrote: > If you don't use Matrix, you can ignore this. I use Emacs. Is that Matrix? :o) Ali

Re: All Community Discord channels are now being bridged to Matrix

2022-01-15 Thread Ali Çehreli via Digitalmars-d-announce
On 1/15/22 10:45, WebFreak001 wrote: > we have now bridged all the Discord rooms to Matrix rooms. What does all that mean? Is that something that only Discord users should understand be interested in? :) Ali

Re: Starting and managing threads

2022-01-15 Thread Ali Çehreli via Digitalmars-d-learn
On 1/12/22 00:50, Bagomot wrote: > If I change the run method of the Guard class so that it starts a new > thread, the program just does nothing: > ```d > public void run() { > spawn((shared EventLoop eventLoop) { > while ((cast() eventLoop).loop()) { > continue; The

Re: parallel foreach stuck on 8 workUnitSize

2022-01-08 Thread Ali Çehreli via Digitalmars-d-learn
On 1/8/22 7:18 PM, Booster wrote: > https://dlang.org/library/std/parallelism/task_pool.parallel.html#workUnitSize > > > Basically have no difference than the above but when I change the > workUnitSize to 1, 4, whatever it is always running 8 parallel loops. > > Either a bug or workUnitSize

Re: srand in D

2022-01-08 Thread Ali Çehreli via Digitalmars-d-learn
On 1/8/22 4:17 PM, kdevel wrote: > enum immutable (char) [] allowed_chars = [ >'c', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 't', 'v', >'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9' > ]; > > char q = uniform!allowed_chars; > writeln (q);

Re: srand in D

2022-01-08 Thread Ali Çehreli via Digitalmars-d-learn
On 1/8/22 12:27 PM, kdevel wrote: On Sunday, 31 October 2021 at 17:02:00 UTC, Ali Çehreli wrote: Just comment that line out. :) D's pseudo-random generators start randomized by default: [...]   https://dlang.org/phobos/std_random.html Klicking at "Run" always delivers "mango":

Re: Added copy constructors to "Programming in D"

2022-01-08 Thread Ali Çehreli via Digitalmars-d-announce
On 1/8/22 5:49 AM, Stanislav Blinov wrote: >> Of course, now I stress that postblit is discouraged. > > Bit early, methinks. Copy ctors aren't even fully supported by the > runtime yet. Thanks. The spec discourages postblit as well. What's the best of saying it then? Something like "There are

Added copy constructors to "Programming in D"

2022-01-07 Thread Ali Çehreli via Digitalmars-d-announce
1) After about three years, I finally added copy constructors: http://ddili.org/ders/d.en/special_functions.html#ix_special_functions.copy%20constructor Of course, now I stress that postblit is discouraged. 2) The other noteworthy change in the book is my now-different stance on variables:

Re: what's the proper way to assign this?

2022-01-04 Thread Ali Çehreli via Digitalmars-d-learn
On 1/4/22 10:53 AM, Dr Machine Code wrote: what made that change, assuming it worked at time. Library? compile changes? Here is the changelog item: https://dlang.org/changelog/2.088.0.html#remove-nullable-alias-get-this A reminder at least to myself: I found it by searching for Nullable in

Re: Libc functions undefined when linking

2022-01-04 Thread Ali Çehreli via Digitalmars-d-learn
On 1/4/22 10:13 AM, Ben Jones wrote: > So I think I need to specify that I want to explicitly include libc when > I link it. `-lc` didn't seem to work. Did you add -lc and -lpthread on the linker line? > ld build/*.o -L. -lphobos2 -o build/executable Ali

Re: Is there a File-like object for unit tests?

2022-01-04 Thread Ali Çehreli via Digitalmars-d-learn
On 1/4/22 9:48 AM, Paul Backus wrote: > On Tuesday, 4 January 2022 at 17:01:41 UTC, Amit wrote: >> I need a File (or a general IO interface) that reads from an >> in-memory buffer, similar to python's `StringIO` or go's >> `strings.Reader`. >> >> How can I achieve that? I don't think it exists

Re: Printing a quoted string

2022-01-02 Thread Ali Çehreli via Digitalmars-d-learn
On 1/2/22 9:27 AM, Amit wrote: > string s = "one \"two\"\nthree four"; The issue there is that the string does not contain the two characters \" but the single character ". So, that's a syntax issue. The solution is to use back ticks to tell the compiler what you really mean. > And get as

Re: How are extra copy constructor parameters used?

2021-12-29 Thread Ali Çehreli via Digitalmars-d-learn
On 12/29/21 5:14 PM, Paul Backus wrote: Therefore, when you write your own copy constructors, you should always use `inout` if possible, so that compiler-generated copy constructors will be able to copy instances of your struct that appear as members of other structs. Excellent point. I

How are extra copy constructor parameters used?

2021-12-29 Thread Ali Çehreli via Digitalmars-d-learn
The second item in the documentation mentions "any number of default parameters" when describing copy constructor syntax: https://dlang.org/spec/struct.html#struct-copy-constructor 1) I can't figure out how to use those extra parameters. For example, I can't find a special function name to

Re: Is there a way to make a function parameter accept only values that can be checked at compile time?

2021-12-28 Thread Ali Çehreli via Digitalmars-d-learn
On 12/28/21 2:06 PM, Steven Schveighoffer wrote:    void print_num(int mul)(int num) { Wasn't there a way of telling whether an 'auto ref' parameter is copied or not? void print_num()(int num, auto ref int mul) { // ? } And that would indicate that the argument was an rvalue? I

Re: Starting and managing threads

2021-12-27 Thread Ali Çehreli via Digitalmars-d-learn
On 12/27/21 1:33 AM, Bagomot wrote: > separate thread, without blocking the main one. I think you can use std.concurrency there. I have a chapter here: http://ddili.org/ders/d.en/concurrency.html Look for 'struct Exit' to see how the main thread signals workers to stop running. And some

Re: On the D Blog -- Teaching D from Scratch: Is it a viable first language?

2021-12-23 Thread Ali Çehreli via Digitalmars-d-announce
On 12/23/21 5:11 PM, zjh wrote: On Thursday, 23 December 2021 at 14:48:43 UTC, zjh wrote: every time I visit `https://dlang.org`,it crashes. `wrong`,`https://dlang.org/blog`. Does the site crash e.g. with an error code or does the browser crash? More information may help debug it. Ali

Re: Thread exits immediately with no reason.

2021-12-21 Thread Ali Çehreli via Digitalmars-d-learn
On 12/21/21 10:07 AM, solidstate1991 wrote: > I couldn't add those lines unfortunately, Perhaps I had typos? Or the code is not yours to modify? In any case, you should be able to introduce a top level thread entry function and put the try-catch in there. > but I do get an exception in a >

Re: Thread exits immediately with no reason.

2021-12-20 Thread Ali Çehreli via Digitalmars-d-learn
On 12/20/21 3:56 PM, solidstate1991 wrote: > The problem is, that even with disabling all error-handling that would > allow to shut down the thread safely, the thread only runs that while > loop once. I bet it's throwing an Error. Call your thread entry function from a try-catch wrapping

Re: This is bug or not? (immutable class containing struct with dtor)

2021-12-17 Thread Ali Çehreli via Digitalmars-d-learn
On 12/17/21 10:01 AM, Tejas wrote: > I think since `immutable` objects are kept in Read Only Storage, There is no such requirement nor guarantee. > you > can't call destructors on them Destructor is nothing but a piece of code that is executed when an object's life ends. A destructor need

Re: Is it possible to do this with a template?

2021-12-17 Thread Ali Çehreli via Digitalmars-d-learn
On 12/17/21 1:57 AM, bauss wrote: > You can also do it as a normal template: > > ```d > template is_same(alias value, T) > { > enum is_same = is(typeof(value) == T); > } > ``` And even shorter by realizing that it's an eponymous template: enum is_same(alias value, T) = is(typeof(value) ==

Re: Any workaround for "closures are not yet supported in CTFE"?

2021-12-07 Thread Ali Çehreli via Digitalmars-d-learn
On 12/7/21 7:30 AM, Andrey Zherikov wrote: > auto createDelegate(string s) > { > return { s.writeln; }; // Error: closures are not yet > supported in CTFE > } I don't know whether the workaround works with your program but that delegate is the equivalent of the following struct (the

Re: Null pointer in __vptr

2021-11-20 Thread Ali Çehreli via Digitalmars-d-learn
On 11/20/21 7:19 AM, frame wrote: > I think this is true for an object instance. But from an interface > instance, the object instance must be accessible somewhere? Everything needed is available from the pointer to a class object (or interface). There are some offset arithmetic involved to

Re: DConf Online 2021 Links

2021-11-20 Thread Ali Çehreli via Digitalmars-d-announce
On 11/19/21 2:10 AM, Mike Parker wrote: See you there! Reminder: DConf Online 2021 is on now. Ali

Re: Null pointer in __vptr

2021-11-19 Thread Ali Çehreli via Digitalmars-d-learn
On 11/19/21 10:04 AM, frame wrote: > On Friday, 19 November 2021 at 15:46:41 UTC, Adam D Ruppe wrote: > >> The `destroy` function (as well as other class destruction) will null >> out the whole vtable to help make use-after-free an obvious error. >> Possible that happened to you. > > So, a

Re: Incomplete words read from file

2021-11-17 Thread Ali Çehreli via Digitalmars-d-learn
On 11/17/21 3:46 PM, pascal111 wrote: > I made small program that shows the content of textual files, and it > succeeded to show non-English (Ascii code) language, but in many lines > some words are not complete and their rests are in next lines, how can > fix it? D assumes UTF-8 encoding by

Re: Simple text editor program

2021-11-17 Thread Ali Çehreli via Digitalmars-d-learn
On 11/17/21 12:49 PM, Dr Machine Code wrote: >> I think 'med' is by Walter Bright; he uses that editor daily. >> >> Ali > > It's a emacs' variant written in D? Yes. The dub page explains that the original author is Dave G. Conroy. med is written by Walter and is his favorite editor. (I should

Re: Simple text editor program

2021-11-16 Thread Ali Çehreli via Digitalmars-d-learn
On 11/16/21 6:10 PM, pascal111 wrote: Is there a so simple text editor written in D as an example for learners. I hope the editor whose code is written in D is available with someone. I am not familiar with any of them but searching for 'editor' finds some packages:

Re: Exceptions names list

2021-11-16 Thread Ali Çehreli via Digitalmars-d-learn
On 11/16/21 12:30 AM, bauss wrote: > On Monday, 15 November 2021 at 22:44:35 UTC, Ali Çehreli wrote: >> On 11/15/21 2:28 PM, pascal111 wrote: >> > I want to know where the list of exceptions names that >> "throw" statement >> > uses. >> >> Such a list is not very useful. Normally all we need is

Re: Make sure lifetime of helper structs is less than owning struct

2021-11-16 Thread Ali Çehreli via Digitalmars-d-learn
On 11/16/21 12:35 AM, Sebastiaan Koppe wrote: > Here the > OP wanted to (have the compiler) destroy the FileReader when it left the > scope, while disallowing any use after free. Thanks! That's what I missed. Ali

Re: Exceptions names list

2021-11-15 Thread Ali Çehreli via Digitalmars-d-learn
On 11/15/21 3:00 PM, pascal111 wrote: > On Monday, 15 November 2021 at 22:44:35 UTC, Ali Çehreli wrote: >> On 11/15/21 2:28 PM, pascal111 wrote: >> > [...] >> "throw" statement >> > [...] >> >> Such a list is not very useful. Normally all we need is Exception and >> occasionally one or two other

Re: Make sure lifetime of helper structs is less than owning struct

2021-11-15 Thread Ali Çehreli via Digitalmars-d-learn
On 11/15/21 2:33 PM, Imperatorn wrote: On Monday, 15 November 2021 at 22:27:24 UTC, Ali Çehreli wrote: On 11/15/21 1:58 PM, WebFreak001 wrote: > [...] wrote: >> [...] wrote: >> [...] the use of >> [...] variable `file` > [...] copying the > [...] a pointer. I don't see how it solves the

Re: Exceptions names list

2021-11-15 Thread Ali Çehreli via Digitalmars-d-learn
On 11/15/21 2:28 PM, pascal111 wrote: > I want to know where the list of exceptions names that "throw" statement > uses. Such a list is not very useful. Normally all we need is Exception and occasionally one or two other specific types. There can be hundreds of specific types of Exception a

Re: Make sure lifetime of helper structs is less than owning struct

2021-11-15 Thread Ali Çehreli via Digitalmars-d-learn
On 11/15/21 1:58 PM, WebFreak001 wrote: > On Monday, 15 November 2021 at 19:24:56 UTC, Sebastiaan Koppe wrote: >> On Monday, 15 November 2021 at 15:56:57 UTC, WebFreak001 wrote: >>> is this currently possible or maybe possible with DIP1000? >> >> Yes it is. But besides `-dip1000` and `@safe`, it

Re: using __traits to get line number of a member

2021-11-14 Thread Ali Çehreli via Digitalmars-d-learn
On 11/13/21 8:05 PM, forkit wrote: > static import mod = mixin(__MODULE__); > > That statement above would not compile. I don't understand myself but all of it as a single string works: mixin("static import mod = " ~ __MODULE__ ~ ";"); Ali

Re: using __traits to get line number of a member

2021-11-12 Thread Ali Çehreli via Digitalmars-d-learn
On 11/12/21 11:00 PM, forkit wrote: > // nope. writes the line no. of the foreach loop mixin(m) seems to solve the issue, which I think necessitates 'static if': static foreach(m; __traits(allMembers, mixin(__MODULE__))) { static if(m == "std" || m == "object" || m == "main")

Re: Completing C code with D style

2021-11-11 Thread Ali Çehreli via Digitalmars-d-learn
On 11/11/21 11:34 AM, Stanislav Blinov wrote: > Pessimization, though, is laughably easy, and > should be avoided at all costs. I am not passionate about this topic at all and I am here mostly because I have fun in this forum. So, I am fine in general. However, I don't agree that

Re: How to use dmd code coverage

2021-11-11 Thread Ali Çehreli via Digitalmars-d-learn
On 11/11/21 1:37 PM, forkit wrote: dmd test.d -cov ...but no .lst file anywhere to be found. Huh! I don't get it. Please run the program! :) Ali

Re: Completing C code with D style

2021-11-10 Thread Ali Çehreli via Digitalmars-d-learn
On 11/10/21 3:05 PM, H. S. Teoh wrote: > I cannot compile > even simple programs on a low-memory system because the compiler runs > out of memory Did the -lowmem switch help in some cases? Is -betterC any better? ;) Ali

Re: How to return a reference to structs?

2021-11-06 Thread Ali Çehreli via Digitalmars-d-learn
On 11/6/21 8:57 AM, Andrey Zherikov wrote: > On Saturday, 6 November 2021 at 13:57:47 UTC, Ali Çehreli wrote: >> Have you considered std.range.indexed, which should at least cover the >> case of accessing: >> >> https://dlang.org/phobos/std_range.html#indexed > > I don't need to iterate over

Re: How to return a reference to structs?

2021-11-06 Thread Ali Çehreli via Digitalmars-d-learn
On 11/6/21 5:05 AM, Andrey Zherikov wrote: > Some one can complain that `foo()` returns pointers that are not > available in CTFE but remember that the real code is more complex and > `foo()` cam be return just length (`return b.get().length;`) having the > same result. I think the error you

Re: How to return a reference to structs?

2021-11-05 Thread Ali Çehreli via Digitalmars-d-learn
On 11/5/21 5:43 PM, Andrey Zherikov wrote: In case others want to work, here are the modules that need to be imported: import std.algorithm; import std.range; import std.stdio; > struct A {} > struct B > { > A[] ar = [A.init]; > size_t[] idx = [0]; I don't know why but those initial

Re: How to return a reference to structs?

2021-11-05 Thread Ali Çehreli via Digitalmars-d-learn
On 11/5/21 11:44 AM, kdevel wrote: > On Thursday, 4 November 2021 at 11:26:30 UTC, Andrey Zherikov wrote: >> I have the following code example: > > [...] > >> A[5] a; >> ulong[] idx = [1,3,4]; > > [...] > >> return idx.map!(_ => a[_]); > > How can one make the type of idx's elements portable

Re: D Language Foundation Quarterly Meeting, October 2021

2021-11-05 Thread Ali Çehreli via Digitalmars-d-announce
Thank you, Mike. I love these digests. On 11/5/21 4:57 AM, Mike Parker wrote: > One of the things > [Petar Kirov] would like to do is use DLang Tour to make all of the examples in > Ali's book runnable. (That discussion must have happened when I had to leave to for another meeting.)

Re: How do you declare manifest constants?

2021-11-04 Thread Ali Çehreli via Digitalmars-d-learn
On 11/4/21 10:36 AM, H. S. Teoh wrote: >import __stdin : myversion; Where can we learn more of that magic? :) Ali

Re: Completing C code with D style

2021-11-03 Thread Ali Çehreli via Digitalmars-d-learn
On 11/3/21 11:41 AM, Stanislav Blinov wrote: > And Ali... associative arrays? For this? What are you trying to teach > the good beginner here? :D Obviously, we're all enjoying this thread but once the OP made it clear that they wanted a C-like solution, I took some liberty with alternative

Re: Using "strcpy" to assign value to dynamic char array

2021-11-03 Thread Ali Çehreli via Digitalmars-d-learn
On 11/2/21 9:06 PM, tsbockman wrote: UFCS works for setters, too: Oh yeah! Pretty cool. :) Ali

Re: A problem in converting C code

2021-11-02 Thread Ali Çehreli via Digitalmars-d-learn
On 11/2/21 6:49 PM, zjh wrote: On Tuesday, 2 November 2021 at 23:02:42 UTC, Ali Çehreli wrote: On 11/2/21 3:57 PM, Ali Çehreli wrote:   const x = readln.strip; this is very interesting .`readln` then `strip;`,Very natural. Yes, UFCS (universal function call syntax) makes code natural,

Re: Completing C code with D style

2021-11-02 Thread Ali Çehreli via Digitalmars-d-learn
On 11/2/21 5:50 PM, Siarhei Siamashka wrote: Your code can be changed to something like this: And I over-engineered it. :) import std.stdio; import std.algorithm; import std.range; import std.exception; import std.format; // A readable name; corresponds to C's typedef alias FilterFunction =

Re: A problem in converting C code

2021-11-02 Thread Ali Çehreli via Digitalmars-d-learn
On 11/2/21 3:57 PM, Ali Çehreli wrote: Here is a more idiomatic version: import std.stdio; import std.string; void main() { const x = strip(readln()); writeln(x); if (x == "hello world!") { writeln("yes"); } } The first line in main can be written with UFCS syntax as well:

Re: A problem in converting C code

2021-11-02 Thread Ali Çehreli via Digitalmars-d-learn
On 11/2/21 3:36 PM, pascal111 wrote: can we keep the C style of it as it is As you hint, this really is not D but still... :) I had to make three changes: import std.stdio; // Ali - Importing stdin under a different name // to prevent name conflict with std.stdio.stdin; import

Re: Average function using Variadic Functions method

2021-11-02 Thread Ali Çehreli via Digitalmars-d-learn
On 11/2/21 9:35 AM, pascal111 wrote: > "input..." seems nice, where can I get more information about it? I include most of the language in this free book: http://ddili.org/ders/d.en/index.html which has an Index Section that I find useful to locate information:

Re: Using "strcpy" to assign value to dynamic char array

2021-11-01 Thread Ali Çehreli via Digitalmars-d-learn
On 11/1/21 2:28 PM, pascal111 wrote: > This can serve the style I want. I am feeling funny right now and showing incorrect code. It's impossible to fit "Hello World!" in "xyz". As Steve said, don't do that. :) > It uses OOP style like C++ by putting a > pointer as a property, D's slices are

Re: Using "strcpy" to assign value to dynamic char array

2021-11-01 Thread Ali Çehreli via Digitalmars-d-learn
On 11/1/21 2:01 PM, Ali Çehreli wrote: > The program is as incorrect as its C equivalent would be. ;) I wrote a cool function to make it easy to disregard memory safety: import std.stdio; auto assumedLength(S)(ref S slice) { struct LengthSetter { void opAssign(size_t length) { //

Re: Using "strcpy" to assign value to dynamic char array

2021-11-01 Thread Ali Çehreli via Digitalmars-d-learn
On 11/1/21 1:49 PM, pascal111 wrote: > Yes, I'm practicing doing things in low level style like standard C. All you needed extra was to let the slice know about the new length: import std.stdio; import core.stdc.string; void main() { char[] s="xyz".dup; strcpy([0], "Hello World!"); s =

Re: On the Blog: DLang News for September/October 2021

2021-10-31 Thread Ali Çehreli via Digitalmars-d-announce
On 10/31/21 12:26 PM, Mike Parker wrote: The video version is done and is available here: https://youtu.be/jX9grHMTGAU WOW! That's amazing! :) Ali

Re: The type inference everywhere

2021-10-31 Thread Ali Çehreli via Digitalmars-d-learn
On 10/31/21 7:07 AM, Salih Dincer wrote: > ```d > auto foo(int value, auto s = Section(2, 60)) { > int max; /* ^--- ? > ...*/ > return Section (0, max) > } > ``` > Is possible something like above pointed. OK, I know it isn't because I > tried! Well, wouldn't

Re: Does associative array change the location of values?

2021-10-31 Thread Ali Çehreli via Digitalmars-d-learn
On 10/31/21 9:54 AM, Ali Çehreli wrote: Or other smart ways where I store the pointer in two parts as page+offset? Ok, that was silly. There would be no reference to the GC memory if I chopped off a pointer. :/ Ali

Re: srand in D

2021-10-31 Thread Ali Çehreli via Digitalmars-d-learn
On 10/31/21 9:54 AM, pascal111 wrote: Hi! I'm C learner and found the high similarity between C and D, and was converting C code into D Welcome! :) In case it makes to your use cases, check out -betterC as well: https://dlang.org/spec/betterc.html > but couldn't get the equivalent of this

Re: abs and minimum values

2021-10-31 Thread Ali Çehreli via Digitalmars-d-learn
On 10/30/21 10:04 PM, Dom DiSc wrote: > On Friday, 29 October 2021 at 14:20:09 UTC, Ali Çehreli wrote: >> Unsigned!T abs(T)(const(T) x) if(isIntegral!T) >> { >>static if(isSigned!T) if(x < 0) return cast(Unsigned!T)-x; >>return x; >> } >> >> void main() { >> int a = -5; >> int b = -4;

Re: Does associative array change the location of values?

2021-10-31 Thread Ali Çehreli via Digitalmars-d-learn
On 10/31/21 9:49 AM, H. S. Teoh wrote: > The current spec explicitly states that masking pointers this way is UB. Ok. :) What about unions? union U { ulong u; void* p; } Can the GC deal with that? Or other smart ways where I store the pointer in two parts as page+offset? (Perhaps that's

Re: Does associative array change the location of values?

2021-10-30 Thread Ali Çehreli via Digitalmars-d-learn
On 10/30/21 3:47 PM, Elronnd wrote: > If the GC were moving, it would also have to move the pointers you took > to AA elements. I doubt D's GC can ever change pointer values because the values may be hiding inside e.g. ulong variables. And we would definitely not want the GC to change ulong

Re: abs and minimum values

2021-10-29 Thread Ali Çehreli via Digitalmars-d-learn
On 10/29/21 1:05 AM, Dom DiSc wrote: > I recommend to implement your own abs function this way (was not > accepted for phobos, as it now does NOT return the same type as the > argument, which was considered a "breaking change" :-( ): Combined with automatic type conversions we got from C, it

Re: Threading challenge: calculate fib(45) while spinning

2021-10-15 Thread Ali Çehreli via Digitalmars-d-learn
On 10/14/21 8:54 PM, Ali Çehreli wrote: >writefln!"\rFibonacci(%d) = %d"(n, fibN); That '\r' bothered me because the actual work needs to know what the spinner is doing to clear its remaining character. >receiveTimeout(delay, > (OwnerTerminated msg) { And

<    1   2   3   4   5   6   7   8   9   10   >