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

2024-04-16 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 17 April 2024 at 01:36:59 UTC, Liam McGillivray wrote: To better understand what I mean, take the following example, where I have a function, and two structs. ``` struct typeA { // Some member variables here } struct typeB { // Some similar member variables here, but in a

Re: Why does disabling a struct's postblit increase its size in memory?

2024-03-03 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 2 March 2024 at 19:29:47 UTC, Per Nordlöw wrote: On Saturday, 2 March 2024 at 19:28:08 UTC, Per Nordlöw wrote: On Saturday, 2 March 2024 at 19:11:42 UTC, kinke wrote: Not according to run.dlang.io, for all available DMD versions. Perhaps your tested `S` was nested in some

Re: Error when using `import`.

2024-02-24 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 24 February 2024 at 10:31:06 UTC, Liam McGillivray wrote: `Unit.d` & `Map.d` are longer files. `Map.d` begins with `import Tile;`, and `Unit.d` begins with `import Map;`. Why are the errors happening? What's the problem? Why is it `currentclass.importedclass` instead of simply

Re: Circular enum member references in UDAs

2024-02-15 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 15 February 2024 at 18:12:42 UTC, realhet wrote: Hello, Today I tried to upgrade my sources to the latest LDC, but failed with this unfortunate error. ``` import std; struct S{ E e; } enum E { @S(e2) e1, @S(e1) e2 } ``` Looks like someone reported a similar bug in

Re: std.uni CodepointSet toString

2024-02-12 Thread Paul Backus via Digitalmars-d-learn
On Friday, 9 February 2024 at 08:04:28 UTC, Danilo wrote: Incredible! Seems like D is experiencing featuritis. Priorities may be wrong. Instead of bug fixing and stabilization, people concentrate on getting new stuff like ˋ:blubˋ into the language. If you look at the work actually being done

Re: real.sizeof

2024-02-05 Thread Paul Backus via Digitalmars-d-learn
On Monday, 5 February 2024 at 16:45:03 UTC, Dom DiSc wrote: Why is real.sizeof == 16 on x86-systems?!? Its the IEEE 754 extended format: 64bit mantissa + 15bit exponent + sign. It should be size 10! I mean, alignment may be different, but why wasting so much memory even in arrays? According

Re: template/mixin magic for to! auto inferring type from variable

2024-02-02 Thread Paul Backus via Digitalmars-d-learn
On Friday, 2 February 2024 at 23:25:37 UTC, Chris Katko wrote: The auto solution won't work for a struct however which I'm using: ```D struct procTable{ //contains all the fields inside a file I'm parsing uint time; int priority; string name; // etc } ``` Maybe you can use

Re: template/mixin magic for to! auto inferring type from variable

2024-02-02 Thread Paul Backus via Digitalmars-d-learn
On Friday, 2 February 2024 at 07:43:09 UTC, Chris Katko wrote: Is there some way to do: ```D string[3] data; //strings from some file input, some are ints, uints, etc. auto into!(T)(T value){return to!???(value); } // ??? uint time = into!(data[1]); // We already know this is uint int

Re: Scripting with Variant from std.variant: parameter passing

2024-02-02 Thread Paul Backus via Digitalmars-d-learn
On Friday, 2 February 2024 at 20:28:50 UTC, Carl Sturtivant wrote: On Friday, 2 February 2024 at 19:22:22 UTC, Steven Schveighoffer wrote: ```d // shim auto foo(Args...)(Args args) if (!allSatisfy!(isVariant, Args)) { mixin("return foo(", argsAsVariants(args.length), ");"); } ``` Thanks

Re: Safety is not what you think

2024-01-30 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 30 January 2024 at 02:05:23 UTC, user1234 wrote: I want to share a stupid program to show you that D safety is more complex than you might think: ```d module test; void test() @safe { int i; int b = (*&(*&++i))++; } void main() @safe { test(); } ``` I'm not showing a

Re: Function Composition

2024-01-25 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 25 January 2024 at 08:25:02 UTC, atzensepp wrote: ```d int function(int) t = compose!(f,g,g,f,g,g,f,g,g,f); ``` This leads to: ``` gdc lambda4.d lambda4.d:28:25: error: template compose(E)(E a) has no value int function(int) t = compose!(f,g,g,f,g,g,f,g,g,f); ``` Try using

Re: opApply + const

2024-01-23 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 23 January 2024 at 16:11:25 UTC, ryuukk_ wrote: It works fine.. but when the variable becomes ``const(Stuff)* stuff;`` It gives me: ``` onlineapp.d(13): Error: cannot uniquely infer `foreach` argument types ``` I have no idea what i should be doing, does anyone have a clue?

Re: std.sumtype nested SumTypes

2024-01-22 Thread Paul Backus via Digitalmars-d-learn
On Monday, 22 January 2024 at 21:11:17 UTC, NonNull wrote: I'd like SumType to combine the implicit tags so there's only one tag. SumType does not do this automatically (because sometimes you might want to keep the inner SumTypes separate), but you can do it yourself like this: alias

Re: compute from a string the text of a string literal

2024-01-17 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 17 January 2024 at 18:44:14 UTC, Carl Sturtivant wrote: Hello, I'd like a function like this, ``` string image(string s) ``` that maps any string s into the doubly quoted backslash escaped text that would be a string literal for s were it pasted into a program. Perhaps with a

Re: Generating custom toString for structs

2024-01-07 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 7 January 2024 at 09:49:36 UTC, Renato wrote: Is the above a "good" way to do this? It looks ok to me. There are some minor changes I would make, like using `typeof(this)` instead of `S` to refer to the type of the struct you're mixing it into, but the overall approach is fine.

Re: Trying to understand map being a template

2024-01-06 Thread Paul Backus via Digitalmars-d-learn
On Friday, 5 January 2024 at 20:41:53 UTC, Noé Falzon wrote: In fact, how can the template be instantiated at all in the following example, where no functions can possibly be known at compile time: ``` auto do_random_map(int delegate(int)[] funcs, int[] values) { auto func =

Re: Problems using rawWrite in an experiment with WAVs in D

2023-12-27 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 27 December 2023 at 20:20:23 UTC, tososdk wrote: I was recreating some code from C++ to D: [...] But since I am somewhat new to these topics and even more so to Dlang, I don't understand very well. The problem occurs in the creation of the .wav, regarding rawWrite, I'm not

Re: Something similar to "inline"

2023-12-27 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 27 December 2023 at 15:57:14 UTC, tososdk wrote: Two things: Could you explain how "inline" works? Is there something similar in Dlang? In C and C++, `inline` is a suggestion to the compiler that it should consider using [inline expansion][1] for calls to a particular function.

Re: Permutations of array (slice) ?

2023-12-12 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 12 December 2023 at 14:57:48 UTC, Kevin Bailey wrote: perm.d:8:26: error: none of the overloads of template ‘std.algorithm.iteration.permutations’ are callable using argument types ‘!()(char[])’ 8 | foreach (perm; as.permutations) | ^

Re: opApply seems like it can infer delegate types AND parameters!?

2023-12-11 Thread Paul Backus via Digitalmars-d-learn
On Monday, 11 December 2023 at 23:21:45 UTC, Quirin Schroll wrote: I always thought you had to provide aliases with all 16 combinations of the attributes `@safe`, `@nogc`, `pure`, and `nothrow` for each actually desired instance. But you don’t and **I have no clue why**. Why does it work?

Re: struct initializer

2023-12-01 Thread Paul Backus via Digitalmars-d-learn
On Friday, 1 December 2023 at 13:02:06 UTC, Dom DiSc wrote: ```d S Fun(){ return { 5, 2 }; } ``` This IS an initialization and the type is known. Requiring the repetition of the type is also here annoying. Technically you don't *have* to repeat the type. You can write the return type as

Re: struct initializer

2023-11-29 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 29 November 2023 at 17:23:04 UTC, Antonio wrote: On Wednesday, 29 November 2023 at 16:48:09 UTC, Paul Backus wrote: ... it even supports named arguments: - Witch version of DMD supports named arguments? Is it an experimental compiler option? I don't know what the earliest

Re: struct initializer

2023-11-29 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 29 November 2023 at 16:38:36 UTC, Dom DiSc wrote: ```d struct S2 { int a; int b; this(int c, int d) { a=c; b=d; } } S2 fun3() { return S2( 5, 2 ); } // works but requires explicit constructor ``` You can use this syntax without an explicit constructor: struct S3 { int a;

Re: mixin issue

2023-11-29 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 29 November 2023 at 13:31:14 UTC, DLearner wrote: ``` Error: found `End of File` when expecting `;` following statement ``` If an extra ; is added: ``` }(` ~ strStartPtr ~ `,` ~ strPLPtr ~ `);`; ``` it works but doesn't seem correct. This is an annoying limitation of the D

Re: interface inference

2023-11-28 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 23 November 2023 at 19:17:20 UTC, Antonio wrote: Basically, the ternary conditional ```?:``` result type is not inferred even if the type returned by the two possibilities are the same. **Is it a bug or the expected behaviour?** Known bug, first reported in 2009:

Re: mixin under -betterC

2023-11-23 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 23 November 2023 at 16:33:52 UTC, DLearner wrote: Code below is intended to test simple mixin with lambda function under -betterC. Works with full-D, but fails with 'needs GC' errors under -betterC. Why is this so, bearing in mind the concatenations are executed at compile, not

Re: Doubt about type Inference on templates

2023-11-22 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 22 November 2023 at 17:53:15 UTC, Antonio wrote: Basically, it doesn't know witch version of ```filter``` to use, because it is inferring `i=>i%2==0` is `void` ?!?!?! ``` !()(IIterable!int, void) ``` If I explicitly write `(int i)=>i%2==0`, it compiles correctly again. **Is

Re: What is :-) ?

2023-11-20 Thread Paul Backus via Digitalmars-d-learn
On Monday, 20 November 2023 at 16:09:33 UTC, Antonio wrote: What "breaks" my mind is that a compiler decision (treat a piece of code as function or delegate) is not completely transparent causing "side" effects on your code (writeln doesn't work the same way: it shows the delegate signature,

Re: What is :-) ?

2023-11-20 Thread Paul Backus via Digitalmars-d-learn
On Monday, 20 November 2023 at 08:47:34 UTC, Antonio wrote: - What is the way to do ```writeln``` work with ```Counter``` function the same way it works with ```next``` function? `writeln()` should do it.

Re: What is :-) ?

2023-11-20 Thread Paul Backus via Digitalmars-d-learn
On Monday, 20 November 2023 at 08:47:34 UTC, Antonio wrote: I understand the problem with UFCS (``next`` is not using UFCS because it is a delegate defined in the own main() function, and ``Counter``` without () is treated as a function call because it is UFCS eligible ) This is not UFCS,

