Re: static if - unexpected results

2023-06-23 Thread FeepingCreature via Digitalmars-d-learn
On Friday, 23 June 2023 at 18:43:06 UTC, Steven Schveighoffer 
wrote:
It should be a spec change. Change POD to say "type" instead of 
"struct".


The goal of `isPOD` is to determine how careful generic code 
needs to be to pass the type around, or copy it. Changing it to 
false implies that it is not "plain old data". I.e. it has a 
destructor, it has hidden members, or it cannot be copied via 
bit copying (all of these do not fit the type in question).


The only other option is to error on calling `__traits(isPOD, 
char)`, but I think that's even worse.


-Steve


Yeah, I think that's also where I'm standing. The current 
behavior seems correct and useful, it's just not documented 
correctly.


Re: How to setup dub project for contributing to a dub package?

2023-06-23 Thread Steven Schveighoffer via Digitalmars-d-learn

On 6/23/23 8:23 PM, Ki Rill wrote:

It works fine if I add it as a `dub` package, but I want to add the 
repository so I can modify it, then test it on a new project, and make a 
PR that improves the package.


How do people usually set up their projects for such tasks?


1. git clone to a local directory (use your fork, so you can change and 
submit when you want to do a PR)

2. CD to that directory
3. `dub add-local .`

Now it will use the local directory as the equivalent of the latest tag 
instead of downloading from the repository.


You may have to `dub upgrade` your project that depends on it, or 
temporarily depend on a different version.


-Steve


Re: A couple of questions about arrays and slices

2023-06-23 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, June 23, 2023 7:02:12 PM MDT Cecil Ward via Digitalmars-d-learn 
wrote:
> I just had a fight with LDC over the following code when I tried
> out reserve. I have an associative array that maps strings to
> ‘ordinals’ ie uints that are unique, and the compiler hates the
> call to reserve.
>
> ==
>
>
>
> struct decls_t
>   {
>   uintn_entries = 0;
>   uint[ dstring ] ordinals;   // Associative array maps variable
> names to ordinals
>   }
>
> static decls_t Decls;
>
> enum NPreAllocEntries = 32;
> Decls.ordinals.reserve( NPreAllocEntries );
>
> source>(82): Error: none of the overloads of template
> `object.reserve` are callable using argument types
> `!()(uint[dstring], ulong)`
> /opt/compiler-explorer/ldc1.32.1/ldc2-1.32.1-linux-x86_64/bin/../import/obje
> ct.d(3983):Candidate is: `reserve(T)(ref T[] arr, size_t
> newcapacity)` Compiler returned: 1

Associative arrays and dynamic arrays are completely different things.
Associative arrays are hash tables, and reserve really doesn't make sense
for them. reserve is for telling the GC to make sure that a dynamic array
has at least a specific amount of room to grow into before the GC needs to
do a reallocation so that the dynamic array refers to a different memory
block with enough memory to hold the data, whereas if and when associative
arrays have to reallocate any of their internals is largely
implementation-defined.

Any time that you add or remove elements from an AA, it might reallocate
some of its internals depending on its current state and what the key of the
element is - and that could be different between different compiler releases
(though it's unlikely to change very often, since I don't think that the AA
implementation gets messed with much).

You can use the rehash function on AAs to tell the GC to try to reorder how
it's structured all of its buckets so that lookups are more efficient with
the data that's currently in there, and you can call clear to remove all its
elements, but in general, you don't do much to manage an AA's memory. It's a
much more complicated data structure than an array.

https://dlang.org/spec/hash-map.html

- Jonathan M Davis






Re: A couple of questions about arrays and slices

2023-06-23 Thread Cecil Ward via Digitalmars-d-learn

On Thursday, 22 June 2023 at 05:21:52 UTC, Cecil Ward wrote:
On Thursday, 22 June 2023 at 01:44:22 UTC, Jonathan M Davis 
wrote:
On Wednesday, June 21, 2023 7:05:28 PM MDT Paul Backus via 
Digitalmars-d-learn wrote:

