Re: In general, who should do more work: popFront or front?

2021-06-15 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 15 June 2021 at 04:24:09 UTC, surlymoor wrote:
All my custom range types perform all their meaningful work in 
their respective popFront methods, in addition to its expected 
source data iteration duties. The reason I do this is because I 
swear I read in a github discussion that front is expected to 
be O(1), and the only way I can think to achieve this is to 
stash the front element of a range in a private field; popFront 
would thus also set this field to a new value upon every call, 
and front would forward to it. (Or front would be the cache 
itself.)
At the moment, I feel that as long as the stashed front element 
isn't too "big" (For some definition of big, I guess.), that 
built-in caching should be fine. But is this acceptable? What's 
the best practice for determining which range member should 
perform what work? (Other than iterating, of course.)


It's a time-space tradeoff. As you say, caching requires 
additional space to store the cached element. On the other hand, 
*not* caching means that you spend unnecessary time computing the 
next element in cases where the range is only partially consumed. 
For example:


```d
import std.range: generate, take;
import std.algorithm: each;
import std.stdio: writeln;

generate!someExpensiveFunction.take(3).each!writeln;
```

Naively, you'd expect that `someExpensiveFunction` would be 
called 3 times--but it is actually called 4 times, because 
`generate` does its work in its constructor and `popFront` 
instead of in `front`.


Re: cannot take address of scope local in safe function

2021-06-13 Thread Paul Backus via Digitalmars-d-learn

On Sunday, 13 June 2021 at 17:49:50 UTC, vit wrote:


Is possible create and use scope output range allocated on 
stack in @safe code?


Example:
```d
//-dip1000

struct OutputRange{
private bool valid = true;
private void* ptr;
int count = 0;

void put(Val)(auto ref scope Val val){
assert(this.valid == true);
this.count += 1;
}


~this()scope pure nothrow @safe @nogc{
this.valid = false;
}


}

void main()@safe pure nothrow @nogc{
import std.algorithm : copy;
import std.range : only;

scope OutputRange or;

only(1, 2, 3, 4).copy();   ///Error: cannot take 
address of `scope` local `or` in `@safe` function `main`

assert(or.count == 4);
}

```


Simply remove the `scope` annotation from `or` and from the 
destructor and it works:


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

Because `OutputRange` is a `struct`, it will still be allocated 
on the stack even if you don't annotate it as `scope`.


Re: Cast class reference to pointer of another class?

2021-06-10 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 10 June 2021 at 21:25:35 UTC, JN wrote:
I have to disagree. I don't see a good reason for this behavior 
and it's just one more thing to trip people. I think it'd be 
better if such thing was done explicit, something like:


```d
Bar b = new Bar();
Foo* f2 = cast(Foo*)b.ptr;
```


Isn't having to write out `cast(Foo*)` already pretty explicit?


Re: best approach to code hierarchical classes ?

2021-06-07 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 8 June 2021 at 01:17:05 UTC, someone wrote:

On Tuesday, 8 June 2021 at 00:54:41 UTC, someone wrote:

Are there alternatives to nested classes for such scenarios ?


Self-reply: I created two files for classComputers and 
classComputer and I replaced the nested-classComputer code 
within classComputers with:


import classComputers;

But it won't go:

Error: undefined identifier `classComputer` in module 
`classComputers`, did you mean class `classComputers`?


Your module and class are both named `classComputers`, with an 
`s` at the end. You should change one of the to have a different 
name so that there's no ambiguity.


Re: Schroedinger's Ranges

2021-06-02 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 3 June 2021 at 00:39:04 UTC, vacuum_tube wrote:
I've been trying to make a struct for CSV parsing and 
manipulating.  The code was as follows:

```
struct CSVData(bool HeaderFromFirstLine)
{
char[][] header = [];
char[][][] rest = [];

this(string filename)
{
auto tmp = File(filename).byLine();

if(HeaderFromFirstLine)
{
this.header = CSVData.parseCSV(tmp.front()).array;
tmp.popFront();
}

this.rest = tmp.map!(e => parseCSV(e)).array;
}
```

[...]

The "testdata" text file looked like this:
```
10,15,Hello world
stuff,,more stuff
```
And the output from running it looked like this:
```
["st", "ff", ",more stuff"]
["stuff", "", "more stuff"]


`File.byLine` overwrites the previous line's data every time it 
reads a new line. If you want to store each line's data for later 
use, you need to use [`byLineCopy`][1] instead.


[1]: https://phobos.dpldocs.info/std.stdio.File.byLineCopy.1.html


Re: foreach: How start a foreach count with specific number?

2021-06-02 Thread Paul Backus via Digitalmars-d-learn

On Wednesday, 2 June 2021 at 15:49:36 UTC, Marcone wrote:
But I don't want it starts with 0, but other number. How can I 
do it?


Easiest way is to just add the starting number:

size_t start = 5;
foreach (n, i; glob("*")) {
print("{} DATA {}".format(n, start + i));
}

You can also use [`std.range.enumerate`][1], which takes the 
number to start with as an optional argument:


foreach (n, i; glob("*").enumerate(5)) {
print("{} DATA {}".format(n, start + i));
}

[1]: https://phobos.dpldocs.info/std.range.enumerate.html


Re: Is there a nicer way to get the first element or typeof(element).init from a range?

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

On Sunday, 30 May 2021 at 12:16:19 UTC, realhet wrote:

Is there a prettier way to do this?

Thanks in advance.


```d
import std.range;

auto getFirst(R)(R range)
if (isInputRange!R)
{
if (range.empty) return ElementType!Range.init;
else return range.front;
}
```


Re: How long does the context of a delegate exist?

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

On Sunday, 30 May 2021 at 09:39:28 UTC, cc wrote:


Is there any way to enforce at compile time that we're not 
accidentally allocating when creating a delegate, other than 
being carefully aware of what variables are referenced inside 
the body?  Something like:

```d
auto dg = delegate {...}
assert(dg.ptr is null, "Oops, we unintentionally allocated on 
GC here, check delegate body!"); // can't static assert

```


`@nogc`. Or check the output of `dmd -vgc` if you want something 
less strict.


Re: is there a way to output formatted text following the locale settings ?

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

On Saturday, 29 May 2021 at 02:26:57 UTC, someone wrote:

```d
   core.stdc.stdio.printf("%'d", intAmount); /// Deprecation: 
format specifier `"%'"` is invalid ... but it works: 1,234,567 
as specified in my locale :)

```


D warns for format specifiers that do not conform to the C99 
standard, and the `'` flag character is not part of C99. GCC 
gives a similar warning with `-Wall -pedantic`:


```
$ gcc -Wall -pedantic -o example example.c
example.c: In function ‘main’:
example.c:5:9: warning: ISO C does not support the ''' printf 
flag [-Wformat=]

  printf("%'d\n", 123456789);
 ^~~
```


Re: Working with ranges

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

On Wednesday, 26 May 2021 at 13:58:56 UTC, Elmar wrote:

This example will not compile:

```
auto starts = arr[0..$].stride(2);
auto ends = arr[1..$].stride(2);
randomNumbers[] = ends[] - starts[];
```

Because `[]` is not defined for the Result range. Is there a 
standard wrapper function which wraps an elementwise `[]` 
operator implementation around a range?


Something like this ought to work:

```d
import std.range: zip;
import std.algorithm: map, copy;

/// calls `fun` with the members of a struct or tuple as arguments
alias apply(alias fun) = args => fun(args.tupleof);

zip(starts, ends)
.map!(apply!((start, end) => end - start))
.copy(randomNumbers[]);
```

In general, array operators like `[]` only work with arrays. The 
Result ranges you get from `stride` are not arrays, so to work 
with them, you need to use range algorithms like the ones in 
`std.range` and `std.algorithm`.


(Some ranges actually do support `[]`, but it is never 
guaranteed. You can check for such support with 
[`std.range.primitives.hasSlicing`][1].)


If you would prefer a more convenient syntax for working with 
things like strided arrays, I recommend giving [libmir][2] a 
look. It's a high-quality collection of D libraries for numerical 
and scientific computing.


[1]: 
https://phobos.dpldocs.info/std.range.primitives.hasSlicing.html

[2]: https://www.libmir.org/


Re: is there a way to output formatted text following the locale settings ?

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

On Wednesday, 26 May 2021 at 00:18:29 UTC, someone wrote:


Thanks for your reply, but no, let me clarify:

