Re: Any suggestions on dmd error message formatting?

2021-05-14 Thread SealabJaster via Digitalmars-d-learn

On Saturday, 15 May 2021 at 06:12:25 UTC, SealabJaster wrote:

...


Honestly I also kind of wish that the D compilers could format 
functions better on the command line, because these giant blobs 
of "where's ~~wally~~ the difference?" are just horrid when they 
show up.





Re: Any suggestions on dmd error message formatting?

2021-05-14 Thread SealabJaster via Digitalmars-d-learn

On Saturday, 15 May 2021 at 04:54:15 UTC, Chris Piker wrote:

Hi D


T_T My eyes burn.

Anyway, Here it is formatted slightly better:

```d
PriorityRange.this(
DasRange!(
Tuple!(int, int)[],
int function(Tuple!(int, int)) pure nothrow @nogc @safe,
int function(Tuple!(int, int)) pure nothrow @nogc @safe,
Tuple!(int, int),
int
) range,
int function(Tuple!(int, int)) priority
) is not callable using argument types (
DasRange!(
Tuple!(int, int)[],
int function(Tuple!(int, int)) pure nothrow @nogc @safe,
int function(Tuple!(int, int)) pure nothrow @nogc @safe,
Tuple!(int, int),
int
),
int function() pure nothrow @nogc @safe
)
```

Seems the final `int function` parameter needs to accept a 
`Tuple!(int, int)`




Any suggestions on dmd error message formatting?

2021-05-14 Thread Chris Piker via Digitalmars-d-learn

Hi D

So the compile error messages getting from dmd are starting to 
remind me of the notorious 130 line error message I once got from 
a C++ compiler for missing a comma in a template.  :-/


(After fixing that bug, I left work early and came back the next 
day with a python book.)


So, like the noob I am, I've been copying error messages to a 
text editor, deleting all the @ annotations, replacing common 
patterns with short tokens, indenting the result, and then trying 
to understand the problem.


I figured this community is so inventive that I'm obviously doing 
it wrong and that there is a better way.  So does anyone have any 
suggestions on how I make error messages like this one more 
grokable?


```
das2/range.d(359,31): Error: constructor 
das2.range.PriorityRange!(DasRange!(Tuple!(int, int)[], int 
function(Tuple!(int, int)) pure nothrow @nogc @safe, int 
function(Tuple!(int, int)) pure nothrow @nogc @safe, Tuple!(int, 
int), int), int function() pure nothrow @nogc 
@safe).PriorityRange.this(DasRange!(Tuple!(int, int)[], int 
function(Tuple!(int, int)) pure nothrow @nogc @safe, int 
function(Tuple!(int, int)) pure nothrow @nogc @safe, Tuple!(int, 
int), int) range, int function(Tuple!(int, int)) priority) is not 
callable using argument types (DasRange!(Tuple!(int, int)[], int 
function(Tuple!(int, int)) pure nothrow @nogc @safe, int 
function(Tuple!(int, int)) pure nothrow @nogc @safe, Tuple!(int, 
int), int), int function() pure nothrow @nogc @safe)

```

As always, your advice is much appreciated



Re: Filter for opDispatch?

2021-05-14 Thread frame via Digitalmars-d-learn

On Friday, 14 May 2021 at 22:39:29 UTC, frame wrote:

When using opDispatch()


Thanks! I stumbled around with static asserts and mixins... 
totally forgot about the constraints but they will to the trick.





Re: Filter for opDispatch?

2021-05-14 Thread Paul Backus via Digitalmars-d-learn

On Friday, 14 May 2021 at 22:39:29 UTC, frame wrote:

When using opDispatch()

- how can I tell the compiler that I do not want to handle some 
calls? Some code is testing for range methods (empty, front, 
popFront) and I don't know where and which side effects it 
causes.


Use a template constraint. For example:

auto opDispatch(string method, Args...)(Args args)
if (shouldHandle!method)
{
// ...
}