[...]


To add to that, it _has_ to know the element type, because 
aside from anything related to a type's size, it bit-blits the 
type's init value onto the new elements when it increases the 
length of the dynamic array.


You'd probably be dealing with bytes if you were explicitly 
asking for memory and the like (e.g. with malloc), but a 
dynamic array is properly typed, and everything you do with it 
in @safe code is going to deal with it as properly typed. For 
it to be otherwise would require @system casts.


- Jonathan M Davis


Thankyou Jonathan!


I just had a fight with LDC over the following code when I tried 
out reserve. I have an associative array that maps strings to 
‘ordinals’ ie uints that are unique, and the compiler hates the 
call to reserve.


==



struct decls_t
{
uintn_entries = 0;
	uint[ dstring ]		ordinals;	// Associative array maps variable 
names to ordinals

}

static decls_t Decls;

enum NPreAllocEntries = 32;
Decls.ordinals.reserve( NPreAllocEntries );

source>(82): Error: none of the overloads of template 
`object.reserve` are callable using argument types 
`!()(uint[dstring], ulong)`

/opt/compiler-explorer/ldc1.32.1/ldc2-1.32.1-linux-x86_64/bin/../import/object.d(3983):
Candidate is: `reserve(T)(ref T[] arr, size_t newcapacity)`
Compiler returned: 1



Re: How to setup dub project for contributing to a dub package?

2023-06-23 Thread Ki Rill via Digitalmars-d-learn
On Friday, 23 June 2023 at 15:52:44 UTC, Richard (Rikki) Andrew 
Cattermole wrote:
First things first, dcv is added to the dub-registry, so use 
this.


https://code.dlang.org/packages/dcv

```json
"dependencies": {
"dcv": "~>0.3.0"
}
```

For ffmpeg the binding tells you what to add for search paths 
in the lflags directive.


https://github.com/ljubobratovicrelja/ffmpeg-d#adding-to-dub

All I can suggest is make sure you have the right dev packages 
for ffmpeg installed and findable by the linker.


It works fine if I add it as a `dub` package, but I want to add 
the repository so I can modify it, then test it on a new project, 
and make a PR that improves the package.


How do people usually set up their projects for such tasks?


How to read live output from another process ?

2023-06-23 Thread Vinod K Chandran via Digitalmars-d-learn

Hi all,
I am trying to create a program which burns time codes to a 
video. I am using ffmpeg for this. So far, I can successfully 
start ffmpeg in another thread and stop it when I need. But I 
can't read the live outputs from ffmpeg. This is my code.

```d
void onBtnBurnClick(Control c, EventArgs e) {
if (!burnStarted) {
burnStarted = true;
btnBurn.text = "Stop Burning";
auto ffCmd = makeFFMPEGCommand(selVideo);
  // ffPipe is a global ProcessPipes
auto tsk = task!runFFMPEG(ffCmd, , frm.handle);
tsk.executeInNewThread();
} else {
ffPipe.stdin.writeln("q");
ffPipe.stdin.close();
btnBurn.text = "Burn Time Code";
}
}
```
This is a button's click event.


Re: static if - unexpected results

2023-06-23 Thread Steven Schveighoffer via Digitalmars-d-learn

On 6/23/23 10:31 AM, FeepingCreature wrote:

On Friday, 23 June 2023 at 14:22:24 UTC, DLearner wrote:

Hi

