Re: Linker issues with struct postblit

2021-10-30 Thread James Blachly via Digitalmars-d-learn

On 10/29/21 7:10 AM, Stanislav Blinov wrote:

On Friday, 29 October 2021 at 11:05:14 UTC, Imperatorn wrote:

On Thursday, 28 October 2021 at 01:39:10 UTC, Thomas Gregory wrote:
I am a maintainer of the 
[dhtslib](https://github.com/blachlylab/dhtslib) package and I have 
been running into issues with a new implementation of reference 
counting we are using.


[...]


Postblit?


https://dlang.org/spec/struct.html#struct-postblit


I imagine Imperatorn is quite familiar with postblit and was pointing 
out that it is strange to use postblit constructor, which is deprecated, 
in a "new implementation".


The original post was long and really buried the lede, but OP was 
pointing out what looks like a compiler bug: a linker error referencing 
a postblit symbol only shows up when compiling with DMD or with LDC 
earlier than 1.25.


Can someone give some insight?



Re: D equivalent to Rust or Scala's Optional and Result and best practice ?

2021-10-18 Thread James Blachly via Digitalmars-d-learn

On 10/18/21 12:03 PM, dangbinghoo wrote:

hi,

It seems that now we have `Optional` and `Result` packages in Dub, are 
these enough or fully equal to Rust or Scala's error-handling and 
pattern-matching?


if these are enough for real-code, any best practice advice?

thanks!



Not quite the same as tagged algebraic union ("sum type" == Rust enum) 
are not (yet?) a language feature.


That being said, I recently integrated mir's Algebraic and Nullable into 
a project. Integration was pretty easy, and the code was clear and easy 
to understand. That being said, the terminology of "Some", "None", 
"Result" and "Err" in Rust are yet easier to understand.


http://mir-core.libmir.org/mir_algebraic.html

Finally, in addition to `mir.algebraic`, `sumtype`, `Optional`, and 
`Result` packages, I would add that Tchaloupka's `expected` package 
looks great:


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


Re: Performance issue with fiber

2021-07-28 Thread James Blachly via Digitalmars-d-learn

On 7/27/21 9:12 PM, Denis Feklushkin wrote:
Spawning fiber is expensive (but not so expensive as spawning thread, of 
course), but switching is fast.


Thus, you can spawn and pause "workers" fibers for avaiting of jobs.
(Probably, this behaviour is already implemented in number of libraries 
and it isn't actually need to implement another one.)


Agree with sibling comment by hanabi1224: spawning fiber (in other 
language runtimes) is INCREDIBLY fast, even though they have stack.


Something is wrong here.


Re: How to parse JSON in D?

2021-03-29 Thread James Blachly via Digitalmars-d-learn

On 3/29/21 1:44 PM, drug wrote:

I use asdf https://code.dlang.org/packages/asdf
Also vibe-d https://code.dlang.org/packages/vibe-d has
vibe-d:data subpackage




Also a happy `asdf` user


Re: noobie question, dub or meson?

2021-03-20 Thread James Blachly via Digitalmars-d-learn

On 3/19/21 4:04 AM, Chris Piker wrote:


Has there ever been talk of adding C source code support to dub, or is 
that a forbidden topic?  I know if dub supported C, all my C libs and 
all my necessary dependencies (openssl, expat, etc.) would have dub.json 
files before the weekend was over.




Chris: for one of my (D) libraries that also links in an .o file that's 
built from C source, I have a makefile for the C and call `make` during 
the dub build process.


It is not incredibly sophisticated, but works well for us. Here is the 
dubfile with `preBuildCommands`:


https://github.com/blachlylab/intervaltree/blob/master/dub.json

Kind regards


Re: Introspection of exceptions that a function can throw

2021-02-25 Thread James Blachly via Digitalmars-d-learn

On 2/24/21 2:38 PM, Mark wrote:
Is there a way to obtain a list, at compile-time, of all the exception 
types that a function might throw (directly or through a call to another 
function)?


Thanks.


Crazy idea:

Could a program import its own source file as a string (`string source = 
import('thisfile.d')`) and `-J` , then use a lexer/parser to 
generate AST of the source code and extract exceptions potentially 
thrown by given functions -- all at compile  time?


Re: Profiling

2021-02-12 Thread James Blachly via Digitalmars-d-learn

On 2/9/21 12:45 AM, JG wrote:
I was trying to profile a d program. So I ran: dub build 
--build=profile. I then ran the program and it produced trace.log and 
trace.def. I then ran d-profile-viewer and got the following error:


std.conv.ConvException@/home/jg/dlang/ldc-1.24.0/bin/../import/std/conv.d(2382): 
Unexpected '-' when converting from type char[] to type ulong


??:? [0x564a8630fda5]
??:? [0x564a86333286]
??:? [0x564a863199fd]
/home/jg/dlang/ldc-1.24.0/bin/../import/std/conv.d:2382 [0x564a862c89a1]
/home/jg/dlang/ldc-1.24.0/bin/../import/std/conv.d:1941 [0x564a862c86cc]
/home/jg/dlang/ldc-1.24.0/bin/../import/std/conv.d:223 [0x564a862c869c]
app.d:1095 [0x564a862cdd71]
app.d:1138 [0x564a862ce7ba]
??:? [0x564a863196cb]
??:? [0x564a863195c7]
??:? [0x564a8631941d]
/home/jg/dlang/ldc-1.24.0/bin/../import/core/internal/entrypoint.d:42 
[0x564a862ce7e4]

??:? __libc_start_main [0x7fd482807cb1]

Is d-profile-viewer no longer working? Or did I do something wrong?


Speaking of D profile viewer [1] we had good luck with it after fixing 
the missing timestamps in the profile (I think on Linux platforms it 
cannot actually measure times and so we had to rewrite the profile file 
to generate some fake timestamp(s) or timings -- hazy on details).


Anyway, if you can get it working it was a nice callgraph tool


[1] https://bitbucket.org/andrewtrotman/d-profile-viewer/src/master/


Re: How can I create a Standalone Bundle Portable file application using Dlang?

2021-01-23 Thread James Blachly via Digitalmars-d-learn

On 1/20/21 6:50 AM, Marcone wrote:

On Tuesday, 19 January 2021 at 14:20:06 UTC, Imperatorn wrote:

On Tuesday, 19 January 2021 at 11:10:25 UTC, Marcone wrote:

On Tuesday, 19 January 2021 at 06:25:31 UTC, Imperatorn wrote:

On Monday, 18 January 2021 at 19:42:22 UTC, Marcone wrote:
How can I create a Standalone Bundle Portable file application 
using Dlang?


Could you describe what you mean with "Bundle portable file 
application"?


All dependencies inside an exe file. Like Python Pyinstaller.


Do you with "dependencies" mean "resources"? In that case, yeah import 
is an option someone mentioned.


I do not mean resources .res, except if is possible use files inside 
resources without copy to hard disc and make accessible as it is in 
local path.


I am afraid we are not speaking the same language.

Because it sounds like you may not be using "dependencies" as it is 
conventionally understood in most programming communities, you'll need 
to give examples.


For the record, dependencies are typically either compile-time 
dependencies or run-time dependencies, and in both cases I think the 
commonest example would be a library.


Re: Why many programmers don't like GC?

2021-01-17 Thread James Blachly via Digitalmars-d-learn

On 1/15/21 4:55 PM, Ola Fosheim Grøstad wrote:

On Friday, 15 January 2021 at 21:18:55 UTC, aberba wrote:

TL;DR:

In summation, the garbage collection system is a robust part of 
Unreal Engine that affords C++ programmers a lot of safety from 
memory leaks, as well as convenience. With this high-level 
discussion, I was aiming to introduce the system at a conceptual 
level, and I hope I have achieved that.


What is your conceptual level? You haven't described what it does, and 
does not do. But yes, frameworks need that allow "scripting" in some 
shape or form (compiled or not) has to hide internal structures and 
intricacies and provide some convenience.


Those were not aberba's words, but the author of the first link, in 
which one does find a conceptual, high level description of GC.


Re: Are there an equivalent to C#'s List in D's stdlib?

2021-01-07 Thread James Blachly via Digitalmars-d-learn

On 1/7/21 9:53 PM, Jack wrote:
I coduln't find an equivalent in the documentation, I could see 
appender, Array, container etc but none of has a Remove(T item) method 
like C#'s [1]. Are there not such implementation and I do have to write 
one myself or I just couldn't find?


[1]: 
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.remove?view=net-5.0 



Speaking for me personally, I would try to work with Ranges[0], and in a 
functional style simply filter [1] out in the same way one calls 
`Remove` in C#.


for example:

```D-ish
auto someRangeType = functionThatDoesWhatever();

auto rangeWithItemRemoved = someRangeType.filter(x => x.prop != whatever);
```

In the C# docs, Remove is called on a class which has overloaded 
`Equals` such that the call to `Remove(new Part(){PartId=1534, 
PartName="cogs"});` considers only the PartId and does not match on name.


Again, you could `filter(x => x.PartId != 1534)`, or you could, as in 
C#, overload the comparison operator for the class/struct you are 
comparing, then `filter(x => x != new Part(1534, "cogs"))`


Finally, although you asked about the standard library, there are many 
other container libraries, like emsi-containers [2] which do have List 
types that implement a `remove` function just as in C#


[0] http://ddili.org/ders/d.en/ranges.html
[1] https://dlang.org/phobos/std_algorithm_iteration.html#filter
[2] https://dlang-community.github.io/containers/index.html



Re: vibe.d and my first web service

2020-11-29 Thread James Blachly via Digitalmars-d-learn

On 11/27/20 12:05 PM, Steven Schveighoffer wrote:


FYI, this bug was just fixed (eventcore version 0.9.11). I tested it and 
it works.


Thanks Sönke for fixing this!

-Steve

https://github.com/vibe-d/vibe-core/issues/205


Hooray and congrats to Soenke and team!


Re: Return values from auto function

2020-11-07 Thread James Blachly via Digitalmars-d-learn

On 11/6/20 5:51 AM, Andrey Zherikov wrote:
I have auto function 'f' that might return either an error (with some 
text) or a result (with some value). The problem is that the type of the 
error is not the same as the type of result so compilation fails.



...
How can I make the original code compilable without templatizing 
`failure` function?


Paul Backus has already replied to you so I am surprised he did not plug 
is own package "SumType". Maybe it is modesty? This seems like an ideal 
time to use this.


All of that being said, I tried to solve this for you using Paul's 
SumType package and myself was initially stymied by the fact that the 
implicit conversion of a `Success(T)` or `Failure` type did not work 
within the ternary op expression*. I rewrote it with if/else which works 
great and is only slightly more verbose than with ternary op.


```D
alias Result = SumType!(Success!int, Failure);

auto f(int i)
{
Result retval;
if (i > 0)
retval = Success!int(i);
else
retval = Failure("Sorry!");
return retval;
}
```

the above relies on suitable definition of `Success(T)` and `Failure` 
structs, obviously.


* This fails due to different types within the same expression:

```
retval = i > 0 ? Success!int(i) : Failure("Sorry");
```

casting each to `Result` compiles, but is verbose:

```
return i > 0 ? cast(Result) Success!int(i) : cast(Result) 
Failure("Sorry");

```

** Could someone more knowledgeable than me explain why implicit 
conversion does not happen with the ternary op, but works fine with 
if/else? Presumably, it is because the op returns a single type and 
implicit conversion is performed after computing the expression's return 
type? If this somehow worked, it would make the SumType package much 
more ergonomic **


Re: Dub cmdline overrides?

2020-11-07 Thread James Blachly via Digitalmars-d-learn

On 11/7/20 7:58 AM, Arjan wrote:
Is there a cmdline switch to DUB to override certain dub.sdl settings 
for a dependency? Like the addition or override to lib dirs or link-libs?


For example once and a while I run into linking issues with symbols not 
found due to the `soname` being different on my system than specified in 
some dependency dub.sdl. Or the lib path to the shared lib is different.




I don't think so, as I have also looked for that very feature in the past.

Probably it is not supported in order to make builds more deterministic 
 the dubfile is a record of how the binary was built (kind of, since 
there are obviously multiple configurations...)


Re: How to use bootstrap with vibe.d.

2020-11-05 Thread James Blachly via Digitalmars-d-learn

On Thursday, 5 November 2020 at 16:22:11 UTC, Alaindevos wrote:
This is from the bootstrap documentation. I think you must 
adapt this to .dt files.


Correct. It looks like flask_bootstrap is just a convenience that 
injects the js and css scripts or script URI for you into master 
flask template.




Re: How to use bootstrap with vibe.d.

2020-11-04 Thread James Blachly via Digitalmars-d-learn

On Wednesday, 4 November 2020 at 19:01:16 UTC, Alaindevos wrote:

(title).
In flask it is "from flask_bootstrap import Bootstrap".
But what do you have to do with vibe.d ?


I am not sure you really understand what bootstrap is?


Re: Why is vibe.d json serializer/deserializer so complex?

2020-11-01 Thread James Blachly via Digitalmars-d-learn

On 10/31/20 8:28 PM, Jesse Phillips wrote:
Well I was putting this together, but didn't want to attempt submission 
until I felt I would be able to put in the time for the review process


https://github.com/JesseKPhillips/DIPs/blob/serialize/attribute/DIPs/1NNN-jkp.md 


Bravo! An idea whose time has come.

I will follow this with interest.
May take some horse-trading, I imagine.


Re: Why is vibe.d json serializer/deserializer so complex?

2020-10-31 Thread James Blachly via Digitalmars-d-learn

On 10/30/20 1:56 PM, Steven Schveighoffer wrote:
I was looking to report an enhancement request to vibe.data.json (filed 
here: https://github.com/vibe-d/vibe.d/issues/2490), and I started 
looking at the serialization code for vibe. It's really really 
complicated, and I'm just wondering if this is a case of 
overengineering, or if there's a logic behind making this so 
complicated. My iopipejson serializer is super simple comparatively.


Is there a benefit to having all the complexity? I know they support 
both json and bson, but I cannot follow the code very well, and I'm not 
sure what happens where, and which types are responsible for what.


-Steve


So I've been meaning to ask this as I have been learning Rust off-and-on 
recently for web development, and was impressed by the traits 
functionality. In particular, with traits and some agreed upon API, many 
packages are interchangeable in terms of various functionalities, 
including JSON serialization/deserialization.


What would be the nearest analog facility in D -- supposing we could 
agree on a standard API -- to facilitate pluggable serializers?


I am a big fan of asdf (and Steve, haven't tried iopipejson yet, but 
will do). It would be nice to not rewrite code to try a different 
serializer, and Rust is really neat in this regard.




Re: Is there something I'm not getting here?

2020-10-26 Thread James Blachly via Digitalmars-d-learn

On 10/26/20 9:19 PM, Ruby The Roobster wrote:
Following code doesn't work(it's not the actual code but it represents 
it). Is there some rule about function overrides that I don't know about?



...
The error I keep getting no matter what says: Error: Multiple Overrides 
of Same Function. Anybody know what I should do?


It works for me.

The "shorten" link on run.dlang.io no longer works, but here is a 
working example:


```
import std;

class A {
public override string toString() {
return "a";
}
}

class B : A {
public override string toString() {
return "b";
}
}

void main()
{
A a = new A();
B b = new B();
writeln(a);
writeln(b);
}
```


Re: Passing pointer to extern(C++) templated function

2020-10-13 Thread James Blachly via Digitalmars-d-learn

On 10/13/20 5:23 AM, Jamie wrote:

Building with:
g++ -c a.cpp
dmd main.d a.o

Throws the error:
/usr/bin/ld: main.o: in function `_Dmain':
main.d:(.text._Dmain[_Dmain]+0x31): undefined reference to `void 
func3(int*, int*)'
/usr/bin/ld: main.d:(.text._Dmain[_Dmain]+0x3e): undefined reference to 
`void func4(int const*, int const)'

collect2: error: ld returned 1 exit status
Error: linker exited with status 1



Is the template even instantiated? With just a template definition and 
an object file I am surprised you even have a symbol?


Re: question on dub and postgresql

2020-10-05 Thread James Blachly via Digitalmars-d-learn

On 10/5/20 11:42 AM, Alaindevos wrote:

Use "rangify" template to get forward range from answer

Thanks, this answers the question on programmatic level.
Yet I guess something is special with the answer datatype which makes 
foreach to fail.


Instead of thinking of it as "foreach failing," instead consider that 
the library author did not implement foreach.


Re: Can opDollar support a jagged 2D array?

2020-10-02 Thread James Blachly via Digitalmars-d-learn

On 10/2/20 9:32 AM, Steven Schveighoffer wrote:

This seems like an oversight. But it's not impossible.


Thank you Steve. Is there any chance that this mechanism will ever be 
revised? Presumably it would require a DIP.


Just curry the information to the receiver. opDollar doesn't have to 
return a size_t.


This is indeed pretty clever; I would not have ever though to have 
opDollar return a non-integral value. This again suggests the whole 
thing needs to be revised, but this will work for now -- thanks again!




Can opDollar support a jagged 2D array?

2020-10-01 Thread James Blachly via Digitalmars-d-learn

Suppose I have a data structure encoding sequence lengths:

seq1: 0 1 2 ... N
seq2: 0 1 2 3 4 ... M
seq3: 0 1 ... P

I would like to write opIndex and opDollar to support the notation 
obj[seq, x .. $] to retrieve sequences.


However, given that opDollar is templated on dimension (always 1 in this 
example) and has no information calling function's context/dimension 0 
parameter, this seems impossible.


Am I missing an easy solution?

Kind regards


Re: what's the best way to convert a sysTime to local machine's time (zone)?

2020-09-24 Thread James Blachly via Digitalmars-d-learn

On 9/24/20 6:22 PM, mw wrote:

Hi,

I'm just wondering what's the best way to convert sysTime to local 
machine's time (zone)?


Is there any library function does this already?


https://dlang.org/phobos/std_datetime_systime.html#SysTime

(The time in SysTime is kept internally in hnsecs from midnight, January 
1st, 1 A.D. UTC.)


Thanks.


It is definitely not easy to find.

https://dlang.org/phobos/std_datetime_systime.html#.SysTime.timezone

SysTime struct contains member fn timezone which will return the current 
settings' TZ ; you can then apply this TZ to other functions in 
std.datetime to transform datetimes to the TZ [of your choice].


Re: DDoc generation

2020-09-18 Thread James Blachly via Digitalmars-d-learn

On 9/18/20 9:35 AM, Russel Winder wrote:

On Fri, 2020-09-18 at 09:02 -0400, Steven Schveighoffer via Digitalmars-d-
learn wrote:

[…]


it ddoc files, and compile those along with your
application.

https://dlang.org/spec/ddoc.html#using_ddoc_for_other_documentation



Any small project examples anywhere?



I am also learning about ddoc generation (something that IMO could stand 
to be much better , ahem, documented). A nice example I've found is the 
libmir site:


https://www.libmir.org/
http://mir-algorithm.libmir.org/ (mir-core., mir-random., etc.)

and its documentation generation infrastructure:

https://github.com/libmir/circle-dlang



Re: Proper way to exit with specific exit code?

2020-09-17 Thread James Blachly via Digitalmars-d-learn

On 9/17/20 12:46 PM, IGotD- wrote:
The only way is to return from main. The thing is that druntime runs 
initialization before main and then returning from main it runs all the 
tear down code including cleaning up the GC. This means there is no 
equivalent of the exit function in the C library. Calling exit from D 
means that there will be no cleanup in D environment.

...

I never considered this -- so when I call core.stdc.stdlib : exit, none 
of my destructors get called?


Presumably also not scope(exit) blocks?

If this is the case, could we simply add a publically-accessible 
shutdown hook in the runtime?




Re: Why is BOM required to use unicode in tokens?

2020-09-15 Thread James Blachly via Digitalmars-d-learn

On 9/15/20 8:24 PM, James Blachly wrote:

Again with the self-reply :/

Forgot the reference: 
https://www.unicode.org/versions/Unicode13.0.0/ch22.pdf


Re: Why is BOM required to use unicode in tokens?

2020-09-15 Thread James Blachly via Digitalmars-d-learn

On 9/15/20 8:10 PM, James Blachly wrote:
Steve: It sounds as if the spec is correct but the glyph (codepoint?) 
range is outdated. If this is the case, it would be a worthwhile update. 
Do you really think it would be rejected out of hand?


OK interestingly this code point 0x2202 falls within the range 
"mathematical operators" [0] , and I could see why in general a range 
called "operators" (which includes e.g. set membership, relations, 
operators you would see in abstract algebra, etc.) however, the first 8 
codepoints in the range are "Miscellaneous mathematical symbols" and 
include several that would be appropriately included as/in token names.


Indeed, chapter 22, page 823 of the Unicode standard groups ∂ U+2202 
(the partial differential symbol in question) along with "Basic Set of 
Alphanumeric Characters" that includes Latin 0-9, [a-z,A-Z], uppercase 
greek A-Ω, nabla and variant theta, the lowercase Greek letters, and 
besides U+2202 ∂, six additional glyph variants.


Due to de-duplication of code points, some things that may rightly 
appear in multiple ranges (like U+2202 ∂) are deduplicated and that I 
think is the fate that befell this variant delta.




Re: Why is BOM required to use unicode in tokens?

2020-09-15 Thread James Blachly via Digitalmars-d-learn

On 9/15/20 10:59 AM, Steven Schveighoffer wrote:

Thanks to Paul, Jon, Dominikus and H.S. for thoughtful responses.

What will it take (i.e. order of difficulty) to get this fixed -- will 
merely a bug report (and PR, not sure if I can tackle or not) do it, 
or will this require more in-depth discussion with compiler maintainers?


I'm thinking your issue will not be fixed (just like we don't allow $abc 
to be an identifier). But the spec can be fixed to refer to the correct 
standards.


-Steve


Steve: It sounds as if the spec is correct but the glyph (codepoint?) 
range is outdated. If this is the case, it would be a worthwhile update. 
Do you really think it would be rejected out of hand?




Re: Why is BOM required to use unicode in tokens?

2020-09-15 Thread James Blachly via Digitalmars-d-learn

On 9/15/20 4:36 AM, Dominikus Dittes Scherkl wrote:

On Tuesday, 15 September 2020 at 06:49:08 UTC, Jon Degenhardt wrote:

On Tuesday, 15 September 2020 at 02:23:31 UTC, Paul Backus wrote:
Identifiers start with a letter, _, or universal alpha, and are 
followed by any number of letters, _, digits, or universal alphas. 
Universal alphas are as defined in ISO/IEC 9899:1999(E) Appendix D of 
the C99 Standard.


I was unable to find the definition of a "universal alpha", or whether 
that includes non-ascii alphabetic characters.


ISO/IEC 9899:1999 (E)
Annex D

Universal character names for identifiers
-

...

---

This is outdated to the brim. Also it doesn't allow for letter-like 
symbols (which is debatable, but especially the mathematical ones like 
double-struck letters are intended for such use).
Instead of some old C-Standard, D should better rely directly on the 
properties from UnicodeData.txt, which is updated with every new unicode 
version.




Thanks to Paul, Jon, Dominikus and H.S. for thoughtful responses.

What will it take (i.e. order of difficulty) to get this fixed -- will 
merely a bug report (and PR, not sure if I can tackle or not) do it, or 
will this require more in-depth discussion with compiler maintainers?


James


Why is BOM required to use unicode in tokens?

2020-09-14 Thread James Blachly via Digitalmars-d-learn
I wish to write a function including ∂x and ∂y (these are trivial to 
type with appropriate keyboard shortcuts - alt+d on Mac), but without a 
unicode byte order mark at the beginning of the file, the lexer rejects 
the tokens.


It is not apparently easy to insert such marks (AFAICT no common tool 
does this specifically), while other languages work fine (i.e., accept 
unicode in their source) without it.


Is there a downside to at least presuming UTF-8?



Re: Call C variadic function from D variadic function

2020-09-13 Thread James Blachly via Digitalmars-d-learn

On 9/13/20 2:35 PM, ag0aep6g wrote:

Easy peasy:

     import std.meta: Repeat;
     Repeat!(kvargs.length, const(char)*) zs;
     foreach (i, ref z; zs) z = toStringz(kvargs[i]);
     return sam_hdr_add_line(this.h, type.ptr, zs, null);


Great, thank you!

By the way, `kvargs` is not a dynamic array. Its length is not dynamic, 
and it's not an array.


I was incorrectly recalling the error message compiler emitted when 
using the typesafe variadic FUNCTION (your method 1, below)


Also, you don't just want to pass the parameters forward. That would be 
trivial: `sam_hdr_add_line(this.h, type.ptr, kvargs, null)`. You want to 
run them through another function first. That's where the difficulty 
comes from.


Right, I indeed left that out of the problem statement; the trivial 
variadic template params pass right on through which is awesome.


I don't see when it would matter how the function is called, but you can 
declare it in two different ways:


My assumption is that if called passing a [runtime] dynamic array, of 
course the parameter list cannot be known, but the (below) "Type 1 True 
typesafe variadic" can also be called with a fixed parameter list known 
at compile-time.



1) True typesafe variadic:

     auto addLine(RecordType type, string[] kvargs ...)

2) Template + typesafe variadic:

     auto addLine(size_t n)(RecordType type, string[n] kvargs ...)

In the first one, `kvargs.length` is a dynamic value. You can't use it 
to generate the arguments for a `sam_hdr_add_line` call.


My point of surprise was that -- depending on how invoked ( f(anArray) 
versus f(1,2,3) the compiler may know at compile-time the parameter 
list. But from a complexity standpoint it makes sense that it is 
nonetheless not possible to use.


In the second one, `kvargs.length` is a static value. So you can do the 
same things as in the `T...` template. And just like the `T...` 
template, it will generate a new function for every distinct 
`kvargs.length`.


Great, I never thought of parameterizing as a static array. This looks 
the best IMO.


Call C variadic function from D variadic function

2020-09-13 Thread James Blachly via Digitalmars-d-learn

Summary:
Can a typesafe D variadic function, or D variadic template pass its 
parameters to a C variadic function?


Background:
I maintain a library binding [0] to htslib, a high-performance and very 
widely used C library for high-throughput sequencing (hts) data files. 
We use this internally and haven't polished it for a release on the 
announce forum or biomed twitter, etc. yet.


In the course of upgrading it to support the latest API/ABI (htslib-1.10 
/ so.3), I need to add support for several new functions.


One of these is a variadic function with the signature:

`int sam_hdr_add_line(sam_hdr_t *h, const char *type, ...);`

Of course, we can call this directly from D with hardcoded parameters. 
However, one of the focuses of our library is "bindings + wrappers" to 
make this feel more like native D. Thus, we want a variadic function to 
which we may pass D strings (it is also a struct member function).


With help from Herringway on IRC, we came up with a solution using mixin:


```
/// Add a single line to an existing header
auto addLine(T...)(RecordType type, T kvargs)
if(kvargs.length > 0 && isSomeString!(T[0]))
{
static assert (kvargs.length %2 == 0);   // K-V pairs => even 
number of variadic args


string varargMagic(size_t len)
{
string args = "sam_hdr_add_line(this.h, type.ptr, ";
for(int i=0; iInterestingly, compilation fails if the mixin consists only of the 
comma-separated parameters ("Comma expression" [1])



Question:
If a variadic template, despite presenting to the user a "dynamic 
array", MUST know its parameter list at compile-time, is there a way 
(other than with mixins as shown) to pass this parameter list to 
extern(C) linkage function with variadic parameters?



(bonus question: if yes, can it be done with typesafe variadic function 
where I believe parameter list is known at either compile time OR 
runtime, depending on how called)



Thanks all in advance



[0] dhtslib
https://code.dlang.org/packages/dhtslib
https://github.com/blachlylab/dhtslib

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


Re: Vibe-D File Question

2020-09-11 Thread James Blachly via Digitalmars-d-learn

On 9/11/20 7:28 AM, Daniel Kozak wrote:

void fun(HTTPServerRequest req, HTTPServerResponse res) nothrow
{
try
{
res.headers["Content-Disposition"] = "filename=\"muj.csv\"";
res.writeBody("some;csv;data", "text/csv");
}
catch (Exception e)
{}
}


Selim, note the Content-Disposition header in particular.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition


Re: I think Associative Array should throw Exception

2020-09-01 Thread James Blachly via Digitalmars-d-learn

On 9/1/20 2:55 PM, Steven Schveighoffer wrote:

On 9/1/20 2:20 PM, Jesse Phillips wrote:

Using RangeError is nice as it allows code to use array index inside 
`nothrow.`


This is the big sticking point -- code that is nothrow would no longer 
be able to use AAs. It makes the idea, unfortunately, a non-starter.

...

-Steve


Steve, are there not several (probably better, faster) alternatives to 
the built-in AA that are nothrow? I think a nice way to look at the 
built-in AA is an easy default for quick scripts, new users, etc., much 
like the default of `throw` status of a function or code block.


Advanced users, (i.e. those using nothrow annotation) could select a 
more efficient AA implementation anyway.


Re: How do I statically build a project using DUB?

2020-08-29 Thread James Blachly via Digitalmars-d-learn

On Saturday, 29 August 2020 at 11:27:28 UTC, Kirill wrote:
I need a stand-alone executable that does not require the user 
to install any libraries on their computer. Everything should 
be packed into the executable.


I understand that I need to statically link all of the 
libraries I use in my project, but how do I do this? What do I 
need to add to dub.json file? What compiler flags do I need to 
use?


Thanks in advance.


It may be extremely difficult if you intend to also link libc; 
the gethostbyname DNS functions in glibc do not support static 
linking.


OTOH you can create a fairly portably binary by static linking 
everything EXCEPT libc (I.e., do not pass -static flag to linker) 
— here you would need to link to the .a files of all your 
projects library deps as well as your dependencies dependencies...






Re: Vibe.d timer - change callback?

2020-08-27 Thread James Blachly via Digitalmars-d-learn

On Tuesday, 25 August 2020 at 18:42:53 UTC, codic wrote:
I'd like to be able to change the callback of a vibe.d Timer 
(eg created with 
http://vibe-core.dpldocs.info/v1.9.3/vibe.core.core.createTimer.html) after creation, something like:


auto timer = createTimer();
timer.rearm(duration, /*...*/);
timer.callback = delegate {
   // things
}


Would creating a dynamic redirection function as delegate serve 
your purpose?


https://dlang.org/library/std/functional/to_delegate.html

You can also pass object having `opCall` to `to_delegate` and th 
is could also provide dynamic dispatch.


in; scope; scope ref; DIP1000; documentation

2020-08-27 Thread James Blachly via Digitalmars-d-learn
Peeling off from Mathias Lang's thread in General about making 
'in' useful, for some novice questions:



1. The thread involves 'in' qualifier. Documentation 
(https://dlang.org/spec/function.html#param-storage) indicates 
that `in` is defined as `scope const` and should not be used as 
it is not implemented. **Is this [fact and recommendation] still 
true?**



2. Regarding scope:

On 8/5/20 3:27 AM, Fynn Schröder wrote:

On Friday, 31 July 2020 at 21:49:25 UTC, Mathias LANG wrote:
I hope this will generate interest with people hitting the 
same problem.


I've literally yesterday written some new code with `const 
scope ref` in almost every function to pass large, complex 
structs. Occasionally, I had to store rvalues temporarily to 
pass as lvalues (non-templated code). I would rather simply put 
`in` on those parameters :-) It's a lot easier to grasp 
function signatures only using `in` and `out` on parameters 
(and their effect/purpose being immediately obvious to new D 
programmers!)


Is "scope ref" documented somewhere specifically? I found 
https://dlang.org/spec/function.html#scope-parameters which 
discusses the use of `scope` with ref type parameters, but the 
example given is pointer-based. Is it correct that `scope ref T` 
behaves the same as `scope T*` ?


Regarding `scope` more generally, DIP1000 shows as "superseded" 
-- **can I still rely on this document for guidance?** We have a 
`-dip1000` flag but a superseded DIP. The discordance is 
extremely confusing.



I am glad D is iterating quickly and improving on safety, but I 
have found that documentation may not well recent changes in this 
area. Consequently I am reluctant to use (newer) features related 
to memory safety. If this is all comprehensively documented 
somewhere please let me know!




Re: Master Thesis using D Programming language.

2020-08-24 Thread James Blachly via Digitalmars-d-learn

On Monday, 24 August 2020 at 19:56:49 UTC, Tariq Siddiqui wrote:

Hello everyone,

I am looking for a master thesis topic using D Programming 
language. Earlier I choose Design by Introspection, but I did 
not find enough material in academic journals. DbI total 
material which I found is DConf Andrei talk and the similar 
talk at Google campus, which starts with 50 Deutsche Mark story 
and ends with explaining PbD, DbI and other Dlang internals. No 
doubt that Andrei's discussion was great. DbI as a topic is 
excellent, but as per my university requirements, I was not 
able to find five articles in the last five years in any 
academic journal, to start the initial discussion about the 
research topic.


I am still opened with my topic, next week, I have to submit my 
draft proposal. Till now, I did not meet my dissertation 
advisor.


I am looking for something practical and less theoretical. I 
have found some thesis done using Rust, you guys as a language 
expert can these below projects be achievable using D 
Programming language.


http://www.barrelfish.org/publications/ma-foellmic-bfrust.pdf
https://www.diva-portal.org/smash/get/diva2:1238890/FULLTEXT01.pdf
https://github.com/Gankra/thesis/blob/master/thesis.pdf

Regards,
Tariq Siddiqui.


Well, what would you like to study? Generally, the thesis will be 
of a topic that is (a) great interest to you and (b) has room for 
improvement in the field.


The theses you listed cover OS development, memory safe kernel 
programming, and safety generally, respectively. Is this the area 
you wish to work in?  What about other areas, such as 
concurrency, message passing, high performance, Type Theory, etc.?


You mentioned Design by Introspection hard to find material 
about. What if you broaden this a bit and consider the topic of 
metaprogramming more generally?


Best of luck to you




Re: How to sort a multidimensional ndslice?

2020-08-18 Thread James Blachly via Digitalmars-d-learn

On Tuesday, 18 August 2020 at 13:07:56 UTC, Arredondo wrote:

On Tuesday, 18 August 2020 at 04:07:56 UTC, 9il wrote:

To reorder the columns data according to precomputed index:
auto index = a.byDim!1.map!sum.slice;


Hello Ilya, thanks for the answer!

Unfortunately I can't use it because I don't have (and can't 
define) a sorting index for my columns. I only have a predicate 
`larger(c1, c2)` that compares two columns to decide which one 
is "larger".


Cheers!
Armando.


Armando, I believe you can just pass your predicate to the sort! 
template


http://docs.algorithm.dlang.io/latest/mir_ndslice_sorting.html



Re: vibe.d and my first web service

2020-08-12 Thread James Blachly via Digitalmars-d-learn

On 7/18/20 8:16 AM, Andre Pany wrote:

On Saturday, 18 July 2020 at 09:10:04 UTC, Mr. Backup wrote:

>> ...
I started the program with "dub" command and everything worked as I 
expected. Except that there were a lot "deprecation" warnings and long 
time to compile.But when I exit the service by ctrl + c and start 
again the program cannot start again with error message:

...


I assume you are using vibe.d 0.8.4 or older. Please check whether 
adding this to dub.json solves your problem:


​"versions": [ "VibeHighEventPriority" ]

(Restart your pc)

Vibe.d 0.9 0 will be released soon. I hope it will work out of the box 
there.


Unfortunately the problem still occurs with Vibe.d 0.9.0

IMO **this is the single most important problem to fix** for vibe.d -- 
if the most basic of examples (indeed, supplied by dub itself) fails so 
spectacularly, the casual new user will not spend the time to find out 
why this is happening, but instead move on. The ctrl-C non-termination 
bug has existed since at least 2015 from what I can tell from the forums.


Tangent:

Does Sönke have a Patreon or the project an OpenCollective etc. ? I 
would be willing to support fixing of some of these bugs.


Alternatively, could we vote on whether a web framework is worthy of 
foundation support? Having an ergonomic, workable web framework is 
absolutely essential to surviving as a language in 2020 (notable 
exception being 800# gorilla incumbents C/C++).


Re: generating random numbers

2020-08-10 Thread James Blachly via Digitalmars-d-learn

On 8/10/20 1:51 AM, Andy Balba wrote:
generating random numbers using 
https://dlang.org/library/std/random/uniform01.html


I find the example given in this section totally incomprehensible
.. Can any help me answer two simple questions:
How to generate a random floating number in range [0,1) ?
How to set a seed value, prior to generating random values ?


Tangential: I also find mir-random [0] a very nice library that is easy 
to use and has no deps on D runtime, so can be used in Das Better C mode.


I think it is also a better random engine than phobos

[0] https://code.dlang.org/packages/mir-random


Re: Get symbols (and/or UDAs) of subclass from superclass

2020-03-16 Thread James Blachly via Digitalmars-d-learn

On 3/15/20 5:28 PM, Simen Kjærås wrote:
...
Since toStringImpl will always call toStringBase, this could perhaps 
better be modeled with a template mixin:


mixin template DerivedToString() {
     override string toStringImpl() {
     return this.toStringBase();
     }
}

class Derived2 : Base {
     mixin DerivedToString!();
}

This way, you can have all the logic of toString in the base class, and 
the only thing a subclass will have to include is one line for the 
mixin. In addition, since toStringImpl is abstract, the implementer of a 
subclass will get a compile-time error if he or she forgets to do either 
the mixin or override toStringImpl themselves.


--
   Simen


Thanks to you and Basile for responses and distinct solutions to 
consider. I am working on the mixin approach now, but with a brief 
mixinline in each derived subclass, I do not even need to call a 
superclass method -- just write the display code once with UDA 
filtering. I think this is the most sensible approach, until something 
else unexpected comes up :)


Get symbols (and/or UDAs) of subclass from superclass

2020-03-15 Thread James Blachly via Digitalmars-d-learn
I would like to programmatically retrieve members of a subclass to 
create a self-documenting interface. I am afraid that my approach is not 
possible due to need for compile time __traits / std.traits, and runtime 
typeinfo. My proposed approach is as follows:


class Base
{
string whatever;

string toString()
{
// loop over __traits(allMembers, typeof(this)) or 
getSymbolsByUDA, etc.

}
}

/// There may be a dozen Derived classes
class Derived1 : Base
{
@Config("a name", "a description")
float probability;
}
class Derived2 : Base
{
@Config("another", "more description")
int replicates;
}
...

Unfortunately, I am afraid this doesn't look possible because of the 
need for compile-time UDAs and runtime TypeInfo. Is there a way to do 
this without re-implementing toString in every single derived class? I 
expect there to be many derived classes.


Other ideas welcomed, as I usually write C-style D and am only recently 
taking a stab at OO Inheritance features.


Thanks in advance.
James


Re: weekly news?

2020-01-23 Thread James Blachly via Digitalmars-d-learn

On 1/23/20 8:13 PM, Mike Parker wrote:

On Thursday, 23 January 2020 at 15:44:10 UTC, Adam D. Ruppe wrote:


Or delete all that wordpress junk and make something in D :P


I intend to delete all that Wordpress junk and go completely static 
eventually.


Mike, I know we're not a golang shop, but I highly recommend switching 
to HUGO for the blog.




Re: weekly news?

2020-01-23 Thread James Blachly via Digitalmars-d-learn

On 1/22/20 7:58 PM, Adam D. Ruppe wrote:

On Thursday, 23 January 2020 at 00:52:10 UTC, Mike Parker wrote:
Got any examples? No one has reported this to me before and I haven’t 
encountered a 404 in a while.


Almost all of them!

Hit F12 to open browser tools and notice the network tab:

https://dlang.org/blog/2020/01/08/recent-d-compiler-releases/
https://dlang.org/blog/2020/01/04/dconf-2020-double-decker-edition/

and more. The HTML is displayed, but it has the 404 code so according to 
the http spec you are actually displaying it all as error pages!


HOLY NEGATIVE SEO BATMAN

curl -v shows that it is returning 404 indeed.



Re: Some comments on learning D using the tour

2020-01-16 Thread James Blachly via Digitalmars-d-learn

On 1/15/20 3:06 PM, mark wrote:

I am learning D for the first time.

While I wait for Mike Parker's "Learning D" book to arrive, I have 
started using the tour.


I really like the tour, especially the fact that you can run and tweak 
the code as well as read the explanations.


However, ...


Mark, thank you for your comments. It is always valuable to hear the 
perspective of someone approaching any problem or situation with a fresh 
set of eyes.




Re: druntime 2.087 regression? (was: Old codebase stops compiling at 2.087)

2019-12-03 Thread James Blachly via Digitalmars-d-learn

On 11/17/19 7:15 PM, Steven Schveighoffer wrote:

On 11/17/19 10:45 AM, James Blachly wrote:

/home/james/dmd2.087/dmd2/linux/bin64/../../src/druntime/import/object.d(3453,36): 
Error: cannot implicitly convert expression aa of type 
shared(GSeqAllele[string]) to const(shared(GSeqAllele)[string])


There's a subtle removing of the shared from the entire AA. Is that on 
purpose?


i.e. I see shared(GSeqAllele[string]) in one, and 
shared(GSeqAllele)[string] in another.


-Steve


Steve:

Sorry for the delay in replying. That is the runtime removing the shared 
from the entire AA, not our code. I point out the change that induces 
this with a link to github.


Should I move this discussion to the main D group? My primary concerns are:

0. Ask if it is intended behavior that the runtime cannot handle a 
shared AA post 2.086.1?


1. Report this as bug

2. Ask why this commit ("object: Declare private struct AA and fix aaA 
function signatures ") not mentioned in the changelog for 2.087?


Thanks in advance


druntime 2.087 regression? (was: Old codebase stops compiling at 2.087)

2019-11-17 Thread James Blachly via Digitalmars-d-learn
TL;DR druntime regression? 
https://gist.github.com/jblachly/78c5762bbfea65b09e7a1417ad763019


---

Our team has an older codebase that compiled fine up until frontend 
version 2.087 (builds with 2.086.1).  Now, compiler (DMD2 and LDC2) 
complains about implicit conversion of shared(AA) to const(shared(AA)).


The codebase is private (could possibly make it public soon) but the 
error involves an AA inside a shared struct.


```
/home/james/dmd2.087/dmd2/linux/bin64/../../src/druntime/import/object.d(3453,36): 
Error: cannot implicitly convert expression aa of type 
shared(GSeqAllele[string]) to const(shared(GSeqAllele)[string])


source/polytyper/gseq.d(231,33): Error: template instance 
`object.values!(shared(GSeqAllele[string]), shared(GSeqAllele), string)` 
error instantiating


/home/james/dmd2.087/dmd2/linux/bin64/../../src/druntime/import/object.d(3402,36): 
Error: cannot implicitly convert expression aa of type 
shared(GSeqAllele[string]) to const(shared(GSeqAllele)[string])


source/polytyper/gseq.d(280,35): Error: template instance 
`object.keys!(shared(GSeqAllele[string]), shared(GSeqAllele), string)` 
error instantiating

```
(line breaks added for clarity)

The two triggering lines both iterate over a built-in AA

gseq.d:231:foreach (a; this.alleles.values)
gseq.d:208:foreach (key; this.alleles.keys)

Looking at the changelog for 2.087 ( 
https://dlang.org/changelog/2.087.0.html ) I do not see any obvious 
culprits. git blame suggests this is the responsible commit:


https://github.com/dlang/druntime/commit/af86e7db58f4b792e45bdc3ee43c17aadc9d54ab#diff-a68e58fcf0de5aa198fcaceafe4e8cf9L3430-R3454

From this change, it looks like the runtime now cannot handle a shared 
AA -- is this right? A minimal reproducible example is here: 
https://gist.github.com/jblachly/78c5762bbfea65b09e7a1417ad763019


Thanks in advance for help
James

PS: Why is this commit ("object: Declare private struct AA and fix aaA 
function signatures ") not mentioned in the changelog?


Re: How to Unqual an array?

2019-11-17 Thread James Blachly via Digitalmars-d-learn

On 11/16/19 10:20 AM, James Blachly wrote:

On 11/16/19 9:48 AM, James Blachly wrote:
I am trying to write templated code that will take a character array 
-- mutable, const, or immutable (string).


I am aware of isSomeString, but I am still curious about Unqual array 
from a learning perspective.





Thank you both Steve and Anonymouse!

Regarding Steve's comment -- this should be made clear in documentation 
no? Regarding Anonymouse's code snippet, I've definitely saved that for 
future. thank you.


James


Re: How to Unqual an array?

2019-11-16 Thread James Blachly via Digitalmars-d-learn

On 11/16/19 9:48 AM, James Blachly wrote:
I am trying to write templated code that will take a character array -- 
mutable, const, or immutable (string).


I am aware of isSomeString, but I am still curious about Unqual array 
from a learning perspective.





How to Unqual an array?

2019-11-16 Thread James Blachly via Digitalmars-d-learn
I am trying to write templated code that will take a character array -- 
mutable, const, or immutable (string).



static assert(is(Unqual!(const char) == char));

(Succeeds)

static assert(is(Unqual!(const(char)[]) == char[]));

Error: static assert:  `is(const(char)[] == char[])` is false


Is the template trying to strip the qualifier from the [] rather than 
the char?


Re: Why is sformat and formattedWrite (appender) allocating GC mem here?

2019-09-03 Thread James Blachly via Digitalmars-d-learn

On 8/31/19 5:12 PM, ag0aep6g wrote:

I've made a pull request to get rid of those allocations:
https://github.com/dlang/phobos/pull/7163


Wonderful!
For my own learning, why was a unittest to ensure no GC added to sformat 
instead of a @nogc annotation?


Do I understand std.experimental.allocator composition correctly?

2019-08-25 Thread James Blachly via Digitalmars-d-learn
The documentation for std.experimental.allocator is a little dense and I 
wanted to make sure I am understanding composition correctly.


Suppose I have the following, taken more-or-less direct from the docs:

auto batchAllocator = AllocatorList!(
(size_t n) => Region!Mallocator(max(n, 1024*1024))
)();

https://dlang.org/phobos/std_experimental_allocator_building_blocks_region.html#.Region

Is my understanding correct that Mallocator, the ParentAllocator in 
Region's construction will allocate a block of at least 1 MiB when a 
request comes in, but calls to batchAllocator.make!(T) will allocate 
only enough (rounded up to some ideal like a power of 2) to store T -- 
until it runs out, then the AllocatorList will allocate another block of 
1 MiB, and so on?


Essentially, I need to allocate memory for objects in an inner loop, and 
I thought a better strategy would be to make big block alloc(s) as a 
pool and then hand out internal pointers into the pool. I previously did 
this will malloc() and std.conv.emplace, but this looks like a better 
solution, if my understanding is correct.


Thank you!


Re: How to get name of my application (project)

2019-08-03 Thread James Blachly via Digitalmars-d-learn

On 8/3/19 5:26 AM, Andrey wrote:
Hello, how to get name of my application (project) that we write in 
dub.json? Is there any compile-time constant like __MODULE__?


Dear Andrey:

Perhaps this is similar to what you are looking for:

https://dlang.org/spec/grammar.html#SpecialKeyword

SpecialKeyword:
__FILE__
__FILE_FULL_PATH__
__MODULE__
__LINE__
__FUNCTION__
__PRETTY_FUNCTION__


These are available at compile time.  Kind regards.


Re: Basic Linear Algebra and D's Array Operation

2019-05-19 Thread James Blachly via Digitalmars-d-learn

On 5/19/19 2:34 AM, Andrew Edwards wrote:
P.S. Why do we still have two sets of documentations ([1],[2]) for the 
language? Which is the official one and when can we get rid of the other?


[1] https://dlang.org/library/std/array.html
[2] https://dlang.org/phobos/std_array.html


I have wondered this myself -- anyone?


Re: dub preBuildCommand removed?

2019-05-04 Thread James Blachly via Digitalmars-d-learn

On 5/4/19 5:27 PM, Andre Pany wrote:

On Saturday, 4 May 2019 at 21:16:25 UTC, James Blachly wrote:



It is listed in the build settings table of the referenced link.

Kind regards
Andre


Somehow firefox enabled "whole word" search *rolleyes*, which I did not 
even realize was a thing. Thanks and sorry for the traffic.




dub preBuildCommand removed?

2019-05-04 Thread James Blachly via Digitalmars-d-learn
the preBuildCommand is no longer listed in the dub format spec[1] 
although it shows up in the google cache for the page. When was it 
removed, and why? I can't find any announcement.


[1] https://dub.pm/package-format-json.html


Re: Cast ptr/len to D-style array

2019-05-04 Thread James Blachly via Digitalmars-d-learn

On 5/3/19 5:42 AM, Ali Çehreli wrote:

On 05/02/2019 08:21 PM, James Blachly wrote:

On 5/2/19 4:05 PM, ag0aep6g wrote:

Just slice the pointer with the length:

 int* ptr;
 size_t len;
 int[] arr = ptr[0 .. len];


Perfect thanks. I searched but without using the magic word "slice" I 
couldn't find meaningful results.


Thanks again.


Looks like I indexed it under "slice from pointer":


http://ddili.org/ders/d.en/pointers.html#ix_pointers.slice%20from%20pointer

Ali



Thanks Ali, also helpful examples!


Re: Cast ptr/len to D-style array

2019-05-02 Thread James Blachly via Digitalmars-d-learn

On 5/2/19 4:05 PM, ag0aep6g wrote:

Just slice the pointer with the length:

     int* ptr;
     size_t len;
     int[] arr = ptr[0 .. len];


Perfect thanks. I searched but without using the magic word "slice" I 
couldn't find meaningful results.


Thanks again.


Cast ptr/len to D-style array

2019-05-02 Thread James Blachly via Digitalmars-d-learn

I work a lot with C functions, many of which yield pointer + length.

Is there a way to cast this or materialize a D-style array backed by the 
already allocated data (with length) to avoid copies which slow things down?


I recognize memory management is a complication. Typically, I would be 
responsible for free'ing the array later.


Thanks in advance


Re: Easiest way to use Linux system C files / tiny C libraries

2019-04-07 Thread James Blachly via Digitalmars-d-learn

On 3/29/19 7:52 PM, H. S. Teoh wrote:

On Fri, Mar 29, 2019 at 10:48:47PM +, Chris Katko via Digitalmars-d-learn 
wrote:
...> There are probably other similar gotchas, but these are the ones 
off the

top of my head.  Feel free to ask if you're having trouble correctly
translating something from C.  If all else fails, run the header through
a C processor and inspect the output to find out what the *real*
definition of something is, if the original C source is so obfuscated
you can't easily tell otherwise.



Sorry I'm late to the party but another one that we have come across 
quite a bit is in function signatures:


C:
const char *stringparam

D:
const(char)* stringparam


Best wishes



Re: Member is @disable this(this), parent uncopyable

2019-03-22 Thread James Blachly via Digitalmars-d-learn

On 3/22/19 9:24 AM, Alex wrote:

On Friday, 22 March 2019 at 12:08:39 UTC, James Blachly wrote:
First, how do we deal with toString, std.format, writeln, etc. with 
un-copyable objects, when it is only a member that is uncopyable?  In 
my case I got around this by creating a pointer and moving the 
initialization to the constructor, but I wonder if there are other ways?


You could (and should) define your custom toString() method inside the 
struct S, where un-copyable (or other strange kinds) members are part of.

In such a way, you could
S s;
s.toString(), without any harm, possibly omitting the members, which you 
are sure of (such as UnrolledList?).


Hi Alex,

Thanks for replying. I attribute the lack of sensibleness in my post to 
lack of sleep.


I actually am defining toString as member function of S, but it only now 
occurred to me that structs are always passed by value, not by 
reference. Of course then it cannot be copied.


Thanks again =)


Member is @disable this(this), parent uncopyable

2019-03-22 Thread James Blachly via Digitalmars-d-learn
I have a struct S with member containers.UnrolledList [1]. UnrolledList 
is @disable this(this), but this unfortunately makes my struct S also 
un-copyable, which now breaks some of my debugging statements which rely 
on toString, as writeln, format, etc. all copy the object. This leaves 
me in the unfortunate situation that my release builds work, but debug 
builds do not.


First, how do we deal with toString, std.format, writeln, etc. with 
un-copyable objects, when it is only a member that is uncopyable?  In my 
case I got around this by creating a pointer and moving the 
initialization to the constructor, but I wonder if there are other ways?


Second, why  must an object in its entirety be copy-able for these 
functions to call toString() ?


Finally, the error message gave no clue that it was a member 
(UnrolledList in my case) that was @disable 'd and it took some digging 
back through several commits to figure out what was happening -- how 
could we improve the error messaging?


Thanks as always in advance

[1] https://github.com/dlang-community/containers


Re: Where is GDC being developed?

2019-03-21 Thread James Blachly via Digitalmars-d-learn

On 3/21/19 6:51 PM, James Blachly wrote:

On 3/21/19 6:01 AM, Johannes Pfau wrote:

On Thursday, 21 March 2019 at 08:19:56 UTC, Per Nordlöw wrote:

At

https://github.com/D-Programming-GDC/GDC/commits/master

there's the heading

"This repository has been archived by the owner. It is now read-only."

Where will the development of GDC continue?


We use https://github.com/D-Programming-GDC/gcc for CI, but commits 
will go to the GCC SVN first, so GCC SVN or snapshot tarballs is the 
recommended way to get the latest GDC.


There is one exception: When GCC development is in feature freeze, we 
might provide newer DMD frontends in a gdc-next branch at 
https://github.com/D-Programming-GDC/gcc . However, so far we have not 
set up this branch, this will probably happen in the next two weeks. 
Maybe I'll also provide DDMD-FE backports for GCC9 in that repo, but 
I'm not sure yet. The latest DDMD-FE is somewhere in the archived 
repos, but it hasn't been updated for some time.


Thanks -- I also tried to figure out how to install GDC just yesterday 
and gave up. All wiki links and google top results seemed dead ends.




A little bit of further information: the downloads listed on

https://gdcproject.org/downloads

are 2016 versions.


Re: Where is GDC being developed?

2019-03-21 Thread James Blachly via Digitalmars-d-learn

On 3/21/19 6:01 AM, Johannes Pfau wrote:

On Thursday, 21 March 2019 at 08:19:56 UTC, Per Nordlöw wrote:

At

https://github.com/D-Programming-GDC/GDC/commits/master

there's the heading

"This repository has been archived by the owner. It is now read-only."

Where will the development of GDC continue?


We use https://github.com/D-Programming-GDC/gcc for CI, but commits will 
go to the GCC SVN first, so GCC SVN or snapshot tarballs is the 
recommended way to get the latest GDC.


There is one exception: When GCC development is in feature freeze, we 
might provide newer DMD frontends in a gdc-next branch at 
https://github.com/D-Programming-GDC/gcc . However, so far we have not 
set up this branch, this will probably happen in the next two weeks. 
Maybe I'll also provide DDMD-FE backports for GCC9 in that repo, but I'm 
not sure yet. The latest DDMD-FE is somewhere in the archived repos, but 
it hasn't been updated for some time.


Thanks -- I also tried to figure out how to install GDC just yesterday 
and gave up. All wiki links and google top results seemed dead ends.




Re: How can I make a nested array and flatten it at run time in D?

2019-03-07 Thread James Blachly via Digitalmars-d-learn

On 3/7/19 8:00 PM, Philos Kim wrote:

I want to make a nested array and flatten it at run-time like this.

auto nestedArray = [1, 2, [3, 4], 5];

auto flattenedArray = myFun(nestedArray);

writeln(flattenedArray);   // => [1, 2, 3, 4, 5]


How can I do this in D?

Please help me out!



There are simpler ways, but looking at the below Rosetta Code link can 
be very instructive:


https://rosettacode.org/wiki/Flatten_a_list



Re: DMD2 vs LDC2 inliner

2019-02-25 Thread James Blachly via Digitalmars-d-learn

On 2/25/19 2:09 AM, Nicholas Wilson wrote:

Leaving aside the issue of why DMD can't handle this, the entire reason
pragma(inline, bool) takes a bool is for it to be (potentially) 
predicated.

In this case you want:

version(DigitalMars)
  private enum inline_overlaps = false;
else // assuming GDC is good
  private enum inline_overlaps = true;

pragma(inline, inline_overlaps)
bool overlaps(IntervalType1, IntervalType2)(IntervalType1 int1, 
IntervalType2 int2)

{
  return ...;
}


Sadly, the pragma does not accept enum:

Error: pragma `inline` pragma(inline, true or false) expected, not 
inline_overlaps


Re: DMD2 vs LDC2 inliner

2019-02-25 Thread James Blachly via Digitalmars-d-learn

On 2/25/19 2:09 AM, Nicholas Wilson wrote:

Leaving aside the issue of why DMD can't handle this, the entire reason
pragma(inline, bool) takes a bool is for it to be (potentially) predicated.
In this case you want:

version(DigitalMars)
  private enum inline_overlaps = false;
else // assuming GDC is good
  private enum inline_overlaps = true;

pragma(inline, inline_overlaps)
bool overlaps(IntervalType1, IntervalType2)(IntervalType1 int1, 
IntervalType2 int2)

{
  return ...;
}


I still have yet to internalize the power of compile-time values. Thanks 
for the reminder and solution!


DMD2 vs LDC2 inliner

2019-02-24 Thread James Blachly via Digitalmars-d-learn
Any ideas why DMD2 cannot inline this, but LDC2 has no problem doing so 
-- or suggestions for what I can do to make DMD2 inline it?


Alternatively, I could version(DigitalMars) and version(LDC), but AFAICT 
this requires me to duplicate the entire template, because I cannot 
figure out how to make the version() directive apply only to the pragma.


Template instantiation is with a simple struct having integer members 
start and end.


Thanks in advance for tips.
---
pragma(inline, true)
bool overlaps(IntervalType1, IntervalType2)(IntervalType1 int1, 
IntervalType2 int2)

if (__traits(hasMember, IntervalType1, "start") &&
__traits(hasMember, IntervalType1, "end") &&
__traits(hasMember, IntervalType2, "start") &&
__traits(hasMember, IntervalType2, "end"))
{
// int1   ====
// int2 ===  ===
if (int2.start <= int1.start &&  int1.start < int2.end) return true;

// int1  ===  ===
// int2   ===  ===
else if (int1.start <= int2.start && int2.start < int1.end) return 
true;


// int1  =|   =
// int2   =   |  =
else return false;
}


Re: std.container.rbtree as Interval Tree?

2019-02-05 Thread James Blachly via Digitalmars-d-learn
On Tuesday, 5 February 2019 at 16:24:03 UTC, Eduard Staniloiu 
wrote:
I think you are making a slight confusion. Your `Interval` 
struct and the `Elem` type that `lowerBound` takes, are the 
same type.


You can define your RBTree and Interval as follows
```
struct Interval
{
int start;
int end;
}

alias IntervalTree = RedBlackTree!(Interval, (i1, i2) => 
i1.start < i2.start);

```

Please see this runable example: https://run.dlang.io/is/4cPTik

The in-order traversal will be the same as the wikipedia 
example here: https://en.wikipedia.org/wiki/Interval_tree


Hope this helps.

Cheers,
Edi



Edi, thanks for your quick reply!

I do understand that Elem is aliased to my Interval type.

Your suggested rewrite of the less fn is precisely what I was 
groaning about (although not explicitly) in terms of rewriting 
opCmp to be a simple `this.start < other.start`. The reason that 
this is undesirable is that distinct intervals with same starting 
coordinates are then considered equal and not added, unless 
RBTree tepmlate is instantiated with allowDuplicates. However, 
even when allowing (pseudo)duplicates, this means the distinct 
intervals with same start but different end coordinates are not 
deterministically placed/sorted within the tree, because they are 
not sortable with the simple `this.start < other.start` less 
function.


Anyway, in the end, I will assume that in my problem domain there 
are no degenerate intervals with same start coordinate and use 
the simpler less to accomplish goal.


Hopefully the upperBound and lowerBound functions are lazy...


Re: std.container.rbtree as Interval Tree?

2019-02-05 Thread James Blachly via Digitalmars-d-learn

On Tuesday, 5 February 2019 at 10:10:44 UTC, RazvanN wrote:

On Monday, 4 February 2019 at 22:54:01 UTC, James Blachly wrote:
I tried to implement an interval tree backed by 
std.container.rbtree today and fell flat.


[...]

You can use alias this [1] in your interval element type:

struct IntervalElem
{
size_t start, end;
/* ... other declarations */
alias start this;
}

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



Thanks -- I always seem to forget about `alias this` !
However, I don't think that this helps me to call functions 
expecting type Elem with an integer. At least, it failed in my 
test -- could this be because Elem itself is already an alias?


Q2: Would replacing "Elem" with a generic type "T" in the 
function signatures for upperBound, lowerBound, and various 
related fns like _firstGreater / _firstGreaterEqual solve this 
problem?


[...]
Elem is already a generic type. I don't know how you can make 
it more generic without adding other template parameters to the 
class declaration (which means reimplementing RBTree from 
std.container);


Agree, (although I think I would only need to revise only perhaps 
25% of the module in this case) but I definitely wanted to avoid 
this if possible.





std.container.rbtree as Interval Tree?

2019-02-04 Thread James Blachly via Digitalmars-d-learn
I tried to implement an interval tree backed by 
std.container.rbtree today and fell flat.


A standard way to make an interval tree is to make an augmented 
tree; I supposed since rbtree was a generic container and because 
I could define opCmp, this should be a cinch. I ran into two 
problems.


First (minor problem), RedBlackTree considers a return value of 0 
from opCmp equivalent to "equals", which is discordant with the 
guidance given for opCmp.[1] This is a  minor pedantic point, 
since you cannot store un-orderable elements in the tree anyway.


More importantly, though, I cannot figure out how to implement an 
interval query function on the tree. Typically, the tree would 
have a "key" that is the left position of the interval and the 
augmented part of the tree would be that a second value -- a 
right, or end, position. My Elem == key type is a struct 
encapsulating both of these (start, end; plus some metadata).


For my Interval element type, I overloaded opCmp to take an 
integer, but unfortunately RedBlackTree's upperBound() and 
lowerBound() functions are defined in terms of "Elem" which is 
aliased to the contained element type, rather than a generic type.


Q1: Is there a simple or elegant way to do this without 
re-implementing RedBlackTree? I apologize if it is obvious and I 
am missing it. I suppose it may work if I rewrite Interval's 
opCmp to not consider the upper bound, and by creating a dummy 
interval to pass to upperBound and lowerBound, but that seems 
inelegant compared to passing an integer.


Q2: Would replacing "Elem" with a generic type "T" in the 
function signatures for upperBound, lowerBound, and various 
related fns like _firstGreater / _firstGreaterEqual solve this 
problem?


James

[1] https://dlang.org/spec/operatoroverloading.html#eqcmp ("For 
example ... x and y are disjoint sets, then neither x < y nor y < 
x holds, but that does not imply that x == y. Thus, it is 
insufficient to determine equality purely based on opCmp alone. ")


Re: Shared, ref, arrays, and reserve template instantiation

2018-09-12 Thread James Blachly via Digitalmars-d-learn

Great -- Thank you both.

I previously found Unqual, but it looks like that needs template 
support so wasn't feasible, hence my question.


Neia is right that I tried to cast as in the second case ( but 
without UFCS -- reserve( cast(int[]), N); ).  As an aside, what 
is going on behind the scenes with the compiler when casting away 
a property? I did not think cast operations copied data, so was 
surprised that a cast value is not an lvalue.


Regarding Jonathan's comments, we had definitely protected the ~= 
operations with Mutex, but realized we were doing lots of array 
appends in a hot loop, and since we have an idea of cardinality 
ahead of time just wanted to preallocate.  Since it is all 
initialization before concurrent code enters the picture, we'll 
do what you've suggested and set it up as TL and then cast to 
shared.


James


Shared, ref, arrays, and reserve template instantiation

2018-09-12 Thread James Blachly via Digitalmars-d-learn
When I add the "shared" attribute to an array, I am no longer 
able to call reserve because the template won't instantiate:


Error: template object.reserve cannot deduce function from 
argument types !()(shared(int[]), int), candidates are:
/dlang/dmd/linux/bin64/../../src/druntime/import/object.d(4091):  
  object.reserve(T)(ref T[] arr, size_t newcapacity)


1. Shared modifies the type, so the template does not match. Even 
casting does not seem to work however. Is there something about 
shared that makes it unable to be taken by reference?
2. Is there a workaround for me to be able to preallocate the 
array?


Kind regards


Re: extern __gshared const(char)* symbol fails

2018-08-31 Thread James Blachly via Digitalmars-d-learn
On Friday, 31 August 2018 at 17:50:17 UTC, Steven Schveighoffer 
wrote:
What the C compiler is doing is storing it as data, and then 
storing the symbol to point at the first element in the data.


When you use const char* in D, it's expecting a *pointer* to be 
stored at that address, not the data itself. So using it means 
segfault. The static array is the correct translation, even 
though it leaks implementation details.


In C, it's working because C has the notion of a symbol being 
where an array starts. D has no concept of a C array like that, 
every array must have a length. So there is no equivalent you 
can use in D -- you have to supply the length.



NKML also wrote:
You need to declare your extern array as array in D and also in 
C, so that the compiler would know what that is (an array, not 
a pointer). In many situations C compiler would silently 
convert an array into a pointer (when it already knows its 
dealing with array), but it won't convert a pointer into an 
array.


Thank you Steve and NKML for your very clear and concise answers. 
This makes perfect sense.


I would like not to write as a static array in D because I cannot 
guarantee future version of the library to which I am linking 
would not change the length of the data. Steve's trick, below, 
looks like the ticket.




Alternatively, you can treat it as a const char:

extern(C) extern const(char) seq_nt16_str;

void main()
{
   import core.stdc.stdio;
   printf("%s\n", _nt16_str); // should print the string
}

You could wrap it like this:

pragma(mangle, "seq_nt16_str");
private extern(C) extern const(char) _seq_nt16_str_STORAGE;

@property const(char)* seq_nt16_str()
{
   return &_seq_nt16_str_STORAGE;
}

To make the code look similar.

-Steve


That is a great trick, and I will use it.





Re: extern __gshared const(char)* symbol fails

2018-08-31 Thread James Blachly via Digitalmars-d-learn

On Friday, 31 August 2018 at 17:18:58 UTC, Neia Neutuladh wrote:

On Friday, 31 August 2018 at 06:20:09 UTC, James Blachly wrote:

Hi all,

...

When linking to this library from D, I have declared it as:

extern __gshared const(char)* seq_nt16_str;

***But this segfaults when I treat it like an array (e.g. by 
accessing members by index).***


I believe this should be extern extern(C)? I'm surprised that 
this segfaults rather than having a link error.


A bare `extern` means "this symbol is defined somewhere else".

`extern(C)` means "this symbol should have C linkage".




I am so sorry -- I should have been more clear that this is in 
the context of a large header-to-D translation .d file, so the 
whole thing is wrapped in extern(C) via an extern(C): at the top 
of the file.




Re: Is there any reason to use non-ref foreach?

2018-08-31 Thread James Blachly via Digitalmars-d-learn

On Friday, 31 August 2018 at 12:52:17 UTC, bauss wrote:


So basically ... Instead of copying the value, you're just 
copying the address.


I can't see the benefit other than added complexity.


I assume a benefit could be observed if you are copying a large 
struct instead of an int.


extern __gshared const(char)* symbol fails

2018-08-31 Thread James Blachly via Digitalmars-d-learn

Hi all,

I am linking to a C library which defines a symbol,

const char seq_nt16_str[] = "=ACMGRSVTWYHKDBN";

In the C sources, this is an array of 16 bytes (17 I guess, 
because it is written as a string).


In the C headers, it is listed as extern const char 
seq_nt16_str[];


When linking to this library from another C program, I am able to 
treat seq_nt16_str as any other array, and being defined as [] 
fundamentally it is a pointer.


When linking to this library from D, I have declared it as:

extern __gshared const(char)* seq_nt16_str;

***But this segfaults when I treat it like an array (e.g. by 
accessing members by index).***


Because I know the length, I can instead declare:

extern __gshared const(char)[16] seq_nt16_str;

My question is: Why can I not treat it opaquely and use it 
declared as char* ? Does this have anything to do with it being a 
global stored in the static data segment?




Re: InputRange help: (1) repeated dtor calls and (2) managing resources needing free()

2018-08-19 Thread James Blachly via Digitalmars-d-learn

On Monday, 13 August 2018 at 13:20:25 UTC, Seb wrote:

BTW it's very uncommon for empty to do work, it's much more 
common to do such lazy initialization in `.front`.




Thanks Seb, that entire reply is a huge help.

By lazy initialization in `.front`, do you mean that I should 
find a way for `front` to preload the first record?


If so, could you help me understand what you mean by lazy init 
with `front`? `empty` is called before `front` upon first 
iteration through the Range, so really the init has to be done in 
the constructor, yes?





Re: InputRange help: (1) repeated dtor calls and (2) managing resources needing free()

2018-08-12 Thread James Blachly via Digitalmars-d-learn

On Thursday, 14 June 2018 at 00:42:25 UTC, James Blachly wrote:

...
I assume the (apparent) lack of parity between ctor and dtor is 
because the "default postblit" (which I figured out for a 
struct means an empty `this(this)` ctor) is called when a copy 
is made. My understanding is that I cannot disable the default 
postblit and still act as a range, correct? Should I be 
overloading this?


2. Directly related to the above, I need, when the range is 
consumed, to free() the underlying library's iterator handle. 
Naively, I had the destructor do this, but obviously with 
multiple calls to ~this I end up with an error free()'ing a 
pointer that is no longer alloc'd.  What is the correct way to 
handle this situation in D?


Other Range and destructor advice generally (e.g., "You should 
totally change your design or approach to X instead") is always 
welcomed.


James


I think I have a handle on #1 (copy of the range is made for 
consumption which is why dtor is called more often than ctor), 
but would still be interested in advice regarding #2 (as well as 
general Range and dtor advice).


Here: 
https://github.com/blachlylab/dhtslib/blob/master/source/dhtslib/tabix.d#L98 I need to free the library's iterator, but the Range's destructor is the wrong place to do this, otherwise memory is freed more than once.


Is it a better approach to (a) somehow guard the call to 
tbx_itr_destroy or (b) create a postblit that creates a new 
iterator and pointer? (or (c), None of the above) As above, my 
understanding is that disabling the default posblit prohibits 
acting as a Range.


Thanks in advance


InputRange help: (1) repeated dtor calls and (2) managing resources needing free()

2018-06-13 Thread James Blachly via Digitalmars-d-learn

Hi all,

I now really appreciate the power Ranges provide and am an avid 
consumer, but am only slowly becoming accustomed to implementing 
my own.


In the present problem, I am writing a binding to a C library 
(htslib) that provides many functions related to high-throughput 
sequencing files. One of these functions is for rapid indexed 
lookup into multi-GB files. The library provides a handle to an 
iterator which must be supplied to a "get next matching row" type 
function, which overall seems perfect for implementation as a 
range.  You can see my naive implementation here:


https://github.com/blachlylab/dhtslib/blob/master/source/dhtslib/tabix.d

Note that TabixIndexedFile::region returns an InputRange; in the 
original implementation, this Range preloaded the first record 
(the ctor called popFirst()), but ultimately I realized this was 
not workable because copies of the object would always be 
non-empty. In some ways, this problem is generalizable to all 
InputRanges that represent a file or record stream.



My problems now are at least twofold.

1. If I use the range, the destructor seems to be called many, 
many times. This is directly related to problem 2, below, but I 
would be interested to understand why this is happening 
generally. For example, see:

https://github.com/blachlylab/dhtslib/blob/master/test/tabix_gffreader.d
Here, when I create the range but do not consume it, the ctor and 
dtor are called once each, as expected. However, if I 
foreach(line; r) { } the destructor is called twice. If I reason 
through this, it is because use of the range created a copy to 
consume. (?) However, if instead, I writeln( r ), the destructor 
is called *five* times. I cannot understand the reason for this, 
unless it is black magic required by writeln().


I assume the (apparent) lack of parity between ctor and dtor is 
because the "default postblit" (which I figured out for a struct 
means an empty `this(this)` ctor) is called when a copy is made. 
My understanding is that I cannot disable the default postblit 
and still act as a range, correct? Should I be overloading this?


2. Directly related to the above, I need, when the range is 
consumed, to free() the underlying library's iterator handle. 
Naively, I had the destructor do this, but obviously with 
multiple calls to ~this I end up with an error free()'ing a 
pointer that is no longer alloc'd.  What is the correct way to 
handle this situation in D?


Other Range and destructor advice generally (e.g., "You should 
totally change your design or approach to X instead") is always 
welcomed.


James


What is special about an immutable array of class objects, and why can I not .dup ?

2018-05-28 Thread James Blachly via Digitalmars-d-learn

Consider the below:

```
class C
{
int x;
}

struct S
{
int x;
}


void main()
{
immutable C[] c = [ new C(), new C()];
immutable S[] s = [ S(), S() ];
immutable int[] i = [ 1, 2 ];

auto x = c.dup;
auto y = s.dup;
auto z = i.dup;

}
```

This fails to compile with a `.dup` template matching error at 
line `auto x = c.dup;`. However, calling `.idup` works just fine. 
The immutable struct array and int array of course `.dup` just 
fine.


I would have guessed that changing the definition of `C[]` to 
`immutable(C)[]` would have also helped, but it did not.


Why are the class objects special in this case, and why does 
`immutable(C)[]` not help?   I believed that this defined a 
dynamic array `c` which was itself mutable, the elements of which 
were immutable.


Thanks for insights.




Why does enumerate over range return dchar, when ranging without returns char?

2018-05-02 Thread James Blachly via Digitalmars-d-learn
I am puzzled why enumerating in a foreach returns a dchar (which 
forces me to cast), whereas without the enumerate the range 
returns a char as expected.


Example:

```
import std.stdio;
import std.range : enumerate;

void main()
{
char[] s = ['a','b','c'];

char[3] x;
auto i = 0;
foreach(c; s) {
x[i] = c;
i++;
}

writeln(x);
}
```
Above works without cast.

'''
import std.stdio;
import std.range : enumerate;

void main()
{
char[] s = ['a','b','c'];

char[3] x;
foreach(i, c; enumerate(s)) {
x[i] = c;
i++;
}

writeln(x);
}
```
Above fails without casting c to type char.

The function signature for enumerate shows "auto" return type, so 
that does not help me understand.


Kind regards


Re: What is the "right" way to create a generic type getter (and setter) ?

2018-03-15 Thread James Blachly via Digitalmars-d-learn

On Wednesday, 14 March 2018 at 22:58:25 UTC, ag0aep6g wrote:
You can probably get around the (manually maintained?) `FIELDS` 
array with `.tupleof` or something similar:



static foreach (i, f; S.tupleof)
{
case __traits(identifier, f):
}


Any pointers / design patterns on this particular type of 
problem class would be greatly appreciated.  (Sidenote, I 
realize I could probably use the witchcraft library, but I am 
also using this as exercise to learn D beyond the basics).


You simply cannot have a method that returns different types 
based on a run-time value. You could possibly return a 
std.variant.Variant. But if the goal is just to print the value 
to the screen, all you need is a string.


So the signature would be `string get(string field)`. And for 
the implementation you could use `.tupleof` to iterate over all 
fields, and then return `f.to!string`.


`set` can be done similarly. Take two `string`s: the field 
name, and the value. `static foreach` over all fields. On a 
match, convert the given value string to the type of the field 
that matched.


Thanks - to!string certainly seems to be a good option in this 
case (CLI) and I was definitely overthinking this part, perhaps 
because I was trying to write everything as generically / 
extensibly as possible (for example, to use the same framework 
but with a GUI or web front end, for example).


I would still think an AA mapping (string) field name to a type 
would be useful and will see if I can construct it as a mixin 
using typeof(Struct.member) somehow.




What is the "right" way to create a generic type getter (and setter) ?

2018-03-14 Thread James Blachly via Digitalmars-d-learn
For context, please keep in mind I am coming from a python 
background, but am very much enjoying strong typing, although it 
is taking some significant adjustment.


Suppose I have a struct (which is really a memory map of a data 
file I am reading in) with too many data members to reasonably 
code getters/setters for by hand.  I wish to either retrieve 
individual values or set individual values, which could be 
numeric, boolean, or string, from the command line, à la:


$ prog -i inputfile.bin get field_name;
(prints "300" or "false" or "Welcome to the jungle")

$ prog -i inputfile.bin set some_field:9000
$ prog -i inputfile.bin set other_field:Whatever_String

Each field itself is strongly typed, for what that's worth.

Approaches I have considered and implemented in part are:
* templated getter (T get(T)(string field) {...}) but this 
approach requires knowledge of field types which I cannot 
reasonably expect to know at runtime(?)
*  modification to the above whereby I could have an AA 
holding type information for each field, generated by static 
foreach {mixin ...}, although I cannot get this to work as my 
struct's static constructor complains (rightly) that it cannot 
work without knowing 'this' at compile time. Code: 
`mixin("field_types[\"" ~ prop ~ "\"] = typeid(this." ~ prop ~ 
");");`  Is there another __trait I am missing that will give me 
the type of the struct member without requiring an instance of 
the struct?


I did manage to use metaprogramming inside my templated get 
function to handle numeric values, which was fascinating  
(although this is probably ugly code and it required a large enum 
array FIELDS):


```
GetterSwitch:
switch (field)
{
static foreach(prop; FIELDS ) {
mixin("case \"" ~ prop ~ "\": val = this." ~ prop 
~ "; break GetterSwitch;");

}
default:
val = 0;
assert(0);  // This is to prevent subtle bugs, 
but I need a better error handler

}
```


Any pointers / design patterns on this particular type of problem 
class would be greatly appreciated.  (Sidenote, I realize I could 
probably use the witchcraft library, but I am also using this as 
exercise to learn D beyond the basics).


Thanks in advance
James