Traits: getting all *public* members of a class?

2013-06-08 Thread Q
Hi there, I want to iterate over all the public members of a class, but I've hit a stumbling block when the class resides in a different module and also contains private members. For example: $ cat foo.d class Foo { private int x; public int y; } $ cat main.d import foo; void main() {

Re: Traits: getting all *public* members of a class?

2013-06-08 Thread Q
On Saturday, 8 June 2013 at 14:06:32 UTC, Andrej Mitrovic wrote: On Saturday, 8 June 2013 at 13:57:13 UTC, Q wrote: Hi there, I want to iterate over all the public members of a class. It is a known problem. The workaround is to use is(typeof()): void main() { foreach (mem; __traits

std.conv question.

2009-07-15 Thread Q
Does to! (uint) recognize hexadecimal strings? Cause if i put int by = to!(int)("0x0FFF"); throws an exception saying; std.conv.ConvError: Can't convert value `x0FFF' of type const(char)[] to type int dmd 2.031;

std.conv question.

2009-07-15 Thread Q
Does to! (uint) recognize hexadecimal strings? Cause if i put int by = to!(int)("0x0FFF"); throws an exception saying; std.conv.ConvError: Can't convert value `x0FFF' of type const(char)[] to type int dmd 2.031;

std.conv question.

2009-07-15 Thread Q
Does to! (uint) recognize hexadecimal strings? Cause if i put int by = to!(int)("0x0FFF"); throws an exception saying; std.conv.ConvError: Can't convert value `x0FFF' of type const(char)[] to type int dmd 2.031;

class destruction

2015-09-09 Thread Q via Digitalmars-d-learn
Hi. I'm playing around with D for a while and I would like to switch. But here is one thing, I need an answer for. In the Docs is mentioned that it is not sure that the DTor of a class is called. But what if I have struct, which holds a C Handle which is destroyed as soon as the struct gets des

Re: class destruction

2015-09-09 Thread Q via Digitalmars-d-learn
But if others use my code they must think about finalizing the classes? That is very awkward. I'll take a look at Rust and otherwise I will stick with C++. Thanks for you answer. :)

Re: class destruction

2015-09-09 Thread Q via Digitalmars-d-learn
On Wednesday, 9 September 2015 at 14:57:26 UTC, Adam D. Ruppe wrote: On Wednesday, 9 September 2015 at 07:19:58 UTC, Q wrote: Can I be sure that the Handle is destroyed as soon as the class is destroyed? It will do that automatically. Like the others said, you won't be sure when the cla

Re: class destruction

2015-09-09 Thread Q via Digitalmars-d-learn
And sorry if that sounds rude, I'm just in a hurry. I just think D is not mature enough for serious stuff. :) That is of course only my personal opinion.

Re: class destruction

2015-09-09 Thread Q via Digitalmars-d-learn
On Wednesday, 9 September 2015 at 15:19:04 UTC, Adam D. Ruppe wrote: On Wednesday, 9 September 2015 at 15:10:33 UTC, Q wrote: But since D has a GC and (per default) force to heap allocate a class. So IMO the GC should also destroy it, everything else is just awkward. Well, it *does* by

Re: class destruction

2015-09-09 Thread Q via Digitalmars-d-learn
On Wednesday, 9 September 2015 at 15:32:58 UTC, Adam D. Ruppe wrote: On Wednesday, 9 September 2015 at 15:24:57 UTC, Q wrote: I thought that is not guaranteed, according to the docs? It is possible that the GC will never actually run, but you can force it to if you need it to by calling

Overload Resulution Template with 2 Functions vs. 2 Templates difference

2018-05-06 Thread Q. Schroll via Digitalmars-d-learn
Why is there a difference between struct S { template func(T) { enum impl = "whatever string"; auto func(T arg) { mixin(impl); } // eponymous template auto func(T arg) const { mixin(impl); } // eponymous template } } and struct R { private enum impl = "whatever string"

Re: Purity of delegate-style toString

2018-05-06 Thread Q. Schroll via Digitalmars-d-learn
On Tuesday, 1 May 2018 at 12:03:15 UTC, ag0aep6g wrote: On 05/01/2018 01:44 PM, Per Nordlöw wrote: In which cases (if any) is it possible to make a delegate-style implementation of toString such as     void toString(scope void delegate(const(char)[]) sink) const @trusted pure     {   

Re: Why The D Style constants are written in camelCase?

2018-05-13 Thread Q. Schroll via Digitalmars-d-learn
On Wednesday, 9 May 2018 at 09:38:14 UTC, BoQsc wrote: The D Style suggest to camelCase constants, while Java naming conventions always promoted uppercase letter. Is there an explanation why D Style chose to use camelCase instead of all UPPERCASE for constants, was there any technical problem

Re: What's the purpose of the 'in' keyword ?

2018-05-30 Thread Q. Schroll via Digitalmars-d-learn
On Sunday, 27 May 2018 at 16:00:15 UTC, Jonathan M Davis wrote: [...] Honestly, I'd suggest that folks never use in at this point. There's zero benefit to it. [...] Exactly. If you intend const, just write const. If you intend const scope, write const scope.

Move and CTFE

2018-05-30 Thread Q. Schroll via Digitalmars-d-learn
It seems one cannot std.algorithm.mutation.move objects explicitly. Say I have a non-copyable type struct NoCopy { int payload; // some payload pure nothrow @nogc @safe @disable: this(this); // make it non copyable } that is being used in a compile-time function

Re: Move and CTFE

2018-05-30 Thread Q. Schroll via Digitalmars-d-learn
On Wednesday, 30 May 2018 at 21:02:07 UTC, Jonathan M Davis wrote: On Wednesday, May 30, 2018 20:42:38 Q. Schroll via Digitalmars-d-learn wrote: It seems one cannot std.algorithm.mutation.move objects explicitly. Say I have a non-copyable type [...] It fails because move() cannot be executed

Re: Can you move a disabled this(this) struct in to a container type if it's an rvalue?

2018-12-15 Thread Q. Schroll via Digitalmars-d-learn
On Thursday, 13 December 2018 at 23:33:39 UTC, Stanislav Blinov wrote: On Thursday, 13 December 2018 at 13:17:05 UTC, aliak wrote: Ah. Is there any case where you would not want to do that when you have a T value as parameter? Hypothetically, yes, e.g. an object that contains references to i

Re: Am I misusing with?

2019-01-19 Thread Q. Schroll via Digitalmars-d-learn
On Saturday, 19 January 2019 at 20:38:00 UTC, faissaloo wrote: On Saturday, 19 January 2019 at 20:07:34 UTC, Rubn wrote: On Saturday, 19 January 2019 at 17:49:31 UTC, faissaloo wrote: [...] If you look at the implementation, "lines" is a struct. https://github.com/dlang/phobos/blob/v2.084.0/

Syntax for Pointer to Class

2019-01-25 Thread Q. Schroll via Digitalmars-d-learn
Say I have a class C and I want a pointer to a C handle. I tried the following pieces of syntax: C* obj = new C(); // gives me a C C* obj = new C*(); // gives me a C** C* obj = C*(); // refuses to compile Is it even possible? This sounds so newbie... I know it's fairly simple with structs

Re: Syntax for Pointer to Class

2019-01-25 Thread Q. Schroll via Digitalmars-d-learn
On Friday, 25 January 2019 at 20:31:29 UTC, H. S. Teoh wrote: On Fri, Jan 25, 2019 at 08:12:33PM +, Q. Schroll via Digitalmars-d-learn wrote: Say I have a class C and I want a pointer to a C handle. Note that taking the address of `C` will actually give you a pointer to the reference

Re: My template tuple code does not compile

2019-02-26 Thread Q. Schroll via Digitalmars-d-learn
On Tuesday, 26 February 2019 at 21:43:31 UTC, Victor Porton wrote: Compilation of unittest at the bottom of this file fails with an error. What is my error? I cannot tell you, why exactly you get these error messages. I can explain you the probable cause of the errors. I have not tested anyth

Re: My template tuple code does not compile

2019-02-26 Thread Q. Schroll via Digitalmars-d-learn
On Tuesday, 26 February 2019 at 22:56:37 UTC, Victor Porton wrote: On Tuesday, 26 February 2019 at 22:51:15 UTC, Q. Schroll wrote: Grouping arguments could be done, but from my experience, it does not buy you anything; rather it makes it worse. Better just deal with heterogeneous stuff just

Why is my @pure function @system when placed in a struct?

2019-02-27 Thread Q. Schroll via Digitalmars-d-learn
I have a template function `fImpl` I whish to instantiate manually using the new name `f`. Reason is simple: `f` should not be a template, but overloading it makes it easier that way. Nothing's more simple in D: int fImpl(T)(T value) { return cast(int) value; } alias f = fImpl!int;

Re: My template tuple code does not compile

2019-02-27 Thread Q. Schroll via Digitalmars-d-learn
On Wednesday, 27 February 2019 at 03:53:35 UTC, Victor Porton wrote: After following your suggestion to rewrite it with Stride it does not work either. I assume the error is somehow related to allSatisfy!. https://github.com/vporton/struct-params-dlang/blob/c1adc86672f46fd6b743903cc270dceef120

How to attach function attributes to delegate type?

2019-02-27 Thread Q. Schroll via Digitalmars-d-learn
For any type constructors like const, I can use ConstOf!T to get `T` with const attached. For a delegate/function type DG, e.g. int delegate(int), how can I get the @safe version of that type, i.e. int delegate(int) @safe? I tried alias SafeOf(DG) = DG @safe; but it didn't compile. The

Re: Access outer member of struct from inner struct

2019-04-02 Thread Q. Schroll via Digitalmars-d-learn
On Tuesday, 2 April 2019 at 18:20:09 UTC, Andrey wrote: Hello, In this example how can I access the members "read" and "q" of struct Outer from Inner struct? struct Outer { ulong q = 1; Inner inner; void read(ulong value) { q += value;

Re: Access outer member of struct from inner struct

2019-04-02 Thread Q. Schroll via Digitalmars-d-learn
On Tuesday, 2 April 2019 at 18:52:07 UTC, Jacob Carlborg wrote: On 2019-04-02 20:44, Q. Schroll wrote: After removing the calls to writeln, the error I get is: `this` for `read` needs to be type `Outer` not type `Inner` You cannot access stuff in Outer because Inner objects are not outer

pure opApply

2016-03-31 Thread Q. Schroll via Digitalmars-d-learn
Simple as that, shouldn't this work? struct X { int opApply(int delegate(string) dg) { return dg("impure"); } int opApply(int delegate(string) pure dg) pure { return dg("pure"); } } void main() { X x; string result; x.opApply( (string

Re: foreach UFCS

2016-03-31 Thread Q. Schroll via Digitalmars-d-learn
On Thursday, 31 March 2016 at 13:39:25 UTC, ixid wrote: What is going on with UFCS and foreach? foreach(i;0..5).writeln; This is not UFCS; it is calling writeln on module scope. See http://dlang.org/spec/module.html#module_scope_operators Your code is semantically identical with foreach (i

Re: pure opApply

2016-03-31 Thread Q. Schroll via Digitalmars-d-learn
On Thursday, 31 March 2016 at 13:51:00 UTC, Steven Schveighoffer wrote: I think it's a bug. foreach and opApply are specially coded in the compiler I think, so there's bound to be inconsistencies between them and normal overload rules. -Steve I have never filed a bug report. Should this be r

Formatted Output: Exact number of Decimal Places

2016-05-16 Thread Q. Schroll via Digitalmars-d-learn
Lets say I want to print a table with floats. How can it be formatted like that: | 2.4 | | 12.2 | | 8.131 | | 17.44 | Also acceptable is | 2.400 | | 12.200 | | 8.131 | | 17.440 | but not | 02.4 | | 12.2 | | 08.131 | | 17.44 | or any other solutions with leading zeros.

Re: Associative array of const items

2016-06-30 Thread Q. Schroll via Digitalmars-d-learn
On Thursday, 30 June 2016 at 17:08:45 UTC, Jonathan Marler wrote: Is there a way to have an associative array of const values? I thought it would have been: const(T)[K] map; map[x] = y; but the second line gives Error: cannot modify const expression. I would think that the const(T)[K] would

Use dup on Containers with const Elements

2016-07-29 Thread Q. Schroll via Digitalmars-d-learn
Cases to consider: Arrays and AAs with const(T) Elements, where T is a value or a reference type respectively. class C { } struct S { } const(S)[] varr; const(C)[] carr; const(S)[S] vaav; const(C)[S] caav; const(S)[C] vaac; const(C)[C] caac; pragma(msg, ty

Re: Use dup on Containers with const Elements

2016-07-30 Thread Q. Schroll via Digitalmars-d-learn
On Friday, 29 July 2016 at 19:24:59 UTC, Steven Schveighoffer wrote: On 7/29/16 3:00 PM, Q. Schroll wrote: Cases to consider: Arrays and AAs with const(T) Elements, where T is a value or a reference type respectively. [snip] Questions: (1) Why do I have to specify the type here? Why does

opAssign return type

2016-09-02 Thread Q. Schroll via Digitalmars-d-learn
When overloading assignment, I've been taught in my C++ course to return a reference to the lvalue being assigned to. This is easily possible in D, but in Phobos it is used rarely and in Ali Çehreli's Book it is neither taught to do so. Is there any reason to it? To me it seems very obvious to

Re: opAssign return type

2016-09-02 Thread Q. Schroll via Digitalmars-d-learn
On Friday, 2 September 2016 at 17:33:22 UTC, Steven Schveighoffer wrote: On 9/2/16 1:18 PM, Q. Schroll wrote: When overloading assignment, I've been taught in my C++ course to return a reference to the lvalue being assigned to. This is easily possible in D, but in Phobos it is used rarel

inferred vs. annotated attributes

2016-09-10 Thread Q. Schroll via Digitalmars-d-learn
Is there a difference between inferred and annotated attributes? Example: struct X(T) { this(S)(in S[] arr) // inferred pure { } } void main() pure { X!uint mut = [ 1, 2 ]; // proves inference (cf. main is pure) // immutable X!uint imm1 = [ 1

Re: inferred vs. annotated attributes

2016-09-10 Thread Q. Schroll via Digitalmars-d-learn
On Saturday, 10 September 2016 at 09:38:15 UTC, Lodovico Giaretta wrote: On Saturday, 10 September 2016 at 08:23:35 UTC, Q. Schroll wrote: Is there a difference between inferred and annotated attributes? Example: struct X(T) { this(S)(in S[] arr) // inferred pure

std.algorithm.iteration.each requires opApply to have ref elements?

2016-09-15 Thread Q. Schroll via Digitalmars-d-learn
Why does it do that? And seemingly it does not require it for opApply with more than two arguments.

Using opApply and Attributes

2016-11-20 Thread Q. Schroll via Digitalmars-d-learn
When using functions with delegate (or function ptr) parameters which should comply with some attributes, I cannot call the delegate in the function what makes it pretty useless (I haven't done research, but I claim that generally most functions taking delegate parameters call them). void f

Why don't Function Lambdas Cast to Delegate?

2016-11-21 Thread Q. Schroll via Digitalmars-d-learn
Why don't lambdas cast to a delegate if they are of type R function(Args)? I don't see any reason to that; a lambda should be a delegate type by default, and a function only as a special guarantee/optimization. It just makes them cumbersome to use with toDelegate. Probably there is a good reaso

How to compile against GitHub fork of Phobos?

2017-03-06 Thread Q. Schroll via Digitalmars-d-learn
I have a fork of the standard-library in the folder "phobos". In version 2.072.1 of the compiler, I could use code like void main() { import phobos.std.format; /* code for checking my changes to format */ } compiled with $ dmd -I"phobos" test_format.d in the pare

Re: How to compile against GitHub fork of Phobos?

2017-03-06 Thread Q. Schroll via Digitalmars-d-learn
On Tuesday, 7 March 2017 at 01:45:48 UTC, Adam D. Ruppe wrote: You pass your modified file to the compiler: dmd yourfile.d format.d The format.d there can be a copy of the one from phobos (or a fork or whatever) and since you passed it explicitly on the command line, it takes precedence over

Re: How to compile against GitHub fork of Phobos?

2017-03-06 Thread Q. Schroll via Digitalmars-d-learn
On Tuesday, 7 March 2017 at 02:30:21 UTC, ketmar wrote: Q. Schroll wrote: On Tuesday, 7 March 2017 at 01:45:48 UTC, Adam D. Ruppe wrote: You pass your modified file to the compiler: dmd yourfile.d format.d The format.d there can be a copy of the one from phobos (or a fork or whatever) and

Function Template Overloading

2017-03-14 Thread Q. Schroll via Digitalmars-d-learn
void test(T)(T* arg); void test(T)(ref T arg); Let p be any pointer. Why is test(p) not an ambiguity error? Why is the second overload chosen? Making the first one take auto ref T* lets the compiler choose the first. Making the second one non-ref lets the compiler give me an ambiguity error.

Re: Function Template Overloading

2017-03-14 Thread Q. Schroll via Digitalmars-d-learn
On Wednesday, 15 March 2017 at 02:33:36 UTC, ketmar wrote: Q. Schroll wrote: void test(T)(T* arg); void test(T)(ref T arg); Let p be any pointer. Why is test(p) not an ambiguity error? Why is the second overload chosen? 'cause `ref T` is more generic than `T*`. think of it as &q

Search for, o/w create element for AA

2017-06-19 Thread Q. Schroll via Digitalmars-d-learn
Trying to implement some random function I encountered this: uint randFunc(uint x) { static uint[uint] vals; if (auto r = x in vals) return *r; return vals[x] = uniform!uint(); } I have to lookup x twice and it seems that there is no way around it. Can't I te

Re: Search for, o/w create element for AA

2017-06-19 Thread Q. Schroll via Digitalmars-d-learn
On Monday, 19 June 2017 at 16:54:46 UTC, Ali Çehreli wrote: On 06/19/2017 08:19 AM, Q. Schroll wrote: Can't I tell the AA to set a value for a given key if it doesn't already have one (1) with only one lookup, and (2) in a safe way? aa.get(key, defaultValue) https://dlang.org

BigInt foreach loop

2017-08-04 Thread Q. Schroll via Digitalmars-d-learn
One can do BigInt n = returnsBigInt(); foreach (i; BigInt(0) .. n) { .. } How is this implemented? The code for BigInt is very large and I didn't find it. And is it more efficient than for (BigInt i = 0; i < n; ++i) { .. } as incrementing is a costly operation?

Re: BigInt foreach loop

2017-08-09 Thread Q. Schroll via Digitalmars-d-learn
On Friday, 4 August 2017 at 16:40:08 UTC, Stefan Koch wrote: [..] foreach(x;A .. B) it's lowerd to auto limit = B; auto key = A; for(auto x = key;key < limit;++key) { // use x } That's enough to know that the foreach loop does not reuse the space for the iteration variable. That was what I c

if (auto x = cast(C) x)

2017-08-09 Thread Q. Schroll via Digitalmars-d-learn
For a class/interface type `A` and a class `C` inheriting from `A` one can do A a = getA(); if (auto c = cast(C) a) { .. use c .. } to get a `C` view on `a` if it happens to be a `C`-instance. Sometimes one cannot find a good new name for `c` while there is no advantage of accessing `a`

Re: Why 2 ^^ 1 ^^ 2 = 2?

2017-11-01 Thread Q. Schroll via Digitalmars-d-learn
On Saturday, 28 October 2017 at 00:14:15 UTC, Ivan Kazmenko wrote: For an argument, the TEX command "^" accepts either a single character or a bracket-enclosed string of arbitrary length. So $3^3^3$ indeed transforms to ${3^3}^3$, but not for some deeper reason this time. On my TeX compiler,

simple DIP1000 test fails?

2017-11-02 Thread Q. Schroll via Digitalmars-d-learn
I've tried to get into the changes by DIP1000 and discovered this: struct S { ref S id() return { return this; } } void main() { S* p = &S().id(); } Should it really compile? (rdmd -dip1000 .\test_return_ref.d)

Re: Anybody know if I can build DMD with Visual Studio 2019?

2020-12-04 Thread Q. Schroll via Digitalmars-d-learn
On Friday, 4 December 2020 at 09:32:29 UTC, Imperatorn wrote: On Wednesday, 2 December 2020 at 22:37:06 UTC, WhatMeWorry wrote: It works now. Not sure what I did to _not_ make it work yesterday. That's easy. You made a post here about it and the universe got scared. This made my day.

Re: Pass enum variable as const ref arg

2020-12-08 Thread Q. Schroll via Digitalmars-d-learn
On Friday, 4 December 2020 at 12:54:25 UTC, Andrey wrote: [...] WTF? If you come from a C or C++ background, it's quite reasonable to think of enum stuff like a #define macro. You're using static arrays, but note that array literals allocate in many use cases. It really is like a C/C++ macro

Re: Why can I call a function with mismatched parameter type?

2020-12-11 Thread Q. Schroll via Digitalmars-d-learn
On Friday, 11 December 2020 at 11:32:09 UTC, rikki cattermole wrote: string is not a built in type. It is an alias defined by druntime. https://github.com/dlang/druntime/blob/master/src/object.d#L35 int on the other hand is defined by the compiler. It understands it. It doesn't magically un

Re: UFCS functions with both pointers and refs

2020-12-15 Thread Q. Schroll via Digitalmars-d-learn
On Sunday, 13 December 2020 at 19:02:34 UTC, Dave P. wrote: On Sunday, 13 December 2020 at 18:44:20 UTC, Mike Parker wrote: On Sunday, 13 December 2020 at 18:31:54 UTC, Dave P. wrote: Do I have to write both and have one forward to the other for more complicated functions? For free functions

Re: closures + struct: Error: forward reference to inferred return type of function call

2020-12-15 Thread Q. Schroll via Digitalmars-d-learn
On Monday, 14 December 2020 at 14:39:14 UTC, ddcovery wrote: On Monday, 14 December 2020 at 12:22:26 UTC, ddcovery wrote: int opCmp(Number other){ return _value - other.value; }; Correction: bool opEquals(Number other){ return _value == other.value; }; You could just give the

Re: UFCS functions with both pointers and refs

2020-12-17 Thread Q. Schroll via Digitalmars-d-learn
On Tuesday, 15 December 2020 at 20:38:04 UTC, Dave P. wrote: The use case would be to define extension methods on a struct outside of where the struct is defined. The extension method mutates the state of the struct, so I want to ensure I am modifying the original struct and not a copy. If it’s

Re: Avoid deallocate empty arrays?

2020-12-17 Thread Q. Schroll via Digitalmars-d-learn
On Thursday, 17 December 2020 at 16:11:37 UTC, IGotD- wrote: It's common using arrays for buffering Outside of CTFE, use an Appender.¹ Unless you're having a const/immutable element type, Appender can shrink and reuse space.² If you have, reallocation is necessary anyway not to break const/i

Re: Alias woes

2020-12-17 Thread Q. Schroll via Digitalmars-d-learn
On Saturday, 12 December 2020 at 01:02:56 UTC, SealabJaster wrote: Please see this shortened snippet: https://godbolt.org/z/j8f3x5 I've ran into annoyances before when using aliases to member fields, but something subtle like this was rather annoying to figure out. Why does the compiler feel

[Understanding] Classes and delegate inheritance vs function pointers

2021-01-09 Thread Q. Schroll via Digitalmars-d-learn
Say I have a class hierarchy like this: class Base { } class Derived : Base { } A Derived object cannot be referenced as a Base object, but as a const(Base) object. That makes sense to me. One can replace Base by a @system delegate type (SysDG) and Derived by a @safe delegate type (SafeDG)

Re: [Understanding] Classes and delegate inheritance vs function pointers

2021-01-09 Thread Q. Schroll via Digitalmars-d-learn
On Saturday, 9 January 2021 at 20:00:35 UTC, Jacob Carlborg wrote: On 2021-01-09 19:16, Q. Schroll wrote: Say I have a class hierarchy like this:   class Base { }   class Derived : Base { } A Derived object cannot be referenced as a Base object, but as a const(Base) object. That makes sense

Re: [Understanding] Classes and delegate inheritance vs function pointers

2021-01-09 Thread Q. Schroll via Digitalmars-d-learn
On Saturday, 9 January 2021 at 21:57:43 UTC, sighoya wrote: On Saturday, 9 January 2021 at 20:20:38 UTC, Q. Schroll wrote: That's not what I mean. You copy the reference. That's not what referencing meant. Derived d = new Derived(); Base* bp = &d; // fails const(

Re: Variadic Struct Parameter

2021-01-12 Thread Q. Schroll via Digitalmars-d-learn
On Tuesday, 12 January 2021 at 17:26:15 UTC, Jonathan Levi wrote: Why is this not working? ``` struct S { int x; string y; } void fun(S s ...) { This is intended for arrays and classes, not structs. Using ... for something other than arrays and c fun(S(5,"hi")); That one sho

Re: properly passing strings to functions? (C++ vs D)

2021-01-12 Thread Q. Schroll via Digitalmars-d-learn
On Monday, 11 January 2021 at 14:12:57 UTC, zack wrote: A beginner question: How to pass strings properly to functions in D? Is there any allocation going on if just use a function as "myPrint"? In C++ I have often seen calls where one just passes a reference/const reference to a string to avoi

Re: properly passing strings to functions? (C++ vs D)

2021-01-12 Thread Q. Schroll via Digitalmars-d-learn
On Monday, 11 January 2021 at 16:53:50 UTC, IGotD- wrote: I usually use "const string text" because D has no implicit declaration of variables. So using "ref" will not create a variable. This is contrary to C++ where passing as "const std::string &text" has a performance benefit and also C++ c

Re: Variadic Struct Parameter

2021-01-12 Thread Q. Schroll via Digitalmars-d-learn
On Tuesday, 12 January 2021 at 18:44:53 UTC, Jonathan Levi wrote: On Tuesday, 12 January 2021 at 17:46:14 UTC, Q. Schroll wrote: It's obvious why arrays work, it's the primary use case. I have no idea why classes are allowed. That classes are allowed, but structs are not, makes no s

Re: How do I overload += operator?

2021-01-25 Thread Q. Schroll via Digitalmars-d-learn
On Monday, 25 January 2021 at 21:53:15 UTC, Jack wrote: That naming is confusing op: it is an operator method Op: it takes an operator as a parameter Assign: kinda obvious. As an example, there are opIndex, opIndexAssign and opIndexOpAssign. opIndex overloads obj[i]. opIndexAssign overloads

Re: How can I use UFCS for a loop

2021-01-25 Thread Q. Schroll via Digitalmars-d-learn
On Tuesday, 26 January 2021 at 00:47:09 UTC, Tim wrote: Hi all, How can I change the following to a more D-like approach by using UFCS? double[3] result; Unless you have a good reason, use a slice and not a static array: double[] result; The result of std.array.array will be a slice

Re: How can I use UFCS for a loop

2021-01-25 Thread Q. Schroll via Digitalmars-d-learn
On Tuesday, 26 January 2021 at 00:47:09 UTC, Tim wrote: Hi all, How can I change the following to a more D-like approach by using UFCS? double[3] result; Json json = res.readJson; for(int i = 0; i < json.length; i++){ result[i] = json[i].to!double; } I'd prefer to do something like:

Re: How to covert dchar and wchar to string?

2021-01-25 Thread Q. Schroll via Digitalmars-d-learn
On Monday, 25 January 2021 at 18:45:11 UTC, Rempas wrote: Actually what the title says. For example I have dchar c = '\u03B3'; and I want to make it into string. I don't want to use "to!string(c);". Any help? char[] dcharToChars(char[] buffer, dchar value) { import std.range : put; aut

Re: Why are multiple instances of the single enum created?

2021-02-01 Thread Q. Schroll via Digitalmars-d-learn
On Monday, 1 February 2021 at 09:40:20 UTC, Per Nordlöw wrote: An enum only exists at compile-time, and does not occupy any space. Each time it's referenced, a new instance of the value is created. Why is that? Short answer: An enum is a literal you can refer to by name. That's my mind-model f

Re: Why are multiple instances of the single enum created?

2021-02-01 Thread Q. Schroll via Digitalmars-d-learn
On Monday, 1 February 2021 at 20:00:26 UTC, Q. Schroll wrote: On Monday, 1 February 2021 at 09:40:20 UTC, Per Nordlöw wrote: An enum only exists at compile-time, and does not occupy any space. Each time it's referenced, a new instance of the value is created. Why is that? Short answe

Re: Why are multiple instances of the single enum created?

2021-02-01 Thread Q. Schroll via Digitalmars-d-learn
On Monday, 1 February 2021 at 11:39:26 UTC, Per Nordlöw wrote: On Monday, 1 February 2021 at 11:37:49 UTC, Per Nordlöw wrote: Ok, so then my follow-up question becomes, does the right hand sides of these two assignment share the same AST node? If not, why? Because such a shared AST node could

Compiler version "dirty"

2021-03-08 Thread Q. Schroll via Digitalmars-d-learn
When I enter `dmd --version`, it says: DMD64 D Compiler v2.095.1-dirty What should the "dirty" mean? To me, it seems looks something went wrong somewhere.

Manually check struct invariants

2021-03-23 Thread Q. Schroll via Digitalmars-d-learn
For a class object obj, one can use assert(obj) to get its invariants checked. How to do this for structs?

Re: Manually check struct invariants

2021-03-23 Thread Q. Schroll via Digitalmars-d-learn
On Tuesday, 23 March 2021 at 23:27:54 UTC, Paul Backus wrote: On Tuesday, 23 March 2021 at 22:22:12 UTC, Q. Schroll wrote: For a class object obj, one can use assert(obj) to get its invariants checked. How to do this for structs? https://dlang.org/spec/expression.html#assert_expressions If

Re: Is there a more elegant way to do this in D?

2021-04-11 Thread Q. Schroll via Digitalmars-d-learn
On Thursday, 8 April 2021 at 18:06:25 UTC, Meta wrote: On Thursday, 8 April 2021 at 18:01:56 UTC, Meta wrote: On Thursday, 8 April 2021 at 12:19:29 UTC, WebFreak001 wrote: ```d string to01String(int[] x) @safe { auto conv = x.to!(ubyte[]); // allocates new array, so later cast to string i

Re: Should this always work?

2021-05-03 Thread Q. Schroll via Digitalmars-d-learn
On Saturday, 1 May 2021 at 06:17:36 UTC, Mike Parker wrote: On Saturday, 1 May 2021 at 04:55:10 UTC, frame wrote: I always thought as long as an object implements an interface, it should be able to cast it from a void* if it really points to a supporting object. No. An interface is like a poi

Re: Mixin template overloads not working

2021-12-07 Thread Q. Schroll via Digitalmars-d-learn
On Tuesday, 7 December 2021 at 12:43:40 UTC, Rumbu wrote: Bug or feature? Feature. It even has a name: "overload set". It keeps you from accidentally calling a function you had no idea existed, for example because of a name clash. Is there any workaround? Yes, the error message is very c

Alias to template instance wokrs, but non-template does not?

2019-05-14 Thread Q. Schroll via Digitalmars-d-learn
I have the rather strange question: Why does this work? struct S { int value0; alias opApply = opApplyImpl!(int delegate(ref int)); //1 int opApplyImpl(DG : int delegate(ref int))(scope DG callback) //2 { if (auto result = callback(value0)) r

Re: Parameter pack expansion

2019-05-30 Thread Q. Schroll via Digitalmars-d-learn
On Thursday, 30 May 2019 at 20:20:47 UTC, Tomas wrote: I'm very new to D, coming from C++ I'm missing parameter pack expansion and fold expressions. I also miss the expansion operator and fold expressions. In D, there is no such thing, and simulating it in a general way has so much friction, I

How to compile my DMD fork?

2019-06-14 Thread Q. Schroll via Digitalmars-d-learn
Basically the headline. I want to try to implement my DIP. I've already forked DMD from GitHub. Now, what would I have to do in order to get a D compiler with my changes? I have Windows on x86-64 and Visual Studio on my machine.

For loop with separator

2019-07-04 Thread Q. Schroll via Digitalmars-d-learn
Probably you've come over this problem once in a while, too. You have a repeating solution, so you use a for(each) loop. Sometimes, there is an action to be performed between the end of one iteration and the beginning of the next, if there is one. The prime example is printing the comma when pri

Array Vararg Template Paremeters

2019-10-30 Thread Q. Schroll via Digitalmars-d-learn
For a function, one can have a vararg parameter at the end like this: int sum(int[] ar ...) { /* code */ } But for templates, such a declaration is an error. Is there a specific reason why? I know I can do it and ensure all the template parameters are values and are convertible to the desired

Bug or Feature: `this` necessary to call function with template this parameter

2019-10-30 Thread Q. Schroll via Digitalmars-d-learn
struct Example { private void helper(int i, this X)() { } void funcTempl(T, this X)(T value) { this.helper!0(); // ^ Why do I need this? } } void main() { auto ex = Example(); ex.funcTempl(1); } The question is in the comment in the code. Is that intentio

Re: Understand signature of opOpAssign in std/complex.d

2019-11-11 Thread Q. Schroll via Digitalmars-d-learn
On Saturday, 9 November 2019 at 13:26:12 UTC, Adam D. Ruppe wrote: On Saturday, 9 November 2019 at 12:30:46 UTC, René Heldmaier wrote: The part i don't understand is "is(C R == Complex!R)". What does that mean? That's checking the if the template argument C is a case of Complex!R, while at th

Re: How to port C++ std::is_reference to D ?

2020-05-06 Thread Q. Schroll via Digitalmars-d-learn
On Wednesday, 6 May 2020 at 09:07:22 UTC, wjoe wrote: Hello, I'm choking on a piece of C++ I have no idea about how to translate to D. std::is_reference In general, you can't. In D, `ref` is not part of the type, it's a "storage class", and as such it is a property that a function paramet

Re: How to port C++ std::is_reference to D ?

2020-05-11 Thread Q. Schroll via Digitalmars-d-learn
On Saturday, 9 May 2020 at 13:44:27 UTC, Per Nordlöw wrote: On Wednesday, 6 May 2020 at 17:46:28 UTC, Q. Schroll wrote: C++'s decision to make references part of the type has some advantages, but D didn't do it because of many disadvantages. Can you outline or give a link desc

Re: Is this a good tree?

2020-06-02 Thread Q. Schroll via Digitalmars-d-learn
On Tuesday, 2 June 2020 at 11:44:10 UTC, Vladimirs Nordholm wrote: Hello. I want to create a tree which maps sequences of numbers to data. ex: - [1, 2]-> 1 - [1, 3, 1] -> 2 - [1, 3, 2] -> 3 I have no prior knowledge of what makes a good tree, and I am unsure if what I've got is good. I m

Re: Fastest way to check using if identifier has already been defined, using static if or similar?

2020-06-04 Thread Q. Schroll via Digitalmars-d-learn
On Thursday, 4 June 2020 at 09:03:40 UTC, Simen Kjærås wrote: string exists(string s) { return "__traits(compiles, { mixin(\"alias _ = "~s~";\"); })"; } Little nitpicking, but D has many forms of string literals. Escaping is hard to read especially without syntax highlighting. Escaping

Re: How to port C++ std::is_reference to D ?

2020-06-06 Thread Q. Schroll via Digitalmars-d-learn
On Wednesday, 13 May 2020 at 13:36:14 UTC, wjoe wrote: On Monday, 11 May 2020 at 19:08:09 UTC, Q. Schroll wrote: [...] 1. You can have variables ("data members") of reference type in structs. (They work like head-const pointers; if D had head-const or at least head-const point

Why is there no range iteration with index by the language?

2020-06-09 Thread Q. Schroll via Digitalmars-d-learn
Is there any particular reason why std.range : enumerate is a thing and foreach (i, e; range) { ... } doesn't work from the get-go? I wouldn't have such an issue with it if static foreach would work with enumerate just fine. As far as I can tell, foreach (e; range) { ... } is being

Re: Why is there no range iteration with index by the language?

2020-06-09 Thread Q. Schroll via Digitalmars-d-learn
On Wednesday, 10 June 2020 at 00:53:30 UTC, Seb wrote: It's a bit more complicated though as you need to avoid subtle breakage with ranges that return tuples that are auto-expanded like e.g. `foreach (k,v; myDict)`. Okay, I can accept that. It's a poor decision, but well, it is what it is.

Re: static alias this and if/do/while/for

2020-10-26 Thread Q. Schroll via Digitalmars-d-learn
On Thursday, 22 October 2020 at 21:55:59 UTC, Jack Applegame wrote: There is a funny feature (or bug) in the D language: static alias this and static operator overloading. For example interface Foo { static { int value; void opAssign(int v) { value = v; } int get()

How to unit-test a phobos module?

2020-11-25 Thread Q. Schroll via Digitalmars-d-learn
When trying to unit-test an unchanged phobos module from phobos/master, I get errors such as module core.lifetime import copyEmplace not found and template instantiation errors. What is the correct arguments to pass to (r)dmd? I know it worked for me some years ago, but somehow, it doesn'

Re: How to unit-test a phobos module?

2020-11-25 Thread Q. Schroll via Digitalmars-d-learn
On Wednesday, 25 November 2020 at 21:11:24 UTC, Paul Backus wrote: On Wednesday, 25 November 2020 at 20:58:20 UTC, Q. Schroll wrote: My setup: * A fresh DMD installed a few minutes ago. * Clone of my Phobos fork with up-to-date changes from dlang/phobos/master. Do you have clones of dmd and

Re: How to unit-test a phobos module?

2020-11-25 Thread Q. Schroll via Digitalmars-d-learn
On Wednesday, 25 November 2020 at 21:16:15 UTC, Steven Schveighoffer wrote: I typically do: make -f posix.mak std/.test -Steve For some reason, [1] says `make.exe` would be installed by the DMD installer, but I found none. It explicitly says not to use GNU make. (I'm on Windows.) [1] http

  1   2   >