Was looking for compile-time detection of a struct variable.
However, the following test code gave the two 'FAILS' shown below.
Comments?
```
void main() {
   import std.stdio : writeln;
   import std.traits;

   string mxnTst(string VarName) {
  return

  `static if (is(typeof(` ~ VarName ~ `) == char)) {` ~
 `writeln("` ~ VarName ~ ` ", " is a char");` ~
  `} else static if (__traits(isPOD, typeof(` ~ VarName ~ `))) {` ~
 `writeln("` ~ VarName ~ ` ", " is a struct");` ~
  `} else static if (is(typeof(` ~ VarName ~ `) == int)) {` ~
 `writeln("` ~ VarName ~ ` ", " is an int");` ~
  `} else {` ~
 `static assert(false, "mxnTst Variable '` ~ VarName  ~ `' is 
of unknown type");` ~

  `}`
   ;
   }

   char char1;
   int  int1;
   byte byte1;

   struct foo {
  int  fooint;
  char foochar;
   }
   foo foovar1;

   mixin(mxnTst("char1"));   // Expected: char1 is a char. Actual: 
char1 is a char. (ok)
   mixin(mxnTst("int1"));    // Expected: int1 is an int. Actual: int1 
is a struct. (FAIL)
   mixin(mxnTst("foovar1")); // Expected: foovar1 is a struct. Actual: 
foovar1 is a struct. (ok)
   mixin(mxnTst("byte1"));   // Expected: Run to fail with the static 
assert message. Actual: byte1 is a struct. (FAIL)

}
```


```
static assert(__traits(isPOD, int)); // ok.
static assert(__traits(isPOD, byte)); // ok.
```
It's a bug in either the spec or the compiler.


It should be a spec change. Change POD to say "type" instead of "struct".

The goal of `isPOD` is to determine how careful generic code needs to be 
to pass the type around, or copy it. Changing it to false implies that 
it is not "plain old data". I.e. it has a destructor, it has hidden 
members, or it cannot be copied via bit copying (all of these do not fit 
the type in question).


The only other option is to error on calling `__traits(isPOD, char)`, 
but I think that's even worse.


-Steve


Re: static if - unexpected results

2023-06-23 Thread DLearner via Digitalmars-d-learn

On Friday, 23 June 2023 at 16:51:16 UTC, Ali Çehreli wrote:

On 6/23/23 07:22, DLearner wrote:

>`} else static if (__traits(isPOD, typeof(` ~ VarName
~ `))) {` ~

Regardless, you can also use the 'is' expression with the 
'struct' keyword. If T is a struct,


  is (T == struct)

that will produce true at compile time.

Ali


Thanks for this - I can confirm it works.


Re: static if - unexpected results

2023-06-23 Thread Ali Çehreli via Digitalmars-d-learn

On 6/23/23 07:22, DLearner wrote:

>`} else static if (__traits(isPOD, typeof(` ~ VarName ~ `))) {` ~

Regardless, you can also use the 'is' expression with the 'struct' 
keyword. If T is a struct,


  is (T == struct)

that will produce true at compile time.

Ali



Re: static if - unexpected results

2023-06-23 Thread DLearner via Digitalmars-d-learn

On Friday, 23 June 2023 at 15:48:44 UTC, H. S. Teoh wrote:

On Friday, 23 June 2023 at 15:22:36 UTC, DLearner wrote:

On Friday, 23 June 2023 at 14:31:45 UTC, FeepingCreature wrote:

On Friday, 23 June 2023 at 14:22:24 UTC, DLearner wrote:

[...]


```
static assert(__traits(isPOD, int)); // ok.
static assert(__traits(isPOD, byte)); // ok.
```
It's a bug in either the spec or the compiler.


I am using
```
DMD64 D Compiler v2.103.0-dirty

```
under
 ```
Windows [Version 10.0.19045.3086]
```

Do I need to report this anywhere?


Tested your original code on latest dmd git master, here's the 
output:


d
char1  is a char
int1  is a struct
foovar1  is a struct
byte1  is a struct


Looks like there isn't a problem? Or at least, it's now fixed 
in git master.


Which exact version of dmd are you using?  Did you download 
from dlang.org or did you build your own?



--T


Probably I misunderstand, but to me:
```
int1  is a struct
```
and

```
byte1  is a struct
```
are both errors.

The 'struct' test is being triggered for things that are not 
structs.


Re: How to setup dub project for contributing to a dub package?

2023-06-23 Thread Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn

First things first, dcv is added to the dub-registry, so use this.

https://code.dlang.org/packages/dcv

```json
"dependencies": {
"dcv": "~>0.3.0"
}
```

For ffmpeg the binding tells you what to add for search paths in the 
lflags directive.


https://github.com/ljubobratovicrelja/ffmpeg-d#adding-to-dub

All I can suggest is make sure you have the right dev packages for 
ffmpeg installed and findable by the linker.


Re: static if - unexpected results

2023-06-23 Thread H. S. Teoh via Digitalmars-d-learn

On Friday, 23 June 2023 at 15:22:36 UTC, DLearner wrote:

On Friday, 23 June 2023 at 14:31:45 UTC, FeepingCreature wrote:

On Friday, 23 June 2023 at 14:22:24 UTC, DLearner wrote:

[...]


```
static assert(__traits(isPOD, int)); // ok.
static assert(__traits(isPOD, byte)); // ok.
```
It's a bug in either the spec or the compiler.


I am using
```
DMD64 D Compiler v2.103.0-dirty

```
under
 ```
Windows [Version 10.0.19045.3086]
```

Do I need to report this anywhere?


Tested your original code on latest dmd git master, here's the 
output:


d
char1  is a char
int1  is a struct
foovar1  is a struct
byte1  is a struct


Looks like there isn't a problem? Or at least, it's now fixed in 
git master.


Which exact version of dmd are you using?  Did you download from 
dlang.org or did you build your own?



--T


Re: static if - unexpected results

2023-06-23 Thread DLearner via Digitalmars-d-learn

On Friday, 23 June 2023 at 14:31:45 UTC, FeepingCreature wrote:

On Friday, 23 June 2023 at 14:22:24 UTC, DLearner wrote:

[...]


```
static assert(__traits(isPOD, int)); // ok.
static assert(__traits(isPOD, byte)); // ok.
```
It's a bug in either the spec or the compiler.


I am using
```
DMD64 D Compiler v2.103.0-dirty

```
under
 ```
Windows [Version 10.0.19045.3086]
```

Do I need to report this anywhere?


How to setup dub project for contributing to a dub package?

2023-06-23 Thread Ki Rill via Digitalmars-d-learn
Recently, I tried to set up `dcv` with `dub` to improve a few 
things in the library, but I faced some strange issues.


When doing `dub add dcv`, it works fine. But when I try to add a 
local dcv project folder, or a repository, it fails to link the 
necessary package libraries. Seems like it's only `ffmpeg` 
libraries that are not linked.


What am I missing here?

```
// dub.json

...

"dependencies": {
"dcv": {
"repository": "git+https://github.com/rillki/dcv.git;,
"version": "~master"
}
},

...
```

Output:
```
Undefined symbols for architecture x86_64:
  "_av_free_packet", referenced from:
  
__D3dcv7videoio5input11InputStream13readFrameImplMFNbNiKCQCc4core5image5ImageZb in libdcv.a(input_2c65_47c.o)
  
__D3dcv7videoio6output12OutputStream__T10writeSliceTS3mir7ndslice5slice__T9mir_sliceTPhVmi2VEQBoQBnQBi14mir_slice_kindi2ZQBvZQDiMFNbNiQDeEQFf4core5image11ImageFormatZb in libdcv.a(output_2c76_14e7.o)
  
__D3dcv7videoio6output12OutputStream__T10writeSliceTS3mir7ndslice5slice__T9mir_sliceTPhVmi3VEQBoQBnQBi14mir_slice_kindi2ZQBvZQDiMFNbNiQDeEQFf4core5image11ImageFormatZb in libdcv.a(output_2c74_14e8.o)

  "_av_register_all", referenced from:
  __D3dcv7videoio6common9AVStarter6__ctorMFNbNiZCQBsQBrQBmQBi 
in libdcv.a(common_2c56_37c.o)

  "_avcodec_decode_video2", referenced from:

  ETC

ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to 
see invocation)

