Re: Calling assumeSorted on const(std.container.Array)

2023-03-25 Thread Steven Schveighoffer via Digitalmars-d-learn

On 3/25/23 9:45 AM, Olivier Prat wrote:

Would someone explain the exact nature of this error as I fail to 
understand as how just telling that an Array ConstRange is assumed to be 
sorted actually modifies anything??


It's because a Range keeps a copy of the array (Array is a reference 
counted type). Since that is labeled `const`, then editing the Range is 
forbidden.


Inside SortedRange, it has this, which is causing the issue:

```d
static if (isForwardRange!Range)
@property auto save()
{
// Avoid the constructor
typeof(this) result = this;
result._input = _input.save;
return result;
}
```

Overwriting the input isn't possible here, because it contains a const 
member.


Now, this possibly could be fixed with something like:

`return typeof(this)(_input.save);`

But it might just push the error to another place.

The whole thing is kind of ugly.

There is a note inside the Array Range which says it's trying to work 
around some old bug that is now marked as "works for me", so maybe that 
can be reexamined.


https://github.com/dlang/phobos/blob/17b1a11afd74f9f8a69af93d77d4548a557e1b89/std/container/array.d#L137

-Steve


Re: Including parts of a diet template in another

2023-03-25 Thread Steven Schveighoffer via Digitalmars-d-learn

On 3/25/23 4:56 PM, seany wrote:

Hello

If we are creating a multipage Vibe.d application, we need to use diet 
templates. I can't find any info on how to include parts or whole of a 
diet template in another.


You can include an entire other diet template like:

```pug
include commondiv
```

Note that it gets copy-pasted as code, so it can use local 
symbols/functions you define in code islands.


I would also note that you can template the entire page (a common 
model), and allow other pages to inherit that common layout.


So something like:

layout.dt:
```pug
html
  head
// stuff in head (always the same)
  body
commondiv
  // always the same stuff here
block content
```

page1.dt:
```pug
extends layout
block content
  // all the page1 specific content
```

Which is what I use. I also define `extraCss` and `extraJs` blocks to 
allow for more customization in those sections.


-Steve


Re: Calling assumeSorted on const(std.container.Array)

2023-03-25 Thread Ali Çehreli via Digitalmars-d-learn

On 3/25/23 09:31, Olivier Prat wrote:

On Saturday, 25 March 2023 at 13:45:36 UTC, Olivier Prat wrote:
I'm trying to call assumeSorted on a const(Array) using this code 
snippet:


[...]


In a similar fashion, a number of methods in SortedRange do not compile 
if called on a const(SortedRange) or immutable(SortedRange), such as 
length, or front. Is this on purpose?


There are a number of interesting points in your original post but I 
couldn't find time to answer all of those.


I can inject this for now: :) assumeSorted returns a range object. The 
concept of a const range object is flawed because by nature iteration of 
a range happens by mutating it: For example, popBack has to do that.


So, it's normal that any range algorithm will assume a non-const object.

Ali



Including parts of a diet template in another

2023-03-25 Thread seany via Digitalmars-d-learn

Hello

If we are creating a multipage Vibe.d application, we need to use 
diet templates. I can't find any info on how to include parts or 
whole of a diet template in another.


So for example, if i had pages :

1 =>

```
html
  head
//stuff in head
  body
// a common div goes here
.commonDiv
   // common div stuff here
.first-page-specific-div
   // first page specific stuff ...


```

and

2 =>

```
html
  head
//stuff in head
  body
// a common div goes here
.commonDiv
   // common div stuff here
.second-page-specific-div
   // second page specific stuff ...
```

I would like to do something like this

commonDivFile.extension =>

.commonDiv
   \\ common div stuff goes here
   \\ add css file affecting the common div
   \\ add javascript with event listeners for common div 
elements..


and then

1 =>

```
html
  head
//stuff in head
  body
// some method to include commonDiv here, unknow to me
.first-page-specific-div
   // first page specific stuff ...


```

and

2 =>

```
html
  head
//stuff in head
  body
// some method to include commonDiv here, unknow to me
.second-page-specific-div
   // second page specific stuff ...
```


I can't find a way to do this in documents. If it is in the 
documentation, please point me to it. I do not want to use an 
iframe.


What can be done in this case? Thank you.


Re: Calling assumeSorted on const(std.container.Array)

2023-03-25 Thread Olivier Prat via Digitalmars-d-learn

On Saturday, 25 March 2023 at 13:45:36 UTC, Olivier Prat wrote:
I'm trying to call assumeSorted on a const(Array) using this 
code snippet:


[...]


In a similar fashion, a number of methods in SortedRange do not 
compile if called on a const(SortedRange) or 
immutable(SortedRange), such as length, or front. Is this on 
purpose?


Calling assumeSorted on const(std.container.Array)

2023-03-25 Thread Olivier Prat via Digitalmars-d-learn
I'm trying to call assumeSorted on a const(Array) using this code 
snippet:


void test(T)(const ref Array!T a)
{
auto b = a[].assumeSorted;
writeln(b);
}

void main() {
Array!int a = [1, 5, 7, 8, 15, 100];
test(a);
}