I do not want 123\`456\`789 or whatever else custom delimiter.

I just want to output what is already set in the LC_ALL locale 
environment variable which in my case results in 123,456,789 
which of course I can mimic hard-coding it, but the idea is ... 
quite the opposite.


In that case, you will want to use the C library functions from 
[`core.stdc.stdio`][1].


[1]: https://dlang.org/phobos/core_stdc_stdio.html


Re: where do I find the complete phobos function list names ?

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

On Tuesday, 25 May 2021 at 22:05:16 UTC, someone wrote:
I was unsuccessfully searching the site for them in the form of 
a master index to begin with.


I need them, in plain text, in order to add them to a VIM 
custom syntax highlight plugin I already made which I am 
already using but is lacking phobos support.


Can anyone point me to the right place please ?


There is no global index in the online documentation; the best 
you can get is an index of each module.


If you really want this, your best bet is probably to run a 
source-code indexer like `ctags` on the Phobos source tree, and 
do some scripting to transform the results into something usable 
in your Vim plugin.


Re: is there a way to output formatted text following the locale settings ?

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

On Tuesday, 25 May 2021 at 22:37:25 UTC, someone wrote:
   writeln(format("%`d", intAmount)); /// please, note the 
backtick instead of ,




From [the documentation of `formattedWrite`][1]:


Separator

   Inserts the separator symbols ',' every X digits, from right 
to left, into numeric values to increase readability. The 
fractional part of floating point values inserts the separator 
from left to right. Entering an integer after the ',' allows to 
specify X. If a '*' is placed after the ',' then X is specified 
by an additional parameter to the format function. Adding a '?' 
after the ',' or X specifier allows to specify the separator 
character as an additional parameter.


So the syntax you want is:

writeln(format("%,?d", '`', intAmount));

[1]: 
https://forum.dlang.org/post/vedxtnkolsmdlyfvm...@forum.dlang.org


Re: General rule when not to write ;

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

On Wednesday, 19 May 2021 at 13:46:55 UTC, Jesse Phillips wrote:
The ; is used to end a statement, but I don't know how to 
define that and distinguish it from an expression.


An expression has a value. A statement doesn't.

You can add a `;` at the end of an expression to make a statement 
from it. When you do, the value of that expression is discarded.


Re: dual-context deprecation

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

On Monday, 17 May 2021 at 13:47:28 UTC, jmh530 wrote:
The code below (simplified from my actual problem) generates a 
warning that member function b "requires a dual-context, which 
is deprecated".


However when I look at the list of deprecated features [1], I'm 
not seeing which one this is referring to. Is it a valid 
deprecation?


See this issue for context:

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


Re: property functions

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

On Sunday, 16 May 2021 at 15:47:55 UTC, Adam D. Ruppe wrote:

On Sunday, 16 May 2021 at 15:12:25 UTC, Nick wrote:

Is this warning still valid?


The @property thing doesn't do much. All it does is change 
typeof(a.prop) from function over to the return value of the 
function. (Which actually makes it required for the range empty 
and front things!)


It's not required:

struct Example
{
int front() { return 42; }
bool empty() { return false; }
void popFront() {}
}

import std.range;
static assert(isInputRange!Example);


Re: Filter for opDispatch?

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

On Sunday, 16 May 2021 at 00:04:39 UTC, frame wrote:

On Saturday, 15 May 2021 at 23:59:49 UTC, Paul Backus wrote:


Does it work if you remove `protected` from the derived class?


Yes, but why should the derived class not have access to it?


Possibly because you're accessing it from an `opDispatch` method 
defined in the base class. I'm not 100% sure how `protected` 
works in that case, but just from reading the spec it seems like 
it could be an issue.


Re: Filter for opDispatch?

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

On Saturday, 15 May 2021 at 23:41:19 UTC, frame wrote:

On Friday, 14 May 2021 at 23:02:22 UTC, frame wrote:

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


Now I run into the error

"class Foo member s is not accessible"
"template instance Base.opDispatch!("s", int, Foo) error 
instantiating"


and I have no clue why.


Does it work if you remove `protected` from the derived class?


Re: Recommendations on avoiding range pipeline type hell

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

On Saturday, 15 May 2021 at 13:46:57 UTC, Chris Piker wrote:


Every time I slightly change the inputs to range2, then a 
function that operates on *range3* output types blows up with a 
helpful message similar to:

[snip]


If you post your code (or at least a self-contained subset of it) 
someone can probably help you figure out where you're running 
into trouble. The error messages by themselves do not provide 
enough information--all I can say from them is, "you must be 
doing something wrong."


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: 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: 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.


Re: String "dequote" in phobos?

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

On Thursday, 13 May 2021 at 14:10:08 UTC, cc wrote:
Does something to dequote (unquote? or what would you call it?) 
a string exist in the standard library?  I didn't see one in 
std.string, just wondering before reinventing the wheel.


Something like:
```d
assert(dequote(`"foo"`) == "foo");
assert(dequote(`'foo'`) == "foo");
assert(dequote(`"foo's"`) == "foo's");
assert(dequote(`'foo "bar"'`) == `foo "bar"`);
assert(dequote(`"fo\"o"`) == `fo"o`);
dequote(`"fo"o"`); // bad quoting, maybe throw an exception 
here or something?

```


I don't think there's anything like this in the standard library. 
If you write your own, consider publishing it on code.dlang.org.


Re: Improved diagnostics for mismatched template constraints

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

On Wednesday, 12 May 2021 at 12:54:24 UTC, Per Nordlöw wrote:
I'm certain I've seen dmd be able to diagnose which 
sub-expression of `isDumb` and `isInputRange` that evaluated to 
false. Did I just dream this or is there a way? If there is a 
way does this improvement still reside in a dmd PR?


It will diagnose which sub-expression of the template constraint 
failed, but it will not descend into templates. So, if you inline 
the constraint:


void f(Args...)(Args args)
if (Args.length == 2 && Args.length == 3)
{
}

You get the following error message:

onlineapp.d(18): Error: template `onlineapp.f` cannot deduce 
function from argument types `!()(int, int)`, candidates are:

onlineapp.d(6):`f(Args...)(Args args)`
  with `Args = (int, int)`
  must satisfy the following constraint:
`   Args.length == 3`

It might be possible to extend this to work with simple `enum` 
templates like `isDumb` and `isInputRange`, whose bodies consist 
of a single boolean expression, although I expect you would run 
into many of the same difficulties that caused past attempts at 
fixing issue 1807 [1] to fail.


[1] https://issues.dlang.org/show_bug.cgi?id=1807


Re: Testing for object property supporting "<" comparison

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

On Tuesday, 11 May 2021 at 19:42:34 UTC, Chris Piker wrote:
My problem is that I don't know how to specify that properties 
must be comparable via "<".  I took a look at the Traits 
module, but there are more things that are comparable then 
Arithmetic or Floating point types.  Also, since the compiler 
will catch objects that don't have these properties, maybe an 
enum check is not worth implementing.


std.traits.isOrderingComparable

https://phobos.dpldocs.info/std.traits.isOrderingComparable.html


Re: can I reuse statements?

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

On Monday, 10 May 2021 at 21:01:53 UTC, Jack wrote:
mixin template seems to allow only declarations so if I put a 
if or case-statement in the body it doesn't work. I'd like to 
make something like this work:


```d
switch(code) {
case X, Y: // that specific case repeats alot 
in the code in different procedures

  mixin handleXY;

default:
 }
```

then

```d
mixin template foo()
{
auto c = arr[i]; // arr and i are available at 
switch(code)'s scope

auto m = Message(...);

switch(code)
{
case BAA_A:  c.doSomething(m); break;
case BAA_B: c.doSomething(m); break;
default: assert(0, "error");
}
}
```


You can do it with a string mixin:

```d
// Note: q{ ... } creates a "token string", a special kind of 
string literal

