Re: public vs private alias this

2022-11-01 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 1 November 2022 at 23:01:57 UTC, Per Nordlöw wrote: When is it preferrable to use ```d struct S { private T t; alias t this; } ``` instead of ```d struct S { public T t; alias t this; } ``` for any given type `T`? If the `alias this` needs to work outside the module where `S`

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 25 October 2022 at 20:27:18 UTC, Ali Çehreli wrote: On 10/25/22 13:12, Paul Backus wrote: > In order to create a copy of a static array Although .dup works for static arrays as well, you meant "dynamic array" and everyones knows it. :) Yes; thank you for the correction. :)

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 25 October 2022 at 17:54:16 UTC, Salih Dincer wrote: On Tuesday, 25 October 2022 at 17:18:35 UTC, Paul Backus wrote: It's not a bug. They're pointing to the exact same instance of `A` in memory: I don't understand? So I don't understand why it causes problems with dynamic

Re: Is "auto t=T();" not the same as "T t;"?

2022-10-25 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 25 October 2022 at 16:52:48 UTC, Salih Dincer wrote: On Tuesday, 25 October 2022 at 13:51:30 UTC, Andrey Zherikov wrote: Does the second piece of code shows a bug or my expectation is not correct (and why if so)? This is a bug: ```d void main() { struct B { struct A {

Re: Design question regarding saving changes in the original array and/or returning a new set

2022-10-23 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 23 October 2022 at 13:32:44 UTC, matheus wrote: I have a design question and I'd like to hear some advice. Let's say that I want to create a method to sort an array: arr.sort(asc); I think usually this would usually return a new set of that array but now sorted. But If I

Re: Real simple question... for good programmers

2022-10-22 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 22 October 2022 at 21:53:05 UTC, WhatMeWorry wrote: string[] tokens = userSID.output.split!isWhite; writeln("tokens = ", tokens); [...] Is there a clever way that I can discard all the extra null strings in the resultant string array? Easiest way is to use [`filter`][1].

Re: Find in assoc array then iterate

2022-10-22 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 22 October 2022 at 15:21:07 UTC, Kevin Bailey wrote: OTOH, a forward iterator (i.e. copyable but does not need to go backwards) solves the problem elegantly and efficiently. The builtin function [`byKeyValue`][1] returns a forward range (D's version of a forward iterator) over

Re: Can someone tell me what the compiler thought I was trying to do?

2022-10-18 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 19 October 2022 at 01:49:26 UTC, mw wrote: On Wednesday, 19 October 2022 at 01:38:27 UTC, Adam D Ruppe wrote: On Wednesday, 19 October 2022 at 01:34:54 UTC, mw wrote: Is there any (design) doc about this? scroll up, click the link from this very thread.

Re: rotate left an array

2022-10-03 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 4 October 2022 at 00:38:25 UTC, Ali Çehreli wrote: Good catch but I think what we want is a copy of the front element, at least for InputRanges (.save does not work for File.byLine :/). What is the generic way of copying an element? I wonder whether we have to use isSomeString to

Re: rotate left an array

2022-10-03 Thread Paul Backus via Digitalmars-d-learn
On Monday, 3 October 2022 at 21:06:36 UTC, Ali Çehreli wrote: On 10/3/22 13:48, Andrey Zherikov wrote: a "rotated view". Without indexes: import std.range : empty; auto rotatedView(R)(R range) in (!range.empty) { import std.range : chain, front, only, popFront; const fr =

Re: Is `void` the correct way to say "do not initialize this variable"?

2022-10-03 Thread Paul Backus via Digitalmars-d-learn
On Monday, 3 October 2022 at 14:37:35 UTC, kdevel wrote: On Sunday, 2 October 2022 at 23:37:26 UTC, ryuukk_ wrote: I got the answer thanks to IRC chat: https://dlang.org/spec/declaration.html#void_init Quote: Implementation Defined: If a void initialized variable's value is used

Re: Stop writeln from calling object destructor

2022-10-02 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 2 October 2022 at 18:24:51 UTC, Ali Çehreli wrote: On 10/2/22 10:55, data pulverizer wrote: > ``` > this(T)(ref return scope T original) > if(is(T == RVector!(Type))) > { > //... code ... > } > ``` I've just tested. That is used only for explicit constructor syntax: auto

Re: Stop writeln from calling object destructor

2022-10-02 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 2 October 2022 at 16:21:47 UTC, data pulverizer wrote: I've noticed that `writeln` calls the destructor of a struct multiple times and would like to know how to stop this from happening. It's because `writeln` is copying the object, and each of the copies is being destroyed. If

Re: Detect uninitialized class var access

2022-09-25 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 25 September 2022 at 03:04:45 UTC, Tejas wrote: On Saturday, 24 September 2022 at 23:04:00 UTC, rassoc wrote: On 9/24/22 15:28, Adam D Ruppe via Digitalmars-d-learn wrote: gdb --args ./your_program and then it will tell you all the details you want to know about when this happens.

Re: to delete the '\0' characters

2022-09-23 Thread Paul Backus via Digitalmars-d-learn
On Friday, 23 September 2022 at 18:37:59 UTC, Salih Dincer wrote: On Thursday, 22 September 2022 at 10:53:32 UTC, Salih Dincer wrote: Is there a more accurate way to delete **the '\0' characters at the end of the string?** * character**S** * at the **END** * of the **STRING** Apologies for

Re: to delete the '\0' characters

2022-09-22 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 22 September 2022 at 10:53:32 UTC, 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 ```d auto foo(string s) { string r; foreach(c; s) {

Re: Function attribute best practices

2022-09-13 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 13 September 2022 at 14:16:39 UTC, Ali Çehreli wrote: 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

Re: Linker Error with Template Function

2022-09-12 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 13 September 2022 at 00:57:58 UTC, Kyle Ingraham wrote: I am writing a library where I would like to be able to store instances of a type of class to an associative array for later usage. Each class stored has to implement a function as part of the required interface. The argument

Re: Function attribute best practices

2022-09-12 Thread Paul Backus via Digitalmars-d-learn
On Monday, 12 September 2022 at 16:14:42 UTC, Ali Çehreli wrote: Is this accurate: Because Foo is a template, it should not put any attribute on member functions? Or only member functions that use a member that depends on a template parameter? And non-members that are templates? Yes. Except

Re: Validate static asserts

2022-09-09 Thread Paul Backus via Digitalmars-d-learn
On Friday, 9 September 2022 at 14:35:33 UTC, Andrey Zherikov wrote: I have bunch of `static assert(, )` in my code and would like to validate that specific code triggers specific assert by checking what `` is thrown. It sounds like maybe your goal here is to test that attempting to compile a

Re: How to add struct definition?

2022-09-08 Thread Paul Backus via Digitalmars-d-learn
On Friday, 9 September 2022 at 00:16:01 UTC, Injeckt wrote: I need to add this struct definition in my project. But how to do that? This structure: https://docs.microsoft.com/en-us/windows/win32/api/iptypes/ns-iptypes-ip_adapter_info import core.sys.windows.iptypes;

Re: Storing a lambda alongside type-erased data

2022-09-08 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 8 September 2022 at 03:18:08 UTC, Ali Çehreli wrote: I looked at how std.variant.VariantN prints the correct type and failed to understand the magic there. :( Then I came up with storing a lambda that is created when the exact type is known. The following simple variant can carry

Re: Does D actually support flexible array members?

2022-09-06 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 6 September 2022 at 11:51:35 UTC, IchorDev wrote: On Thursday, 18 August 2022 at 11:25:22 UTC, Paul Backus wrote: I think the closest way to approximate this in D is to use a zero-length static array: ```d struct ArenaChunk { size_t size; ArenaChunk* next; char[0]

Re: Comparing slices with std.variant.Algebraic

2022-09-05 Thread Paul Backus via Digitalmars-d-learn
On Monday, 5 September 2022 at 08:58:21 UTC, anonymouse wrote: On a related note, std.variant.Algebraic has been deprecated and the suggested replacement is std.sumtype.SumType. What is the proper way to make this conversion? Attempting to do a drop-in replacement results in the following

Re: BetterC stack traces?

2022-09-05 Thread Paul Backus via Digitalmars-d-learn
On Monday, 5 September 2022 at 12:07:35 UTC, Paul Backus wrote: On Monday, 5 September 2022 at 10:47:38 UTC, IchorDev wrote: Ah, I'm actually trying to create my own implementation of this, so the goal would be to not rely on a dependency. I can't exactly make head nor tail of the library's

Re: BetterC stack traces?

2022-09-05 Thread Paul Backus via Digitalmars-d-learn
On Monday, 5 September 2022 at 10:47:38 UTC, IchorDev wrote: Ah, I'm actually trying to create my own implementation of this, so the goal would be to not rely on a dependency. I can't exactly make head nor tail of the library's source other than that it seems to have a unique implementation on

Re: BetterC stack traces?

2022-09-04 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 4 September 2022 at 17:43:01 UTC, IchorDev wrote: I'm trying to implement a custom exception system in BetterC. Does anyone know how I'd go about getting a stack trace so that I can print it to stdout? :) I was thinking of utilising UDAs & `__LINE__` but it turns out that UDAs don't

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

2022-09-04 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 4 September 2022 at 14:14:55 UTC, Ali Çehreli wrote: The program output is different whether an Error is thrown from main or from the unittest block: Because the default test runner catches the Error, and doesn't print the stack trace:

Re: How to use a non-static objects in string `mixin`?

2022-08-27 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 27 August 2022 at 13:20:13 UTC, hype_editor wrote: I need to use function `eval` sometimes, but compiler throws an error: `Error: variable `firstOperand` cannot be read at compile time`. ```d override public double eval() { double firstOperand =

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

2022-08-22 Thread Paul Backus via Digitalmars-d-learn
On Monday, 22 August 2022 at 16:19:06 UTC, Andrey Zherikov wrote: I have an impression that template function can be called without parenthesis if it doesn't have run-time parameters so `func!0` is the same as `func!0()`. Am I wrong? You're not wrong. This behavior is a special case in

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

2022-08-22 Thread Paul Backus via Digitalmars-d-learn
On Monday, 22 August 2022 at 16:06:37 UTC, Andrey Zherikov wrote: On Monday, 22 August 2022 at 15:15:22 UTC, Paul Backus wrote: My first instinct is to say that this is the user's mistake. UDAs are not evaluated like normal expressions, and anyone using UDAs is going to have to learn that

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

2022-08-22 Thread Paul Backus via Digitalmars-d-learn
On Monday, 22 August 2022 at 14:43:24 UTC, Andrey Zherikov wrote: But the question is still opened: why is `typeof(U().func!0)` not the same as `typeof(U().func!0())`? Probably because if it were the same, it would be completely impossible to introspect on the type of `U.func!0` directly. The

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

2022-08-22 Thread Paul Backus via Digitalmars-d-learn
On Monday, 22 August 2022 at 14:42:10 UTC, Andrey Zherikov wrote: My situation is that user can write some UDA expression and I'm checking whether it's of a type `U` using `hasUDA!(sym, U)` and `getUDAs!(sym, U)`. Is the users uses `U()` or `U().func!0()`, everything works. But `U().func!0`

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

2022-08-22 Thread Paul Backus via Digitalmars-d-learn
On Monday, 22 August 2022 at 11:24:59 UTC, Andrey Zherikov wrote: On Monday, 22 August 2022 at 06:01:11 UTC, JG wrote: Why not just change to: alias type = typeof(U().func!0()); This is user's code and `U().func!0` is legit syntax. Workaround: wrap it in a lambda. ```d import std.traits;

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

2022-08-20 Thread Paul Backus via Digitalmars-d-learn
On Monday, 15 August 2022 at 02:59:59 UTC, Ali Çehreli wrote: class Point { int x; int y; Color color; // ... override size_t toHash() const { return x + y; } } The book is simply forgetting to show that function. (Otherwise, I never put any code without testing.) WARNING:

Re: Compile time int to string conversion in BetterC

2022-08-19 Thread Paul Backus via Digitalmars-d-learn
On Friday, 19 August 2022 at 10:22:25 UTC, bauss wrote: Is there a reason why .stringof is implementation defined and not clearly defined in the spec how types and declarations should be treated when being "converted to a string"? I find it really odd that it's implementation defined and you

Re: Compile time int to string conversion in BetterC

2022-08-18 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 17 August 2022 at 11:38:31 UTC, Steven Schveighoffer wrote: On 8/17/22 6:38 AM, Dennis wrote: On Wednesday, 17 August 2022 at 08:44:30 UTC, Ogi wrote: Maybe I’m missing something? I had the same problem, and came up with the following trick: ```D enum itoa(int i) = i.stringof;

Re: Does D actually support flexible array members?

2022-08-18 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 18 August 2022 at 08:41:02 UTC, LinguisticMystic wrote: I'm porting some C code for arena allocator to D, and somehow the flexible array members (a feature of C99 for dynamically-sized structs) work in D without significant changes in the code. Here's my arena definition: ```

Re: How do I get line numbers using pegged?

2022-08-15 Thread Paul Backus via Digitalmars-d-learn
On Monday, 15 August 2022 at 22:55:30 UTC, TheZipCreator wrote: So I've looked at the [pegged library](https://github.com/PhilippeSigaud/Pegged) and used it a little bit and it seems really good. But there doesn't seem like there's a built-in way to get line numbers of nodes in the parse tree

Re: chain of exceptions, next method

2022-08-13 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 14 August 2022 at 02:07:05 UTC, Ali Çehreli wrote: This automatic "combining" of exceptions happens for cleanup code like scope(exit). (I remember bug(s) for scope(failure).): To be precise, an exception thrown inside a 'finally' block gets chained onto the previous exception,

Re: std.algorithm.cmp is conflicting with itself.

2022-08-11 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 11 August 2022 at 17:46:00 UTC, realhet wrote: Here's the utility module: ```d module testcmpmodule; //publicly output these modules, like I did in my always used 'utils' module. public import std.algorithm, std.math, std.stdio; import std.range, std.traits; //create function

Re: "min" and "max"

2022-08-09 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 9 August 2022 at 23:35:23 UTC, pascal111 wrote: "min" and "max" in "std.algorithm" can be used with single values to pick up the min and max values, but it didn't mention how they can be used with ranges in the documentation:

Re: Fix template parameter

2022-08-09 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 9 August 2022 at 22:36:23 UTC, Dom Disc wrote: On Tuesday, 9 August 2022 at 22:32:23 UTC, Dom Disc wrote: On Tuesday, 9 August 2022 at 21:16:22 UTC, Paul Backus wrote: Yes, this syntax allows anything that implicitly converts to `BigInt`; Oh, or do you mean I will get two

Re: Fix template parameter

2022-08-09 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 9 August 2022 at 21:08:52 UTC, Meta wrote: (it may also include anything that is a subtype of BigInt... I've received different answers on what exactly `(T: SomeType)` means in this context). Yes, this syntax allows anything that implicitly converts to `BigInt`; for example:

Re: Copying and array into another

2022-08-09 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 9 August 2022 at 18:33:04 UTC, pascal111 wrote: I tried to copy an array into another without affecting in the original array when I try to change the value of any element of the new array, but I failed except with this way in the next code: '''D int[] x=[1,2,3]; int[]

Re: Unittest Absurdity

2022-08-04 Thread Paul Backus via Digitalmars-d-learn
On Friday, 5 August 2022 at 01:47:07 UTC, Ruby The Roobster wrote: I found the issue: opOpAssign isn't getting called at all. I have no idea why, though. Given that the example works, the problem must be in some other part of your code that you haven't posted. If you can post a more

Re: Hacking C code vs D code

2022-08-04 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 4 August 2022 at 23:11:36 UTC, pascal111 wrote: One of problems faced me in C programming is hacking data with C code that some hackers do with C code which make me needs more tools to protect my C code, but I don't have good resources in my current time, while I noticed that D

Re: curses/ncurses liberary in D

2022-08-04 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 4 August 2022 at 21:15:39 UTC, pascal111 wrote: On Wednesday, 3 November 2021 at 05:43:05 UTC, harakim wrote: https://github.com/adamdruppe/arsd/blob/master/terminal.d How can I use this terminal module? Is there a document for it? It is part of the arsd-official package,

Re: How to cast away shared?

2022-08-03 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 3 August 2022 at 12:50:17 UTC, Ruby The Roobster wrote: Any way to 'cast away' shared for an unknown type T? There's actually an `Unshared` template for this in `std.traits`, but for some reason it's `private`, so you can't use it directly. Fortunately, it's only two lines:

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread Paul Backus via Digitalmars-d-learn
On Monday, 1 August 2022 at 14:52:03 UTC, pascal111 wrote: If `foo => bar` == `(foo) { return bar; }`, then `foo => bar` is a function. "=>" is not an operator, it's a special symbol for lambda "function". If A == B, so A's types is the same of B's type. How can it be withstanding `foo =>

Re: Exclamation symbol "!" within functions syntax

2022-08-01 Thread Paul Backus via Digitalmars-d-learn
On Monday, 1 August 2022 at 11:35:25 UTC, pascal111 wrote: I noticed that filter is using the concept of templates but this time it's with a lambda function, not with a data type, how can we explain this? isn't supposed to use a data type after the exclamation mark: "auto r = chain(a,

Re: "strstr" D equivalent

2022-07-29 Thread Paul Backus via Digitalmars-d-learn
On Friday, 29 July 2022 at 14:14:54 UTC, pascal111 wrote: and if I'm right, with returning back to the definitions of "indexOf" @ https://dlang.org/phobos/std_string.html#.indexOf we won't find that there is a definition for it with just two parameters, so from where you got this new

Re: "strtok" D equivalent

2022-07-28 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 28 July 2022 at 21:52:28 UTC, pascal111 wrote: On Thursday, 28 July 2022 at 20:36:31 UTC, Paul Backus wrote: ```d import std.algorithm: filter; import std.range: empty; import std.functional: not; // ... auto tokens = input .splitter!(c => delimiters.canFind(c))

Re: "strtok" D equivalent

2022-07-28 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 28 July 2022 at 19:17:26 UTC, pascal111 wrote: What's the "strtok" - C function - D equivalent? https://en.cppreference.com/w/cpp/string/byte/strtok Closest thing is probably `std.algorithm.splitter` with a predicate: ```d import std.algorithm: splitter, canFind; import

Re: does the format of coverage files have a name?

2022-07-28 Thread Paul Backus via Digitalmars-d-learn
On Monday, 25 July 2022 at 13:17:41 UTC, Moth wrote: or was the .lst extension chosen arbitrarily? my text editor [notepad++] thinks it's COBOL for some reason but that's obviously not correct, so i'm wondering if it has an official spec or anything. knowing the name of it would help - maybe

Re: mixin template bug with opBinary?

2022-07-22 Thread Paul Backus via Digitalmars-d-learn
On Friday, 22 July 2022 at 12:33:37 UTC, Anthony Quizon wrote: I get: ``` foo.d(16): Error: mixin `foo.B.opBi!(B, ["+":function (B a, B b) pure nothrow @nogc @safe => a])` does not match template declaration `opBi(A, A function(A, A)[string] f0)` ``` Is this a bug or am I doing something

Re: Why does inputRangeObject fail to derive correctly for RandomAccessInfinite ranges?

2022-07-16 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 16 July 2022 at 08:40:10 UTC, D Lark wrote: On Wednesday, 13 July 2022 at 01:40:43 UTC, Paul Backus wrote: On Wednesday, 13 July 2022 at 01:23:35 UTC, D Lark wrote: First, please can someone clarify if the behaviour I expect in the last line is consistent with the intention of the

Re: Unable to use map() and array() inside a class-field's initializer.

2022-07-14 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 14 July 2022 at 13:57:24 UTC, realhet wrote: Hello, Somehow it can't reach map and array inside a class field initializer. If I put that small expression inside a function, it works. If I encapsulate the initializer expression into a lambda and evaluate it right away, it also

Re: Why does inputRangeObject fail to derive correctly for RandomAccessInfinite ranges?

2022-07-12 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 13 July 2022 at 01:23:35 UTC, D Lark wrote: First, please can someone clarify if the behaviour I expect in the last line is consistent with the intention of the library? Yes, it should behave the way you expect. The current behavior is a bug. I've submitted a report for it

Re: null == "" is true?

2022-07-12 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 16:40:38 UTC, H. S. Teoh wrote: Because an empty string is, by default, represented by an empty slice of the null pointer. Do not rely on this, however; it's possible sometimes to get an empty string that isn't null, e.g., if you incrementally shrink a slice over

Re: How to obtain Variant underlying type?

2022-07-10 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 10 July 2022 at 18:31:46 UTC, drug007 wrote: On 7/10/22 20:26, anonymouse wrote: On Sunday, 10 July 2022 at 06:26:37 UTC, jfondren wrote: ```d import std.variant : Variant; size_t[] shape(Variant v) {     import std.variant : VariantException;     size_t[] s;     try {    

Re: Ascertaining!

2022-07-09 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 9 July 2022 at 10:12:00 UTC, Salih Dincer wrote: Hi All, I didn't know compiling was bottom-up. This example proves that. Because in single-line expressions, it takes value from the right, firstly. ```d void main() { int n; // true (n == 0) int i = 1; //

Re: Unwrap variadic template into vararg of pointers of the same types

2022-07-08 Thread Paul Backus via Digitalmars-d-learn
On Friday, 8 July 2022 at 12:20:13 UTC, ryuukk_ wrote: The problem when i try to introduce variadic template, is i can't seem to understand how to unwrap the parameter as pointer type T -> T* ```D struct Includes(Args...) { alias args = Args; } void view_it(Includes)(void function(entity_t,

Re: vibe.d Serialize/Deserialize SumType to/from json

2022-07-04 Thread Paul Backus via Digitalmars-d-learn
On Monday, 4 July 2022 at 11:35:24 UTC, Antonio wrote: (or a way for creating a custom struct "inheriting" SumType with serialization capabilities)? You can "inherit" from a struct using `alias this`: ```d struct CustomStruct { SumType!(A, B, C) unwrap; alias unwrap this; //

Re: Importing module from the perspective of submodule.

2022-07-03 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 3 July 2022 at 05:40:30 UTC, BoQsc wrote: Is it possible to import module that is not in the module's current directory's folder or subfolders? For example: I want to import `somemodule2.d` and `somemodule3.d` into a **`somemodule.d`** **.\somefolder\somemodule.d**

Re: How can I check if a type is a pointer?

2022-06-25 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 25 June 2022 at 14:18:10 UTC, rempas wrote: For example, something like the following: ```d void main() { char accepted_type; char* non_accepted_type; if (__traits(isPointer, typeof(accepted_type))) { // The type is not accepted } ``` Use an [`is()` expression:][1]

Re: DIP1000

2022-06-23 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 23 June 2022 at 21:34:27 UTC, Ola Fosheim Grøstad wrote: On Thursday, 23 June 2022 at 21:05:57 UTC, ag0aep6g wrote: It's a weird rule for sure. Another slightly annoying thing is that it cares about destruction order when there are no destructors. If there are no destructors

Re: destroy and @safe

2022-06-21 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 21 June 2022 at 17:33:46 UTC, H. S. Teoh wrote: Does the language allow you to declare a @system delegate inside @safe code? Yes. This compiles: void main() @safe { void delegate() @system dg = () @system { /* do scary stuff */ }; }

Re: destroy and @safe

2022-06-21 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 21 June 2022 at 14:40:41 UTC, Antonio wrote: The problem: "use" can't be @safe because it contains a call to "destroy". `destroy` should be `@safe` as long as the destructor it's calling is `@safe`. If the destructor is `@system`, then the only way to call `destroy` in `@safe`

Re: Failure due to memcpy being called at compile time

2022-06-21 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 21 June 2022 at 04:30:25 UTC, JG wrote: On Tuesday, 21 June 2022 at 01:39:43 UTC, Paul Backus wrote: Update: a new release of [the `sumtype` package on code.dlang.org][1] is available that includes the fix for this bug. `std.sumtype` will be fixed in the next Phobos release,

Re: Failure due to memcpy being called at compile time

2022-06-20 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 14 June 2022 at 05:35:46 UTC, JG wrote: On Monday, 13 June 2022 at 21:45:39 UTC, Paul Backus wrote: The call to `move` is coming from `SumType.opAssign`: https://github.com/dlang/phobos/blob/v2.100.0/std/sumtype.d#L681 I've filed a bugzilla issue for this here:

Re: Conpile-Time module constructor

2022-06-19 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 19 June 2022 at 15:57:41 UTC, Ruby The Roobster wrote: On 6/19/2022 11:55 AM, Paul Backus wrote: You can do this with a `static if` check:     static if (__traits(compiles, { import mymodule; }))     {     // mymodule is included     enum compileTimeArray = ...;     }

Re: Conpile-Time module constructor

2022-06-19 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 19 June 2022 at 15:34:48 UTC, Ruby The Roobster wrote: On 6/19/2022 11:19 AM, Paul Backus wrote: On Sunday, 19 June 2022 at 14:51:26 UTC, Ruby The Roobster wrote: Is it possible to make a module constructor run at compile-time?  If so, how? No, it's not. What are you trying to

Re: Conpile-Time module constructor

2022-06-19 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 19 June 2022 at 14:51:26 UTC, Ruby The Roobster wrote: Is it possible to make a module constructor run at compile-time? If so, how? No, it's not. What are you trying to accomplish that lead you to ask this question? There is probably a different way to do it without involving

Re: UFCS limit

2022-06-16 Thread Paul Backus via Digitalmars-d-learn
On Friday, 17 June 2022 at 05:17:20 UTC, Tejas wrote: On Friday, 17 June 2022 at 01:04:28 UTC, Paul Backus wrote: Nope. The way UFCS works is that allows you to call free functions using member-function syntax, and member-function syntax is always `object.memberName`, so UFCS only works for

Re: UFCS limit

2022-06-16 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 16 June 2022 at 23:59:06 UTC, Antonio wrote: Is it there any way to apply UFCS on the returned method in the same expression? Nope. The way UFCS works is that allows you to call free functions using member-function syntax, and member-function syntax is always

Re: getSymbolsByUDA in constructor/member functions

2022-06-16 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 16 June 2022 at 13:27:25 UTC, frame wrote: But it looks like a compiler bug since the output of `getSymbolsByUDA` is just an alias sequence and nothing should happen before consuming it? Yes, this is a compiler bug. I've filed a report for it on bugzilla:

Re: Failure due to memcpy being called at compile time

2022-06-13 Thread Paul Backus via Digitalmars-d-learn
On Monday, 13 June 2022 at 19:48:06 UTC, JG wrote: Hi, I reduced my code to the following. Could anyone help me to discover why the line marked with //THIS LINE causes memcpy to be called, and how can I avoid this? Reduced further: ```d import std.sumtype; struct Tuple { void

Re: Generating unique identifiers at compile time

2022-06-12 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 9 June 2022 at 21:20:27 UTC, JG wrote: In doing so I wanted to produce unique identifiers (something like gensym in racket.) I did this in a very hacky way: [...] Is there some way to ask the compiler for a unique name or a better way of achieving this? Here's a `gensym`

Re: sha256 to bigint

2022-06-11 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 11 June 2022 at 13:09:44 UTC, vc wrote: Hello, is there any way to represnts a sha256 hash like 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 in BigInt ? I've try so far auto t = BigInt("2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824") and

Re: Range to Nullable conversion

2022-06-10 Thread Paul Backus via Digitalmars-d-learn
On Friday, 10 June 2022 at 17:22:53 UTC, Antonio wrote: Can this code be written as a **simple** expression? (without having to write helper methods). ```d import std.range, std.typecons; Nullable!(ElementType!R) maybeFront(R)(auto ref R r) if (isInputRange!R) { if (r.empty)

Re: Copy Constructor

2022-06-05 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 5 June 2022 at 18:50:13 UTC, Salih Dincer wrote: On Sunday, 5 June 2022 at 15:45:17 UTC, Salih Dincer wrote: Hi, Let be the structure Foo that wraps an int pointer. Let's setup Foo in 3 different ways: 1. Foo one = Foo(1); 2. Foo two = 2; 3. [ Foo(3) ]; There is a fourth

Re: Comparing Exceptions and Errors

2022-06-04 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 4 June 2022 at 22:03:08 UTC, kdevel wrote: On Saturday, 4 June 2022 at 14:05:14 UTC, Paul Backus wrote: This is entirely a question of API design. If it should be the caller's responsibility to check for some condition before calling the function, then you can throw an `Error`

Re: Comparing Exceptions and Errors

2022-06-04 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 4 June 2022 at 11:57:32 UTC, kdevel wrote: 2. Since 2017 or so I have written some 10 KLOC of D, maybe about two dozen classes deriving from Exception. But I did not annotate any of my methods or function with "nothrow" nor did I author any class deriving from `Error`.

Re: Graphing a D function : possible?

2022-06-02 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 2 June 2022 at 03:37:13 UTC, z wrote: Is there a quick way of obtaining the graph of D functions like these? ```d T f(T) if (isScalarType!T){} ``` or ```D T[2] f(T, T)if (isScalarType!T){} ``` I know that there are graphing calculators already, but these don't support low level

Re: Templatized delegates

2022-05-31 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 31 May 2022 at 21:15:24 UTC, Andrey Zherikov wrote: I have tightly coupled code which I'd like to decouple but I'm a bit stuck. For simplicity, I reduced the amount of code to something simple to understand. So I have a struct `S` that has templated member function that does

Re: Inferred attributes errors in template function.

2022-05-27 Thread Paul Backus via Digitalmars-d-learn
On Friday, 27 May 2022 at 08:39:08 UTC, vit wrote: Is in dmd some flag that print errors similarly to this?: ```d void main()@safe pure{ foo!long(); foo!float(); //Error: `pure` function `D main` cannot call impure function `onlineapp.foo!float.foo` //Error:

Re: Why (v1<<8 + v2) different from (v1<<8 | v2)?

2022-05-25 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 25 May 2022 at 12:42:04 UTC, step8 wrote: I run following test code: int v1 = 22; int v2 = 23; writeln( v1<<8 + v2 ); writeln( v1<<8 | v2 ); result is 0 and 5655 Why ( v1<<8 + v2 ) = 0 ? `+` has a higher precedence than `<<`, so the

Re: What are (were) the most difficult parts of D?

2022-05-12 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 12 May 2022 at 18:07:05 UTC, H. S. Teoh wrote: On Thu, May 12, 2022 at 09:04:09AM -0700, Ali Çehreli via Digitalmars-d-learn wrote: Error: template `std.algorithm.iteration.sum` cannot deduce function from argument types `!()(int[3])`

Re: What are (were) the most difficult parts of D?

2022-05-11 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 11 May 2022 at 15:10:28 UTC, Tejas wrote: That'll be true the day when `@safe` becomes the default... Until then, I'll atleast do `@safe:` on top of every module :) `@safe:` is actually a bad idea if you're writing templated code, because it turns inference of `@system` into a

Re: Compile delegate with enum into proper function?

2022-05-07 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 7 May 2022 at 20:24:39 UTC, jmh530 wrote: On Saturday, 7 May 2022 at 18:46:03 UTC, Paul Backus wrote: ```d import std.functional: partial; enum int a = 1; alias foo2 = partial!(foo, a); ``` [snip] Thanks. This is basically equivalent to ```d int foo(int a)(int x) { return x +

Re: Compile delegate with enum into proper function?

2022-05-07 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 7 May 2022 at 18:36:40 UTC, jmh530 wrote: In the code below, there is a two parameter function `foo` and an override of it with only one parameter. In the override case, I force the second one to be 1, but ideally there should be a way to specify it at compile-time. Have you

Re: std.typecons Typedef initializers?

2022-04-25 Thread Paul Backus via Digitalmars-d-learn
On Monday, 25 April 2022 at 08:54:52 UTC, Chris Katko wrote: D alias sPair = Typedef!pair; // pair of xy in screen space coordinates alias vPair = Typedef!pair; // pair of xy in viewport space coordinates //etc This doesn't do what you think it does. Both `sPair` and `vPair` are

Re: Understanding alias template parameters

2022-04-21 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 21 April 2022 at 21:02:47 UTC, JG wrote: Hi, Could someone possibly help me to understand why the commented line doesn't compile? ```d import std; struct MapResult(R,F) { R r; const F f; auto empty() { return r.empty; } auto front() { return f(r.front); }

Re: unexpected noreturn behavior

2022-04-21 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 21 April 2022 at 12:54:12 UTC, Dennis wrote: On Thursday, 21 April 2022 at 12:41:08 UTC, WebFreak001 wrote: which I think is a little bug-prone, but at least that would solve my issues. What issue do you have with it returning `true`? Presumably the problem is that if you write

Re: Calling template member function?

2022-04-19 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 19 April 2022 at 13:36:26 UTC, Andrey Zherikov wrote: I want to migrate my library API from standalone function that takes delegate as argument to a template member function that takes delegate as a template parameter but compiler errors out. Here is code example: ```d import

Re: Collect the arguments of all the function calls at a compile time in the whole program.

2022-04-15 Thread Paul Backus via Digitalmars-d-learn
On Friday, 15 April 2022 at 18:11:11 UTC, BoQsc wrote: Let's say I have this example program. I want to get the arguments of all the `some_function();` in the whole program. **Even if the scope of the function call is never executed.** (Ex. due to IF statement being negative.) You can't do

Re: Why do immutable variables need reference counting?

2022-04-11 Thread Paul Backus via Digitalmars-d-learn
On Monday, 11 April 2022 at 12:12:39 UTC, Salih Dincer wrote: It worked for me in a different way. 1 is (about to be) alive! 2 is (already) dead. 2 is (already) dead. 2 is (already) dead. 2 is (already) dead. 2 is (already) dead. Hello D! 1 is (already) dead. Because I changed the code

Re: Removing elements from dynamic arrays?

2022-04-05 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 5 April 2022 at 14:10:44 UTC, Steven Schveighoffer wrote: I'd implement it probably like this (for D2): ```d auto drop(T)(ref T[] arr, T which) { import std.algorithm, std.range; auto f = arr.find(which); debug if(f.empty) throw ...; auto result = arr.front; arr =

Re: I want to append to lists without using append

2022-04-04 Thread Paul Backus via Digitalmars-d-learn
On Monday, 4 April 2022 at 12:57:28 UTC, V3nom wrote: define the lists: (define liste (cons 10(cons 20(cons 30(cons 40 ' ()) (define liste2 (cons 20(cons 30(cons 10(cons 40 ' ()) define the "function": (define (listapp list1 list2)( if (null? (cdr list1))

<    1   2   3   4   5   6   7   8   9   >