Unfortunately, I keep having a compilation error:
/dlang/dmd/linux/bin64/../../src/phobos/std/range/package.d(10871): Error: 
cannot modify struct instance `result._input` of type 
`RangeT!(const(Array!int))` because it contains `const` or `immutable` members
/dlang/dmd/linux/bin64/../../src/phobos/std/range/package.d(10918): Error: 
cannot modify struct instance `result._input` of type 
`RangeT!(const(Array!int))` because it contains `const` or `immutable` members
/dlang/dmd/linux/bin64/../../src/phobos/std/range/package.d(11500): Error: template instance 
`std.range.SortedRange!(RangeT!(const(Array!int)), "a < b", 
SortedRangeOptions.assumeSorted)` error instantiating
onlineapp.d(10):instantiated from here: `assumeSorted!("a 
< b", RangeT!(const(Array!int)))`

onlineapp.d(17):instantiated from here: `test!int`

Would someone explain the exact nature of this error as I fail to 
understand as how just telling that an Array ConstRange is 
assumed to be sorted actually modifies anything??


Re: Traverse a DList and insert / remove in place?

2023-03-25 Thread Bastiaan Veelo via Digitalmars-d-learn

On Sunday, 19 March 2023 at 13:15:58 UTC, Armando wrote:
I would like to do something like traversing a DList, operating 
on the current element, and potentially removing that element 
or inserting a new one before/after it - an easy operation if 
you code a DList yourself. Maybe I missed something?


This is one way to do that:
```d
import std;

struct MyType
{
int id;
// [...] other stuff
}

void main()
{
auto list = DList!MyType();

// Fill the list.
foreach (i; 0 .. 10)
list.insertBack(MyType(i));

// Traverse the list, conditionally remove one element.
for (auto range = list[]; !range.empty;)
if (range.front.id == 3)
list.popFirstOf(range);
else
range.popFront();

// Traverse the list, conditionally insert one element.
for (auto range = list[]; !range.empty;)
{
if (range.front.id == 6)
list.insertBefore(range, MyType(66));
range.popFront();
}

// Print modified list.
foreach (e; list)
writeln(e);

}
```

Output:
```
MyType(0)
MyType(1)
MyType(2)
MyType(4)
MyType(5)
MyType(66)
MyType(6)
MyType(7)
MyType(8)
MyType(9)
```

https://run.dlang.io/is/kk80FD

-- Bastiaan.


Re: better video rendering in d

2023-03-25 Thread Ogi via Digitalmars-d-learn

On Tuesday, 21 March 2023 at 16:57:49 UTC, monkyyy wrote:
My current method of making videos of using raylib to generate 
screenshots, throwing those screenshots into a folder and 
calling a magic ffmpeg command is ... slow.




Why not use ffmpeg as a library? Here are the 
[bindings](https://code.dlang.org/packages/ffmpeg-d).


Re: Segfault with std.variant

2023-03-25 Thread Salih Dincer via Digitalmars-d-learn

On Saturday, 25 March 2023 at 07:42:28 UTC, Ali Çehreli wrote:


This looks like a bug to me.


Such a problem does not occur when you set all objects with the 
new operator.


```d
void main()
{
  import std.variant;

  auto var = Variant([
    "one": new Variant(1), "two": new Variant(2),
    "three": new Variant(3), "four": new Variant(4),
    "five": new Variant(5)
  ]);
  auto six = new Variant(6);
  var["six"] = new Variant(6);
  assert(var.length == 6);
}
```

SDB@79


Re: Segfault with std.variant

2023-03-25 Thread Ali Çehreli via Digitalmars-d-learn

On 3/24/23 23:07, Mitchell wrote:

>variant["four"] = Variant(4); // Segfault

Today I learned that VariantN forwards to associative array operations. 
Cool I guess. :)


> with a segfault. I'm using LDC2:

Same with dmd. It fails in the destructor of VariantN.

static if (!AllowedTypes.length || 
anySatisfy!(hasElaborateDestructor, AllowedTypes))

{
~this()
{
// Infer the safety of the provided types
static if (AllowedTypes.length)
{
if (0)
{
AllowedTypes var;
}
}
(() @trusted => fptr(OpID.destruct, , null))();
}
}

That @trusted lambda call segfaults.

This looks like a bug to me. Reported:

  https://issues.dlang.org/show_bug.cgi?id=23809

Ali



Segfault with std.variant

2023-03-25 Thread Mitchell via Digitalmars-d-learn

Howdy,

I've just tried out `std.variant` and I've noticed that I can 
induce a segfault by having a variant of variants. Should this 
work?


```d
import std.stdio;
import std.variant;

void main()
{
  Variant variant = Variant([
"one": Variant(1),
"two": Variant(2),
"three": Variant(3)
  ]);

  writefln("Segfault occurs on the next line");
  variant["four"] = Variant(4); // Segfault
  writefln("This line will not be reached");
}
```

For whatever reason, the `variant["four"] = Variant(4)` line will 
fail with a segfault. I'm using LDC2:

```
LDC - the LLVM D compiler (1.31.0):
  based on DMD v2.101.2 and LLVM 14.0.3
  built with LDC - the LLVM D compiler (1.31.0)
  Default target: x86_64-unknown-linux-gnu
  Host CPU: ivybridge
```