// that's used for code strings.
// See https://dlang.org/spec/lex.html#token_strings
enum string handleXY = q{
auto c = arr[i]; // arr and i are available at 
switch(code)'s scope

auto m = Message(...);

switch(code)
{
case BAA_A:  c.doSomething(m); break;
case BAA_B: c.doSomething(m); break;
default: assert(0, "error");
}
};
```

Usage:

```d
case X, Y:
mixin(handleXY);
```


Re: Remove own post from forum

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

On Monday, 10 May 2021 at 03:36:02 UTC, Виталий Фадеев wrote:
I have missformated post in thread: 
https://forum.dlang.org/thread/kwpqyzwgczdpzgsvo...@forum.dlang.org


Say, please,
how to remove own post from this forum ?


There's currently no way to remove or edit an existing post.


Re: Fixed length vs dynamic arrays

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

On Friday, 7 May 2021 at 22:21:09 UTC, Alain De Vos wrote:
Following code compiles without errors, only errors during 
runtime

```
int main(){
int[2]  a=new int[2];
int[]   b=new int[0];
int[2]  c=[1,2];
a=a~3;
b=b~3;
c=c~3;
c=b;
b=a;
a=c;
return 0;
}
```


The result of a `~` expression is always a newly-allocated 
dynamic array on the GC heap. [1]


When you assign a dynamic array to a static array, the elements 
from the dynamic array on the right-hand side are copied into the 
static array on the left-hand side. [2] So the statement


a = a ~ 3;

...is essentially shorthand for

auto _tmp = a ~ 3;
foreach (i; 0 .. _tmp.length)
a[i] = _tmp[i];

Because `a` has length `2` and `a ~ 3` has length `3`, this 
results in a `RangeViolation` at runtime when attempting to copy 
`_tmp[2]` into `a[2]`.


[1] https://dlang.org/spec/expression.html#cat_expressions
[2] https://dlang.org/spec/arrays.html#array-copying


Re: Fixed length vs dynamic arrays

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

On Thursday, 6 May 2021 at 22:35:38 UTC, Alain De Vos wrote:
Is a fixed length array created on the heap and a dynamic array 
on the stack ? And is this important with relation to the 
garbage collector ?


Static arrays (`T[N]`) are value types, like structs. If you 
declare them as local variables, they're allocated on the stack.


Dynamic arrays (`T[]`), also known as slices, are reference 
types, consisting of a pointer and a length. If you declare them 
as local variables, the pointer and length will be stored on the 
stack. The data they point to, however, may be stored on either 
the stack or the heap.


For more information about slices in D, I highly recommend this 
excellent article by Steven Schveighoffer:


https://dlang.org/articles/d-array-article.html


Re: How suppress DMC File name and path showing after compile?

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

On Thursday, 6 May 2021 at 15:55:07 UTC, Marcone wrote:

dmc Programa.cpp && Programa.exe

is showing this, but I want show only "Hello World!"


C:\Users\Usuario\Arquivos\Estudando\C\Programa.cpp: <--- I 
don't want DMC show this.

Hello World!
[Finished in 0.2s]


Redirect the compiler's output to NUL:

https://docs.microsoft.com/en-US/troubleshoot/cpp/redirecting-error-command-prompt


Re: How to check for combinations of versions

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

On Wednesday, 5 May 2021 at 15:03:16 UTC, Blatnik wrote:
Currently I resort to something like this, but I'm curious if 
there's a nicer way to do it.


```D
version (Version_A) {
  enum Cool_Feature_Supported = true;
} else version (Version_B) {
  enum Cool_Feature_Supported = true;
} else {
  enum Cool_Feature_Supported = false;
}
```


This is the officially-recommended way to do it. D's `version` 
system is deliberately restricted, in order to avoid the 
"`#ifdef` hell" that often plagues C and C++ projects.


However, if you really want something more expressive, and are 
confident in your ability to use the extra power responsibly, it 
is possible to work around these limitations:


template hasVersion(string identifier) {
mixin(
"version(", identifier, ") enum hasVersion = true;",
"else enum hasVersion = false;"
);
}

// Usage
static if (hasVersion!"Version_A" || hasVersion!"Version_B") {
enum Cool_Feature_Supported = true;
} else {
enum Cool_Feature_Supported = false;
}


Re: Should this always work?

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

On Tuesday, 4 May 2021 at 10:21:42 UTC, Ola Fosheim Grøstad wrote:
On Saturday, 1 May 2021 at 16:06:05 UTC, Steven Schveighoffer 
wrote:
An interface cast involves a thunk (constant pointer 
adjustment) to get to the interface/object


Yes, but it isn't a https://en.wikipedia.org/wiki/Thunk ?


The article literally gives this exact use-case as an example:

https://en.wikipedia.org/wiki/Thunk#Object-oriented_programming


Re: Struct initialization extra comma - should it compile

2021-04-25 Thread Paul Backus via Digitalmars-d-learn

On Sunday, 25 April 2021 at 13:39:54 UTC, JN wrote:

struct Foo
{
int x, y, z;
}

void main()
{
 Foo bar = Foo(1,);
}

This compiles without syntax errors, is this expected?


Yes, D allows trailing commas in argument lists.


Re: get type name from current class at compile time?

2021-04-24 Thread Paul Backus via Digitalmars-d-learn

On Sunday, 25 April 2021 at 02:26:00 UTC, Jack wrote:


doesn't this work when called from member in a derived class?

```d

class A
{
void doSomething(this T)()
{
writefln("name = [%s]", __traits(identifier, T));
}
}

class K : A
{
void baa()
{
doSomething();
}
}
```

result in the error:

Error: template `foo.A.doSomething` cannot deduce function from 
argument types `!()()`,


It works if you call it with `this.doSomething()`: 
https://run.dlang.io/is/eIygNG


Re: When should I use SortedRange.release?

2021-04-23 Thread Paul Backus via Digitalmars-d-learn

On Friday, 23 April 2021 at 17:35:13 UTC, Bastiaan Veelo wrote:

What happens when a range is released?
What happens if a range is not released?
What happens if a range is released more than once?

And what does "controlled" imply here? In what way has 
`SortedRange` control over the underlaying data? What can I do 
and what can't I do to the underlying data as long as 
`SortedRange` has control?


I had to look at the source to figure this out. Fortunately, the 
source is pretty simple:


https://phobos.dpldocs.info/source/std.range.d.html#L10833

Turns out, it just calls `core.lifetime.move`. So I guess when 
the docs say "control", what they are really talking about is 
ownership.


Practically speaking, this means that in generic code, we should 
avoid using a `SortedRange` after calling `release`, since we do 
not know whether the range is owning or non-owning w.r.t. the 
underlying data.


Re: Parallel foreach iteration with Associative Arrays

2021-04-16 Thread Paul Backus via Digitalmars-d-learn

On Saturday, 17 April 2021 at 01:57:34 UTC, Kirill wrote:
I'd like to iterate over an associative array and output it's 
key and value using parallel from std.parallelism.


But I get an error message: ParallelForeach!(int[string]) error 
instantiating.


My code:

auto example = ["apples": 100, "orange": 250, "banana": 175];
foreach(key, value; parallel(example)) { writeln(key, ": ", 
value); }


What am I doing wrong?

Thanks in advance.


`parallel` requires a range [1], and an associative array is not 
a range. To get a range of an AA's keys and values, you can use 
the method `.byKeyValue`:


foreach (pair; parallel(example.byKeyValue)) {
writeln(pair.key, ": ", pair.value);
}

If you're confused about what a "range" is, the short answer is 
that it's kind of like an iterator. For the long answer, check 
out Andrei Alexandrescu's article "On Iteration" [2], or the 
"Ranges" chapter of Ali Çehreli's "Programming in D" [3].


[1] 
https://phobos.dpldocs.info/std.parallelism.TaskPool.parallel.2.html

[2] https://www.informit.com/articles/printerfriendly/1407357
[3] http://ddili.org/ders/d.en/ranges.html


Re: AliasSeq different from just using the symbol name(s)?

2021-04-15 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 15 April 2021 at 19:38:04 UTC, z wrote:
I understand that it won't be possible to pinpoint the cause 
without a reduced test case, but :


```D
int[] a,b,c,d,e;
void templatef(args...){/*...*/}
//...
auto seq = AliasSeq!(b,c,d);
templatef!(a,seq,e);
templatef!(a,b,c,d,e); //am i being mistaken for thinking these 
two template calls should be equivalent in behavior?

```
And if not, does it mean that the problem i encountered is a 
possible bug?


They're not *exactly* the same. When you write

auto seq = AliasSeq!(a, b, c);

...you are declaring a sequence of three *new* array variables 
[1] and initializing them with copies of the original arrays. 
It's as though you'd written:


auto seq_a = a;
auto seq_b = b;
auto seq_c = c;
alias seq = AliasSeq!(a, b, c);

If you want to refer directly to the original variables, you need 
to create your sequence with `alias` instead of `auto`:


alias seq = AliasSeq!(a, b, c);

[1] 
https://dlang.org/articles/ctarguments.html#type-seq-instantiation


Re: AliasSeq different from just using the symbol name(s)?

2021-04-15 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 15 April 2021 at 18:43:29 UTC, z wrote:
To see what i mean : https://run.dlang.io/is/VXDRL4 (i could 
not manage to trigger it here however.)


Without an example that shows the actual problem you encountered, 
it will be almost impossible for anyone to help you figure out 
what is causing it.


Since you were not able to trigger it, it seems likely that the 
problem is related to something other than the AliasSeq which you 
have left out of the example.


Re: What are virtual functions?

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

On Wednesday, 14 April 2021 at 13:43:20 UTC, Berni44 wrote:
I'm asking, because I'm currently writing new docs for 
`std.format`. The [current (stable) docs of 
`formatValue`](https://dlang.org/phobos/std_format.html#formatValue) tell, that some `toString` versions are discouraged, but not for virtual functions. I would like to understand the reason for that, so I can put that properly into the new docs. The reasons, why it's better to not use these versions in normal functions is explained in the [changelog](https://dlang.org/changelog/2.079.0.html#toString). But I miss the reason, why virtual functions should still use them; probably, because I did not understand, what that is.


The recommended `toString` versions are templates, but virtual 
functions can't be templates (because you can't have a function 
pointer that points to a template). So, the non-template versions 
are still considered acceptable for cases when `toString` has to 
be virtual--i.e., when you're overriding `Object.toString`.


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

2021-04-08 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 8 April 2021 at 22:27:38 UTC, Alain De Vos wrote:

So which concrete types do you give for the two auto's.


The first `auto` is the return type of `to!(ubyte[])`--so, it's 
`ubyte[]`.


The second `auto` is the return type of `map`. If you look at the 
documentation [2], you'll see that it doesn't give a concrete 
type for the return value; it just says that `map` returns "an 
input range." That's because the concrete type is a so-called 
"Voldemort type" [1]--a type whose name is private to the 
function, and can't be used externally.


Why use such a type? Because it gives the authors of `map` the 
freedom to change the concrete type without breaking code that 
uses `map`, as long as the type they change it to still supports 
the input range interface.


[1] https://wiki.dlang.org/Voldemort_types
[2] 
https://phobos.dpldocs.info/std.algorithm.iteration.map.map.html


Re: Is this bug ? format %(%)

2021-04-07 Thread Paul Backus via Digitalmars-d-learn

On Wednesday, 7 April 2021 at 17:04:56 UTC, novice2 wrote:

On Wednesday, 7 April 2021 at 13:43:18 UTC, Paul Backus wrote:

So, you should change your code to

writefln("%-(%s, %)", s);


sorry i dont read docs so carefully
thanks


It's not your fault--this is a pretty obscure feature, and it's 
not documented very well. Even after you've found the correct 
page in the documentation (the page for `formattedWrite` [1]), 
you have to scroll down past multiple examples to find the text 
that explains it.


[1] https://dlang.org/phobos/std_format.html#formattedWrite


Re: Is this bug ? format %(%)

2021-04-07 Thread Paul Backus via Digitalmars-d-learn

On Wednesday, 7 April 2021 at 13:31:59 UTC, novice3 wrote:

there is extra quotes, wich not present in firmat specifier.
is this bug, or i should change something in my code?


This is actually an intentional feature (albeit kind of a stupid 
one). From the documentation:


Inside a compound format specifier, strings and characters are 
escaped automatically. To avoid this behavior, add '-' flag to 
"%(".


So, you should change your code to

writefln("%-(%s, %)", s);


Re: templated overload of opAssign

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

On Saturday, 3 April 2021 at 13:46:17 UTC, kdevel wrote:

Why does this code

[...]
```d
   ec.opAssign (bar (1)); // okay
//   ec = bar (1); // Error: expression bar(1) is void and has 
no value

```
[...]

compile with the abovementioned error?


This is a compiler bug. You're not allowed to have a `void` 
expression on the right-hand side of an assignment, but you are 
allowed to pass a `void` expression to a function that takes a 
`lazy` parameter. Currently, the compiler checks for errors 
before rewriting the assignment to an `opAssign` call, which 
means that it will issue an error even in cases where the rewrite 
would have worked. What it should do instead is rewrite the 
assignment first, and *then* check for errors.


Re: what exactly is string length?

2021-04-02 Thread Paul Backus via Digitalmars-d-learn

On Friday, 2 April 2021 at 05:39:26 UTC, mw wrote:

Finally, I'm using:

https://run.dlang.io/is/651lT6

string t = text("head-", s[].until('\0').array, "-tail");


FYI, you don't need the call to `.array` there--`text` accepts 
input ranges.


Re: Best way to make a template function conditionally @trusted

2021-04-01 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 1 April 2021 at 22:35:01 UTC, tsbockman wrote:
Suppose I have a templated struct member function for which I 
can compute at compile-time when the function is memory safe, 
and when it is not. But, the compiler cannot correctly 
determine this automatically.


What is the best way to express this in code? Other than 
straight-up duplicating the implementation, the only answer 
I've come up with so far is to create a `private @system` 
implementation function, and then separate `public @system` and 
`public @trusted` wrappers with appropriate template 
constraints.


Is there a better way?


Here's a technique I've used:

// infer function attributes
auto func(...)
{
// do your compile-time memory-safety check here
enum bool shouldBeSystem = ...;

static if (shouldBeSystem) {
// force inference of @system
cast(void) () @system {}();
}

() @trusted {
// rest of code goes here
}();
}

As long as the compile-time check is correct, this is sound: the 
@trusted lambda can be called from @safe code if and only if 
`shouldBeSystem == false`.


Re: How to declare "type of function, passed as an argument, which should have it's type inferred"? (or if D had an "any" type)

2021-03-29 Thread Paul Backus via Digitalmars-d-learn

On Monday, 29 March 2021 at 16:20:59 UTC, Ali Çehreli wrote:

auto myFunc(F)(string name, F func)
{
  // This condition could be a template constraint but they 
don't

  // support error messages.
  static assert (is (Parameters!func == AliasSeq!(string)),
 "'func' must be a callable that takes 
'string'.");

  return func(name);
}

void main() {
  // One trouble with this "solution" is that for the compiler 
to

  // know the return type of the lambda, the parameter must be
  // declared as 'string' (in this case).
  writeln(myFunc("foo", (string a) => a ~ '.'));
}

Ali


Alternatively:

auto myFunc(F)(string name, F func)
{
  static assert (__traits(compiles, (string s) => func(s)),
 "'func' must be a callable that takes 
'string'.");

  return func(name);
}

void main() {
  // No need to write out the argument type
  writeln(myFunc("foo", a => a ~ '.'));
}

You can generalize this into a helper template:

enum bool isCallableWith(alias fun, ArgTypes...) =
  __traits(compiles, (ArgTypes args) => fun(args));

Usage:

static assert(isCallableWith!(func, string));


Re: Manually check struct invariants

2021-03-23 Thread Paul Backus via Digitalmars-d-learn

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 the first AssignExpression is a pointer to a struct instance 
for which a Struct Invariant exists, the Struct Invariant must 
hold.


So, you would write `assert()` for a struct instance.


Re: How do I check if this field is of this template struct?

2021-03-19 Thread Paul Backus via Digitalmars-d-learn

On Friday, 19 March 2021 at 07:14:46 UTC, Jack wrote:
give below template struct, how can I list the members x, y and 
z? I've tried something with OriginalType and TemplateOf but no 
luck... it seems if I do foo!"str1" the "str1" became "part of 
type"? give .stringof from typeof(__traits(getMember, foo, 
field)) I thought the type would be foo!string or something.


You want std.traits.isInstanceOf:

static if(!isType!m && isInstanceOf!(foo, typeof(m)))


Re: How to delete dynamic array ?

2021-03-18 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 18 March 2021 at 18:48:26 UTC, Vinod K Chandran 
wrote:
On Wednesday, 17 March 2021 at 14:30:26 UTC, Guillaume Piolat 
wrote:


I made this article to clear up that point: 
https://p0nce.github.io/d-idioms/#Slices-.capacity,-the-mysterious-property


Sorry for this off-topic question. I am amazed with eye catchy 
look of that d-idioms page. I want to create a page like it for 
my ongoing project. I think it's a good form of documentation. 
How do i create one like it ?


The source code is here: https://github.com/p0nce/d-idioms/


Re: Forbidden file names?

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

On Sunday, 14 March 2021 at 20:47:00 UTC, Brian wrote:

Hello --

Apologies if this is answered somewhere in the documentation.
I was trying out the sample code on the dlang.org home page.

When I got to the "Sort an Array at Compile-Time" example, I 
saved it on my machine as sort.d. When I tried to build sort.d, 
the compile failed. But when I renamed sort.d to anything else 
(e.g., array.d), the compilation was fine.

[...]

/home/brian/d $ dmd sort.d
sort.d(9): Error: function expected before `()`, not `module 
sort` of type `void`


This is the error you get when you try to call a function that 
has the same name as the current module. The best way to fix it 
is to rename the module, but if you can't, you can use an alias 
to disambiguate:


alias sort = std.algorithm.sort;


Re: Two functions with different args. Taking address of the one

2021-03-11 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 11 March 2021 at 12:56:34 UTC, Виталий Фадеев wrote:


This will generate lambda:
  __processMouseKey = (ref MouseKeyEvent event) { 
process(event); };


two calls:
  call labnda;
call process;

What right way to call function directly with selecting one of 
two ?


Something like this:

template Overloads(alias symbol)
{
static if (__traits(compiles, __traits(parent, symbol)))
alias Overloads = __traits(getOverloads,
__traits(parent, symbol),
__traits(identifier, symbol)
);
else
alias Overloads = symbol;
}

auto getOverloadFor(alias fun, T)()
{
foreach (overload; Overloads!fun)
static if (__traits(compiles, (T arg) { 
overload(arg); }))

return 
}

Usage:

__processMouseKey = getOverloadFor!(process, MouseKeyEvent);


Re: Two functions with different args. Taking address of the one

2021-03-11 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 11 March 2021 at 12:26:07 UTC, Виталий Фадеев wrote:

Have:
void process( ref MouseKeyEvent event )
{
   ...
}

void process( ref MouseMoveEvent event )
{
   ...
}

Want:
_processMouseKey  =  // <-- not works
_processMouseMove =  // <-- not works