...where `shouldHandle` checks the method name and returns `true` 
if you want to handle it, or `false` if you don't.



- how can I dismiss calls from __traits(compiles)?


You can't.


Re: Filter for opDispatch?

2021-05-14 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 14 May 2021 at 22:39:29 UTC, frame wrote:
- how can I tell the compiler that I do not want to handle some 
calls?


Put a template constraint on it. `void opDispatch(string s)() 
if(s == "whatever")` or minimally like `if(s != "popFront")`


This kind of thing is why I always put opDispatch on a helper 
thing now though, and still do the != "popFront" just to stop the 
ducktypers.




Filter for opDispatch?

2021-05-14 Thread frame via Digitalmars-d-learn

When using opDispatch()

- how can I tell the compiler that I do not want to handle some 
calls? Some code is testing for range methods (empty, front, 
popFront) and I don't know where and which side effects it causes.


- how can I dismiss calls from __traits(compiles)?


Re: Can rdmd (under Windows 10) use linker other than Optlink?

2021-05-14 Thread rikki cattermole via Digitalmars-d-learn



On 15/05/2021 9:42 AM, DLearner wrote:
I am getting 'Error 42: Symbol Undefined' while testing some (fairly) 
complex imports.


There was a reference in January to an Optlink bug that seemed like it 
could be responsible.


If rdmd can use another linker (and one was recommended), I might be 
able to test things further.


Best regards


Just don't use -m32, i.e. -m64 or -m32mscoff


Can rdmd (under Windows 10) use linker other than Optlink?

2021-05-14 Thread DLearner via Digitalmars-d-learn
I am getting 'Error 42: Symbol Undefined' while testing some 
(fairly) complex imports.


There was a reference in January to an Optlink bug that seemed 
like it could be responsible.


If rdmd can use another linker (and one was recommended), I might 
be able to test things further.


Best regards


Re: SDL2 Android vulkan question

2021-05-14 Thread Danny Arends via Digitalmars-d-learn

On Sunday, 2 May 2021 at 13:43:11 UTC, evilrat wrote:
Anyway, I might try to look at this next weekend. Do you have 
this project available on github/google drive?


Open-sourced the code see:

https://github.com/DannyArends/CalderaD

Danny


Re: Learning D

2021-05-14 Thread Mike Parker via Digitalmars-d-learn

On Friday, 14 May 2021 at 15:30:06 UTC, Imperatorn wrote:

https://www.amazon.com/Programming-Language-Former-Python-Developers-ebook/dp/B08MD7ZB2X

Anyone read it?


From the thread title, I thought you were asking about my book!




Re: how do I implement opSlice for retro range?

2021-05-14 Thread Jack via Digitalmars-d-learn
On Friday, 14 May 2021 at 14:14:03 UTC, Steven Schveighoffer 
wrote:

On 5/13/21 11:49 PM, Jack wrote:

[...]


Just slice the `a`, appropriately. You have to translate the 
indexes back into the original array.


```d
auto opSlice(size_t start, size_t end)
{
  return typeof(this)(a[$ - end .. $ - start]);
}
```

You should also define `length`, `save`, `opIndex`, and 
`opDollar` so that it fits in the range hierarchy as a proper 
random-access range.


But I question whether you shouldn't just use `std.range.retro` 
directly? It does all this for you:


```d
// inside A
auto retro() {
   import std.range : retro;
   return arr.retro;
}
```

-Steve


much easier, I'll be just using the one from std.range. Thanks!


Re: how do I implement opSlice for retro range?

2021-05-14 Thread Jack via Digitalmars-d-learn

On Friday, 14 May 2021 at 10:00:44 UTC, Christian Köstlin wrote:

On 2021-05-14 05:49, Jack wrote:

[...]

arr.retro()[0..2] already works.

see https://run.dlang.io/is/U8u3br


oh, how i silly of i didn't notice that before