Re: Struct copy constructor with inout

2023-11-14 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 14 November 2023 at 14:36:57 UTC, dhs wrote: Just to clarify some more: isn't "s1 = ss1" similar to something like: ```d const(S1) s1; S1 ss1; // ss1 is now S1.init S1_copy_construct_const_in_const_out(ss1, s1); ``` If this is the case, the compile error is expected,

Re: Does exists some way to define a implementation for a symbol?

2023-11-14 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 14 November 2023 at 13:43:03 UTC, Hipreme wrote: Right now, I've been implementing classes separately, and I need a dummy symbol. The best world is not even having a symbol but having only its implementation, for example, I would like being able to do that: ```d void

Re: Struct copy constructor with inout

2023-11-14 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 14 November 2023 at 13:41:32 UTC, Steven Schveighoffer wrote: ``` Error: copy constructor `testinoutctor.S1.this(ref const(S1) s) const` is not callable using argument types `(const(S1))` ``` I'm not sure what this means. There shouldn't be a copy being made here, as the thing is

Re: Struct copy constructor with inout

2023-11-14 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 14 November 2023 at 08:50:34 UTC, dhs wrote: ```d struct S2 { this(ref inout S2 s) inout { writeln("copy"); } int i; } void test() { const(S1) s1; S1 ss1 = s1; // error, ss1 not qualified as const const(S2) s2; S2 ss2 = s2; // fine, why? } ``` Isn't "inout"

