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

2020-06-18 Thread Denis via Digitalmars-d-learn
Let me clarify the requirements and rephrase the question -- REQUIREMENTS `assert`, `enforce` and the `unless` function I wrote (above) all allow the condition to be expressed in an affirmative sense, and they also achieve the goal of readability. These are the initial requirements.

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

2020-06-18 Thread Denis via Digitalmars-d-learn
On Thursday, 18 June 2020 at 12:50:35 UTC, Stanislav Blinov wrote: No, there isn't a way to write an operator. OK, first choice eliminated. On Thursday, 18 June 2020 at 13:57:39 UTC, Dukc wrote: No reason to use templates here pragma(inline, true) auto not(bool cond) { return !cond(); } I

Flagging special conditions on return from a function call

2020-06-22 Thread Denis via Digitalmars-d-learn
Is there a preferred idiom in D for flagging special conditions when returning from a function call? Here "special conditions" refers to expected situations (e.g. reaching the end of something, like EOF) rather than outright errors (so exception-try-catch is inappropriate). I've come across

Re: Flagging special conditions on return from a function call

2020-06-23 Thread Denis via Digitalmars-d-learn
Perhaps this thread would have been better titled "Returning a value and a status", and the question phrased as "What are your preferred techniques?". I'm planning to port some of my programs to D, so I'd like to standardize on one or two techniques for handling this very common situation,

Calling C functions

2020-06-25 Thread Denis via Digitalmars-d-learn
I have a two questions about calling C functions from D. (1) When passing a D callback to a C function, is there a way to write the code without having to prefix the callback declaration with "extern(C)"? It's not a big deal adding the prefix to the D function declaration. It just seems odd

Re: foreach iterator with closure

2020-06-28 Thread Denis via Digitalmars-d-learn
To keep this reply brief, I'll just summarize: Lots of great takeaways from both of your posts, and a handful of topics you mentioned that I need to dig into further now. This is great (I too like D :) I very much appreciate the extra insight into how things work and why certain design

Re: Calling C functions

2020-06-26 Thread Denis via Digitalmars-d-learn
On Friday, 26 June 2020 at 08:15:27 UTC, Jacob Carlborg wrote: On Friday, 26 June 2020 at 00:30:22 UTC, Denis wrote: extern(C) void cfunc(void function(int)); extern(C) void dcallback(int x) {...} <-- Why extern(C)? cfunc(); Can this be rewritten, dropping the prefix from the

Re: Initializing an associative array of struct

2020-06-14 Thread Denis via Digitalmars-d-learn
@Kagamin: On Sunday, 14 June 2020 at 07:16:18 UTC, Kagamin wrote: parameters[param]=Parameter(); I did not realize that you can use a type on the RHS of an assignment, but it is clear and succinct. This syntax will be very useful going forward -- thank you. @Stanislav B: On Sunday, 14

Re: Initializing an associative array of struct

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

Reading text (I mean "real" text...)

2020-06-19 Thread Denis via Digitalmars-d-learn
THE PROBLEM UTF-8 validation alone is insufficient for ensuring that a file contains only human-readable text, because control characters are UTF-8 valid. Apart from tab, newline, carriage return, and a few less commonly used others considered to be whitespace, human-readable text files

Re: Reading text (I mean "real" text...)

2020-06-19 Thread Denis via Digitalmars-d-learn
On Saturday, 20 June 2020 at 01:41:50 UTC, Paul Backus wrote: It sounds like maybe what you are looking for is Unicode character categories: https://en.wikipedia.org/wiki/Unicode_character_property#General_Category The character validation step could indeed be expressed using Unicode

Some questions about strings

2020-06-21 Thread Denis via Digitalmars-d-learn
I have a few questions about how strings are stored. - First, is there any difference between string, wstring and dstring? For example, a 3-byte Unicode character literal can be assigned to a variable of any of these types, then printed, etc, without errors. - Are the characters of a string

Re: Some questions about strings

2020-06-21 Thread Denis via Digitalmars-d-learn
On Monday, 22 June 2020 at 04:32:32 UTC, Mike Parker wrote: On Monday, 22 June 2020 at 04:08:10 UTC, Denis wrote: On Monday, 22 June 2020 at 03:31:17 UTC, Ali Çehreli wrote: : string is char[] wstring is wchar[] dstring is dchar[] Got it now. This is the critical piece I missed: I understand

Re: Some questions about strings

2020-06-21 Thread Denis via Digitalmars-d-learn
On Monday, 22 June 2020 at 03:24:37 UTC, Adam D. Ruppe wrote: On Monday, 22 June 2020 at 03:17:54 UTC, Denis wrote: - First, is there any difference between string, wstring and dstring? Yes, they encode the same content differently in the bytes. If you cast it to ubyte[] and print that out

Re: Some questions about strings