Re: Learning D

2021-05-14 Thread cc via Digitalmars-d-learn

On Friday, 14 May 2021 at 15:30:06 UTC, Imperatorn wrote:

https://www.amazon.com/Programming-Language-Former-Python-Developers-ebook/dp/B08MD7ZB2X

Anyone read it?


Haven't read it, the title has me at the first five words though.


Learning D

2021-05-14 Thread Imperatorn via Digitalmars-d-learn

https://www.amazon.com/Programming-Language-Former-Python-Developers-ebook/dp/B08MD7ZB2X

Anyone read it?


Re: ref struct member function

2021-05-14 Thread Paul Backus via Digitalmars-d-learn

On Friday, 14 May 2021 at 10:00:28 UTC, 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.


The error message here is definitely not as good as it could be.

As a general rule, if you want to apply an attribute to the 
`this` reference in a member function, you write it after the 
parameter list:


struct S
{
// ...

auto memberFunc() const scope
{
// `this` is const and scope
}

auto anotherOne() share inout
{
// `this` is shared and inout
}
}


Re: how do I implement opSlice for retro range?

2021-05-14 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/13/21 11:49 PM, Jack wrote:
How can I implement ranges in the retro range? I'd like to do this 
without allocate a new array with .array from std.array, can I do that?


use like this:

```d
     auto arr = [1, 2, 3, 4, 5];
     auto a = new A!int(arr);
     auto b = a.retro[0 .. 2]; // 4, 5
```

the class:

```d

class A(T)
{
     private T[] arr;

     this(T[] a)
     {
     arr = a;
     }

     auto opIndex() nothrow
     {
     return Range(arr);
     }

     auto retro() { return RangeRetro(arr); }

     protected static struct Range
     {
     T[] a;
     T front() { return a[0]; }
     T back() { return a[$ - 1]; }
     void popFront() { a = a[1 .. $]; }
     bool empty() { return a.length == 0; }
     }

     protected static struct RangeRetro
     {
     import std.range : popFront;
     import std.range : popBack;

     T[] a;
     T front() { return a[$ - 1]; }
     T back() { return a[0]; }
     void popBack() {  a.popFront(); }
     void popFront() { a.popBack(); }
     bool empty() { return a.length == 0; }

     auto opSlice(size_t start, size_t end)
     {
    ???
     }
     }
}
```




Just slice the `a`, appropriately. You have to translate the indexes 
back into the original array.


```d
auto opSlice(size_t start, size_t end)
{
  return typeof(this)(a[$ - end .. $ - start]);
}
```

You should also define `length`, `save`, `opIndex`, and `opDollar` so 
that it fits in the range hierarchy as a proper random-access range.


But I question whether you shouldn't just use `std.range.retro` 
directly? It does all this for you:


```d
// inside A
auto retro() {
   import std.range : retro;
   return arr.retro;
}
```

-Steve


Re: Scope of 'alias'

2021-05-14 Thread Paul Backus via Digitalmars-d-learn

On Friday, 14 May 2021 at 14:03:17 UTC, DLearner wrote:
So 'alias' only valid from definition to end-of-function, 
rather than whole function?


Best regards


Yes. This applies to all definitions inside a function, not just 
aliases.


Scope of 'alias'

2021-05-14 Thread DLearner via Digitalmars-d-learn

>>>
void foo(pint p1) {
   alias pint=uint;
   import std.stdio;
   writeln("p1 = ", p1);
}

void main() {
  alias pint=uint;
  pint var1;
  var1 = 7;
  foo(var1);
}
<<<

Does not compile.

But the rather similar:



alias pint=uint;

void foo(pint p1) {
   import std.stdio;
   writeln("p1 = ", p1);
}

void main() {
  pint var1;
  var1 = 7;
  foo(var1);
}
<<<

Is fine.

So 'alias' only valid from definition to end-of-function, rather 
than whole function?