Re: How do I install a package globally?

2023-11-10 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 11 November 2023 at 01:50:54 UTC, Trevor wrote: How does one install packages globally, and how can I write programs that use the third-party packages without being wrapped in a dub file? Dub currently isn't able to do this. There's been some discussion about adding a `dub

Re: "is not an lvalue" when passing template function to spawn function

2023-11-08 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 8 November 2023 at 16:30:49 UTC, Bienlein wrote: Hello, I get the error "`addToBiz(T)(Biz!T biz)` is not an lvalue and cannot be modified" when compiling the code below. Can't find a way how to do it right. Am a D newbie and would appreciate some help. [...] static void

Re: How should class objects created in betterC be destroyed

2023-11-06 Thread Paul Backus via Digitalmars-d-learn
On Monday, 6 November 2023 at 05:30:02 UTC, zoe wrote: I customized object.d in -betterc mode and created NEW templates, with modules I can seemingly create classes without extern(C++) mode, and type conversions in function calls seem to work fine. But when destroy doesn't find a way to call

Re: Is a shorter statement possible in this case?

2023-11-06 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 5 November 2023 at 18:36:40 UTC, Ctn-Dev wrote: I wrote this earlier: [...] `if` runs when both "One" and "Two" are in the given array as intended, but its conditional statement looks verbose. Is there a more concise way of getting the same result? If sorting the arrays is an

Re: Why can't we use strings in C++ methods?

2023-11-04 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 4 November 2023 at 03:00:49 UTC, Dadoum wrote: I was wondering why C++ linkage forbids strings as arguments while we can with the C one. With C linkage, it's translated to a template that's defined in the automatically generated header, but it just doesn't compile in C++.

Re: Keyword "package" prevents from importing a package module "package.d"

2023-11-02 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 2 November 2023 at 12:52:35 UTC, BoQsc wrote: Therefore the need to import `package.d` is needed and I can't see a solution, which means that D Language might have to introduce a way to import `package.d` from inside the package, if there is a need to further improve experience

Re: Keyword "package" prevents from importing a package module "package.d"

2023-11-02 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 2 November 2023 at 11:12:58 UTC, BoQsc wrote: Weirdly enough it does not work on Windows operating system. [...] ``` program.d(1): Error: unable to read module `waffles` program.d(1):Expected 'waffles.d' or 'waffles\package.d' in one of the following import paths: import

