Re: Interfacing with basic C++ class

2022-09-28 Thread Ali Çehreli via Digitalmars-d-learn
On 9/28/22 12:57, Riccardo M wrote: > class MyClass { > public: > int field; > MyClass(int a) : field(a) {} Make the following function 'virtual': > int add(int asd) { virtual int add(int asd) { I think the C++ class does not get a vptr without a virtual function and

Re: to delete the '\0' characters

2022-09-23 Thread Ali Çehreli via Digitalmars-d-learn
On 9/23/22 11:37, Salih Dincer wrote: > * character**S** > * at the **END** > * of the **STRING** I think the misunderstanding is due to the following data you've posted earlier (I am abbreviating): 53 F6 6E 6D 65 64 65 6E 20 79 75 72 64 75 6D 75 6E 20 FC 73 74 FC 6E 64 65 20 74 FC 74 65 6E

Re: to delete the '\0' characters

2022-09-22 Thread Ali Çehreli via Digitalmars-d-learn
On 9/22/22 14:31, Salih Dincer wrote: > string splitz(string s) > { >import std.string : indexOf; >auto seekPos = s.indexOf('\0'); >return seekPos > 0 ? s[0..seekPos] : s; > } If you have multiple '\0' chars that you will continue looking for, how about the following? import std;

Re: to delete the '\0' characters

2022-09-22 Thread Ali Çehreli via Digitalmars-d-learn
On 9/22/22 08:19, Ali Çehreli wrote: > string noZeroes(string s) > { > return s.byCodeUnit.filter!(c => c != '\0'); > } That won't compile; the return type must be 'auto'. Ali

Re: to delete the '\0' characters

2022-09-22 Thread Ali Çehreli via Digitalmars-d-learn
On 9/22/22 03:53, Salih Dincer wrote: > Is there a more accurate way to delete the '\0' characters at the end of > the string? I tried functions in this module: > https://dlang.org/phobos/std_string.html Just to remind, the following are always related as well because strings are arrays, which

Re: Why this function just decides to not call another function and do its thing instead?

2022-09-17 Thread Ali Çehreli via Digitalmars-d-learn
On 9/17/22 10:14, frame wrote: On Saturday, 17 September 2022 at 15:04:48 UTC, solidstate1991 wrote: And then instead just decides that the `localName` and `namespaceURI` pairs are not equal, and in those cases the Visual Studio debugger doesn't detect any entering into any of the

Re: dub lint

2022-09-15 Thread Ali Çehreli via Digitalmars-d-learn
On 9/15/22 16:14, Christian Köstlin wrote: > There is `dub run dscanner -- --defaultConfig` which creates a default > config in `~/.config/dscanner/dscanner.ini` (for linux and osx). Thanks! I love such features. It is so useful for a program to write out its configuration file. (My tools did

Re: dub lint

2022-09-15 Thread Ali Çehreli via Digitalmars-d-learn
On 9/15/22 15:04, Ali Çehreli wrote: > Is there a way to silence specific 'dub lint' warnings? Answering myself, I don't think it's possible but luckily my catching an Error was in unittests only so I can do either of the following to skip unittest code when linting: a) Pass --skipTests to

Re: dub lint

2022-09-15 Thread Ali Çehreli via Digitalmars-d-learn
On 9/15/22 14:32, Ali Çehreli wrote: > (However, like all linters it's not perfect but I still like having that > power.) The following code is flagged because it catches Error: unittest { try { assert(false); } catch (Error) { // Cool... } } [warn]:

dub lint

2022-09-15 Thread Ali Çehreli via Digitalmars-d-learn
I've always thought of dub as a package manager and a build tool. But it actually makes it easy to use other tools: - dub lint: Runs some checks on your project. What I liked is how it removed the need to figure out how to install dscanner, which it uses behind the scenes. It installed

Re: need help to translate C into D

2022-09-13 Thread Ali Çehreli via Digitalmars-d-learn
On 9/13/22 04:07, test123 wrote: > On Tuesday, 13 September 2022 at 10:59:36 UTC, Dennis wrote: >> Side node, you can use `immutable` instead of `__gshared const`, it >> amounts to the same for global variables. > > because __enums_layout.ptr need to be part of other object, and this > const ptr

Re: Function attribute best practices

2022-09-13 Thread Ali Çehreli via Digitalmars-d-learn
On 9/13/22 10:08, Paul Backus wrote: > Here's my attempt, covering all the attributes found under > [`MemberFunctionAttribute`][1] in the language spec: > > |Attribute|Affects |Inferred?| > |-||-| > |nothrow |Function|Yes | > |pure |Function|Yes | > |@nogc

Re: Function attribute best practices

2022-09-13 Thread Ali Çehreli via Digitalmars-d-learn
On 9/12/22 09:39, Paul Backus wrote: > Yes. Except for `@trusted`, explicit attributes on template code are a > smell. Except for 'const' as well because some templates are member functions. And 'const' on a member function cannot be left to inference because it happens to be a part of the

Re: Function attribute best practices

2022-09-12 Thread Ali Çehreli via Digitalmars-d-learn
On 9/12/22 11:29, Steven Schveighoffer wrote: > So you are thinking about this the wrong way I believe. Clearly. > When you put `pure` on a template function, you are saying "only > instantiations where this function can be pure are allowed". Makes sense. I was trying to put as many

Re: Function attribute best practices

2022-09-12 Thread Ali Çehreli via Digitalmars-d-learn
On 9/12/22 10:29, H. S. Teoh wrote: write a unittest where you instantiate Foo with a deliberately-impure type Yes. A current on-topic thread on the difficulties of covering all corner cases: https://forum.dlang.org/thread/dmnfdqiplbldxkecp...@forum.dlang.org Ali

Re: Function attribute best practices

2022-09-12 Thread Ali Çehreli via Digitalmars-d-learn
On 9/12/22 09:48, H. S. Teoh wrote: >> @nogc nothrow pure @safe >> unittest >> { >> // ... >> } >> >> No, it isn't because unless my unittest code is impure, I can't catch >> my incorrect 'pure' etc. on my member functions. > [...] > > Sure you can. The `pure unittest` code obviously must

Function attribute best practices

2022-09-12 Thread Ali Çehreli via Digitalmars-d-learn
The following range Foo is trying to be helpful by adding as many attributes as it can ('const' is missing because ranges cannot be 'const' because at least popFront() needs to be mutable): import std.algorithm; struct Foo(R) { R r; int i; bool empty() @nogc nothrow pure @safe

Re: Why I get delegate when passing address of function?

2022-09-11 Thread Ali Çehreli via Digitalmars-d-learn
On 9/11/22 09:26, Ali Çehreli wrote: // This combines a class instance (which is a pointer behind the scene)     // Combine with the class object In both places I meant "class variable". Ali

Re: Why I get delegate when passing address of function?

2022-09-11 Thread Ali Çehreli via Digitalmars-d-learn
On 9/11/22 02:54, Injeckt wrote: > And what I should do to pass non-static function? You can combine your class object with other arguments and your thread function will know how to unwrap your class object to call its member function: import std.stdio; // I am not on Windows, so I am

Re: Dictionary of Templated Functions

2022-09-10 Thread Ali Çehreli via Digitalmars-d-learn
On 9/10/22 15:35, jwatson-CO-edu wrote: > So, my solution > will be to construct a catch-all struct `Payload` and have that be my > argument type from which various functions can draw the data of their > choice. Two Phobos features may be helpful there:

Re: Can you access the same classes from C++ and D and vise versa, or do the classes have to not form dependency cycle?

2022-09-10 Thread Ali Çehreli via Digitalmars-d-learn
On 9/10/22 13:04, Daniel Donnell wrote: > https://dlang.org/spec/cpp_interface.html At DConf, Manu indicated that that page is outdated and that D's C++ support is actually a lot better. He kind-of-promised to update that page but I doubt it happened yet if ever. :) > one has to be compiled

Re: Using .require for struct types

2022-09-10 Thread Ali Çehreli via Digitalmars-d-learn
On 9/10/22 09:33, Erdem Demir wrote: > DListOfA returnVal = temp.require("a", DListOfA());--> I wish I > could use ref DListOfA here But keeping a reference to a temporary would not work because the life of that temporary ends by the end of that expression (practically, at the

Re: Validate static asserts

2022-09-09 Thread Ali Çehreli via Digitalmars-d-learn
On 9/9/22 10:35, Dennis wrote: > On Friday, 9 September 2022 at 16:41:54 UTC, Andrey Zherikov wrote: >> What's about new `compileOutput` trait that returns compiler output? >> ```d >> static assert(__traits(compileOutput, { }) == "message"); >> ``` > > As a compiler dev, that sounds terrifying.

Re: Validate static asserts

2022-09-09 Thread Ali Çehreli via Digitalmars-d-learn
On 9/9/22 07:35, Andrey Zherikov wrote: > might not compile due to many different reasons I faced a related situation recently: My error string generation was buggy, which taught me that the compiler does not even compile the string part of 'static assert' in the 'true' case. The following

Re: Storing a lambda alongside type-erased data

2022-09-08 Thread Ali Çehreli via Digitalmars-d-learn
On 9/8/22 08:02, Paul Backus wrote: > This is actually pretty much exactly what VariantN does Great information, thanks! I am slowly getting up there. :) Ali

Storing a lambda alongside type-erased data

2022-09-07 Thread Ali Çehreli via Digitalmars-d-learn
I am sure nothing is new here and I may have thought of this before but it was a revelation today. :) I've been trying to come up with a way of storing arbitrary number of objects of arbitrary types, which means I would be using a ubyte array. But then how do I use the data later without

Re: Error "Unexpected '\n' when converting from type LockingTextReader to type int"

2022-09-07 Thread Ali Çehreli via Digitalmars-d-learn
On 9/7/22 16:24, Synopsis wrote: > a- What is the difference with this syntax with the exclamation mark? > ```readf!"%s\n"(f1.num);``` That's the templated version, which is safer because it checks at compile time (important distinction) that the arguments and the format specifiers do match.

Re: Comparing slices with std.variant.Algebraic

2022-09-05 Thread Ali Çehreli via Digitalmars-d-learn
On 9/5/22 01:58, anonymouse wrote: > array [1.7, 3.7, 5.7, 7.7, 9.7] in both cases, which is what is being > asserted by those two lines. None of those values can be represented precisely in a floating point type. Without looking at the code, I wonder whether the tests will pass if you can

Re: Why do failed contracts don't dump stack backtrace in unittests?

2022-09-04 Thread Ali Çehreli via Digitalmars-d-learn
On 9/4/22 09:35, Paul Backus wrote:     // TODO: omit stack trace only if assert was thrown     // directly by the unittest. Thank you but I mean... :) I can understand removing a backtrace from the eyes of an end user but the consumer of a unittest output is a developer, no? Ali

Why do failed contracts don't dump stack backtrace in unittests?

2022-09-04 Thread Ali Çehreli via Digitalmars-d-learn
The program output is different whether an Error is thrown from main or from the unittest block: void foo(string s) in (s != "hello") { } unittest { foo("hello"); // No stack backtrace } void main() { foo("hello"); // Yes stack backtrace } Ali

Re: Best practice for dub registry package and module names

2022-09-03 Thread Ali Çehreli via Digitalmars-d-learn
On 9/3/22 20:39, Ali Çehreli wrote: For example, there is fixedsizearray, which does not belong to any package: https://code.dlang.org/packages/fixedsizearray On the other hand, arsd-official:minigui does have a package. (And that answers a question: Dash character is acceptable in package

Re: Best practice for dub registry package and module names

2022-09-03 Thread Ali Çehreli via Digitalmars-d-learn
On 9/3/22 20:04, rikki cattermole wrote: > This slightly smells, single module dub packages. > > What does each module do? The other issue is NIH because some of their functionality already exists. :/ A: Block of elements B: Expanding circular buffer C: Cache of elements I would like to

Best practice for dub registry package and module names

2022-09-03 Thread Ali Çehreli via Digitalmars-d-learn
Let's say I have three modules that work together, which I want to register on dub: A, B, and C. Although the most interesting one is C and A and B are used in its implementation, A and B can be registered individually as well and be used as C's dependencies. I know package-less modules are

Re: Error while generate DNA with uniform()

2022-09-03 Thread Ali Çehreli via Digitalmars-d-learn
On 9/3/22 14:18, Salih Dincer wrote: >uniform!"[]"(DNA.min, DNA.max); Even cleaner: uniform!DNA() :) Ali

Re: Error while generate DNA with uniform()

2022-09-03 Thread Ali Çehreli via Digitalmars-d-learn
On 9/3/22 07:25, Steven Schveighoffer wrote: > There is probably a bug in generate when the element type is an `enum` > which somehow makes it const. Yes, Generator is missing an Unqual: https://issues.dlang.org/show_bug.cgi?id=23319 Salih had asked: >> Can we solve this issue with our own

Re: Constructors not working

2022-09-02 Thread Ali Çehreli via Digitalmars-d-learn
I forgot to say that you don't need to write a constructor for most structs because Time's constructor-generated default constructor works like yours and with default arguments: struct Time { public int hours, minutes, seconds; // No constructor needed here. // Note 'return this;' as

Re: Constructors not working

2022-09-02 Thread Ali Çehreli via Digitalmars-d-learn
On 9/2/22 11:35, Svyat wrote: > Time time = 360; It seems to be more idiomatic to write it like this: auto time = Time(360); Or const, immutable, etc. const time = Time(360); // Now un-assignable But you would get the same compilation error. So, one way to work with it is to

Re: Convert array of tuples into array of arrays.

2022-08-31 Thread Ali Çehreli via Digitalmars-d-learn
On 8/31/22 07:34, musculus wrote: > Hi. I have an array of tuples that I would like to convert to an array > of arrays. I misunderstood as well and wrote the following program which makes separate arrays. You can make an array of arrays from those with the following syntax: auto

Re: is it possible synchronized(null) ? i.e NO-OP

2022-08-26 Thread Ali Çehreli via Digitalmars-d-learn
On the main forum, Paul Backus proposed a nested function as well as a scoped lock. On 8/26/22 10:13, mw wrote: >Object lock = (a particular condition) ? realLock : null; And I want to point out that "a particular condition" must not change between the check above and the following

Re: Is it possible to return mutable and const range from a single method?

2022-08-22 Thread Ali Çehreli via Digitalmars-d-learn
On 8/22/22 09:36, realhet wrote: > It gives the protection I was needed but is it possible to make this > prettier? > auto allParents(){ >struct ParentRange{ > A act; > @property bool empty() const{ return act is null; } > @property A front() { return

Re: typeof(func!0) != typeof(func!0())

2022-08-21 Thread Ali Çehreli via Digitalmars-d-learn
On 8/21/22 21:39, Andrey Zherikov wrote: > alias type = typeof(U().func!0); > pragma(msg, type); // pure nothrow @nogc ref @safe U() > return This is where the @property keyword makes a difference: @property auto ref func(int i)() { return this; } Now U().func!0

Re: Recommendation for parallelism with nested for loops?

2022-08-18 Thread Ali Çehreli via Digitalmars-d-learn
On 8/18/22 18:49, Shriramana Sharma wrote: > Hello. I want to parallelize a computation which has two for loops An option is to add tasks individually but I am not sure how wise doing this and I don't know how to determine whether all tasks are completed. In any case, Roy Margalit's DConf

Re: In-place extension of arrays only for certain alignment?

2022-08-17 Thread Ali Çehreli via Digitalmars-d-learn
On 8/17/22 19:27, Steven Schveighoffer wrote: > On 8/17/22 10:09 PM, Ali Çehreli wrote: >> > IIRC, your data does not need to be sequential in *physical memory*, >> > which means you can use a ring buffer that is segmented instead of >> > virtually mapped, and that can be of any size. >> >> I

Re: In-place extension of arrays only for certain alignment?

2022-08-17 Thread Ali Çehreli via Digitalmars-d-learn
On 8/17/22 18:31, Steven Schveighoffer wrote: > 1. I highly recommend trying out the ring buffer solution to see if it > helps. The disadvantage here is that you need to tie up at least a page > of memory. I started to think holding on to multiple pages of memory should not matter anyway. If

Re: In-place extension of arrays only for certain alignment?

2022-08-17 Thread Ali Çehreli via Digitalmars-d-learn
On 8/16/22 19:33, Steven Schveighoffer wrote: > Everything in the memory allocator is in terms of pages. A pool is a > section of pages. The large blocks are a *multiple* of pages, whereas > the small blocks are pages that are *divided* into same-sized chunks. Thank you. I am appreciating this

Re: Programs in D are huge

2022-08-17 Thread Ali Çehreli via Digitalmars-d-learn
On 8/17/22 09:28, Diego wrote: > I'm writing a little terminal tool, so i think `-betterC` is the best > and simple solution in my case. It depends on what you mean with terminal tool bun in general, no, full features of D is the most useful option. I've written a family of programs that

Re: In-place extension of arrays only for certain alignment?

2022-08-16 Thread Ali Çehreli via Digitalmars-d-learn
Thank you for the quick response. On 8/16/22 12:31, Steven Schveighoffer wrote: > On 8/16/22 2:11 PM, Ali Çehreli wrote: >> Related to my DConf 2022 lightning talk, I am noticing that D >> runtime's in-place array extension optimization is available only for >> array data that are at certain

In-place extension of arrays only for certain alignment?

2022-08-16 Thread Ali Çehreli via Digitalmars-d-learn
Related to my DConf 2022 lightning talk, I am noticing that D runtime's in-place array extension optimization is available only for array data that are at certain memory alignments. I used the following program to test it. In addition to repeatedly adding an element to an array, - 'version

Re: Exercise at end of Ch. 56 of "Programming in D"

2022-08-14 Thread Ali Çehreli via Digitalmars-d-learn
On 8/14/22 19:23, Ali Çehreli wrote: > // BUG DUE TO WISHFUL THINKING: > override size_t toHash() const { >/* Since the 'points' member is an array, we can take > * advantage of the existing toHash algorithm for > * array types. */ >return

Re: Exercise at end of Ch. 56 of "Programming in D"

2022-08-14 Thread Ali Çehreli via Digitalmars-d-learn
On 8/14/22 18:47, johntp wrote: > I'm using DMD64 D Compiler v2.100.0 on linux mint. Same version here. > I copied the author's > solution and got the same thing. Wow! Are people actually working on those? :) At first, I couldn't even get the code to compile due to const-correctness issues

Re: chain of exceptions, next method

2022-08-13 Thread Ali Çehreli via Digitalmars-d-learn
On 8/13/22 15:59, kdevel wrote: > Quote from `src/druntime/src`: > > ``` > /** > * Returns: > * A reference to the _next error in the list. This is used when a new > * $(D Throwable) is thrown from inside a $(D catch) block. The > originally > * caught $(D

Re: "min" and "max"

2022-08-09 Thread Ali Çehreli via Digitalmars-d-learn
On 8/9/22 17:03, pascal111 wrote: > They said " If at least one of the arguments is NaN, the result is an > unspecified value. That's called "unorderedness": http://ddili.org/ders/d.en/floating_point.html#ix_floating_point.unordered > as a beginner how can I guess what "NaNs" > means or if

Re: A look inside "filter" function defintion

2022-08-09 Thread Ali Çehreli via Digitalmars-d-learn
On 8/2/22 05:39, pascal111 wrote: > I'm still stuck. Do you have a > down-to-earth example for beginners to understand this concept? I will refer to my explanation because down-to-earth has always been my goal. I hope i succeeded:

Re: Breaking ";" rule with lambda functions

2022-08-09 Thread Ali Çehreli via Digitalmars-d-learn
On 8/2/22 09:40, pascal111 wrote: > Maybe I'd wrong beliefs about lambda function. It's already in C++, so > it's a traditional feature Lambdas are a common feature of many programming languages. C++ got lambdas in their C++11 release, many years after D and many other languages had them. (It

Re: char* pointers between C and D

2022-08-09 Thread Ali Çehreli via Digitalmars-d-learn
On 7/25/22 06:51, ryuukk_ wrote: > On Monday, 25 July 2022 at 11:14:56 UTC, pascal111 wrote: >> const(char)[] ch1 = "Hello World!"; >> char[] ch2="Hello World!".dup; [...] > `ch1`is a string literal, just like in C, it is null terminated To be pedantic, ch1 is not the string

Re: OK to do bit-packing with GC pointers?

2022-08-09 Thread Ali Çehreli via Digitalmars-d-learn
On 7/22/22 09:50, Ben Jones wrote: > any problems with the GC? The slides don't seem to mention the GC but Amaury Séchet had given a presentation on bit packing: http://dconf.org/2016/talks/sechet.html Ali

Re: "chain" vs "~"

2022-08-08 Thread Ali Çehreli via Digitalmars-d-learn
On 8/6/22 18:22, pascal111 wrote: > Why we use "chain" while we have "~": > > '''D > int[] x=[1,2,3]; > int[] y=[4,5,6]; > > auto z=chain(x,y); > auto j=x~y; > ''' To add to what has already mentioned, - chain can be used on ranges that are of different element types - as usual, some of the

Re: How do I initialize a templated constructor?

2022-08-08 Thread Ali Çehreli via Digitalmars-d-learn
Here is another one that uses nested templates: import std.stdio; template TestArray(ulong element_n) { struct TestArrayImpl(Type) { int[element_n] elements; this(ulong number) { pragma(msg, "The type is: ", Type); writeln("Constructing with ", number); } } auto

Re: Acess variable that was set by thread

2022-08-08 Thread Ali Çehreli via Digitalmars-d-learn
On 8/8/22 00:14, vc wrote: > i will like to hear thoughts even if it works > for me __gshared would work as well but I would consider std.concurrency first. Just a simple example: import std.stdio; import std.concurrency; import core.thread; struct Result { int value; } struct Done { }

Re: How do I initialize a templated constructor?

2022-08-08 Thread Ali Çehreli via Digitalmars-d-learn
On 8/7/22 22:38, rempas wrote: > I want to create it and be able to successfully initialize the template > parameters > of the constructor but until now, I wasn't able to find a way to > successfully do > that. The following method uses a convenience function but it's not really needed: import

Re: Ranges

2022-08-07 Thread Ali Çehreli via Digitalmars-d-learn
On 8/6/22 22:58, Salih Dincer wrote: > Ranges are not like that, all they do is > generate. You may be right. I've never seen it that way. I've been under the following impression: - C++'s iterators are based on an existing concept: pointers. Pointers are iterators. - D's ranges are based

Re: Ranges

2022-08-07 Thread Ali Çehreli via Digitalmars-d-learn
On 8/7/22 08:34, pascal111 wrote: > Everyone knows that slices are not pointers D's slices are "fat pointers": In D's case, that translates to a pointer plus length. > that pointers are real work, Agreed. Pointers are fundamental features of CPUs. > but slices are like a simple un-deep

Re: Ranges

2022-08-06 Thread Ali Çehreli via Digitalmars-d-learn
On 8/6/22 14:10, pascal111 wrote: > the problem is that ranges in D lack the usage of pointers as > an essential tool to make all of ranges functions they need. If ranges > exist in C, they would use pointers, and this is There are a few cases where pointers provide functionality that ranges

Re: Ranges

2022-08-06 Thread Ali Çehreli via Digitalmars-d-learn
On 8/6/22 09:33, Salih Dincer wrote: > the slices feel like ranges, don't they? Yes because they are ranges. :) (Maybe you meant they don't have range member functions, which is true.) D's slices happen to be the most capable range kind: RandonAccessRange. All of the following operations

Re: Ranges

2022-08-05 Thread Ali Çehreli via Digitalmars-d-learn
On 8/5/22 01:59, frame wrote: > On Thursday, 4 August 2022 at 22:14:26 UTC, Ali Çehreli wrote: > >> No element is copied or moved. :) >> >> Ali > > I know that :) And I know that. :) We don't know who else is reading these threads, so I didn't want to give wrong impression. Copying would

Re: Ranges

2022-08-04 Thread Ali Çehreli via Digitalmars-d-learn
On 8/4/22 11:05, frame wrote: > `popFront()` The function was this: void popFront() { students = students[1 .. $]; } > copies all > elements except the first one into the variable (and overwrites it), so > it moves the data forward. That would be very slow. :) What

Re: Ranges

2022-08-04 Thread Ali Çehreli via Digitalmars-d-learn
On 8/4/22 06:08, pascal111 wrote: > In next code from > "https://www.tutorialspoint.com/d_programming/d_programming_ranges.htm;, That page seems to be adapted from this original: http://ddili.org/ders/d.en/ranges.html > we have two issues: > > 1) Why the programmer needs to program

Re: Obsecure problem 2

2022-08-04 Thread Ali Çehreli via Digitalmars-d-learn
On 8/4/22 00:57, pascal111 wrote: > I don't see my post. Some posts are blocked by the spam filter. (Apparently, your message did not have a sender name and cannot be passed because of that.) Ali

Re: Converting JSONValue to AssociativeArray.

2022-08-01 Thread Ali Çehreli via Digitalmars-d-learn
On 8/1/22 15:47, Steven Schveighoffer wrote: You beat me to it. I used .object and .str: import std; void main() { JSONValue data = parseJSON(`{ "name": "Hype Editor", "hobby": "Programming" }`); writefln("%s", data); // This already sees the data as an AA but the type is

Re: Exclamation symbol "!" within functions syntax

2022-07-27 Thread Ali Çehreli via Digitalmars-d-learn
On 7/27/22 04:00, pascal111 wrote: I noticed more than once that the exclamation "!" is used within functions typing, and it seems like an operator with new use, for example "to!int()", ".tee!(l => sum += l.length)", "enforce!MissingArguments...", so what dose it means? The binary !

Re: Particular exceptions names

2022-07-27 Thread Ali Çehreli via Digitalmars-d-learn
On 7/26/22 16:43, pascal111 wrote: > In next example code, it used user-made exception, I am not sure I understand you correctly because the program you show throws Exception, which is not user-made at all. If you want to throw a particual exception that you define, you need to inherit that

Re: How to create Multi Producer-Single Consumer concurrency

2022-07-13 Thread Ali Çehreli via Digitalmars-d-learn
On 7/13/22 02:25, Bagomot wrote: > How to do the same with `taskPool` instead of `spawnLinked`? You are hitting the nail on the head. :) std.parallelism, which taskPool is a concept of, is for cases where operations are independent. However, producer and consumer are by definition dependent,

Re: Background thread, async and GUI (dlangui)

2022-07-12 Thread Ali Çehreli via Digitalmars-d-learn
On 7/12/22 11:47, Bagomot wrote: > I now have a couple more questions about `Task`: > 1) How to run a non-static class method through `task`? > 2) How to use `taskPool` to run a series of tasks of the same type (from > question 1)? As a friendly reminder, these questions could be more useful in

Re: null == "" is true?

2022-07-12 Thread Ali Çehreli via Digitalmars-d-learn
On 7/12/22 10:11, Steven Schveighoffer wrote: > The algorithm to compare *any* arrays is first verify the lengths are > the same. Then for each element in the array, compare them. Since there > are 0 elements in both the empty string and the null string, they are > equal. Checking .empty()

Re: How can I match every instance of a template type (struct)?

2022-07-12 Thread Ali Çehreli via Digitalmars-d-learn
On 7/12/22 06:34, rempas wrote: >static if (is(typeof(obj) == Test)) { printf("YES!!!\n"); } An alternative: import std.traits; static if (isInstanceOf!(Test, typeof(obj))) { printf("YES!!!\n"); } https://dlang.org/phobos/std_traits.html#isInstanceOf Ali

Re: Background thread, async and GUI (dlangui)

2022-07-08 Thread Ali Çehreli via Digitalmars-d-learn
On 7/8/22 06:32, Bagomot wrote: > I do as in Ali's example, but > the GUI is still blocked: I don't know exactly why but something looks suspicious. > ```d > auto btn = new Button("Run"d); > btn.click = delegate(Widget src) { > auto request = Request("dlang.org"); That is a local

Re: Background thread, async and GUI (dlangui)

2022-07-07 Thread Ali Çehreli via Digitalmars-d-learn
On 7/6/22 16:17, Ali Çehreli wrote: > I would consider std.parallelism And it looks more natural with a std.parallelism.Task: struct Progress { size_t percent_; void set(size_t downloaded, size_t total) { if (total != 0) { import core.atomic: atomicStore; const value =

Re: Background thread, async and GUI (dlangui)

2022-07-06 Thread Ali Çehreli via Digitalmars-d-learn
On 7/6/22 02:26, Bagomot wrote: > 1) How to make asynchronous HTTP requests with curl in order to receive > progress and status? And how do you know that the request is completed? I don't know how dlangui or others automate this but I had fun writing the following program that uses

Re: Calling readln() after readf

2022-07-05 Thread Ali Çehreli via Digitalmars-d-learn
On 7/5/22 17:14, Gary Chike wrote: > So, in order to use `parse!int()`, I would need to separate it into two > statements with a variable acting as an intermediary: > ``` > auto input = readln(); > auto age = parse!int(input); Exactly. parse takes the input by reference (necessitating an

Re: How to call a function from a dll created with d ?

2022-07-01 Thread Ali Çehreli via Digitalmars-d-learn
On 7/1/22 12:11, Vinod KC wrote: The following function is dimedll.testFunc: > ```d > module dimedll; // ... > export void testFunc() { > writeln("This is from dll"); > } > ``` We suspect the name of the file that defines main() is dime.d. > extern void testFunc(); That symbol belongs

Re: Better way to achieve the following

2022-06-21 Thread Ali Çehreli via Digitalmars-d-learn
On 6/21/22 10:09, JG wrote: Suppose we are often writing something like ```d theFirstName[theFirstIndex].theSecondName[theSecondIndex].thirdName[theThirdIndex]=x; ``` One would like to something like ```d alias shortName =

Re: Calling readln() after readf

2022-06-20 Thread Ali Çehreli via Digitalmars-d-learn
On 6/20/22 07:00, Gary Chike wrote: > Would it be appropriate to forego `readf` > and read input as a string using `readln` ,benefiting from the `strip` > function, then convert to their appropriate datatype Makes sense. The following are related as well:

Re: Calling readln() after readf

2022-06-19 Thread Ali Çehreli via Digitalmars-d-learn
On 6/19/22 15:52, Gary Chike wrote: > On Saturday, 24 April 2021 at 22:13:45 UTC, Ali Çehreli wrote: >> On 4/24/21 7:46 AM, PinDPlugga wrote: >> ... >> As a general solution, you can use a function like this: >> >> auto readLine(S = string)(File file = stdin) { >> while (!file.eof) { >>

Re: std.conv.to

2022-06-17 Thread Ali Çehreli via Digitalmars-d-learn
On 6/17/22 10:04, Salih Dincer wrote: > Isn't foo and bar the same thing? I don't understand what's the > difference! >auto foo = to!Foo("123"); //?? >auto bar = Foo("321"); Yes, they are the same thing. The OP was looking for a generic way of converting any type to any other type

Re: Creating DLL

2022-06-16 Thread Ali Çehreli via Digitalmars-d-learn
On 6/16/22 09:32, Adam D Ruppe wrote: > This is why an explicit initialization call is the preferred method - > there, the time it is called is well-defined by the user after initial > loading is complete. Agreed but that excludes using the D runtime in 'static this' (and shared) blocks,

Re: Creating DLL

2022-06-16 Thread Ali Çehreli via Digitalmars-d-learn
On 6/16/22 09:07, Sergeant wrote: > May I ask one more question: why a code like this would work in > D-application but not in D-DLL? D programs generated by D compilers automatically initialize the D runtime. You can do the same with rt_init: pragma (crt_constructor) extern(C) int

Re: map! evaluates twice

2022-06-16 Thread Ali Çehreli via Digitalmars-d-learn
On 6/16/22 00:58, Salih Dincer wrote: > I guess the developed cached() and cache() are different things, > right? cache caches only the front element. https://dlang.org/library/std/algorithm/iteration/cache.html > I tried cached() cached() is supposed to cache as many elements as needed as

Re: Consuming D libraries from other languages

2022-06-15 Thread Ali Çehreli via Digitalmars-d-learn
On 6/15/22 10:37, Templated Person wrote: > It there any resources on how to build D static (`.lib` / `.a`) and > dynamic libraries (`.dll` / `.so`), and then use them from C? > > Do I need to link and initialize phobos somehow? Not Phobos but the D runtime. > What if I don't want to > use the

Re: Closures over temporary variables

2022-06-14 Thread Ali Çehreli via Digitalmars-d-learn
On 6/14/22 02:04, bauss wrote: > You have to do it like this: > > ``` > dgs ~= ( (n) => () { writeln(n); })(i); > ``` The same thing with a named function as well as with iota(): import std.range; import std.algorithm; import std.stdio; void main() { void delegate()[] dgs; auto

Re: a struct as an multidimensional array index

2022-06-11 Thread Ali Çehreli via Digitalmars-d-learn
On 6/11/22 13:36, z wrote: > i meant with the syntax in (1), the spec's documentation appears to say > they are equivalent in result with `new *type*[X][Y]` form. > > (1) https://dlang.org/spec/expression#new_multidimensional (3. multiple > argument form) Thank you. I see now: The values in

Re: a struct as an multidimensional array index

2022-06-11 Thread Ali Çehreli via Digitalmars-d-learn
On 6/11/22 00:09, z wrote: > I rechecked and it should be `X Y Z` for static array, but `Z Y X` for > indexing/dynamic array creating with `new` How so? I wrote the following program: import std.stdio; void main() { enum X = 2; enum Y = 3; enum Z = 4; int[X][Y][Z] s; int[X][Y][] d

Re: a struct as an multidimensional array index

2022-06-11 Thread Ali Çehreli via Digitalmars-d-learn
On 6/11/22 04:16, Salih Dincer wrote: > I think D is very consistent with our feelings. That is, the order in > memory is in the form of rows x columns. Yet, there are no rows or columns because neither D nor C (nor C++) have multip-dimensional arrays. They all have arrays where elements are

Re: map! evaluates twice

2022-06-10 Thread Ali Çehreli via Digitalmars-d-learn
On 6/10/22 13:47, Steven Schveighoffer wrote: > `map` calls the lambda for each call to `front`. If you want a cached > version, use `cache`: Others don't know but as I will likely show during a lightning talk at DConf, I am trying to finish a .cached range algorithm that caches all elements

Re: Run a command-line process with data sent, and retrieve the data.

2022-06-10 Thread Ali Çehreli via Digitalmars-d-learn
On 6/10/22 10:40, Ali Çehreli wrote: >https://dlang.org/library/std/process/pipe_process.html I realized you may be happier with the following if all you need is stdout: https://dlang.org/library/std/process/execute.html https://dlang.org/library/std/process/execute_shell.html Ali

Re: Run a command-line process with data sent, and retrieve the data.

2022-06-10 Thread Ali Çehreli via Digitalmars-d-learn
On 6/10/22 10:37, Chris Katko wrote: > I want to pipe in string data to a shell/commandline program, then > retrieve the output. But the documentation I read appears to only show > usage for 'Files' for stdin/stdout/stderr. > > ala something like this: > D > string input = "hello\nworld"; >

Re: Range to Nullable conversion

2022-06-10 Thread Ali Çehreli via Digitalmars-d-learn
On 6/10/22 10:22, Antonio wrote: > Is there any alternative to ***range front*** that returns a Nullable > (i.e. **frontAsMonad** or **frontAsNullable**)? import std; // Spelling? :) auto nullablelize(R)(R range) { alias E = Nullable!(ElementType!R); struct Nullablelize { enum empty =

Re: a struct as an multidimensional array index

2022-06-10 Thread Ali Çehreli via Digitalmars-d-learn
On 6/10/22 08:13, z wrote: > arrays of arrays has different order for declaration and addressing, > and declaring array of arrays has different order depending on how you > declare it and wether it's static or dynamic array, *oof*) > > To give you an idea of the situation : > ```D >

Re: a struct as an multidimensional array index

2022-06-10 Thread Ali Çehreli via Digitalmars-d-learn
On 6/10/22 08:01, Ali Çehreli wrote: > I still don't understand the reason though. The rows would be copied > without ref but should retain their type as bool[3], a static array. (?) Ok, now I see the very sinister problem: It is a disaster to combine static array lambda parameters with the

'each' can take static arrays

2022-06-10 Thread Ali Çehreli via Digitalmars-d-learn
I know static arrays are not ranges but somehow they work with 'each'. With map, I need to slice as 'arr[]': import std; void main() { int[3] arr; arr.each!(e => e);// Compiles // arr.map!(e => e); // Fails to compile arr[].map!(e => e); // Compiles } Why the inconsistency?

Re: a struct as an multidimensional array index

2022-06-10 Thread Ali Çehreli via Digitalmars-d-learn
On 6/10/22 07:38, Ali Çehreli wrote: > I played with that toString function but for some reason it prints all > Ts. (?) Fixed it by changing one of the lambdas to take by reference: void toString(scope void delegate(in char[]) sink) const { import std.algorithm;

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