2020-06-21 Thread Denis via Digitalmars-d-learn
On Monday, 22 June 2020 at 03:31:17 UTC, Ali Çehreli wrote: : string is char[] wstring is wchar[] dstring is dchar[] Got it now. This is the critical piece I missed: I understand the relations between the char types and the UTF encodings (thanks to your book). But I mistakenly thought that

Re: Some questions about strings

2020-06-21 Thread Denis via Digitalmars-d-learn
On Monday, 22 June 2020 at 03:49:01 UTC, Adam D. Ruppe wrote: On Monday, 22 June 2020 at 03:43:58 UTC, Denis wrote: My code reads a UTF-8 encoded file into a buffer and validates, byte by byte, the UTF-8 encoding along with some additional validation. If I simply return the UTF-8 encoded

Re: Reading text (I mean "real" text...)

2020-06-20 Thread Denis via Digitalmars-d-learn
Digging into this a bit further -- POSIX defines a "print" class, which I believe is an exact fit. The Unicode spec doesn't define this class, which I presume is why D's std.uni library also omits it. But there is an isprint() function in libc, which I should be able to use (POSIX here).

Re: Some questions about strings

2020-06-22 Thread Denis via Digitalmars-d-learn
On Monday, 22 June 2020 at 09:06:35 UTC, Jacob Carlborg wrote: String **literals** have a terminating null character, to help with integrating with C functions. But this null character will disappear when manipulating strings. You cannot assume that a function parameter of type `string`

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

2020-06-18 Thread Denis via Digitalmars-d-learn
On Thursday, 18 June 2020 at 17:57:49 UTC, Ali Çehreli wrote: Here is an earlier experiment of nested templates, which may be useful in this case. : I think you should be able to pass callables as 'alias' template arguments Sounds good. This gives me an opportunity to learn how nested

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

2020-06-17 Thread Denis via Digitalmars-d-learn
On Thursday, 18 June 2020 at 00:43:40 UTC, Stanislav Blinov wrote: if( ! (configfile.isFile && configfile.extension == ".conf") ) ? That does indeed clean up the compound logic. One is still left with: if( !( if( ! if( !( if( ! : I was hoping to get away from all the `not`s

"if not" condition check (for data validation)

2020-06-17 Thread Denis via Digitalmars-d-learn
Is there a cleaner way to implement an "if not" condition check? WHY NOT JUST USE "IF"? For data validation, code is cleaner and more intelligible if the condition being checked is written in an affirmative sense; that is, in the same way that `assert` is written. This is especially true

foreach iterator with closure

2020-06-27 Thread Denis via Digitalmars-d-learn
Is it possible to write an iterator that does the following, using a struct and some functions? - Operates in a foreach loop - Has BEGIN-like and END-like blocks or functions that are executed automatically, before and after the iterations - Initializes variables in the BEGIN block that are

Re: foreach iterator with closure

2020-06-28 Thread Denis via Digitalmars-d-learn
Many thanks: your post has helped me get past the initial stumbling blocks I was struggling with. I do have a followup question. First, here are my conclusions up to this point, based on your post above, some additional experimentation, and further research (for future reference, and for any

Re: Flagging special conditions on return from a function call

2020-06-23 Thread Denis via Digitalmars-d-learn
On Tuesday, 23 June 2020 at 21:34:25 UTC, Paul Backus wrote: If you're open to using Dub packages [...] Because this is going to be used in almost every program I write, I need to eliminate outside dependencies as an option. Nonetheless, thanks for this suggestion. [2]

Initializing an associative array of struct

2020-06-13 Thread Denis via Digitalmars-d-learn
Hi, I'm trying to combine a couple of general types (associative array, struct) in a compound data structure, but without success. Here is what I'm trying to achieve: "param" is a string variable "parameters[param].id" is an integer value "parameters[param].value" is a string

comparing with c strings

2023-11-23 Thread denis via Digitalmars-d-learn
Let's say I have a D application, with some callbacks from C, where some arguments to the callbacks are `const char* path`. What is the recommended way to compare them to D strings? Without making allocations, if that's possible

cannot find source code for runtime library file 'object.d'

2023-11-19 Thread denis via Digitalmars-d-learn
``` $ zypper install dmd $ dmd main.d Error: cannot find source code for runtime library file 'object.d' dmd might not be correctly installed. Run 'dmd -man' for installation instructions. config file: /etc/dmd.conf $ cat /etc/dmd.conf ; ; dmd.conf file for dmd ; ; dmd will look

Re: cannot find source code for runtime library file 'object.d'

2023-11-21 Thread denis via Digitalmars-d-learn
On Monday, 20 November 2023 at 07:50:22 UTC, thinkunix wrote: denis via Digitalmars-d-learn wrote: ``` $ zypper install dmd $ dmd main.d Error: cannot find source code for runtime library file 'object.d'    dmd might not be correctly installed. Run 'dmd -man' for installation