Re: Member field of type nested struct must be initialized in constructor: Why?

2023-10-22 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 22 October 2023 at 21:02:32 UTC, Inkrementator wrote: Running the code with `rdmd -version=fix app.d` works, but running `rdmd -version=fix app.d` produces: `Error: field `member` must be initialized in constructor, because it is nested struct` Why? I didn't find anything about this

Re: Forcing my module to be initialized first

2023-10-15 Thread Paul Backus via Digitalmars-d-learn
On Monday, 16 October 2023 at 03:31:13 UTC, dan wrote: I have some code that i would like executed before anything else is. The code is to set an environment variable which is used by a library. I'm trying to find some way to avoid setting the environment variable on the command line, or in

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-03 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 3 October 2023 at 18:29:49 UTC, Salih Dincer wrote: More importantly, is there a priority order? Because in our last example, when we leave a single overload, all features are executed through the ref opIndex except the bit: The spec says: If an index expression can be rewritten

Re: T[] opIndex() Error: .. signal 11

2023-10-03 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 3 October 2023 at 17:05:46 UTC, Steven Schveighoffer wrote: ```d void main() { S s = 0; { scope int[] __r3 = s.opIndex()[]; ulong __key4 = 0LU; for (; __key4 < __r3.length; __key4 += 1LU) {

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-03 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 3 October 2023 at 16:45:39 UTC, Steven Schveighoffer wrote: OK, so it's not as bad as I thought, but surely the compiler should recognize that `opIndexAssign(val, idx)` doesn't work, but `opIndex(idx) = val` does? Maybe. On the other hand, if you make a typo in the body of your

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-03 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 3 October 2023 at 13:07:00 UTC, Steven Schveighoffer wrote: Now, you can define a further `opIndexAssign(T val, size_t idx)`. However, now you lose capabilities like `a[0]++`, which I don't think has a possibility of implementing using an `opIndex` operator, and it would be

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-02 Thread Paul Backus via Digitalmars-d-learn
On Monday, 2 October 2023 at 20:34:11 UTC, Salih Dincer wrote: In an old version (for example, v2.0.83), the code you implemented in the places where Slice is written above works as desired. In the most current versions, the parameterized opIndexAssign(T value) gives the error:

Re: The difference between T[] opIndex() and T[] opSlice()

2023-10-02 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 1 October 2023 at 17:41:08 UTC, Salih Dincer wrote: Hi, What is the difference between T[] opIndex() and T[] opSlice(), which haven't parameters? `T[] opSlice()` is the D1 version and exists only for backwards compatibility. You should use `T[] opIndex()` in new code.

Re: How to print current type of a SumType?

2023-09-30 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 1 October 2023 at 01:17:50 UTC, Chris Piker wrote: Hi D I've a simple question but it's bedeviling me anyway. How do I get a string representation of the current type of a SumType? I'm trying to avoid something like this: ```d alias Vec3 = SumType!(void* /* invalid vector */,

Re: Help on array pointers

2023-09-14 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 14 September 2023 at 14:21:09 UTC, Vino wrote: Questions:1 ``` char[] invalid = (cast(char*)malloc(char.sizeof * length))[0..length]; ``` The above statement allocate memory for char type and the size of the allocated memory is char.sizeof * length so what is the use of this

Re: Unicode validation for Posix

2023-09-03 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 3 September 2023 at 10:06:58 UTC, Vino wrote: Hi All, As per the documentation from std.process it states that an exception is thrown if the variable contains invalid UTF-16 characters and it can also be validated using "validate" function from std.utf, so the question is do

Re: Function Pointer

2023-08-30 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 30 August 2023 at 17:48:19 UTC, Vino wrote: Hi All, Request your help on hot to create a pointer for a function whose function type is Result. ``` ReturnType!(typeof()).stringof; // Result From Vino ``` To get a function pointer type from a function type, you can add `*`

Re: std.experimental.allocator

2023-08-13 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 13 August 2023 at 11:44:50 UTC, IchorDev wrote: I feel like I can't possibly be the first to ask, but I couldn't find any prior discussion of this: When is `std.experimental.allocator` going to be moved out of `experimental`? Is there any roadmap for it? Is it just in limbo? The

Re: Syntax for Static Import of User Define Attributes

2023-07-27 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 27 July 2023 at 21:19:08 UTC, Vijay Nayar wrote: Attempted Fix 2: Enclose the entire attribute name in parenthesis. ``` static import vibe.data.serialization; class ChatCompletionFunctions { @(vibe.data.serialization.name)("name") ... } ``` You almost had it. The correct

Re: `static` on module-level functions

2023-07-07 Thread Paul Backus via Digitalmars-d-learn
On Friday, 7 July 2023 at 13:31:59 UTC, Steven Schveighoffer wrote: However, I can't think of a valid reason to allow `static` on a module-level scope. Applying static to a declaration at module-level should be a no-op. So maybe that's one "use" of static that can be eliminated. Well, it can

Re: std.sumtyp and option ?

2023-06-29 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 29 June 2023 at 14:18:05 UTC, kiriakov wrote: How to create option type over std.sumtype ? ``` enum None; struct Some(T) { T x; } alias Option = SumType!(Some!T, None); ``` I get Error: undefined identifier `T` Looks like you meant to type alias Option(T) = SumType!(Some!T,

Re: Scope guards

2023-06-26 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 24 June 2023 at 17:00:36 UTC, Cecil Ward wrote: I would like to use scope guards but in the guard I need to get access to some local variables at the end of the routine. This doesn’t really seem to make sense as to how it would work, because their values depend on the exact point

Re: A couple of questions about arrays and slices

2023-06-21 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 22 June 2023 at 00:10:19 UTC, Cecil Ward wrote: Is .reserve()’s argument scaled by the entry size after it is supplied, that is it is quoted in elements or is it in bytes? I’m not sure whether the runtime has a knowledge of the element type so maybe it doesn’t know anything about

Re: How does D’s ‘import’ work?

2023-06-18 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 18 June 2023 at 20:24:10 UTC, Cecil Ward wrote: On Thursday, 8 June 2023 at 05:11:04 UTC, Ali Çehreli wrote: dmd's -makedeps command line switch should be helpful there. (I did not use it.) Ali I wasn’t intending to use DMD, rather ldc if possible or GDC because of their

Re: need `this` for `this` of type `ref @safe Test(string reg_arg)

2023-06-18 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 18 June 2023 at 19:05:19 UTC, rempas wrote: On Sunday, 18 June 2023 at 18:17:16 UTC, Paul Backus wrote: `__ctor` doesn't create a new object, it initializes an existing object. You need to create the object first, then call `__ctor` on it: ```d void main() { Test test;

Re: need `this` for `this` of type `ref @safe Test(string reg_arg)

2023-06-18 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 18 June 2023 at 17:43:01 UTC, rempas wrote: Ok, so I'm having a struct that has a constructor that takes a template parameter. I suppose this question could also be named `how to initialize constructors with template parameters` but whatever! The funny thing is, I think that I may

Re: How do I generate `setX` methods for all private mutable variables in a class?

2023-06-05 Thread Paul Backus via Digitalmars-d-learn
On Monday, 5 June 2023 at 13:57:20 UTC, Ki Rill wrote: How do I generate `setX` methods for all private mutable variables in my class? Do I need to use `__traits`? I need this for my [tiny-svg](https://github.com/rillki/tiny-svg) project to generate `setX` methods for all Shapes. Example:

Re: Code duplication where you wish to have a routine called with either immutable or mutable arguments

2023-05-29 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 30 May 2023 at 02:57:52 UTC, Cecil Ward wrote: I have often come into difficulties where I wish to have one routine that can be called with either immutable or (possibly) mutable argument values. The argument(s) in question are in, readonly, passed by value or passed by const

Re: How to use @safe when a C library integration needed

2023-04-14 Thread Paul Backus via Digitalmars-d-learn
On Friday, 14 April 2023 at 14:10:41 UTC, Leonardo wrote: Thanks. But this works only to one function per time. Is there any way to do this to an imported library at all? something like `@trusted import library` No, there isn't. C is an unsafe language, so if you want to call C from `@safe`

Re: Memory leak issue between extern (c) and D function

2023-04-13 Thread Paul Backus via Digitalmars-d-learn
On Friday, 14 April 2023 at 03:50:37 UTC, backtrack wrote: Dear All, I am new to D lang. I have been given a task to consume the .dll generated from a D lang project. I added extern (c) function for call the .dll from CPP file. i have code like below ``` // myfile.d extern(c) {

Re: Returning a reference to be manipulated

2023-04-13 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 13 April 2023 at 07:05:10 UTC, Chris Katko wrote: Right now, I'm using pointers which resolves to: ```D // float* opIndex(string key){...} using pointer (*s["tacos"])++; // works with pointer, but is strange looking s["tacos"]++; // preferred syntax or something similar ``` You

Re: The Phobos Put

2023-03-31 Thread Paul Backus via Digitalmars-d-learn
On Friday, 31 March 2023 at 02:23:29 UTC, Steven Schveighoffer wrote: There's a certain attempt in phobos in some places to try and ensure code that is going to confuse will not compile. I think this is one of those attempts. Consider that if you pass a slice into `put`, then it returns

Re: The Phobos Put

2023-03-30 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 29 March 2023 at 20:50:04 UTC, Steven Schveighoffer wrote: On 3/29/23 4:29 PM, ag0aep6g wrote: But regardless of Salih's exact intent, the broader point is: a non-ref overload could be added to Phobos. And that would enable `a[1..$-1].phobos_put([2, 3])`. Which is what he asked

Re: #define-like behavior

2023-03-15 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 15 March 2023 at 16:40:52 UTC, bomat wrote: Just out of curiosity: Can you explain to me why this is called an `enum` although it's clearly not an enumeration? Seems like a random abuse of a keyword... It's shorthand for defining an unnamed `enum` with a single member: ```d

Re: Better way to compromise on the lack of template alias resolution stuff?

2023-03-15 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 14 March 2023 at 10:19:24 UTC, Elfstone wrote: I went back to some of my old code and couldn't stand what I had ended up with - If I already have a well-defined `Vector`, why do I have to write extra code to implement `isVector`, and use `isVector` instead of simply declaring the

Re: evenChunks on a string - hasLength constraint fails?

2023-03-14 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 14 March 2023 at 08:21:00 UTC, amarillion wrote: I'm trying to understand why this doesn't work. I don't really understand the error. If I interpret this correctly, it's missing a length attribute on a string, but shouldn't length be there? By default, D's standard library treats

Re: #define-like behavior

2023-03-14 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 14 March 2023 at 05:47:35 UTC, Jeremy wrote: Hi, in C and C++ you can use #define to substitute a value in place of an identifier while preprocessing. If you initialize a new string and don't change its value after that, will the compiler substitute the string identifier with its

Re: 'auto' keyword

2023-03-12 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 12 March 2023 at 15:31:07 UTC, Salih Dincer wrote: Moreover, `auto ref` or `ref auto` is needed in functions. That's because `ref` isn't part of the argument or return value's type, so it isn't covered by **type** inference. Instead, D has a totally separate feature for "`ref`

Re: foreach with assoc. array

2023-03-01 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 1 March 2023 at 19:05:10 UTC, DLearner wrote: (1) & (2) compile and run with the expected results. But (3) fails with: ``` Error: variable `wk_Idx` is shadowing variable `for3.main.wk_Idx` ``` Why is this usage wrong? With `foreach`, you can't reuse an existing variable as the

Re: CompilerInvocationException

2023-02-26 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 26 February 2023 at 14:17:50 UTC, ryuukk_ wrote: On Sunday, 26 February 2023 at 07:11:55 UTC, Paul Backus wrote: Since `Expression` contains `Binary` and `Unary`, and `Binary` and `Unary` contain `Expression`, that means `Expression` contains itself--which is not allowed, because it

Re: CompilerInvocationException

2023-02-25 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 26 February 2023 at 02:33:21 UTC, guacs wrote: NOTE: The error is happening when I'm using the SumType and I suspect that I'm using it incorrectly. I am not sure whether this is the specific error that is causing your problem, but there is a mistake in your use of `SumType` in

Re: Template alias parameter: error: need 'this' for ...

2023-02-24 Thread Paul Backus via Digitalmars-d-learn
On Friday, 24 February 2023 at 14:22:17 UTC, user1234 wrote: you can break using `goto`, restore `static` everywhere, and using local introspection determine whether the result exists. ```d struct Bar { @("hello") int t; } static bool hasAttribute(alias F, T)() { static

Re: Template + alias + basic type depends on another parameter = broken?

2023-02-22 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 22 February 2023 at 20:20:46 UTC, Dark Hole wrote: ```d template Foo(T, alias T[] Array) { // ... } // ... Bar[] arr; Foo!(Bar, arr); ``` This is valid D, but it doesn't work. It gives error "Error: template instance `Foo!(Bar, arr)` does not match template declaration

Re: Gneric linkedList range adaptor

2023-02-10 Thread Paul Backus via Digitalmars-d-learn
On Friday, 10 February 2023 at 22:10:20 UTC, Ben Jones wrote: I'm trying to write a range adaptor for linked list types. The range type seems to work OK, but my helper function to deduce the node type has a compiler error. My hunch is that `nextField` loses its association with T when I'm

Re: Assign to Array Column

2023-02-01 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 31 January 2023 at 01:04:41 UTC, Paul wrote: Greetings, for an array byte[3][3] myArr, I can code myArr[0] = 5 and have: 5,5,5 0,0,0 0,0,0 Can I perform a similar assignment to the column? This, myArr[][0] = 5, doesn't work. Thanks! Here's a solution using standard-library

Re: How to access private variable of outer class from an inner struct

2023-01-15 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 15 January 2023 at 12:26:15 UTC, thebluepandabear wrote: If I have the following code: ```D class X { private int num; struct Y { // how to access num? } } ``` How would I access `num` from `Y`? Whenever I try to I get a compilation error. I believe that it's

Re: How should I return multiple const values from a function?

2023-01-02 Thread Paul Backus via Digitalmars-d-learn
On Monday, 2 January 2023 at 23:25:48 UTC, Charles Hixson wrote: They syntax I wanted was something like: bool func (const out Key k, const out Val v) { k = this.key.dup; v = this.val.dup; return true;    } This works for me: import std.typecons; auto func(Key, Value)(Key k, Value

Re: How should I return multiple const values from a function?

2023-01-02 Thread Paul Backus via Digitalmars-d-learn
On Monday, 2 January 2023 at 22:53:13 UTC, Charles Hixson wrote: I want to return values of the template parameter type, so there doesn't seem to be any way to dup or idup them. It's hard to say where exactly you're going wrong if you only post the error message, without the code that

Re: Confusion about `Random`

2022-12-22 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 22 December 2022 at 16:23:16 UTC, jwatson-CO-edu wrote: I am confused about why Program 1 produces random output but Program 2 does not. The code you have posted as "Program 2" is incomplete, and cannot be compiled as-is. I have made some changes in order to get it to compile

Re: /usr/bin/ld: [...] undefined reference to _D3std6format6internal6write...

2022-12-20 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 20 December 2022 at 20:01:04 UTC, Anonymouse wrote: What does `-allinst` even do `-allinst` tells the compiler to generate code for all instantiated templates, even if it thinks that code has already been generated in a different object file. Why would the compiler think that?

Re: Thinking about the difference between fixed and 'dynamic' arrays.

2022-12-03 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 3 December 2022 at 22:46:31 UTC, DLearner wrote: I agree should not change existing meaning of ``` int[] A; ``` But why not allow a construct for value-type variable arrays like: ``` int[*] B; ``` There's no reason to add more complexity to the language for this when the same

Re: Thinking about the difference between fixed and 'dynamic' arrays.

2022-11-29 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 29 November 2022 at 18:59:46 UTC, DLearner wrote: To me, it appears that there are really two (_entirely separate_) concepts: A. Supporting the useful concept of variable length (but otherwise entirely conventional) arrays; B. Supporting a language feature that acts as a window to

Re: __traits isCopyable vs isPOD

2022-11-28 Thread Paul Backus via Digitalmars-d-learn
On Monday, 28 November 2022 at 23:11:37 UTC, Per Nordlöw wrote: the real question I had is whether one should use `isPOD` instead of `isCopyable` in cases ```d static if (__traits(isCopyable, Element)) insertAt(element, index); else

Re: __traits isCopyable vs isPOD

2022-11-28 Thread Paul Backus via Digitalmars-d-learn
On Monday, 28 November 2022 at 20:58:43 UTC, Per Nordlöw wrote: For which types `T` does ```d __traits(isCopyable, T) ``` differ from ```d __traits(isPOD, T) ``` ? Lots of types. For example, types with copy constructors or destructors are not POD but may still be copyable. This should

Re: pointer escaping return scope bug?

2022-11-25 Thread Paul Backus via Digitalmars-d-learn
On Friday, 25 November 2022 at 14:07:28 UTC, ShadoLight wrote: On Saturday, 19 November 2022 at 15:00:16 UTC, Paul Backus wrote: Since, in your example, `lf` has global lifetime, the compiler deduces that `lf.fp` also has global lifetime, and therefore there is nothing wrong with assigning it

Re: "Little Scheme" and PL Design (Code Critique?)

2022-11-21 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 17 November 2022 at 22:05:45 UTC, jwatson-CO-edu wrote: * Compatibility with both Windows and Linux. What do I need to consider? - Can I create threads/processes under Windows? [core.thread][1] and [std.process][2] provide platform-independent interfaces for this that should

Re: pointer escaping return scope bug?

2022-11-19 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 19 November 2022 at 14:07:59 UTC, Nick Treleaven wrote: Hi, The following seems like a bug to me (reduced code, FILE* changed to int*): ```d @safe: struct LockedFile { private int* fps; auto fp() return scope => fps; } void main() { int* p; { auto lf =

Re: Actual lifetime of static array slices?

2022-11-16 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 15 November 2022 at 14:05:42 UTC, Siarhei Siamashka wrote: On Tuesday, 15 November 2022 at 13:16:18 UTC, Paul Backus wrote: D's safety model is the same. In `@safe` code, D will reject anything that the compiler cannot say for sure is memory safe. However, unlike in Rust, `@safe`

Re: Actual lifetime of static array slices?

2022-11-15 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 15 November 2022 at 13:01:39 UTC, Siarhei Siamashka wrote: Well, there's another way to look at it: https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html ('Unsafe Rust exists because, by nature, static analysis is conservative. When the compiler tries to determine whether or not

Re: difference between x.atomicOp!"+="(1) and atomicFetchAdd(x, 1)?

2022-11-10 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 10 November 2022 at 17:04:31 UTC, mw wrote: Hi, Anyone can help explain what is the difference between x.atomicOp!"+="(1) and atomicFetchAdd(x, 1)? Looking at the source in druntime, `atomicOp!"+="` forwards to `atomicFetchAdd` internally, so they should have the same

Re: return scope vs. scope return

2022-11-05 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 5 November 2022 at 16:13:18 UTC, 0xEAB wrote: Apparently there a difference between: - ```d Message withBody(Body body_) return scope { /* … */ } ``` - ```d Message withBody(Body body_) scope return { /* … */ } ``` ``` Deprecation: returning `this._body` escapes a

Re: save() feature for iota

2022-11-03 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 3 November 2022 at 06:26:22 UTC, Salih Dincer wrote: Hi All, Isn't there a save feature for `iota()`? Looking at the source, it seems that only the numeric overloads of `iota` implement `save`. I think this is probably just an oversight, though, since I can't see any reason why

Re: Unit testing a function returning void

2022-11-03 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 3 November 2022 at 10:00:27 UTC, Bruno Pagis wrote: Good morning, I have the following class: ``` class A { int[] array; ... void print() { writeln("array = ", this.array); } } ``` I would like to unit test the print function (yes, I know, not very useful on the

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`

  1   2   3   4   5   6   7   8   9   >