What is correct way to get address of function with specific 
argument ?


You can use __traits(getOverloads, process) plus some 
metaprogramming to get the address of a specific overload. But 
IMO the easiest way is to use lambdas:


__processMouseKey = (ref MouseKeyEvent event) { 
process(event); };
__processMouseMove = (ref MouseMoveEvent event) { 
process(event); };


Re: Very confusing error message when calling a class method from an invariant

2021-03-09 Thread Paul Backus via Digitalmars-d-learn

On Wednesday, 10 March 2021 at 03:39:15 UTC, Meta wrote:

class Human {
static immutable MAX_AGE = 122;

bool alive = true;
int age = 0;
//Error: mutable method onlineapp.Human.checkAge is not 
callable using a const object

invariant(checkAge());

[...]


What the hell does this even mean, and where does it come from? 
Adding `inout` to `checkAge` actually does cause it to compile 
and run too. WTF?


From the language spec [1]:


The invariant is in the form of a const member function.


So, inside the invariant, the object is treated as const, which 
means you can't modify it and can only call const methods.


[1] https://dlang.org/spec/class.html#invariants


Re: How to get number of parameters in lambda?

2021-03-09 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 9 March 2021 at 14:22:44 UTC, Andrey Zherikov wrote:
In case of function template this is possible but the original 
question was about lambdas. There is no way that lambda can 
change number of parameters during instantiation, am I right?


Yes, you're correct. The issue is that the compiler currently 
can't tell the difference between a template lambda and a 
non-lambda function template.


This could be changed, but having a language feature that works 
for lambdas but not for named functions is probably not such a 
great idea.


Re: How to get number of parameters in lambda?

2021-03-08 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 9 March 2021 at 02:24:07 UTC, Andrey Zherikov wrote:

On Tuesday, 9 March 2021 at 01:38:52 UTC, Paul Backus wrote:

What's the larger problem you're trying to solve here?


Just to make things simple: I have a 'caller' that calls some 
function provided as an alias template argument and I want to 
support different number of arguments (purpose is to provide 
additional parameters if 'f' supports):


void caller(alias f)()
{
static if(/*one parameter*/)
f(1);
else static if(/*two parameters*/)
f(1, 2);
}


If you know the arguments you want to call the function with, the 
easiest way to check if you can do it is with __traits(compiles):


static if (__traits(compiles, f(1)))
f(1);
else static if (__traits(compiles, f(1, 2)))
f(2);

I understand that types are not available since they are 
undefined until instantiation. But why can't I get argument 
count? Is it possible that it can change during instantiation?


Yes, it's possible. For example:

template fun(T) {
static if (is(T == int)) {
void fun(T a, T b) {
/* ... */
}
} else {
void fun(T a) {
/* ... */
}
}
}

Of course, code like this is rare in practice, but because it's 
technically possible, the compiler can't assume *anything* about 
a template function before it's been instantiated.


Re: How to get number of parameters in lambda?

2021-03-08 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 9 March 2021 at 03:08:14 UTC, Paul Backus wrote:

else static if (__traits(compiles, f(1, 2)))
f(2);


Typo; this should of course say `f(1, 2)`.


Re: Compiler version "dirty"

2021-03-08 Thread Paul Backus via Digitalmars-d-learn

On Monday, 8 March 2021 at 22:29:58 UTC, Q. Schroll wrote:

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.


It means someone made a mistake when preparing the release. 
Probably harmless.


Re: How to get number of parameters in lambda?

2021-03-08 Thread Paul Backus via Digitalmars-d-learn

On Monday, 8 March 2021 at 23:07:18 UTC, Andrey Zherikov wrote:
What should I use to get number of lambda arguments? Like get 2 
for "(arg1, arg2) {}". std.traits.Parameters doesn't work 
because isCallable returns false.


alias f = (arg1, arg2) {};

writeln(isCallable!f);   // prints false


Specifying argument types works, e.g. isCallable returns true 
for (int arg1, int arg2) {}, but I'm looking for a solution for 
type-less lambdas.


Typeless lambdas are templates, and there's no way to examine the 
arguments of a template function without first instantiating it, 
so I'm afraid you're out of luck.


What's the larger problem you're trying to solve here?


Re: D's Continous Changing

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

On Friday, 5 March 2021 at 03:32:35 UTC, harakim wrote:
That was pretty sweet. However, it kind of goes to the point of 
my post. A one-revision difference means the documentation is 
not accurate for my compiler.


I'm not saying the language shouldn't evolve, I'm just saying 
it might make sense to keep compatibility changes to every 6 
months or a year. Then you could keep the old documentation 
around for the old version, and create new documentation for 
the new version and no matter which version someone is using 
they would have documentation (within limits.)


The website is *supposed* to keep documentation for old versions 
around, and allow you to select them using the drop-down menu at 
the top-right:


https://i.imgur.com/ICu9Z7a.png

However, it looks like this feature is currently broken, since 
the archived documentation stops at version 2.081. I've filed an 
issue in the appropriate repository [1], so hopefully that will 
be fixed soon.


[1] https://github.com/dlang/docarchives.dlang.io/issues/1


Re: How do I check if a type is assignable to null at compile time?

2021-02-25 Thread Paul Backus via Digitalmars-d-learn

On Friday, 26 February 2021 at 05:25:14 UTC, Jack wrote:

I started with:

enum isAssignableNull(T) = is(T : Object) || isPointer(T);

but how do I cover all cases?


Something like this should work:

enum isAssignableNull(T) = __traits(compiles, (T t) => t = null);


Re: Is there an easy way to mimic generics with an accept method of a visitor pattern?

2021-02-18 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 18 February 2021 at 14:51:09 UTC, vitamin wrote:
On Thursday, 18 February 2021 at 14:43:43 UTC, Paul Backus 
wrote:


I don't see what this buys you compared to sticking with one 
or the other, but you are correct that it is technically 
possible.


It infer function atributes (pure, nothrow @nogc @safe) for 
"visitor" and let you use classes and inheritence.
With standard visitor pattern you need PureVisitor. 
NothrowVisitor, PureNothrowVisitor...


It seems to me like you would also get those benefits by just 
using a discriminated union, without the classes.


Re: Struct delegate access corruption

2021-02-18 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 18 February 2021 at 11:00:50 UTC, vitamin wrote:


opPostMove 
https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1014.md can solve this problem but it isn't implemented;


IIRC opPostMove has been abandoned for the same reason postblits 
were abandoned (issues with type-checking and const/immutable).


Walter has a draft DIP that introduces move constructors [1], but 
it is currently on hold because of a new rule requiring that DIPs 
authored by a Language Maintainer (i.e., Walter or Atila) have a 
third-party "champion" to take them through the DIP process. So, 
if anyone wants to "adopt" this DIP, it would be a great service 
to the community.


[1] https://github.com/dlang/DIPs/pull/182


Re: Is there an easy way to mimic generics with an accept method of a visitor pattern?

2021-02-18 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 18 February 2021 at 14:26:37 UTC, vitamin wrote:


Or combination of discriminate uninons and classes:

/+dub.sdl:
dependency "sumtype" version="~>0.10.0"
+/
import std.stdio;

import sumtype;

alias Expression = SumType!(
ExprValue,
ExprBinary,
ExprUnary
);

class Expr{
abstract Expression expression()pure nothrow @safe @nogc;

}


I don't see what this buys you compared to sticking with one or 
the other, but you are correct that it is technically possible.


Re: Is there an easy way to mimic generics with an accept method of a visitor pattern?

2021-02-18 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 18 February 2021 at 11:14:05 UTC, Mina wrote:
I'm following along with the crafting interpreters book 
(https://craftinginterpreters.com) and it goes into 
implementing a visitor pattern that returns generic types, so 
implementing it in D came down to the accept method causing 
undefined symbol error that goes away when changing it to 
returning a concrete type, so here's what I've got working 
(https://github.com/MKamelll/dlox/blob/main/source/loxast.d) 
and here's the book's implementation 
(https://github.com/munificent/craftinginterpreters/blob/master/java/com/craftinginterpreters/lox/Expr.java).



Thanks.


In D, because generics are implemented using templates 
("monomorphization"), generic methods can't be virtual and can't 
be overridden in child classes. As you've discovered, that means 
`accept` has to work entirely with concrete types rather than 
generic ones.


One way to solve this (which is used in the D compiler's source 
code) is to have both `accept` and `visit` return `void` and put 
the result inside the visitor object as a member variable. For 
example:


interface Visitor
{
void visit(Expr.Literal expr);
// etc.
}

class AstPrinter : Visitor
{
string result;

override void visit(Expr.Literal expr)
{
if (!expr.literal.hasValue) result =  "nil";
else result = lexLiteralStr(expr.literal);
}

// etc.

string print(Expr expr)
{
expr.accept(this);
return result;
}
}

Another possibility is to use discriminated unions and tag-based 
dispatch (i.e., switch statements) instead of classes and virtual 
method dispatch. This would make it a bit harder to follow the 
book, but might be a better learning experience if you're up for 
a challenge.


Re: Struct delegate access corruption

2021-02-17 Thread Paul Backus via Digitalmars-d-learn

On Wednesday, 17 February 2021 at 19:42:00 UTC, tsbockman wrote:
On Wednesday, 17 February 2021 at 17:45:01 UTC, H. S. Teoh 
wrote:

I.e., the following is NOT a good idea:

struct S {
void delegate() member;
this() {
member = 
}
private void impl();
}

because a pointer to S is stored inside S itself, so as soon 
as S gets copied or moved, the delegate context pointer is no 
longer valid (or else points to a different copy of the struct 
than the one it will be called from).


A copy constructor and opAssign can be used to update pointers 
that are relative to :

https://dlang.org/spec/struct.html#struct-copy-constructor


Unfortunately this is not enough, because the compiler is free to 
implicitly move struct instances in memory any time it wants to. 
See the bug report below for more details:


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

Until D gets move constructors, structs with interior pointers 
should be avoided.


Re: is it posible to compile individual module separately?

2021-02-16 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 16 February 2021 at 17:49:42 UTC, Anonymouse wrote:

On Tuesday, 16 February 2021 at 17:26:06 UTC, Paul Backus wrote:

On Tuesday, 16 February 2021 at 17:15:25 UTC, Anonymouse wrote:
You can also use dub build --build-mode=singleFile, and it 
will compile one file at a time. It'll be slow but slow is 
better than OOM.


singleFile is for single-file packages [1]. The option you're 
thinking of is --build-mode=separate.


[1] https://dub.pm/advanced_usage.html#single-file


No, I do mean singleFile.

$ dub build --build-mode=singleFile --force
[...]


I stand corrected. Shouldn't have trusted the documentation so 
much, I guess.


Re: is it posible to compile individual module separately?

2021-02-16 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 16 February 2021 at 17:15:25 UTC, Anonymouse wrote:
You can also use dub build --build-mode=singleFile, and it will 
compile one file at a time. It'll be slow but slow is better 
than OOM.


singleFile is for single-file packages [1]. The option you're 
thinking of is --build-mode=separate.


[1] https://dub.pm/advanced_usage.html#single-file


Re: Fastest way to "ignore" elements from array without removal

2021-02-16 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 16 February 2021 at 09:08:33 UTC, z wrote:
Does filter support multiple arguments for the predicate?(i.e. 
using a function that has a "bool function(T1 a, T2 b)" 
prototype)


I am not sure exactly what you are asking here, but you can 
probably accomplish what you want by combining filter with 
std.range.chunks or std.range.slide.


http://phobos.dpldocs.info/std.range.chunks.html
http://phobos.dpldocs.info/std.range.slide.html

If not could still implement the function inside the loop but 
that would be unwieldy.
And does it create copies every call? this is important because 
if i end up using .filter it will be called a 6 to 8 digit 
number of times.


filter does not create any copies of the original array. The same 
is true for pretty much everything in std.range and std.algorithm.


Re: Fastest way to "ignore" elements from array without removal

2021-02-15 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 16 February 2021 at 04:20:06 UTC, z wrote:
What would be the overall best manner(in ease of implementation 
and speed) to arbitrarily remove an item in the middle of an 
array while iterating through it?


http://phobos.dpldocs.info/std.algorithm.iteration.filter.html


Re: Enum template for cpp binding

2021-02-15 Thread Paul Backus via Digitalmars-d-learn

On Monday, 15 February 2021 at 12:37:14 UTC, novice3 wrote:

This is reduced example.
I am sorry for this type of question,
but could please anybody show me template for this boring 
coding?

Is this possible to avoid this manual coding?
Show me direction or examples please.


This will do most of it:

mixin template declareAnonymous(E, string sep = "_")
if (is(E == enum))
{
static foreach (memberName; __traits(allMembers, E))
{
mixin(
"alias ",
__traits(identifier, E), sep, memberName,
" = __traits(getMember, E, memberName);"
);
}
}

Usage:

enum MY_ENUM { A, B, C }

mixin declareAnonymous!MY_ENUM;

static assert(MY_ENUM_A == MY_ENUM.A);
static assert(MY_ENUM_B == MY_ENUM.B);
static assert(MY_ENUM_C == MY_ENUM.C);


Re: GC.addRange in pure function

2021-02-09 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 9 February 2021 at 20:50:12 UTC, Max Haughton wrote:

On Tuesday, 9 February 2021 at 19:53:27 UTC, Temtaime wrote:

On Sunday, 7 February 2021 at 14:13:18 UTC, vitamin wrote:
Why using 'new' is allowed in pure functions but calling 
GC.addRange or GC.removeRange isn't allowed?


pure is broken. Just don't [use it]



[Citation needed]


Allowing memory allocation in pure code in a language that can 
distinguish between pointer equality and value equality is, let's 
say, "unprincipled."


Re: Traits of variadic templates

2021-02-09 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 9 February 2021 at 16:22:16 UTC, Jeff wrote:
But, those don't work because T is a Tuple of the types. Is 
there some trait combination I can use to do this? Something 
like (obviously made up)...


all(TemplateArgsOf!T, t => isIntegral!t || isSomeString!t)

Thanks!


import std.meta: allSatisfy, Or = templateOr;
allSatisfy!(Or!(isIntegral, isSomeString), T);

http://phobos.dpldocs.info/std.meta.allSatisfy.html
http://phobos.dpldocs.info/std.meta.templateOr.html


Re: Operator Overloading for Enum

2021-02-08 Thread Paul Backus via Digitalmars-d-learn

On Monday, 8 February 2021 at 15:56:24 UTC, Michael Brown wrote:

Hi all,

Is it possible to operator overload on enums? I'd like to do a 
opCmp()


Kind regards,
Mike


No, it isn't. Only structs and classes can have overloaded 
operators.


Re: Is there a generic type such as void* or C#'s object?

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

On Saturday, 6 February 2021 at 02:01:28 UTC, Jack wrote:
in C/C++ you have void* and C#'s object, to create a variable 
to hold a genetic type. So in C# you can do:


class A {
  object foo;
}

and

var a = new A();
a.foo = any class...;

does D have something like this or template parameters are used 
instead of?


If it's just for classes, you can use `Object` [1], which is the 
universal base class.


For absolutely any type at all, you can try using `Variant` [2], 
though it has a few limitations.


[1] http://druntime.dpldocs.info/object.Object.html
[2] http://phobos.dpldocs.info/std.variant.Variant.html


Re: How to capture a BitFlags type in a function parameter?

2021-01-31 Thread Paul Backus via Digitalmars-d-learn

On Sunday, 31 January 2021 at 14:04:00 UTC, realhet wrote:

Hi,

I wan't to process every specialization of the BitFlags struct 
in a function:

So far the best I can come up is this:

  static void stdUI(F)(ref F flags) 
if(F.stringof.startsWith("BitFlags!(")){}


But it's obviously lame.
If I try this way:

  static void stdUI(E, U)(ref BitFlags!(E, U) flags){}

I get a cannot deduce arguments error.


Your problem is that you are trying to match `U`, a template type 
parameter [1], with BitFlags' `unsafe`, a template value 
parameter [2]. It should work if you change it to:


static void stdUI(E, Flag!"unsafe" u)(ref BitFlags!(E, u) 
flags) {}


[1] https://dlang.org/spec/template.html#template_type_parameters
[2] https://dlang.org/spec/template.html#template_value_parameter


Re: Bit rotation question/challenge

2021-01-30 Thread Paul Backus via Digitalmars-d-learn

On Saturday, 30 January 2021 at 13:30:49 UTC, burt wrote:

I have a static array of `ubyte`s of arbitrary size:

```d
ubyte[4] x = [ // in reality, ubyte[64]
0b1000,
0b0001,
0b00010101,
0b0010,
];
```

Now I want to bit-rotate the array as if it is one big integer.


You may find `std.bitmanip.BitArray` useful for this:

http://phobos.dpldocs.info/std.bitmanip.BitArray.html


Re: Weird interaction with public and non-public imports

2021-01-28 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 28 January 2021 at 13:07:13 UTC, SealabJaster wrote:

Please see: https://run.dlang.io/is/2mwcPH

I'd expect that the `isInstanceOf` would be true instead of 
false here.


Commenting out the public import changes the output of 
`fullyQualifiedName`. I can kind of see why this happens, but 
it's kind of annoying when things like `isInstanceOf` silently 
fail due to this.


Looks like a bug to me. I recommend filing a bug report on 
issues.dlang.org.


Re: Assigning to class struct member leading to unexpected behavior.

2021-01-27 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 28 January 2021 at 01:43:13 UTC, Paul wrote:
I'm experiencing a compile error, but for the life of me, I 
cannot figure out what is wrong.


I'll try to keep it short but the code is roughly as follows:

class Window{
   Screen screen;
   alias screen this;

this() {
Screen s = {bottom_f: {[0, 1]}};
this.screen = s; // Works
this.screen = {bottom_f: {[0, 1]}}; // Leads to the 
list of errors below

}


The braced-initializer syntax only works in declarations, not 
assignments.


Screen s = {bottom_f: {[0, 1]}}; // Ok - this is a declaration
this.screen = {bottom_f: {[0, 1]}}; // No good - this is an 
assignment


Re: How to dinamically create Tuples?

2021-01-27 Thread Paul Backus via Digitalmars-d-learn

On Wednesday, 27 January 2021 at 17:11:52 UTC, Leonardo wrote:
Hi, I want to know if are some way to dinamically create 
Tuples, with variable size and types defined at runtime. Thanks.


No. D is a statically-typed language, so all types have to be 
defined at compile time.


Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-27 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 27 January 2021 at 14:15:26 UTC, FeepingCreature 
wrote:


Associative arrays allocate per entry added?!

https://github.com/dlang/druntime/blob/master/src/rt/aaA.d#L205 
Oh God, associative arrays allocate per entry added!


Maybe it's to avoid invalidating the result of `key in aa` when 
adding or removing entries? The spec doesn't say anything about 
it either way [1], but allowing invalidation would make AAs much 
more difficult to use in @safe code.


[1] https://dlang.org/spec/hash-map.html


Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-27 Thread Paul Backus via Digitalmars-d-learn

On Wednesday, 27 January 2021 at 15:12:32 UTC, Paul Backus wrote:


Maybe it's to avoid invalidating the result of `key in aa` when 
adding or removing entries? The spec doesn't say anything about 
it either way [1], but allowing invalidation would make AAs 
much more difficult to use in @safe code.


[1] https://dlang.org/spec/hash-map.html


Correction: the GC would take care of the safety issue, of 
course. I haven't had my morning tea yet, and clearly I'm not 
thinking straight. :)


Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-26 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 26 January 2021 at 23:57:43 UTC, methonash wrote:
Using AA's may not necessarily improve performance.  It 
depends on what your code does with it.  Because AA's require 
random access to memory, it's not friendly to the CPU cache 
hierarchy, whereas traversing linear arrays is more 
cache-friendly and in some cases will out-perform AA's.


I figured a built-in AA might be an efficient path to 
performing unique string de-duplication. If there's a more 
performant method available, I'll certainly try it.


You could try sorting the array first, and then using `uniq` [1] 
to discard duplicate elements. There's an example in the docs 
that shows how to do this in-place (without allocating additional 
memory).


[1] http://phobos.dpldocs.info/std.algorithm.iteration.uniq.html


Re: 200-600x slower Dlang performance with nested foreach loop

2021-01-26 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 26 January 2021 at 17:40:36 UTC, methonash wrote:

foreach( i, ref pStr; sortedArr )
{
foreach( j, ref cStr; sortedArr[ i + 1 .. $ ] )
{
if( indexOf( pStr, cStr ) > -1 )
{
// ...
}
}
}

Before adding the code excerpt above, the Dlang program was 
taking ~1 second on an input file containing approx. 64,000 
strings.


By adding the code above, the program now takes 6 minutes to 
complete.


It would be much easier for us to help you with this if you could 
post the full program, or at the very least a reduced version 
that reproduces the same issue. [1] Since your attempts so far 
have failed to fix the problem, it is quite likely that some part 
of the code you do not suspect is actually to blame.


[1] https://idownvotedbecau.se/nomcve/


Re: How do I overload += operator?

2021-01-25 Thread Paul Backus via Digitalmars-d-learn

On Monday, 25 January 2021 at 17:09:22 UTC, Jack wrote:
I'd like to make this work s += 10 where s is a struct. How can 
I do that?


+=, -=, *=, and other compound assignment operators can be 
overloaded by defining `opOpAssign`:


https://dlang.org/spec/operatoroverloading.html#op-assign


Re: std.expreimantal.allocator deallocate

2021-01-24 Thread Paul Backus via Digitalmars-d-learn

On Sunday, 24 January 2021 at 11:00:17 UTC, vitamin wrote:
It is Ok when I call deallocate with smaller slice or I need 
track exact lengtht?


It depends on the specific allocator, but in general, it is only 
guaranteed to work correctly if the slice you pass to deallocate 
is exactly the same as the one you got from allocate.


Re: Deduct and return class type

2021-01-23 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 23 January 2021 at 07:17:38 UTC, Виталий Фадеев 
wrote:

Where source ?
Where deduction implementation ?


Here:

https://github.com/dlang/dmd/blob/v2.095.0/src/dmd/dtemplate.d#L1308


Re: Deduct and return class type

2021-01-22 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 23 January 2021 at 05:54:09 UTC, Виталий Фадеев 
wrote:
But, how to create class instance with type deduction in usual 
way ?


auto list = new List( extensions );


You can't; there's no type deduction for constructors.


Re: A variation of issue 11977?

2021-01-21 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 21 January 2021 at 13:55:48 UTC, kdevel wrote:

~~~gotoskip.d
int main ()
{
   string[string] aa;
   goto A;   // line 4
   aa["X"] = "Y";// line 5
A:
   return 0;
}
~~~

$ dmd gotoskip.d
gotoskip.d(4): Error: goto skips declaration of variable 
gotoskip.main.__aaval2 at gotoskip.d(5)


What's wrong here? Only found Issue 11977 [1] in which a 
declaration and
initialization is jumped over. However, the statement in line 5 
is neither

a declaration nor an initialization.

[1] https://issues.dlang.org/show_bug.cgi?id=11977


__aaval2 is a compiler-generated temporary variable. If you 
comment out the `goto A;` (so that the code compiles) and use the 
compiler flag -vcg-ast, you can see its declaration:


int main()
{
string[string] aa = null;
// goto A;
(string __aaval2 = "Y";) , aa["X"] = __aaval2;
A:
return 0;
}


Re: Exit code -4

2021-01-20 Thread Paul Backus via Digitalmars-d-learn

On Thursday, 21 January 2021 at 00:49:26 UTC, Tim wrote:


Oh, so it's just signal 4, not -4?


The signal is 4. The exit status is -4.

https://en.wikipedia.org/wiki/Signal_(IPC)#POSIX_signals
https://en.wikipedia.org/wiki/Exit_status#POSIX


Re: isCallable fails

2021-01-20 Thread Paul Backus via Digitalmars-d-learn

On Wednesday, 20 January 2021 at 19:01:19 UTC, frame wrote:

On Wednesday, 20 January 2021 at 13:11:09 UTC, Paul Backus


Please post an example with enough code to actually produce 
the error you're seeing.


I don't know when to stop posting code then :(


You should stop (and ideally start) with a Minimal, Complete, 
Verifiable Example:


https://idownvotedbecau.se/nomcve/

Just to illustrate why this matters, let me walk you through the 
steps it takes me to try and figure out what your issue is from 
just the posts you've made so far.


---

Step 1: go through each of your posts and copy each of the 
individual code snippets into a run.dlang.io window.


Result: https://run.dlang.io/is/nidfug

This code compiles--because it's all templates, and you haven't 
actually provided any example of the code that *uses* these 
templates.


---

Step 2: look at the comments and try to guess what the usage code 
must look like.


Result: https://run.dlang.io/is/P7Pcr1

First, we need to define a class with a member named `data` to 
pass as an argument to something. And data's type has to be 
`foo!bar`, so we need a type named `bar` to use for that.


What kind of type? Well, if we look at how `bar` is used in 
`foo`, we can see that it's compared with `null`, so we know it's 
a reference type--probably a class.


Now we can finally attempt to instantiate `something`. What 
happens when we do?


Error: template instance loadAllMatched!TDobj template 
loadAllMatched is not defined


Great, another piece of missing code.

---

Step 3: try to stub out loadAllMatched.

Result: https://run.dlang.io/is/uRj6HW

Error: template instance `isCallable!S` template `isCallable` 
is not defined


Sigh.

---

Step 4: add the missing import.

Result: https://run.dlang.io/is/2by7sU

Oh, look, it compiles with no errors. All that effort, and I 
*still* couldn't reproduce the issue you described in your 
original post. Guess I wasted my time for nothing!


Re: How to use dguihub package ?

2021-01-20 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 20 January 2021 at 19:05:29 UTC, Vinod K Chandran 
wrote:

On Tuesday, 19 January 2021 at 16:52:18 UTC, Paul Backus wrote:
On Tuesday, 19 January 2021 at 16:22:35 UTC, Vinod K Chandran 
wrote:


b ? (tbinfo.fsState |= TBSTATE_ENABLED) : (tbinfo.fsState 
&= ~TBSTATE_ENABLED);


This means, "if b is true, set the TBSTATE_ENABLED flag to 
true; otherwise, set it to false."



Hi Paul Backus,
Thanks for the detailed reply. After reading your reply, I got 
the idea. But think there is one silly mistake in your reply. 
Forgive me if I am wrong.

Instead of
"if b is true, set the TBSTATE_ENABLED flag to true; otherwise, 
set it to false."


This is the meaning of that code.
if (b == true) {tbinfo.fsState = true ; } else {tbinfo.fsState 
= false;}


Because, TBSTATE_ENABLED is a  manifest constant and I cannot 
modify it.

What about simply writing this --
"tbinfo.fsState = b ; " This also worked.


Not quite. If you print out TBSTATE_ENABLED in binary, you will 
see that it is an integer with exactly one bit set to 1, and all 
others set to 0. You can do this with the following line of code:


writefln("%032b", TBSTATE_ENABLED);

By "the TBSTATE_ENABLED flag", I do not mean "the constant 
TBSTATE_ENABLED". What I mean is "the bit in tbinfo.fsState at 
the same position as the 1 bit in TBSTATE_ENABLED". To be 
clearer, I should have said something like "the 'enabled' flag in 
tbinfo.fsState".


If tbinfo.fsState were a struct instead of a bitfield:

struct FsState
{
bool enabled;
// other flags...
}

...then the meaning of the code would be:

if (b == true) {
tbinfo.fsState.enabled = true;
} else {
tbinfo.fsState.enabled = false;
}

Or more concisely:

tbinfo.fsState.enabled = b;

Of course, in reality, tbinfo.fsState is a bitfield, not a 
struct, so we cannot access the individual flags with syntax like 
`.enabled`. Instead, we have to use bitwise operators. But 
conceptually, it's the same thing.


By contrast, in your proposed version:

tbinfo.fsState = b;

...you are overwriting *all* of the flags at once, rather than 
just one of them. Even if this happens to work by coincidence 
(because "enabled" is the first flag), it will certainly cause 
you problems later on.


Re: isCallable fails

2021-01-20 Thread Paul Backus via Digitalmars-d-learn

On Wednesday, 20 January 2021 at 04:43:12 UTC, frame wrote:

struct foo(T) {
  T get() {
static if (is(T : bar)) {
if (value is null) {
value = fun!T;


Error: template instance `fun!T` template `fun` is not defined

Please post an example with enough code to actually produce the 
error you're seeing.


Re: How can I check to see if template type is an array?

2021-01-19 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 19 January 2021 at 22:38:41 UTC, Tim wrote:

On Tuesday, 19 January 2021 at 22:36:47 UTC, Paul Backus wrote:

On Tuesday, 19 January 2021 at 22:34:04 UTC, Tim wrote:
On Tuesday, 19 January 2021 at 22:31:47 UTC, Paul Backus 
wrote:
You've almost got it. The correct syntax is `is(T == U[], 
U)`. You can read it as "there exists some type U such that 
T == U[]".


Thanks! Do I need to specify U in the template function?


No, only inside the is(...) expression.


What if it is a static array i.e. double[3]?


is(T == U[n], U, size_t n)


Re: How can I check to see if template type is an array?

2021-01-19 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 19 January 2021 at 22:34:04 UTC, Tim wrote:

On Tuesday, 19 January 2021 at 22:31:47 UTC, Paul Backus wrote:
You've almost got it. The correct syntax is `is(T == U[], U)`. 
You can read it as "there exists some type U such that T == 
U[]".


Thanks! Do I need to specify U in the template function?


No, only inside the is(...) expression.


Re: How can I check to see if template type is an array?

2021-01-19 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 19 January 2021 at 22:26:52 UTC, Tim wrote:

Hi all,

I need to be able to check in a template whether the type given 
is an array type so that I can do some different logic. How can 
I do this? I would have thought that if(is(T == [])) would 
work, but no.



Thanks in advance


You've almost got it. The correct syntax is `is(T == U[], U)`. 
You can read it as "there exists some type U such that T == U[]".


Re: Value of type enum members

2021-01-19 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 19 January 2021 at 20:27:30 UTC, Andrey Zherikov 
wrote:
Could someone please explain why there is a difference in 
values between compile-time and run-time?



[...]

void main()
{
pragma(msg, TENUM.foo);// T(2)
pragma(msg, TENUM.bar);// T(3)
writeln(TENUM.foo);// foo
writeln(TENUM.bar);// bar
}


There is no difference in the values, they're just being printed 
differently. pragma(msg) uses the compiler's internal 
string-conversion routines, and writeln uses the ones from 
Phobos, so it's not too surprising that they would display some 
values differently.


Re: How to use dguihub package ?

2021-01-19 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 19 January 2021 at 16:22:35 UTC, Vinod K Chandran 
wrote:

Anyhow, I am planning to study this from beginning.
"b ? (tbinfo.fsState |= TBSTATE_ENABLED) : (tbinfo.fsState &= 
~TBSTATE_ENABLED);"
This is the problem line in that property. "b" is a boolean 
parameter. But I dont know what this "|="sign means in D.


`a |= b` is shorthand for `a = a | b`. Similarly, `a &= b` is 
shorthand for `a = a & b`. The `|` symbol is the "bitwise or" 
operator, the `&` symbol is the "bitwise and" operator, and the 
`~` symbols is the "bitwise not" operator. [1]


The way they're being used here is to implement a bit field 
[2]--a data structure in which each bit of an integer is treated 
as a boolean "flag", where 1 is true and 0 is false. In this 
usage, the expression


field |= FLAG

...means "take all of the bits that are set to 1 in FLAG and set 
them to 1 in field"; or, in other words, "set FLAG to true in 
field." And the expression


field &= ~FLAG

...means "take all of the bits that are set to 0 in FLAG and set 
them to 1 in field"; or, in other words, "set FLAG to false in 
field".


You may want to take a minute with pen and paper to convince 
yourself that these particular bitwise operations actually do 
what I'm claiming they do, since it's not really obvious just 
from looking at them.


Armed with this knowledge, we can now understand the line of code 
you quoted in your message:


b ? (tbinfo.fsState |= TBSTATE_ENABLED) : (tbinfo.fsState &= 
~TBSTATE_ENABLED);


This means, "if b is true, set the TBSTATE_ENABLED flag to true; 
otherwise, set it to false."


[1] 
https://en.wikipedia.org/wiki/Bitwise_operation#Bitwise_operators

[2] https://en.wikipedia.org/wiki/Bit_field


Re: native way to tell if output binary is library or executable?

2021-01-17 Thread Paul Backus via Digitalmars-d-learn

On Monday, 18 January 2021 at 02:24:59 UTC, Jack wrote:
I know I can set version but I'd looking for a native way, if 
any, to do that. Is possible to tell if output binary is 
library or executable at compile time? then I'd call different 
version of a function.


Add `-version=library` to your compiler flags for the library and 
`-version=executable` to your compiler flags for the executable. 
Then, in the code, you can check for `version (library)` and 
`version (executable)`.


Before you ask: no, there is no "more native" way to do this, 
because there is actually no difference in how the code is 
compiled between the two versions. The distinction between 
library and executable is only made when the compiled code is 
linked.


Re: Generating documentation help

2021-01-17 Thread Paul Backus via Digitalmars-d-learn

On Sunday, 17 January 2021 at 21:07:31 UTC, Tim wrote:

Hi there,

I have a couple of (probably) fairly simple questions. First, 
when I generate documentation files (-Dd docs) for my project, 
it also generates documentation for the library Mir. How can I 
fix the compiler pulling in those docs as well?


It seems that D won't let me document class constructors. This 
is a problem as I'd like to be able to document the constructor 
parameters. If I put the class documentation above the class 
definition, then ddoc gives an error that the parameters 
specified can't be found.



Thanks


DDoc has a number of annoying bugs like this, and as far as I 
know it isn't really maintained, so they are unlikely to be fixed 
any time soon. I recommend using adrdox instead:


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


Re: Template alias as template specialisation not recognized.

2021-01-15 Thread Paul Backus via Digitalmars-d-learn

On Saturday, 16 January 2021 at 01:21:24 UTC, Paul wrote:
I'm having issues when trying to use a template alias as a 
template specialisation.

When using the following:

alias Vec(uint size, Type) = Mat!(size, 1, Type);


void setUniform(V : Vec!(L, bool), int L)(string name, V 
value) {...}



Vec!(4, bool) a;
setUniform("test", a);


I get the following error:
template `shader.Shader.setUniform` cannot deduce function 
from argument types `!()(string, Mat!(4u, 1u, bool))`, 
candidates are:DUB
shader.d(43, 7): `setUniform(V : Vec!(L, bool), uint L)(string 
name, V value)`


Meanwhile, when using the following, I have no issues:
void setUniform(V : Mat!(L, 1, bool), int L)(string name, V 
value) {}


You have encountered issue 1807:

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

The easiest way to work around it that I know of is to change 
`Vec` from an alias into a struct:


struct Vec(uint size_, Type)
{
Mat!(size, 1, Type) payload;
alias payload this;
}


  1   2   3   4   5   >