Best regards



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 hidden parameter. Every non-static method has it.

So my next question would be how come ref based operator overloads are 
not labelled as return and do not show this warning?


``` D
struct Fraction {
     auto n = 0L;
     auto d = 1L;

     ref Fraction opUnary(string op)()
     if (op == "++") {
     n += d;
     return this;
     }
}

ref Fraction foo() {
     auto f = Fraction(1, 3);

     // Same bug as with reduce
     return ++f;
}
```


Note that opUnary is not an ordinary method. It's a template. That means 
attributes are inferred [1], including the `return` attribute. I.e., the 
compiler adds `return` to the signature for you, just like Steven 
suggested you do manually.


Try omitting the return type of `reduce` in your original code:

 ref reduce() { ... }

That also enables attribute inference, and your code will compile.


[1] https://dlang.org/spec/function.html#function-attribute-inference


Re: ref struct member function

2021-05-14 Thread PinDPlugga via Digitalmars-d-learn

On Thursday, 13 May 2021 at 19:48:44 UTC, Ali Çehreli wrote:
I was writing this example that shows a use case for the 
problem (marked with BUG below):

```D
struct Fraction {
auto n = 0L;
auto d = 1L;

// ... other bits ...

ref Fraction reduce() {
  import std.numeric : gcd;

  // v = gcd(n, d);
  // if (v > 1) {
  //   n /= v;
  //   d /= v;
  // }

  return this;
}

// ... etc ...
}

ref foo() {
  import std.stdio : writeln;
  auto f = Fraction(3, 9);

  // BUG: Returns reference to an object on the stack
  return f.reduce();
}

void main() {
  // Refers to a temporary (it would be a copy without the &)
  auto f = &foo();

  // Do some unrelated things hoping that space for dead 
objects on

  // the stack will be reused.
  import std.stdio;
  import std.random;
  writeln("doing something else: ", uniform(0, 6));

  writeln(f.d); // Access dead and overwritten object (does NOT 
print

// 9 on my system)
}
```
Putting 'return' where Steve shows makes the compiler guard us 
against this misuse and the program thankfully does not compile.


Ali


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.


So my next question would be how come ref based operator 
overloads are not labelled as return and do not show this warning?


``` D
struct Fraction {
auto n = 0L;
auto d = 1L;

ref Fraction opUnary(string op)()
if (op == "++") {
n += d;
return this;
}
}

ref Fraction foo() {
auto f = Fraction(1, 3);

// Same bug as with reduce
return ++f;
}
```


Re: how do I implement opSlice for retro range?

2021-05-14 Thread Christian Köstlin via Digitalmars-d-learn

On 2021-05-14 05:49, Jack wrote:
How can I implement ranges in the retro range? I'd like to do this 
without allocate a new array with .array from std.array, can I do that?


use like this:

```d
     auto arr = [1, 2, 3, 4, 5];
     auto a = new A!int(arr);
     auto b = a.retro[0 .. 2]; // 4, 5
```

the class:

```d

class A(T)
{
     private T[] arr;

     this(T[] a)
     {
     arr = a;
     }

     auto opIndex() nothrow
     {
     return Range(arr);
     }

     auto retro() { return RangeRetro(arr); }

     protected static struct Range
     {
     T[] a;
     T front() { return a[0]; }
     T back() { return a[$ - 1]; }
     void popFront() { a = a[1 .. $]; }
     bool empty() { return a.length == 0; }
     }

     protected static struct RangeRetro
     {
     import std.range : popFront;
     import std.range : popBack;

     T[] a;
     T front() { return a[$ - 1]; }
     T back() { return a[0]; }
     void popBack() {  a.popFront(); }
     void popFront() { a.popBack(); }
     bool empty() { return a.length == 0; }

     auto opSlice(size_t start, size_t end)
     {
    ???
     }
     }
}
```



arr.retro()[0..2] already works.

see https://run.dlang.io/is/U8u3br