Re: How to test if a string is pointing into read-only memory?

2021-10-12 Thread ag0aep6g via Digitalmars-d-learn
On 12.10.21 10:19, jfondren wrote: ```d /+ Unfortunately, this isn't reliable.  We could make this work if string literals are put  in read-only memory and we test if s[] is pointing into  that.  /* Peek past end of s[], if it's 0, no conversion necessary.  * Note that the compiler will

Re: Is this a compiler aliasing bug?

2021-09-17 Thread ag0aep6g via Digitalmars-d-learn
On 17.09.21 11:44, Chris Katko wrote: bool is_colliding_with(drawable_object_t obj) //was a class member { [...] alias x2 = obj.x; alias y2 = obj.y; alias w2 = obj.w; alias h2 = obj.h; [...]     } Those aliases don't work like you want them to. You can't have

Re: Merge 2 structs together (into a single struct)?

2021-09-16 Thread ag0aep6g via Digitalmars-d-learn
On 16.09.21 22:53, jfondren wrote: string joinstruct(A, B)(string name) {     string s = "struct " ~ name ~ " {";     alias memA = __traits(allMembers, A);     alias memB = __traits(allMembers, B);     alias initA = A.init.tupleof;     alias initB = B.init.tupleof;     static foreach (i; 0

Re: Question on Immutability

2021-08-30 Thread ag0aep6g via Digitalmars-d-learn
On 31.08.21 02:50, Mike Parker wrote: Member functions marked as immutable can be called on both mutable and immutable instances. That's not true.

Re: compile time compression for associatve array literal

2021-08-23 Thread ag0aep6g via Digitalmars-d-learn
On 23.08.21 08:14, Brian Tiffin wrote: From ~~a~~ little reading, it seems associative array literal initialization is still pending for global scope, but allowed in a module constructor?  *If I understood the skimming surface reading so far*. ```d immutable string[string] things; static

Re: Union member positions?