Error: linker exited with status 1
Error dmd failed with exit code 1.
```


Re: static if - unexpected results

2023-06-23 Thread FeepingCreature via Digitalmars-d-learn

On Friday, 23 June 2023 at 14:22:24 UTC, DLearner wrote:

Hi

Was looking for compile-time detection of a struct variable.
However, the following test code gave the two 'FAILS' shown 
below.

Comments?
```
void main() {
   import std.stdio : writeln;
   import std.traits;

   string mxnTst(string VarName) {
  return

  `static if (is(typeof(` ~ VarName ~ `) == char)) {` ~
 `writeln("` ~ VarName ~ ` ", " is a char");` ~
  `} else static if (__traits(isPOD, typeof(` ~ VarName ~ 
`))) {` ~

 `writeln("` ~ VarName ~ ` ", " is a struct");` ~
  `} else static if (is(typeof(` ~ VarName ~ `) == int)) {` 
~

 `writeln("` ~ VarName ~ ` ", " is an int");` ~
  `} else {` ~
 `static assert(false, "mxnTst Variable '` ~ VarName  ~ 
`' is of unknown type");` ~

  `}`
   ;
   }

   char char1;
   int  int1;
   byte byte1;

   struct foo {
  int  fooint;
  char foochar;
   }
   foo foovar1;

   mixin(mxnTst("char1"));   // Expected: char1 is a char. 
Actual: char1 is a char. (ok)
   mixin(mxnTst("int1"));// Expected: int1 is an int.  
Actual: int1 is a struct. (FAIL)
   mixin(mxnTst("foovar1")); // Expected: foovar1 is a struct.  
Actual: foovar1 is a struct. (ok)
   mixin(mxnTst("byte1"));   // Expected: Run to fail with the 
static assert message. Actual: byte1 is a struct. (FAIL)

}
```


```
static assert(__traits(isPOD, int)); // ok.
static assert(__traits(isPOD, byte)); // ok.
```
It's a bug in either the spec or the compiler.


static if - unexpected results

2023-06-23 Thread DLearner via Digitalmars-d-learn

Hi

Was looking for compile-time detection of a struct variable.
However, the following test code gave the two 'FAILS' shown below.
Comments?
```
void main() {
   import std.stdio : writeln;
   import std.traits;

   string mxnTst(string VarName) {
  return

  `static if (is(typeof(` ~ VarName ~ `) == char)) {` ~
 `writeln("` ~ VarName ~ ` ", " is a char");` ~
  `} else static if (__traits(isPOD, typeof(` ~ VarName ~ 
`))) {` ~

 `writeln("` ~ VarName ~ ` ", " is a struct");` ~
  `} else static if (is(typeof(` ~ VarName ~ `) == int)) {` ~
 `writeln("` ~ VarName ~ ` ", " is an int");` ~
  `} else {` ~
 `static assert(false, "mxnTst Variable '` ~ VarName  ~ 
`' is of unknown type");` ~

  `}`
   ;
   }

   char char1;
   int  int1;
   byte byte1;

   struct foo {
  int  fooint;
  char foochar;
   }
   foo foovar1;

   mixin(mxnTst("char1"));   // Expected: char1 is a char. 
Actual: char1 is a char. (ok)
   mixin(mxnTst("int1"));// Expected: int1 is an int.  
Actual: int1 is a struct. (FAIL)
   mixin(mxnTst("foovar1")); // Expected: foovar1 is a struct.  
Actual: foovar1 is a struct. (ok)
   mixin(mxnTst("byte1"));   // Expected: Run to fail with the 
static assert message. Actual: byte1 is a struct. (FAIL)

}
```