Re: Challenge Tuples

2024-04-26 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 26 April 2024 at 13:25:34 UTC, Salih Dincer wrote: You have a 5-item data tuples as Tuple(1, 2, 3, [1, 3], 5) and implement the sum (total = 15) with the least codes using the sum() function of the language you are coding... My Python solution (function named dosum to avoid collisio

TIL: statically initializing an Associative Array

2024-05-06 Thread Andy Valencia via Digitalmars-d-learn
I had a set of default error messages to go with error code numbers, and did something along the lines of: string[uint] error_text = [ 400: "A message", 401: "A different message" ]; and got "expression is not a constant" I eventually found this discussion: https://issues.dlan

Re: TIL: statically initializing an Associative Array

2024-05-06 Thread Andy Valencia via Digitalmars-d-learn
On Tuesday, 7 May 2024 at 01:14:24 UTC, Steven Schveighoffer wrote: On Tuesday, 7 May 2024 at 00:10:27 UTC, Andy Valencia wrote: I had a set of default error messages to go with error code numbers, and did something along the lines of: string[uint] error_text = [ 400: "A message", 401:

"in" operator gives a pointer result from a test against an Associative Array?

2024-05-09 Thread Andy Valencia via Digitalmars-d-learn
tst7.d(6): Error: cannot implicitly convert expression `e in this.members` of type `bool*` to `bool` tst7.d(15): Error: template instance `tst7.Foo!uint` error instantiating I'm getting this for this bit of source (trimmed from the bigger code). I switched to this.members.get(e, false) and th

Re: "in" operator gives a pointer result from a test against an Associative Array?

2024-05-09 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 10 May 2024 at 00:40:01 UTC, Meta wrote: Yes. The reason for this is that it avoids having to essentially do the same check twice. If `in` returned a bool instead of a pointer, after checking for whether the element exists (which requires searching for the element in the associative

Re: "in" operator gives a pointer result from a test against an Associative Array?

2024-05-10 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 10 May 2024 at 03:07:43 UTC, Steven Schveighoffer wrote: Yes, we say that a type has "truthiness" if it can be used in a condition (`while`, `if`, `assert`, etc). So if I may ask for one more small clarification... WRT "truthiness", I've observed that empty arrays are treated as fa

Re: "in" operator gives a pointer result from a test against an Associative Array?

2024-05-10 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 10 May 2024 at 16:33:53 UTC, Nick Treleaven wrote: Arrays evaluate to true in boolean conditions if their `.ptr` field is non-null. This is bug-prone and I hope we can remove this in the next edition. ... A string literal's `.ptr` field is always non-null, because it is null-termina

FIFO

2024-05-11 Thread Andy Valencia via Digitalmars-d-learn
I need a FIFO for a work scheduler, and nothing suitable jumped out at me. I wrote the following, but as a newbie, would be happy to receive any suggestions or observations. TIA! /* * fifo.d * FIFO data structure */ module tiny.fifo; import std.exception : enforce; const uint GROWBY

Re: FIFO

2024-05-12 Thread Andy Valencia via Digitalmars-d-learn
On Sunday, 12 May 2024 at 19:45:44 UTC, Ferhat Kurtulmuş wrote: On Saturday, 11 May 2024 at 23:44:28 UTC, Andy Valencia wrote: I need a FIFO for a work scheduler, and nothing suitable jumped out at me. ... https://dlang.org/phobos/std_container_slist.html This is a stack, isn't it? LIFO? An

Re: FIFO

2024-05-13 Thread Andy Valencia via Digitalmars-d-learn
On Sunday, 12 May 2024 at 22:03:21 UTC, Ferhat Kurtulmuş wrote: https://dlang.org/phobos/std_container_slist.html This is a stack, isn't it? LIFO? Ahh yes. Then use dlist Thank you. I read its source, and was curious so I wrote a small performance measurement: put 10,000 things in a FIFO,

Parallel safe associative array?

2024-05-24 Thread Andy Valencia via Digitalmars-d-learn
I was playing with parallel programming, and experienced "undefined behavior" when storing into an Associative Array in parallel. Guarding the assignments with a synchronized barrier fixed it, of course. And obviously loading down your raw AA with thread barriers would be foolish. But this

Problem with clear on shared associative array?

2024-05-26 Thread Andy Valencia via Digitalmars-d-learn
The following code fails to compile; it appears from the error message that the library's clear() function is not ready to act on a shared AA? synchronized class F { private: string[int] mydict; public: void clear() { this.mydict.clear(); } } void main() { auto f = ne

Re: Problem with clear on shared associative array?

2024-05-26 Thread Andy Valencia via Digitalmars-d-learn
On Sunday, 26 May 2024 at 20:00:50 UTC, Jonathan M Davis wrote: No operation on an associative array is thread-safe. As such, you should not be doing _any_ operation on a shared AA without first locking a mutex to protect it. Then you need to cast away shared to access or mutate it or do whatev

Re: Problem with clear on shared associative array?

2024-05-27 Thread Andy Valencia via Digitalmars-d-learn
On Monday, 27 May 2024 at 04:04:03 UTC, mw wrote: Pls NOTE: it is a `sharded` (meaning trunk-ed) NON-concurrent map, not `shared` concurrent map. Assuming I put it in shared memory, in what way is it not able to be used concurrently? It seems to have the needed lock operations? Thanks, A

Socket and spawn()

2024-05-31 Thread Andy Valencia via Digitalmars-d-learn
I'm coding a server which takes TCP connections. I end up in the main thread with .accept() which hands me a Socket. I'd like to hand this off to a spawn()'ed thread to do the actual work. Aliases to mutable thread-local data not allowed. Is there some standard way to get something which

Re: Socket and spawn()

2024-05-31 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 31 May 2024 at 19:48:37 UTC, kdevel wrote: Have you taken into consideration that each of the (pre-spawned) threads can call accept()? Your program may also accept in multiple processes on the same socket. [1] Yes, but I am planning on some global behavior--mostly concerning resour

Re: Socket and spawn()

2024-05-31 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 31 May 2024 at 16:59:08 UTC, Jonathan M Davis wrote: Strictly speaking, unless you're dealing with a module or static-level variable, the object is not in TLS. It's treated as thread-local by the type system, and the type system will assume that no other thread has access to it, but

Re: Socket and spawn()

2024-06-02 Thread Andy Valencia via Digitalmars-d-learn
On Sunday, 2 June 2024 at 17:46:09 UTC, bauss wrote: If anything you should use a thread pool that each handles a set of sockets, instead of each thread being a single socket. Yup, thread pool it is. I'm still fleshing out the data structure which manages the incoming work presented to the p

Re: How to pass in reference a fixed array in parameter

2024-06-04 Thread Andy Valencia via Digitalmars-d-learn
On Tuesday, 4 June 2024 at 12:22:23 UTC, Eric P626 wrote: I tried to find a solution on the internet, but could not find anything, I stumble a lot on threads about Go or Rust language even if I specify "d language" in my search. Aside from the excellent answer already present, I wanted to me

Unintentional sharing?

2024-06-06 Thread Andy Valencia via Digitalmars-d-learn
I was using instance initialization which allocated a new object. My intention was this initialization would happen per-instance, but all instances appear to share the same sub-object? That is, f1.b and f2.b appear to point to a single object? Obviously I moved the new into the initializer c

Pointer to dlang spec for this alias construct?

2024-06-16 Thread Andy Valencia via Digitalmars-d-learn
In the alias: alias Unshared(T) = T; alias Unshared(T: shared U, U) = U; as used in: cast(Unshared!mytype)value turns a mytype with shared attribute into one without shared. I deduce the alias is using some sort of type matching and decomposition? I've read through the language

Re: Pointer to dlang spec for this alias construct?

2024-06-17 Thread Andy Valencia via Digitalmars-d-learn
On Monday, 17 June 2024 at 05:05:06 UTC, Jonathan M Davis wrote: alias Unshared(T) = T; alias Unshared(T: shared U, U) = U; ... Unshared is an eponymous template. https://dlang.org/spec/template.html#implicit_template_properties And it's using a shortcut syntax. ... The second templat

Re: How to find the right function in the Phobos library?

2024-08-20 Thread Andy Valencia via Digitalmars-d-learn
On Saturday, 17 August 2024 at 17:31:53 UTC, Steven Schveighoffer wrote: On Saturday, 17 August 2024 at 05:28:37 UTC, Bruce wrote: What is the best way to search for a function in the Phobos library? Go to dlang.org, select dicumentation, then library reference. Pick any module, click on it

Re: How to find the right function in the Phobos library?

2024-08-21 Thread Andy Valencia via Digitalmars-d-learn
On Wednesday, 21 August 2024 at 20:45:10 UTC, IchorDev wrote: You should’ve probably considered using the equivalent function from Phobos because it’s a D function so it can be inlined and such: https://dlang.org/library/std/bitmanip/native_to_big_endian.html Brilliant, that API gives me exa

Re: Can the send function send an array?

2024-09-10 Thread Andy Valencia via Digitalmars-d-learn
On Tuesday, 10 September 2024 at 13:14:05 UTC, Fox wrote: // I am learning how to send and receive data. The following is my intention, but it cannot be compiled. // aliases to mutable thread-local data not allowed, what does it mean? thank you. dlang tries to use the type system to make one b

Re: assert

2024-09-12 Thread Andy Valencia via Digitalmars-d-learn
On Thursday, 12 September 2024 at 22:34:04 UTC, user1234 wrote: On Wednesday, 11 September 2024 at 10:08:29 UTC, ryuukk_ wrote: On Wednesday, 11 September 2024 at 09:14:39 UTC, Nick Treleaven wrote: On Wednesday, 11 September 2024 at 08:08:45 UTC, ryuukk_ wrote: [...] I again apologies for bei

Question on shared memory concurrency

2024-03-03 Thread Andy Valencia via Digitalmars-d-learn
I tried a shared memory parallel increment. Yes, it's basically a cache line thrasher, but I wanted to see what's involved in shared memory programming. Even though I tried to follow all the rules to make true shared memory (not thread local) it appears I failed, as the wait loop at the end o

Re: Question on shared memory concurrency

2024-03-04 Thread Andy Valencia via Digitalmars-d-learn
On Monday, 4 March 2024 at 03:42:48 UTC, Richard (Rikki) Andrew Cattermole wrote: A way to do this without spawning threads manually: ... Thank you! Of course, a thread dispatch per atomic increment is going to be s.l.o.w., so not surprising you had to trim the iterations. Bug I still hope

Re: Question on shared memory concurrency

2024-03-04 Thread Andy Valencia via Digitalmars-d-learn
On Monday, 4 March 2024 at 16:02:50 UTC, Andy Valencia wrote: On Monday, 4 March 2024 at 03:42:48 UTC, Richard (Rikki) Andrew Cattermole wrote: ... I still hope to be able to share memory between spawned threads, and if it isn't a shared ref of a shared variable, then what would it be? Do I ha

Re: Question on shared memory concurrency

2024-03-05 Thread Andy Valencia via Digitalmars-d-learn
On Monday, 4 March 2024 at 18:08:52 UTC, Andy Valencia wrote: For any other newbie dlang voyagers, here's a version which works as expected using the system memory allocator. On my little i7 I get 1.48 secs wallclock with 5.26 CPU seconds. ... Using a technique I found in a unit test in std/

static functions?

2024-03-11 Thread Andy Valencia via Digitalmars-d-learn
Leveraging my knowledge of C, I assumed a "static" function would be hidden outside of its own source file. I can't find any statement about the semantics of a static function in the documentation, and in practice (ldc2 on Linux) it doesn't hide the function? file tst.d: import std.stdio :

Re: static functions?

2024-03-11 Thread Andy Valencia via Digitalmars-d-learn
On Monday, 11 March 2024 at 16:25:13 UTC, Jonathan M Davis wrote: ... But what exactly static means varies based on the context. Thank you for the list! But none of those appear to apply to a function defined in the outermost scope of the module. Is static accepted here--but has no actual e

varargs when they're not all the same type?

2024-03-14 Thread Andy Valencia via Digitalmars-d-learn
Can somebody give me a starting point for understanding varadic functions? I know that we can declare them int[] args... and pick through whatever the caller provided. But if the caller wants to pass two int's and a _string_? That declaration won't permit it. I've looked into the forma

Re: varargs when they're not all the same type?

2024-03-14 Thread Andy Valencia via Digitalmars-d-learn
On Thursday, 14 March 2024 at 18:05:59 UTC, H. S. Teoh wrote: ... The best way to do multi-type varags in D is to use templates: import std; void myFunc(Args...)(Args args) { Thank you. The first parenthetical list is of types, is it not? I can't find anywhere which says wha

Re: varargs when they're not all the same type?

2024-03-14 Thread Andy Valencia via Digitalmars-d-learn
On Thursday, 14 March 2024 at 23:13:51 UTC, Basile B. wrote: ... However explicit instantiation can take whatever is known at compile time, such as constant expressions or even certain static variables. So that is rather called an `alias sequence` in D. Which statement leads me to section 77

Re: varargs when they're not all the same type?

2024-03-16 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 15 March 2024 at 00:11:11 UTC, Andy Valencia wrote: (varargs & friends) Which statement leads me to section 77.2 of "Programming in D", and now I am deep into the mechanisms behind what you have very kindly shared. Thank you once more. As some fruits of my labors here, below is a

Opinions on iterating a struct to absorb the decoding of a CSV?

2024-03-28 Thread Andy Valencia via Digitalmars-d-learn
I wanted a lightweight and simpler CSV decoder. I won't post the whole thing, but basically you instantiate one as: struct Whatever { ... } ... f = File("path.csv", "r"); auto c = CSVreader!Whatever(f); foreach (rec; c) { ... CSVreader is, of course, templated: struct CSVreader

Re: Why does Nullable implicitly casts when assigning a variable but not when returning from a function?

2024-04-10 Thread Andy Valencia via Digitalmars-d-learn
On Wednesday, 10 April 2024 at 20:41:56 UTC, Lettever wrote: ``` import std; Nullable!int func() => 3; void main() { Nullable!int a = 3; //works fine Nullable!int b = func(); //does not compile } Why make func() Nullable? It just wants to give you an int, right? Making it a

mmap file performance

2024-04-10 Thread Andy Valencia via Digitalmars-d-learn
I wrote a "count newlines" based on mapped files. It used about twice the CPU of the version which just read 1 meg at a time. I thought something was amiss (needless slice indirection or something), so I wrote the code in C. It had the same CPU usage as the D version. So...mapped files, not

Re: mmap file performance

2024-04-11 Thread Andy Valencia via Digitalmars-d-learn
On Thursday, 11 April 2024 at 14:54:36 UTC, Steven Schveighoffer wrote: For a repeatable comparison, you should provide the code which does 1MB reads. With pleasure: import std.stdio : writeln, File, stderr; const uint BUFSIZE = 1024*1024; private uint countnl(File f) { uint res = 0;

Re: mmap file performance

2024-04-15 Thread Andy Valencia via Digitalmars-d-learn
On Monday, 15 April 2024 at 08:05:25 UTC, Patrick Schluter wrote: The setup of a memory mapped file is relatively costly. For smaller files it is a net loss and read/write beats it hands down. Interestingly, this performance deficit is present even when run against the largest conveniently av

Re: Making one struct work in place of another for function calls.

2024-04-16 Thread Andy Valencia via Digitalmars-d-learn
On Wednesday, 17 April 2024 at 03:13:46 UTC, Liam McGillivray wrote: Is there a way I can replace "`TypeB`" in the function parameters with another symbol, and then define that symbol to accept `TypeB` as an argument, but also accept `TypeA` which would get converted to `TypeB` using a function

Negating a short?

2024-11-05 Thread Andy Valencia via Digitalmars-d-learn
I have a template which has a bit where it negates a value. This works well until it encountered a short, where ldc2 complained: integral promotion not done for -val I ended up with this, but is negating a short really this problematic, or did I miss something? static if (!__traits(isUn

Rhyme and reason for function annotations?

2024-11-03 Thread Andy Valencia via Digitalmars-d-learn
A function can be described as, say, private, or pure, or @nogc. When does an annotation have an '@'? Also, a function can be annotated int myfunc(char *arg) pure { } Although I find: pure int myfunc(char *arg) { } Also works. So what annotations have @'s, and when do they go with the fu

Re: Negating a short?

2024-11-05 Thread Andy Valencia via Digitalmars-d-learn
On Tuesday, 5 November 2024 at 19:37:32 UTC, Dennis wrote: On Tuesday, 5 November 2024 at 17:32:00 UTC, Andy Valencia wrote: I ended up with this, but is negating a short really this problematic, or did I miss something? This is a relic from when integer promotion was added to unary operators

Re: Negating a short?

2024-11-05 Thread Andy Valencia via Digitalmars-d-learn
On Wednesday, 6 November 2024 at 00:00:48 UTC, Dennis wrote: That's right, it only removes the deprecation that requires a double cast to fix, but you still need an explicit cast to truncate the result of `-s` (which got promoted to `int`) back to a `short`, unless the compiler can prove at co

Re: Best practices for class instance variables as parameters

2024-09-28 Thread Andy Valencia via Digitalmars-d-learn
On Saturday, 28 September 2024 at 18:16:55 UTC, Ian wrote: Hi, I'm coming from C and some C++ so the way D stores class instance variables is new to me. If I'm not mistaken the basic unadorned instance variable is like a "hidden" pointer. So, when passing class instance variables to a functio

Re: Templates considered impressive

2024-10-01 Thread Andy Valencia via Digitalmars-d-learn
On Tuesday, 1 October 2024 at 11:45:35 UTC, monkyyy wrote: On Tuesday, 1 October 2024 at 05:44:16 UTC, H. S. Teoh wrote: why spend the time and effort when you could have just done: ``` import std.conv; theres a bunch of relivent tradeoffs and phoboes doesnt make a good set of them To be f

Re: Hello dears, where I can deploy my vibe-d project? Someone know any hosting?

2024-10-03 Thread Andy Valencia via Digitalmars-d-learn
On Thursday, 3 October 2024 at 08:51:12 UTC, Danic wrote: I want to know where publish mi D web You didn't say what platform you were comfortable operating. For Linux, I've often used Debian on a Linode nano instance. $5/month, and with an efficient app, by the time you outgrow it, you can

Re: File-like option where the "file contents" comes from a string?

2024-11-27 Thread Andy Valencia via Digitalmars-d-learn
On Wednesday, 27 November 2024 at 20:38:22 UTC, monkyyy wrote: On Wednesday, 27 November 2024 at 14:37:03 UTC, Andy Valencia wrote: With my OO programming goggles on, I can't help but notice that if Phobos had made File a class--or an interface-- oo doesn't own polymorphism you could do 99% of

Re: File-like option where the "file contents" comes from a string?

2024-11-27 Thread Andy Valencia via Digitalmars-d-learn
On Wednesday, 27 November 2024 at 02:43:21 UTC, Ali Çehreli wrote: On 11/26/24 5:12 PM, Andy Valencia wrote: Again and again for testing I run into how nice it would be to have an open "File" which has its contents set by the unit test code: auto f = StringFile("my test data...", "r"); An e

File-like option where the "file contents" comes from a string?

2024-11-26 Thread Andy Valencia via Digitalmars-d-learn
Again and again for testing I run into how nice it would be to have an open "File" which has its contents set by the unit test code: auto f = StringFile("my test data...", "r"); I've searched high and low without discovering something which would fit the bill. Thanks in advance, Andy

Re: Variable modified by different threads.

2024-12-01 Thread Andy Valencia via Digitalmars-d-learn
On Monday, 2 December 2024 at 02:02:56 UTC, Ritina wrote: How can I implement a program where I have a global integer variable g, and three threads: the first thread increments g by 1, the second thread increments g by 2, and the third thread increments g by 3? Additionally, while these threads

Re: Variable modified by different threads.

2024-12-01 Thread Andy Valencia via Digitalmars-d-learn
On Monday, 2 December 2024 at 02:29:39 UTC, Ali Çehreli wrote: I am not sure whether stopRequested = true is correct even when there is a single writer of that variable. There are several other methods of communicating the request. I chose that one for this example. I notice core.atomic h

Re: Variable modified by different threads.

2024-12-03 Thread Andy Valencia via Digitalmars-d-learn
On Tuesday, 3 December 2024 at 23:16:00 UTC, Richard (Rikki) Andrew Cattermole wrote: What owned by a thread means is that a pointer is guaranteed to only be accessible by that thread. I.e. the cpu will segfault if you try to access it from another thread. My experience is that aside from thre

Re: Why 'in' works only for assoc arrays?

2024-12-27 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 27 December 2024 at 19:17:13 UTC, JN wrote: Why not make 'in' work for arrays (and strings also)? ``` int[string] phonebook; if ("John" in phonebook) // works int[] numbers; if (3 in numbers) // doesn't work, compiler recommends std.algorithm.find string buildLog; if ("build error

Re: Simplify some C-style code

2024-12-29 Thread Andy Valencia via Digitalmars-d-learn
On Saturday, 28 December 2024 at 23:23:02 UTC, monkyyy wrote: The spec would need to drastically improve before my opinion changes; Im also uninterested practicing withholding my opinions. There are many many ancient bugs, that you need to inherent implicit knowledge to navigate, either those

Interpolated strings?

2024-12-16 Thread Andy Valencia via Digitalmars-d-learn
I have a string full of JavaScript to serve up, and a couple variable values need to be interpolated into it. I read that dlang now has interpolated strings under i"...", so yay! A bit of example code to show what I tried: string s; int i auto x = i"Message $(s) has value $(i)" writeln(x) a

Re: Interpolated strings?

2024-12-16 Thread Andy Valencia via Digitalmars-d-learn
On Monday, 16 December 2024 at 20:42:45 UTC, Juraj wrote: On Monday, 16 December 2024 at 20:33:27 UTC, Andy Valencia wrote: string x = i"Message $(s) has value $(i)" ```d import std.conv : text; string x = i"Message $(s) has value $(i)".text; ``` [Documentation](https://dlang.org/spec/istri

Re: Using a tuple as a function parameter

2024-11-22 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 22 November 2024 at 16:36:43 UTC, Andrew wrote: I'm getting started using D for some small personal projects and one thing I wanted to do was use a helper function for a tuple. I declared the function like this: string getOrZeroth(Tuple!(string, string, string) tup, int i) pure

Re: Virtual opBinary in interface

2024-12-21 Thread Andy Valencia via Digitalmars-d-learn
On Saturday, 21 December 2024 at 07:02:07 UTC, Steven Schveighoffer wrote: I wrote a blog post on how to use a single mixin to forward all operators to the D1 style overloads. You might find it useful or inspiring. https://www.schveiguy.com/blog/2022/06/how-to-keep-using-d1-operator-overloads/

Re: Immutable (Rosetta code and learning D)

2025-01-04 Thread Andy Valencia via Digitalmars-d-learn
On Saturday, 27 March 2021 at 20:44:12 UTC, Brad wrote: I was looking through lots of sample code on Rosetta Code. D has a lot of solutions out there. I'm following up to this older post, and I'm sure this is old news to many... I recently realized that Rosetta Code is a very nice resource

Re: implicit cast and overload priority

2025-02-14 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 14 February 2025 at 15:58:26 UTC, Paul Backus wrote: void foo(long x) { } void foo(ulong x) { } So I take it that a template with a static isSigned test would be the way to bifurcate foo()'s behavior? Andy

Re: Why is DList insertion log n?

2025-02-16 Thread Andy Valencia via Digitalmars-d-learn
On Sunday, 16 February 2025 at 20:57:58 UTC, rkompass wrote: I'm looking at the double linked list in std.containers and it says that insertion in front or back are O(log n). How come they are not O(1) ? From the source code at [https://github.com/dlang/phobos/blob/master/std/container/dlist.d#

Re: Simple string membership test

2025-02-15 Thread Andy Valencia via Digitalmars-d-learn
On Saturday, 15 February 2025 at 19:27:19 UTC, Ian wrote: canFind is Perfect. Thank you. If performance is an issue, putting them as keys in an Associative Array and simply using "in" should scale nicely to even very large numbers of strings to search. Andy

Re: alias to connect with superclass's constructor

2025-03-24 Thread Andy Valencia via Digitalmars-d-learn
On Monday, 24 March 2025 at 03:46:25 UTC, Salih Dincer wrote: When you add an empty constructor, the code still runs without an error Yes, I had already made it run. I was asking about making it run using aliases which, as it turns out, you can't. I'm grateful that there's even a PR in the

Subclass TcpSocket?

2025-03-17 Thread Andy Valencia via Digitalmars-d-learn
The presence of the "accepting" API in Socket seems to indicate that subclassing Socket/TcpSocket is intended to be supported. But I'm just not seeing my way through the twisty maze of pure and @nothrow barriers? Andy import std.socket : TcpSocket, Address, getAddress; class Wrapped

alias to connect with superclass's constructor

2025-04-05 Thread Andy Valencia via Digitalmars-d-learn
Consider the following, totally contrived, code. The compiler tells me: tst39.d(21): Error: constructor `tst39.B.this(string s)` is not callable using argument types `()` tst39.d(21):constructor `tst39.B.this` hides base class function `tst39.A.this` tst39.d(21):add `alias thi

Re: sqlite3 library which can read class instances (not structs)?

2025-03-15 Thread Andy Valencia via Digitalmars-d-learn
On Monday, 10 March 2025 at 21:39:39 UTC, Sergey wrote: Did you try d2sqlite3? I looked at it, and it only documented struct operations. Given that I now understand the problem at hand, I'd rather go with a fix here than start over. Thanks, Andy

getopt usage help to stderr?

2025-04-08 Thread Andy Valencia via Digitalmars-d-learn
This seems like something which needs to be straightforward, and yet I've spent a good amount of time getting nowhere. How does one get the usage information which getopt embeds in a result, and spit it out to stderr? The internals show some help output, hard-coded to stdout. As any Posix CL

Re: getopt usage help to stderr?

2025-04-08 Thread Andy Valencia via Digitalmars-d-learn
On Wednesday, 9 April 2025 at 01:23:01 UTC, Salih Dincer wrote: On Tuesday, 8 April 2025 at 20:14:56 UTC, Andy Valencia wrote: p.s. Ironically, I could probably have coded a getopt in less time than I've spent on std.getopt... :) Please try the following example with the parameters -h, -e,

TIL: writing to a socket and dying

2025-04-24 Thread Andy Valencia via Digitalmars-d-learn
This has been touched upon way back in forum history, but I thought it was worth a fresh mention. When writing to a socket--especially as a server--you can receive SIGPIPE. Phobos appears to try and inhibit this on some BSD systems, but on Linux if the recipient has closed the socket and you

Re: Create module-level function using mixin template?

2025-04-30 Thread Andy Valencia via Digitalmars-d-learn
On Wednesday, 30 April 2025 at 06:08:23 UTC, cc wrote: On Friday, 25 April 2025 at 16:14:49 UTC, Andy Valencia wrote: I have a code pattern, and would like to generate rather than copy/paste. It _seems_ like mixin templates apply, but I'm not having much luck. I saw one comment that templates

Re: Is there anyway to Deify the following code snippet?

2025-04-15 Thread Andy Valencia via Digitalmars-d-learn
On Wednesday, 16 April 2025 at 01:44:33 UTC, Paul Backus wrote: Nope, you're not missing anything. Using pointers is totally normal and idiomatic in D. That said, I try to code under @safe when I can, and let the compiler guide me clear of a whole class of C-type potential bugs. Andy

Re: Array operations

2025-05-04 Thread Andy Valencia via Digitalmars-d-learn
On Sunday, 4 May 2025 at 00:15:23 UTC, Jonathan M Davis wrote: That sounds like an ldc bug then. With dmd, your program gives [2, 2, 2, 2, 1, 1, 1, 1] [2, 2, 2, 2, 1, 1, 1, 1] core.exception.RangeError@q.d(13): Range violation ??:? onRangeError [0x29f6b6] ??:? _d_arrayboundsp [0

Re: TIL: writing to a socket and dying

2025-04-25 Thread Andy Valencia via Digitalmars-d-learn
On Thursday, 24 April 2025 at 19:36:03 UTC, kdevel wrote: Phobos appears to try and inhibit this on some BSD systems, How does it do that? Sorry, I forgot to answer this question. There's a setsockopt SO_NOSIGPIPE which is used if available during initial socket setup (in Phobos). Some com

Re: TIL: writing to a socket and dying

2025-04-24 Thread Andy Valencia via Digitalmars-d-learn
On Thursday, 24 April 2025 at 19:36:03 UTC, kdevel wrote: [...] I added a SIG_IGN of SIGPIPE and that made the problem stop. You know that it will now throw? [0] Yes; my server was written to handle an exception, and it also is prepared for the send() to return failure. Adding a third vecto

Re: Create module-level function using mixin template?

2025-04-25 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 25 April 2025 at 16:59:16 UTC, monkyyy wrote: its extremely unclear what your trying to do my best geuss: I want to use a mixin template to generate a top-level function. Like, is there a variant of the following which makes a function named "foo1" available? Andy ```d mixin te

Create module-level function using mixin template?

2025-04-25 Thread Andy Valencia via Digitalmars-d-learn
I have a code pattern, and would like to generate rather than copy/paste. It _seems_ like mixin templates apply, but I'm not having much luck. I saw one comment that templates always expand in their own context, so perhaps they're not useful for generating a top-level function? Andy ```d b

Re: Array operations

2025-05-03 Thread Andy Valencia via Digitalmars-d-learn
On Saturday, 3 May 2025 at 11:18:00 UTC, Nick Treleaven wrote: Second, in assigning from arrays of differing sizes, Phobos causes an illegal instruction, rather than the sort of exception I'd have expected. I'm curious why they stepped away from D's exception architecture? It throws a RangeErr

Array operations

2025-05-02 Thread Andy Valencia via Digitalmars-d-learn
In the following code, two questions. First, is there any difference between "x[] = y" and "x[] = y[]"? It appears not. Second, in assigning from arrays of differing sizes, Phobos causes an illegal instruction, rather than the sort of exception I'd have expected. I'm curious why they steppe

Re: string from C function

2025-05-07 Thread Andy Valencia via Digitalmars-d-learn
On Thursday, 8 May 2025 at 00:53:20 UTC, Mike Parker wrote: tst44.d(6): Error: cannot implicitly convert expression `fromStringz(ctime(null))` of type `char[]` to `string` `fromStringz` is giving you a slice of a `char*`, typed `char[]`. `string` is `immutable(char)[]`, so you can't assign `ch

Re: AES in dlang?

2025-02-18 Thread Andy Valencia via Digitalmars-d-learn
On Tuesday, 18 February 2025 at 10:20:44 UTC, Dejan Lekic wrote: On Wednesday, 12 February 2025 at 00:20:02 UTC, Andy Valencia wrote: I was wondering about an @safe dlang version of AES, and just couldn't find one. (Well, there was one, but without any of I am guessing you probably saw the "c

sqlite3 library which can read class instances (not structs)?

2025-03-06 Thread Andy Valencia via Digitalmars-d-learn
Is there a sqlite3 interface library which can correctly read into instances? sqlite3-d will let me compile with a class, but it segv's when run. ddbc seems to support only structs, and I can use it to punch in one value at a time out of each row result. But that feels clunky, and I'd avoid

Re: sqlite3 library which can read class instances (not structs)?

2025-03-10 Thread Andy Valencia via Digitalmars-d-learn
On Monday, 10 March 2025 at 22:55:03 UTC, Ali Çehreli wrote: You would expect that to be provided by one of the following: https://dlang.org/phobos/std_traits.html https://dlang.org/spec/traits.html But in this case it's the "is expression": https://dlang.org/spec/expression.html#is_exp

Re: sqlite3 library which can read class instances (not structs)?

2025-03-10 Thread Andy Valencia via Digitalmars-d-learn
On Thursday, 6 March 2025 at 23:45:01 UTC, Andy Valencia wrote: Is there a sqlite3 interface library which can correctly read into instances? sqlite3-d will let me compile with a class, but it segv's when run. ddbc seems to support only structs, and I can use it to punch in one value at a tim

Re: How does D lang handle multithreaded applications and static members of classes?

2025-04-04 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 4 April 2025 at 16:25:55 UTC, bauss wrote: shared is broken however. In what way? (Says the guy using it for his multi-threaded web service middleware.) Andy

Re: Using bindbc-sdl with D

2025-03-26 Thread Andy Valencia via Digitalmars-d-learn
On Sunday, 12 March 2023 at 03:01:24 UTC, ryuukk_ wrote: So this is a dub issue? It is able to find the linker, why can't it find the preprocessor? I tried to use dub several times, and each and every attempt ended in "huh?" things like this. So I stopped trying to use dub, just bringing

Re: string from C function

2025-05-07 Thread Andy Valencia via Digitalmars-d-learn
On Wednesday, 7 May 2025 at 22:28:32 UTC, Dejan Lekic wrote: https://dlang.org/library/std/string/from_stringz.html So about fromStringz... ```d import std.string : fromStringz; import core.stdc.time : ctime; void main() { string s = fromStringz(ctime(null)); } ``` Like that? tst44.d(6)

string from C function

2025-05-07 Thread Andy Valencia via Digitalmars-d-learn
I feel dumb, because this can't be as elusive as it feels. I'm calling a C library function, and getting a char * (null terminated). Now I want it to be a D string. What's the right way to do this? Performance and memory use are not important; I just want a simple and idiomatic way to get

Re: Sanity check: pithiest test for static if: "is an array of ubyte"

2025-05-24 Thread Andy Valencia via Digitalmars-d-learn
On Sunday, 25 May 2025 at 01:06:12 UTC, Steven Schveighoffer wrote: static if(isArray!(typeof(arg))) && is(typeof(arg[0]) == ubyte)) ``` Note that even if arg.length is 0, this is fine, because the expression isn't actually executed. Hope this helps you to understand the issue. Actually, I

Re: Sanity check: pithiest test for static if: "is an array of ubyte"

2025-05-24 Thread Andy Valencia via Digitalmars-d-learn
On Saturday, 24 May 2025 at 21:58:16 UTC, H. S. Teoh wrote: static if (is(typeof(myArray) == ubyte[])) { ... } Oh! I knew I was missing something. Thank you to both of you for pointing at this. Andy

Sanity check: pithiest test for static if: "is an array of ubyte"

2025-05-24 Thread Andy Valencia via Digitalmars-d-learn
The best I"ve come up with for a "static if" to see if something's an array of ubyte: ```d (isArray!(typeof(arg))) && is(typeof(arg).init[0] == ubyte) ``` Which doesn't seem to work, but it's the closest I've come by poring over the docs and reading Phobos source. TIA! Andy

Re: What the heck am i doing wrong? I'm just trying to create a 8 bit unsigned variable.

2025-05-21 Thread Andy Valencia via Digitalmars-d-learn
On Friday, 16 May 2025 at 19:04:24 UTC, WhatMeWorry wrote: cast(uint8_t) b = cast(uint8_t) b + cast(uint8_t) 5; // onlineapp.d(19): Error: cannot implicitly convert expression

Re: Implicit conversion of string to array of immutable ubytes

2025-05-23 Thread Andy Valencia via Digitalmars-d-learn
On Saturday, 23 March 2024 at 06:55:41 UTC, Jonathan M Davis wrote: If you want to do that conversion without a cast, then you can just use std.string.representation (which will do the cast internally). I somehow missed this in Programming in D, and even here on the forum. So just noting tha

InputRange for data structure without order?

2025-06-03 Thread Andy Valencia via Digitalmars-d-learn
I have a Set data structure which has no concept of order; its members are stored and can be searched efficiently based on their hash. I have a situation where chain()'ing them together would be convenient, but InputRange requires front() and popFront(). There really _isn't_ a front, and cre

Re: InputRange for data structure without order?

2025-06-03 Thread Andy Valencia via Digitalmars-d-learn
On Tuesday, 3 June 2025 at 20:51:05 UTC, Ali Çehreli wrote: front() does not imply an order. It will work as long as you can provide the elements in a sequence. The built-in associative array feature is an example: Ok, but now the data structure is told to popFront(). I completely see how th

Re: InputRange for data structure without order?

2025-06-03 Thread Andy Valencia via Digitalmars-d-learn
On Wednesday, 4 June 2025 at 02:11:18 UTC, H. S. Teoh wrote: The correct way to do this is to create a method in the container that returns a range over its contents. Popping the range should NOT mutate the container. It should be regarded as something separate from the container. Only then

Re: InputRange for data structure without order?

2025-06-04 Thread Andy Valencia via Digitalmars-d-learn
On Wednesday, 4 June 2025 at 10:47:24 UTC, Jonathan M Davis wrote: Typically, a container will implement opSlice (or opIndex, since confusingly, both can be used in this case) and have it return a range over the container. So, then you can do auto range = myContainer[]; ... Brilliant, th

Re: InputRange for data structure without order?

2025-06-04 Thread Andy Valencia via Digitalmars-d-learn
On Tuesday, 3 June 2025 at 16:03:15 UTC, Andy Valencia wrote: I have a situation where chain()'ing them together would be convenient, but InputRange requires front() and popFront(). As an exercise in chaining opApply based containers, I tried: ```d // Chain across containers; their type is T a

  1   2   >