2021-08-17 Thread ag0aep6g via Digitalmars-d-learn
On 17.08.21 15:46, z wrote: Is it possible to set a "position" on a union member? or is there is a language-integrated equivalent? For example, to get access to each byte in an unsigned integer while still supporting the original type. ```D ///a single uint that would be accessed as two

Re: Getting a working example of opIndexAssign using opSlice ... have troubles ...

2021-08-15 Thread ag0aep6g via Digitalmars-d-learn
On Sunday, 15 August 2021 at 20:41:51 UTC, james.p.leblanc wrote: I have been trying to get a working example of slice assignment operator overloading ... and am befuddled. From the spec (section 20.6.2), the code below appears: struct A { int opIndexAssign(int v); //

Re: compare types of functions.

2021-08-04 Thread ag0aep6g via Digitalmars-d-learn
On 02.08.21 22:14, vit wrote: Why this doesn't work: ```d template DestructorType(T){ alias Get(T) = T;     alias DestructorType = Get!(typeof((void*){     T tmp;     })); } struct Foo{     ~this()@safe{} } ``` ```d void main(){     //Error: static assert:  `is(void

Re: Creating immutable arrays in @safe code

2021-07-18 Thread ag0aep6g via Digitalmars-d-learn
On 17.07.21 15:56, ag0aep6g wrote: At a glance, the only meaningful use of `PURE.strong` seems to be in dcast.d, introduced by the PR you linked. Changing that to `PURE.const_` doesn't break any tests for me. So I'm inclined to believe that `PURE.strong` is nonsense, and that `PURE.const_`

Re: Creating immutable arrays in @safe code

2021-07-17 Thread ag0aep6g via Digitalmars-d-learn
On 17.07.21 14:56, Dennis wrote: On Saturday, 17 July 2021 at 12:05:44 UTC, ag0aep6g wrote: Hm, as far as I understand, "strongly pure" doesn't require `immutable` parameters. `const` should be enough. The spec says: "A strongly pure function has no parameters with mutable indirections" [1].

Re: Creating immutable arrays in @safe code

2021-07-17 Thread ag0aep6g via Digitalmars-d-learn
On 17.07.21 13:05, Dennis wrote: There used to be a complex `isReturnIsolated` check, but the [fix for issue 15660](https://github.com/dlang/dmd/pull/8048) reduced it to a check 'is the function strongly `pure`' which means 'parameters are values or immutable'. To reduce code breakage, the

Re: Creating immutable arrays in @safe code

2021-07-16 Thread ag0aep6g via Digitalmars-d-learn
On 17.07.21 00:27, H. S. Teoh wrote: Hmm, OK. Not sure why .array isn't being inferred as unique... but yeah, you probably have to resort to using @trusted with .assumeUnique. In addition to `pure`, you also need a const/immutable input and a mutable output, so that the output cannot be a

Re: mixin template's alias parameter ... ignored ?

2021-07-12 Thread ag0aep6g via Digitalmars-d-learn
On 13.07.21 03:03, someone wrote: On Monday, 12 July 2021 at 23:28:29 UTC, ag0aep6g wrote: [...] I'm not sure where we stand with `in` You mean *we* = D developers ? Yes. Let me rephrase and elaborate: I'm not sure what the current status of `in` is. It used to mean `const scope`. But

Re: mixin template's alias parameter ... ignored ?

2021-07-12 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 12 July 2021 at 22:35:27 UTC, someone wrote: On Monday, 12 July 2021 at 05:33:22 UTC, ag0aep6g wrote: [...] Teach me please: if I declare a variable right after the function declaration like this one ... ain't scope its default visibility ? I understand (not quite sure whether

Re: mixin template's alias parameter ... ignored ?

2021-07-11 Thread ag0aep6g via Digitalmars-d-learn
On 12.07.21 03:37, someone wrote: I ended up with the following (as usual advice/suggestions welcomed): [...]> alias stringUTF16 = dstring; /// same as immutable(dchar)[];> alias stringUTF32 = wstring; /// same as immutable(wchar)[]; Bug: You mixed up `wstring` and `dstring`. `wstring` is

Re: what is D's idiom of Python's list.extend(another_list)?

2021-06-21 Thread ag0aep6g via Digitalmars-d-learn
On 21.06.21 09:02, Mike Parker wrote: On Monday, 21 June 2021 at 06:16:15 UTC, mw wrote: Ha! great. I didn't know `~` works for both single elements and array! `~` by itself is the concatenation operator and only works with two array operands. `~=` is the append operator and can append

Re: @trusted methods

2021-06-18 Thread ag0aep6g via Digitalmars-d-learn
On 18.06.21 14:40, vit wrote: Are asserts enough to make method @trusted or is something like throw exception or return error code necessary? Asserts are a debugging feature. They're not suitable to ensure safety, because they're simply skipped in release mode. `assert(false);` is the

Re: In general, who should do more work: popFront or front?

2021-06-14 Thread ag0aep6g via Digitalmars-d-learn
On 15.06.21 07:17, mw wrote: https://dlang.org/library/std/range/primitives/front.html the 2nd decl: dchar front(T) (   scope const(T)[] a ) pure @property @safe if (isAutodecodableString!(T[])); you can see `const` but https://dlang.org/library/std/range/primitives/pop_front.html void

Re: cannot take address of scope local in safe function

2021-06-14 Thread ag0aep6g via Digitalmars-d-learn
On 13.06.21 19:49, vit wrote: Is possible create and use scope output range allocated on stack in @safe code? Example: ```d //-dip1000     struct OutputRange{     private bool valid = true;     private void* ptr;     int count = 0;     void put(Val)(auto ref scope Val

Re: cannot take address of scope local in safe function

2021-06-13 Thread ag0aep6g via Digitalmars-d-learn
On Sunday, 13 June 2021 at 16:27:18 UTC, vit wrote: Why I can take address of Foo variable but not Bar? ```d //-dip1000 struct Foo{ private double d; } struct Bar{ private void* ptr; } void main()@safe{ ///this is OK: { scope Foo x; scope ptr = }

Re: Cast class reference to pointer of another class?

2021-05-29 Thread ag0aep6g via Digitalmars-d-learn
On Saturday, 29 May 2021 at 21:01:14 UTC, JN wrote: this code compiles. Why? What is even the result in "f" in this case? On Saturday, 29 May 2021 at 21:03:12 UTC, JN wrote: fixed formatting: ```d struct Foo { } class Bar { } void main() { Bar b = new Bar(); Foo* f = cast(Foo*)b; }

Re: stack out of scope ?

2021-05-16 Thread ag0aep6g via Digitalmars-d-learn
On 16.05.21 19:24, Alain De Vos wrote: On Sunday, 16 May 2021 at 16:58:15 UTC, H. S. Teoh wrote: [...] Though I believe if you compile with -dip25 -dip1000 the compiler should emit an error for the above code.  If not, please file a bug against -dip1000. T I use ldc2. No dip flags here.

Re: ref struct member function

2021-05-14 Thread ag0aep6g via Digitalmars-d-learn
On 14.05.21 12:00, PinDPlugga wrote: Hi thank you both for your answers. I had understood from an earlier chapter how this could introduce a bug, but I was confused because the warning suggests attaching ```return``` to the parameter, which is empty in the declaration. `this` is considered a

Re: What's a good approach to DRY with the block code of a case-statement?

2021-04-26 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 26 April 2021 at 20:39:55 UTC, Jack wrote: I have a block of code that the only thing that change is the type passed in one of the template functions called so I'd like to make a DRY for this. But I'm not just replacing by a function due to control-flow, for example, there are

Re: Extern/scope issue

2021-04-03 Thread ag0aep6g via Digitalmars-d-learn
On 03.04.21 15:34, DLearner wrote: The following produces the expected result. However, changing extern(C) to extern(D) causes linker failures. To me, that is bizarre. Testmain: extern(C) int xvar; [...] Testmod: extern extern(C) int xvar; With `extern (C)`, those two `xvar`s refer to the

Re: Need for speed

2021-04-01 Thread ag0aep6g via Digitalmars-d-learn
On 01.04.21 21:53, Steven Schveighoffer wrote: Maybe, but I wasn't responding to that, just your statement not to recommend -boundscheck=off. In any case, it wouldn't hurt, right? Right.

Re: Need for speed

2021-04-01 Thread ag0aep6g via Digitalmars-d-learn
On 01.04.21 21:36, Steven Schveighoffer wrote: On 4/1/21 3:27 PM, ag0aep6g wrote: On 01.04.21 21:00, Berni44 wrote: ``` ldc2 -O3 -release -boundscheck=off -flto=full -defaultlib=phobos2-ldc-lto,druntime-ldc-lto speed.d ``` [...] Yes, but you can recommend `-boundscheck=safeonly`, which

Re: Need for speed

2021-04-01 Thread ag0aep6g via Digitalmars-d-learn
On 01.04.21 21:00, Berni44 wrote: ``` ldc2 -O3 -release -boundscheck=off -flto=full -defaultlib=phobos2-ldc-lto,druntime-ldc-lto speed.d ``` Please don't recommend `-boundscheck=off` to newbies. It's not just an optimization. It breaks @safe. If you want to do welding without eye

Re: Need for speed

2021-04-01 Thread ag0aep6g via Digitalmars-d-learn
On Thursday, 1 April 2021 at 16:52:17 UTC, Nestor wrote: I was hoping to beat my dear Python and get similar results to Go, but that is not the case neither using rdmd nor running the executable generated by dmd. I am getting values between 350-380 ms, and 81ms in Python. [...] ``` for

Re: Implicit conversion of unique chars[] to string

2021-03-23 Thread ag0aep6g via Digitalmars-d-learn
On 23.03.21 02:07, Steven Schveighoffer wrote: const(char)[] x = "foo"; string chained = chainPath(x, "bar").array; Error: cannot implicitly convert expression array(chainPath(x, "bar")) of type const(char)[] to string And the answer is complex. You can't accept a const range, because they

Re: Implicit conversion of unique chars[] to string

2021-03-22 Thread ag0aep6g via Digitalmars-d-learn
On 22.03.21 21:38, Per Nordlöw wrote: Am I the only one being annoyed by the fact that     chainPath(...).array doesn't implicit convert to string despite the array returned from .array is allocated by the GC. Works for me: import std.array: array; import std.path: chainPath; void

Re: question about ref keyword

2021-03-18 Thread ag0aep6g via Digitalmars-d-learn
On Thursday, 18 March 2021 at 16:59:24 UTC, Jack wrote: let's assume this class: class C { private S m_s; this() { m_s = S(30); } ref S value() { return m_s; } ref S value(ref S s) { return m_s = s;

Re: Compiler module import graph

2021-03-13 Thread ag0aep6g via Digitalmars-d-learn
On 13.03.21 15:20, frame wrote: Is there a tool to view module import graph? The compiler verbose output shows that the module is imported/parsed but not why. I wan't to avoid unnecessary imports to speed up compile times or avoid issues if the module contents are not fully compatible with

Re: unittest compiles w/o error though module file is not named after the module

2021-02-06 Thread ag0aep6g via Digitalmars-d-learn
On 06.02.21 16:05, kdevel wrote: On Saturday, 6 February 2021 at 14:52:57 UTC, Adam D. Ruppe wrote: [...] That one `import p;` is kinda weird, it should probably complain then you imported one thing and got another, but generally the name not matching is no problem at all. ```main.d

Re: Type of string literal concatenated with non-immutable char array

2021-02-01 Thread ag0aep6g via Digitalmars-d-learn
On 31.01.21 22:48, Per Nordlöw wrote: Why isn't     "Name " ~ name ~ " could not be found" implicitly convertible to `string`? If concatenation is guaranteed to allocate a new array, then it should be "strongly pure", and the conversion should work. I'm not sure if it is guaranteed to

Re: Renamed but non-selective import?

2021-01-12 Thread ag0aep6g via Digitalmars-d-learn
On 12.01.21 21:09, cc wrote: import core.sys.windows.windows; import mymodule; // contains a struct named MSG Error: `core.sys.windows.winuser.MSG` ... conflicts with `mymodule.MSG` vs import core.sys.windows.windows : winMSG = MSG; // this leaves out other symbols Error: undefined

Re: Easy way to accept X and immutable X in parameters without overloading?

2021-01-11 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 11 January 2021 at 18:51:04 UTC, Jack wrote: Here's what I'm trying to make to work: import std.container : SList; class C { static immutable Foo = new C(); // } alias Callback = void function(const C, int); void main() { auto l = SList!Callback(); auto a = (C

Re: Easy way to accept X and immutable X in parameters without overloading?

2021-01-11 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 11 January 2021 at 18:12:17 UTC, Jack wrote: thanks! now, how would I add const here? import std.container : SList; auto l = SList!Callabck(); doesn't work: auto l = SList!(const(Callabck())); auto l = SList!(const Callabck()); You said you want the callbacks to accept both

Re: Easy way to accept X and immutable X in parameters without overloading?

2021-01-11 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 11 January 2021 at 16:40:01 UTC, Jack wrote: let's say a I have this: void f(X foo) { } but I'd like to make f() accept immutable X too so instead of cast away everywhere in the code where immutable(X) is passed to f() or make a overload for this, are there any way to accept both

Re: Surprising behaviour of std.experimental.allocator

2020-12-26 Thread ag0aep6g via Digitalmars-d-learn
On 26.12.20 13:59, ag0aep6g wrote: Looks like a pretty nasty bug somewhere in std.experimental.allocator or (less likely) the GC. Further reduced code: [...] Apparently, something calls deallocateAll on a Mallocator instance after the memory of that instance has been recycled by

Re: Surprising behaviour of std.experimental.allocator

2020-12-26 Thread ag0aep6g via Digitalmars-d-learn
On 24.12.20 17:12, Saurabh Das wrote: This causes a segfault when run with rdmd -gx: [...] (Tested on DMD 2.094.2 and on https://run.dlang.io/is/p0FsOQ) If the "GC.collect()" line is commented out, it works somehow. Please help me understand why this is happening. This is a very reduced

Re: Why is (int[int] s = int[int].init) not allowed

2020-12-22 Thread ag0aep6g via Digitalmars-d-learn
On Tuesday, 22 December 2020 at 21:11:12 UTC, Andre Pany wrote: I am really confused, why is this valid: void sample(string[string] s = string[string].init){} while this causes syntax errors? void sample_invalid1(double[string] s = double[string].init){} void sample_invalid2(int[int] s =

Re: Trying to understand multidimensional arrays in D

2020-12-22 Thread ag0aep6g via Digitalmars-d-learn
On Tuesday, 22 December 2020 at 16:56:18 UTC, Ali Çehreli wrote: On 12/22/20 6:35 AM, ag0aep6g wrote: > Flip the pointer syntax, too: > > *Foo a; /* a pointer to a Foo */ I am not a language expert but I think that would make D's parsing complicated (like C++'s < token) because * already

Re: Trying to understand multidimensional arrays in D

2020-12-22 Thread ag0aep6g via Digitalmars-d-learn
On 22.12.20 15:15, Mike Parker wrote: On Tuesday, 22 December 2020 at 13:59:54 UTC, Rekel wrote: I am curious by the way, what do you think of the [][4]Row suggestion I gave? In a way you'd have your  & could eat it too, i think ^^ (Still a strange saying to me) Currently, D's variable

Re: Undefined reference error at linktime with unittests

2020-12-10 Thread ag0aep6g via Digitalmars-d-learn
On 10.12.20 13:28, z wrote: When compiling with unit tests(via «dub test», or adding «dflags "-unittest"»), i'm getting this error at link time : lld-link: error: undefined symbol: _D5packagename9subpackage9__mixin119type8toStringMFZAya The same occurs with OPTLINK. Curiously, looking at

Re: where is the memory corruption?

2020-12-09 Thread ag0aep6g via Digitalmars-d-learn
On 09.12.20 21:35, Jack wrote: I'm on linux/opensuse, trying to pass a wchar_* from C to D but I'm getting only the first letter of that string. Could someone help figure out why? this is the piece of D code: extern(C) export void sayHello(const (wchar) *s) [...] and below the piece of C

Re: Are JSONOptions broken?

2020-11-28 Thread ag0aep6g via Digitalmars-d-learn
On 28.11.20 15:21, frame wrote: This throws an UTF-exception: auto json = JSONValue(cast(char[])[0x00, 0x7D, 0xFE, 0xFF, 0x14, 0x32, 0x43, 0x10]); writeln(json.toString(JSONOptions.escapeNonAsciiChars)); Makes no sense. Either the bytes should be properly escaped or there should not be any

Re: implementing default opCmp

2020-11-18 Thread ag0aep6g via Digitalmars-d-learn
On Wednesday, 18 November 2020 at 22:29:17 UTC, Steven Schveighoffer wrote: How do I do something really simple for opCmp? I tried this it didn't work: return this == other ? 0 : this.tupleof < other.tupleof ? -1 : 1; std.typecons.Tuple has opCmp. So this works: int opCmp(S other)

Re: Packing of Struct Fields

2020-10-17 Thread ag0aep6g via Digitalmars-d-learn
On 17.10.20 14:35, Per Nordlöw wrote: struct S {     int i;     bool b; } struct T {     S s; // reinterpreting this as an array can only access this first element anyway     char c; // so why can't this be aligned directly after `s` without any padding? } c does come directly after

Re: Packing of Struct Fields

2020-10-16 Thread ag0aep6g via Digitalmars-d-learn
On 16.10.20 22:32, Per Nordlöw wrote: Why is `T.sizeof` 12 instead of 8 when `U.sizeof` is 8 in the following example? struct S {     int i;     bool b; } struct T {     S s;     char c; } struct U {     int i;     bool b;     char c; } ? S.sizeof: 4 bytes for the int + 1 byte for

Re: Escape this in pure members

2020-09-23 Thread ag0aep6g via Digitalmars-d-learn
On 23.09.20 02:06, DlangUser38 wrote: The following analysis might be wrong but I think that `scope` as a **member** function attribute is not supposed to be used as that is not even documented. It's documented here: https://dlang.org/spec/memory-safe-d.html#scope-return-params Quote:

Re: Call C variadic function from D variadic function

2020-09-13 Thread ag0aep6g via Digitalmars-d-learn
```     /// Add a single line to an existing header     auto addLine(T...)(RecordType type, T kvargs)     if(kvargs.length > 0 && isSomeString!(T[0]))     {     static assert (kvargs.length %2 == 0);   // K-V pairs => even number of variadic args     string varargMagic(size_t

Re: How do I copy struct having immutable pointer member when enabled DIP1000?

2020-08-30 Thread ag0aep6g via Digitalmars-d-learn
On 31.08.20 06:24, outlandkarasu wrote: I thought that I cannot make non-scope `ref` parameters from `scope` array references. But I found It allowed currently. [...] enum Currency : string {     USD = "USD", EUR = "EUR", GBP = "GBP", JPY = "JPY", } struct Instrument {     Currency bid;   

Re: How do I copy struct having immutable pointer member when enabled DIP1000?

2020-08-30 Thread ag0aep6g via Digitalmars-d-learn
On 30.08.20 17:24, outlandkarasu wrote: enum Tag { tag = "tag" } struct A { Tag tag; } A createA() @safe {     scope a = A(Tag.tag);     // Error: scope variable a may not be returned     return a;     // NG     // return A(a);     // return A(a.tag); } [...] I

Re: in; scope; scope ref; DIP1000; documentation

2020-08-27 Thread ag0aep6g via Digitalmars-d-learn
On 27.08.20 20:49, James Blachly wrote: 1. The thread involves 'in' qualifier. Documentation (https://dlang.org/spec/function.html#param-storage) indicates that `in` is defined as `scope const` and should not be used as it is not implemented. **Is this [fact and recommendation] still true?**

Re: String mixin from within a template

2020-08-23 Thread ag0aep6g via Digitalmars-d-learn
On Sunday, 23 August 2020 at 20:54:22 UTC, data pulverizer wrote: compiled string 1: alias x0 = Remove(indicies[i] - i, Args); Error: found - when expecting ) Error: semicolon expected to close alias declaration Error: no identifier for declarator i Error: declaration expected, not , ...

Re: String mixin from within a template

2020-08-23 Thread ag0aep6g via Digitalmars-d-learn
On Sunday, 23 August 2020 at 19:42:47 UTC, data pulverizer wrote: `alias x` ~ i ~ ` = Remove(indexes[i] - i, x`~ (i - 1) ~ `);` [...] ```d Error: no identifier for declarator x ``` Indicating that the concatenation `~` is not working, and I only get the partial string when I print out

Re: Template constraint on alias template parameter.

2020-08-06 Thread ag0aep6g via Digitalmars-d-learn
On Thursday, 6 August 2020 at 16:01:35 UTC, jmh530 wrote: The code below compiles, but I want to put an additional constraint on the `test` function is only called with a Foo struct. I tried things like is(T == Foo) and is(T : Foo), but those don't work. However, something like is(T!int :

Re: dynamic array .length vs .reserve - what's the difference?

2020-07-31 Thread ag0aep6g via Digitalmars-d-learn
On 31.07.20 01:42, wjoe wrote: I could swear just a few weeks ago there was someone asking how to tell if an array was null or of length 0 and an answer was that it's the same and can't be distinguished so I assumed that assigning a slice of 0 length is the same as setting the array to null

Re: constructing labels for static foreach inside switch inside foreach

2020-07-08 Thread ag0aep6g via Digitalmars-d-learn
On 08.07.20 14:24, Steven Schveighoffer wrote: I solved it for now by extrapolating the inner code into a local template function. But this is definitely an awkward situation for static foreach. FWIW, you can write the extra function like this: static foreach (T; Types) () {

Re: [DIP1000] Something I don't quite understand regarding 'scope'

2020-06-29 Thread ag0aep6g via Digitalmars-d-learn
On 29.06.20 02:28, Stanislav Blinov wrote: void local(Args...)(Args args) { } void main() @safe {     import std.stdio;     scope int* p;     local(p);   // Ok     writeln(p); // Error: scope variable p assigned to non-scope parameter _param_0 calling std.stdio.writeln!(int*).writeln }

Re: isInputRange not satisfied even if all individual conditions are satisfied

2020-06-26 Thread ag0aep6g via Digitalmars-d-learn
On 26.06.20 15:35, ag0aep6g wrote: `isInputRange!R` fails because it has no knowledge of your free `empty` function. Without `empty`, `R` is obviously not a range. To be clear: It's the same with `front` and `popFront`. You can't implement any range primitives as free functions. It only

Re: isInputRange not satisfied even if all individual conditions are satisfied

2020-06-26 Thread ag0aep6g via Digitalmars-d-learn
On 26.06.20 15:09, Johannes Loher wrote: import std.meta : allSatisfy, staticMap; import std.traits : allSameType; import std.range : isInputRange, ElementType, empty; [...] @property bool empty(TypeArgs...)(auto ref scope SumType!(TypeArgs) r) if (allSatisfy!(isInputRange, TypeArgs)

Re: Metaprogramming with D

2020-06-09 Thread ag0aep6g via Digitalmars-d-learn
On 09.06.20 20:16, Ali Çehreli wrote: I am biased but I like my :) index of the book, where all such syntax items appear:   https://ddili.org/ders/d.en/ix.html Oh yeah. It's how I got the link. You might want to fix some of the URLs, though. Characters like '{' or '[' need to be

Re: Metaprogramming with D

2020-06-08 Thread ag0aep6g via Digitalmars-d-learn
On 08.06.20 16:45, ag0aep6g wrote: On 08.06.20 16:41, Jan Hönig wrote: On Sunday, 7 June 2020 at 00:45:37 UTC, Ali Çehreli wrote: [...]   writeln(q{   void foo() {   }     }); What is the name of this `q` thing? How do i find it? Are there any recent tutorials on it?

Re: Metaprogramming with D

2020-06-08 Thread ag0aep6g via Digitalmars-d-learn
On 08.06.20 16:41, Jan Hönig wrote: On Sunday, 7 June 2020 at 00:45:37 UTC, Ali Çehreli wrote: [...]   writeln(q{   void foo() {   }     }); What is the name of this `q` thing? How do i find it? Are there any recent tutorials on it? https://dlang.org/spec/lex.html#token_strings

Re: Mixin and imports

2020-06-08 Thread ag0aep6g via Digitalmars-d-learn
On 08.06.20 16:27, data pulverizer wrote: Out of curiosity what does the "." in front of `foo` mean? I've seen that in some D code on the compiler in GitHub and have no idea what it does. I tried Googling it to no avail. It doesn't have anything to do with UFCS does it?

Re: Use classes as keys in associative array

2020-06-06 Thread ag0aep6g via Digitalmars-d-learn
On Saturday, 6 June 2020 at 16:49:29 UTC, JN wrote: Is it possible to use different class instances as keys for associative array, but compare them by the contents? I tried to override opEquals but it doesn't seem to work. You also need toHash.

Re: Determining @trusted-status

2020-05-29 Thread ag0aep6g via Digitalmars-d-learn
On 29.05.20 08:28, JN wrote: Alternatively you could just use @trusted blocks. Unsafe blocks are a common practice in languages like C# or Rust when it comes to calling unsafe code. @safe isn't about 100% bulletproof safety. @safe is (should be) about not having memory related errors outside

Re: Determining @trusted-status

2020-05-29 Thread ag0aep6g via Digitalmars-d-learn
On 29.05.20 02:09, Clarice wrote: It seems that @safe will be de jure, whether by the current state of DIP1028 or otherwise. However, I'm unsure how to responsibly determine whether a FFI may be @trusted: the type signature and the body. Should I run, for example, a C library through valgrind

Re: How to get the pointer of "this" ?

2020-05-26 Thread ag0aep6g via Digitalmars-d-learn
On 26.05.20 15:43, Vinod K Chandran wrote: So far now, two solutions are very clear for this problem. 1. As per John Chapman's suggestion - use cast(DWORD_PTR)cast(void*)this). 2. Use another varibale to use as an lvalue. - Button dummyBtn = this; cast(DWORD_PTR) Among these two, i

Re: Variable "i" can not be read at compile time

2020-05-24 Thread ag0aep6g via Digitalmars-d-learn
On 24.05.20 19:13, data pulverizer wrote: Thank you very much. I though that if I used a `static foreach` loop D would attempt to run the calculation `bench()` at compile time rather than at run time but it doesn't which is good. So `static foreach` allows you to index at compile time and if

Re: Variable "i" can not be read at compile time

2020-05-24 Thread ag0aep6g via Digitalmars-d-learn
On 24.05.20 18:34, data pulverizer wrote: I'm getting the error: ``` Error: variable i cannot be read at compile time Error: template instance script.runKernelBenchmarks!(Tuple!(DotProduct!float, Gaussian!float, Polynomial!float, Exponential!float, Log!float, Cauchy!float, Power!float,

Re: Distinguish between a null array and an empty array

2020-05-24 Thread ag0aep6g via Digitalmars-d-learn
On 24.05.20 14:29, bauss wrote: Dang, that sucks there is no proper way and I would say that's a big flaw of D. Because what I need it for is for some data serialization but if the value is an empty array then it should be present and if it's null then it should not be present. Since null

Re: Distinguish between a null array and an empty array

2020-05-24 Thread ag0aep6g via Digitalmars-d-learn
On 24.05.20 14:12, bauss wrote: Is there a way to do that? Since the following are both true: int[] a = null; int[] b = []; assert(a is null); assert(!a.length); assert(b is null); assert(!b.length); What I would like is to tell that b is an empty array and a is a null array. No way.

Re: Why does indexing a string inside of a recursive call yield a different result?

2020-05-10 Thread ag0aep6g via Digitalmars-d-learn
On 10.05.20 12:02, Adnan wrote: ulong editDistance(const string a, const string b) {     if (a.length == 0)     return b.length;     if (b.length == 0)     return a.length;     const auto delt = a[$ - 1] == b[$ - 1] ? 0 : 1;     import std.algorithm : min;     return min(  

Re: CTFE and Static If Question

2020-05-07 Thread ag0aep6g via Digitalmars-d-learn
On 07.05.20 17:00, jmh530 wrote: Does foo!y0(rt) generate the same code as foo(rt, y0)? How is the code generated by foo(rt, x0) different from foo(rt,y0)? auto foo(bool rtct)(int rt) {     static if (rtct)     return rt + 1;     else     return rt; } auto foo(int rt, bool rtct) {

Re: Thread to watch keyboard during main's infinite loop

2020-05-06 Thread ag0aep6g via Digitalmars-d-learn
On 07.05.20 02:13, Daren Scot Wilson wrote: import std.stdio; import core.stdc.stdio;  // for getchar().  There's nothing similar in D std libs? import std.concurrency; import core.thread; // just for sleep() Instead of the comment you can write: import core.thread: sleep; bool

Re: Help, what is the code mean?

2020-04-28 Thread ag0aep6g via Digitalmars-d-learn
On Tuesday, 28 April 2020 at 20:48:57 UTC, Net wrote: () { ... } (); Is there a name of this kind of function in D? unnamed? anonymous? The spec uses "anonymous". Syntactically, `() { ... }` is a function literal. There is a section called "Anonymous Functions and Anonymous Delegates"

Re: Flatten a range of static arrays

2020-02-08 Thread ag0aep6g via Digitalmars-d-learn
On 08.02.20 07:27, ag0aep6g wrote: On 08.02.20 02:38, ag0aep6g wrote: Simplified, we're looking at this: struct Joiner { int[3] _items; int[] _current; } void main() @safe { Joiner j; j._current = j._items[]; } [...] In the first reduction, `j` might be `scope`,

Re: Flatten a range of static arrays

2020-02-08 Thread ag0aep6g via Digitalmars-d-learn
On 08.02.20 15:57, Steven Schveighoffer wrote: This kind of stuff is so difficult to reason about and develop as a library that people will just end up removing dip1000 from their compilation. I 100% agree that DIP 1000 is hard to reason about. It's pretty limited by design, and the

Re: Flatten a range of static arrays

2020-02-07 Thread ag0aep6g via Digitalmars-d-learn
On 08.02.20 02:38, ag0aep6g wrote: Simplified, we're looking at this: struct Joiner {     int[3] _items;     int[] _current; } void main() @safe {     Joiner j;     j._current = j._items[]; } I.e., a self-referential struct. Or most fundamentally: struct Joiner {    

Re: Flatten a range of static arrays

2020-02-07 Thread ag0aep6g via Digitalmars-d-learn
On 08.02.20 01:17, Steven Schveighoffer wrote: The original code is not invalid though. f is not valid, and that is a bug, but the original code posted by nullptr should be fine by memory safety standards. Maybe. But then it should also work with `int[3] front;`. If there is no possible way

Re: Flatten a range of static arrays

2020-02-07 Thread ag0aep6g via Digitalmars-d-learn
On 08.02.20 00:10, nullptr wrote: ``` import std; struct SomeRange {     int[3] val;     enum empty = false;     auto popFront() @safe {}     ref auto front() @safe     {     return val;     } } void main() @safe {     SomeRange().take(10).map!((return ref x) =>

Re: Cannot take slice of scope static array in @safe code

2020-02-02 Thread ag0aep6g via Digitalmars-d-learn
On 02.02.20 19:49, ag0aep6g wrote: On 02.02.20 19:18, Steven Schveighoffer wrote: I'm not sure if I got it right, but like this?     int*[][] g1;     int*[] g2;     int* g3;     void main() @safe     {     /* An array stored on the stack, of references to heap data: */    

Re: Cannot take slice of scope static array in @safe code

2020-02-02 Thread ag0aep6g via Digitalmars-d-learn
On 02.02.20 19:18, Steven Schveighoffer wrote: On 2/2/20 10:20 AM, ag0aep6g wrote: [...] void main() @safe { int* a0; scope int** b0 = /* accepted */ scope int* a2; scope int** b2 = /* rejected */ } Now it's important to realize that

Re: Cannot take slice of scope static array in @safe code

2020-02-02 Thread ag0aep6g via Digitalmars-d-learn
On 02.02.20 14:40, Dennis wrote: Compiling the following with -dip1000 gives an error. ``` void main() @safe {     string[1] a0;     scope int[1] a1;     scope string[1] a2;     scope string[] b0 = a0[]; // Fine     scope int[] b1 = a1[]; // Fine     scope string[] b2 = a2[]; // Error:

Re: template instantiation problems

2020-01-10 Thread ag0aep6g via Digitalmars-d-learn
On 10.01.20 18:27, berni44 wrote: This clearly shows, that packageName is only instatiated once at top level although mod0 and mod1 are two different things (at least their stringof produces different results) and therefore should be instantiated twice. Now I don't know if this is a bug in

Re: Difference between slice[] and slice

2019-09-25 Thread ag0aep6g via Digitalmars-d-learn
On 25.09.19 22:36, WhatMeWorry wrote: On Wednesday, 25 September 2019 at 19:25:06 UTC, Ali Çehreli wrote: On 09/25/2019 12:06 PM, WhatMeWorry wrote: [...] > In short, is there anytime that one would want to use "slice[] = > something" syntax?I That changes element values. Ok.  But which

Re: Looking for a Simple Doubly Linked List Implementation

2019-09-21 Thread ag0aep6g via Digitalmars-d-learn
On 21.09.19 10:34, Ron Tarrant wrote: Here's a question for the room: Does a doubly-linked list always have to be done with structs? Can it be classes instead? (Maybe that's why I can't get it to work, because I've been trying to make an OOP version?) It can be done with classes. When I

Re: segmentation fault when running void main() {}

2019-09-18 Thread ag0aep6g via Digitalmars-d-learn
On 17.09.19 20:03, berni wrote: I'm trying to install D on my old 32-bit machine (debian stable). First I tried to install a precompiled version and now I followed [1]. In both cases, I always get a segmentation fault when I try to run a compiled program. You're probably hitting this issue:

Re: getting rid of immutable (or const)

2019-09-05 Thread ag0aep6g via Digitalmars-d-learn
On 05.09.19 21:51, berni wrote: void main() {     int[int] a;     immutable int b = 17;     a[1] = b;  // <-- expecting error here     const oldPointer = (1 in a);     immutable int c = 10;     a[1] = c;     assert(oldPointer is (1 in a));     Point[int] d;    

Re: Why is sformat and formattedWrite (appender) allocating GC mem here?

2019-09-03 Thread ag0aep6g via Digitalmars-d-learn
On 03.09.19 16:03, James Blachly wrote: For my own learning, why was a unittest to ensure no GC added to sformat instead of a @nogc annotation? `sformat` uses the GC less now, but it's not @nogc. It can still throw GC-allocated exceptions, e.g. when the arguments don't match the format

Re: Why is sformat and formattedWrite (appender) allocating GC mem here?

2019-08-31 Thread ag0aep6g via Digitalmars-d-learn
On 31.08.19 14:07, cc wrote: I'm guessing it's allocating a string first to write the contents of the array and then inserting that string into the buffer I supplied. Is there no way to have it skip this step and just write each element (plus the joining punctuation, etc) directly into the

Re: += on associative arrays leads to surprising result

2019-08-27 Thread ag0aep6g via Digitalmars-d-learn
On 27.08.19 18:12, berni wrote: import std.stdio; void main() {     real[int] a;     a[0] += 100;     writeln(a); } results (independed of the used compiler) in [0:100] I was a little bit surprised, because a[0] += 100 should be the same as a[0] = a[0]+100, which leads to a range

Re: Template specialized functions creating runtime instructions?

2019-08-20 Thread ag0aep6g via Digitalmars-d-learn
On 21.08.19 01:48, ads wrote: This piece of code creates a fizzbuzz string with template parameters. auto fizzbuzz(uint N)() { string accumulate; return fizzbuzz!N(accumulate); } auto fizzbuzz(uint N)(ref string result) if (N % 3 && N % 5) { import std.conv : to; result ~=

Re: filtering a row of a jagged array

2019-08-11 Thread ag0aep6g via Digitalmars-d-learn
On 11.08.19 18:11, DanielG wrote: auto x = whatever[2].filter(x => x > 7); // error You just forgot an exclamation mark here. auto x = whatever[2].filter!(x => x > 7); // works

Re: Private variables accessible from outside class

2019-08-08 Thread ag0aep6g via Digitalmars-d-learn
On 08.08.19 18:03, Drobet wrote: Then why does it in the tour say that it can only be "seen by Integer"? https://tour.dlang.org/tour/en/basics/classes That's an error in the tour.

Re: Why does choose not work here

2019-08-01 Thread ag0aep6g via Digitalmars-d-learn
On 01.08.19 22:23, Matt wrote: Version 4 does not work when PairedA.previous is null. I'd love to understand why. [...] auto myFilter(R1, R2)(R1 a, R2 b) { import std.algorithm : filter, canFind; return a.filter!(c => b.canFind(c)); } struct A { uint[] starts, stops;

Re: Why in Phobos is empty() sometimes const and sometimes not

2019-07-29 Thread ag0aep6g via Digitalmars-d-learn
On 29.07.19 21:35, H. S. Teoh wrote: Generally, the idiom is to let the compiler do attribute inference by templatizing your code and not writing any explicit attributes, then use unittests to ensure that instantiations of the range that ought to have certain attributes actually have those

  1   2   3   4   5   6   7   >