-preview=in deprecation warning

2023-04-20 Thread Jack Applegame via Digitalmars-d-learn

Can anyone help me get rid of this depreciation?

```d
struct Foo {
string foo;
string getFoo() const @safe { return foo; }
}

size_t bar(in Foo foo) @safe {
return foo.getFoo().length;
}

void main() {
Foo("hello").bar().writeln();
}
```
```sh
$ ldc2 -preview=in --run foo.d
foo.d(9): Deprecation: scope variable `foo` assigned to non-scope 
parameter `this` calling `getFoo`

```
```sh
$ ldc2 --version
LDC - the LLVM D compiler (1.32.1):
  based on DMD v2.102.2 and LLVM 15.0.7
```



Re: -preview=in deprecation warning

2023-04-20 Thread Dennis via Digitalmars-d-learn

On Thursday, 20 April 2023 at 09:14:48 UTC, Jack Applegame wrote:

Can anyone help me get rid of this depreciation?


Annotate `getFoo` with `return scope`:

```d
struct Foo {
string foo;
string getFoo() return scope const @safe { return foo; }
}



Re: -preview=in deprecation warning

2023-04-20 Thread Jack Applegame via Digitalmars-d-learn

On Thursday, 20 April 2023 at 09:41:13 UTC, Dennis wrote:
On Thursday, 20 April 2023 at 09:14:48 UTC, Jack Applegame 
wrote:

Can anyone help me get rid of this depreciation?


Annotate `getFoo` with `return scope`:

```d
struct Foo {
string foo;
string getFoo() return scope const @safe { return foo; }
}


Wow. Thanks.
I tried this, but rearranged the return and scope like this
```d
// doesn't work
string getFoo() scope return const @safe { return foo; }
```
```d
// works
string getFoo() return scope const @safe { return foo; }
```



Getting a total from a user defined variable

2023-04-20 Thread Joel via Digitalmars-d-learn

```d
import std;

struct Person {
string name;
ulong age;
}

void main() {
auto p=[Person("Joel", 43), Person("Timothy", 40)];
writeln("Total: ", p.reduce!((a,b) => a.age+b.age)(0UL)); // 
how do I get the total of ages added together?

}
```


Re: serve-d and emacs

2023-04-20 Thread Christian Köstlin via Digitalmars-d-learn
I tried to reproduce my old eglot experiment, and for me serve-d was not 
even compiling with the newest dmd. Which versions are you using?


Kind regards,
Christian


Re: Getting a total from a user defined variable

2023-04-20 Thread John Chapman via Digitalmars-d-learn

On Thursday, 20 April 2023 at 19:41:21 UTC, Joel wrote:

// how do I get the total of ages added together?


p.map!(x => x.age).sum();


Re: Getting a total from a user defined variable

2023-04-20 Thread Ferhat Kurtulmuş via Digitalmars-d-learn

On Thursday, 20 April 2023 at 19:41:21 UTC, Joel wrote:

```d
import std;

struct Person {
string name;
ulong age;
}

void main() {
auto p=[Person("Joel", 43), Person("Timothy", 40)];
writeln("Total: ", p.reduce!((a,b) => a.age+b.age)(0UL)); 
// how do I get the total of ages added together?

}
```


```d
import std;

struct Person {
string name;
ulong age;
}

void main() {
auto p=[Person("Joel", 43), Person("Timothy", 40)];

writeln("Total: ", p.map!(a => a.age).reduce!"a + b");
}
```


Re: Getting a total from a user defined variable

2023-04-20 Thread Steven Schveighoffer via Digitalmars-d-learn

```d
writeln("Total: ", p.fold!((a,b) => a+b.age)(0UL));
// or
writeln("Total: ", reduce!((a,b) => a+b.age)(0UL, p));
```

Note that `reduce` is the old version of `fold`, which happened when 
UFCS became a thing.


-Steve


Re: Getting a total from a user defined variable

2023-04-20 Thread Joel via Digitalmars-d-learn

On Thursday, 20 April 2023 at 21:28:48 UTC, John Chapman wrote:

On Thursday, 20 April 2023 at 19:41:21 UTC, Joel wrote:

// how do I get the total of ages added together?


p.map!(x => x.age).sum();


Or: p.map!"a.age".sum; works too.