Re: Using map result type

2019-12-09 Thread AA via Digitalmars-d-learn

On Sunday, 8 December 2019 at 01:43:43 UTC, mipri wrote:

On Sunday, 8 December 2019 at 01:10:21 UTC, AA wrote:

[...]


In general this is what you want to do with any kind of range
code, because you're not working with definite types, but with
types that have certain properties. And an advantage of this is
that you can decide to do more or less efficient things at
compile time based on additional properties of the types you're
working with. Just open up phobos and look for 'static if':

https://github.com/dlang/phobos/blob/master/std/algorithm/searching.d#L3858

So if you you give minPos an array, it'll just foreach() over
the array and return a slice. In which case minPos is exactly
as efficient as the code you would've written by hand instead
of using minPos, because it expands to that same code.

And if you give minPos something else, it'll still work.


[...]



[...]


Not really. Even if you try to use typeof() to get the
(internal) type that std.alogorithm.map returns, you'll run
into errors like

Error: function x253.mapAccepter(MapResult!(__lambda5, int[]) 
r) is not callable using argument types (MapResult!(__lambda1, 
int[]))


when you try to use it.


[...]


Certainly. Static reflection's what this kind of generic code
is all about. See all the constraints in the std.algorithm pages
in the library documentation?  Just look at this one, randomly
picked:
https://dlang.org/phobos/std_algorithm_searching.html#.endsWith

  if (isBidirectionalRange!Range
  && (Needles.length > 1)
  && is(typeof(.endsWith!pred(doesThisEnd, 
withOneOfThese[0])) : bool)
  && is(typeof(.endsWith!pred(doesThisEnd, 
withOneOfThese[1..$])) : uint));


So for your code:

  void mapAccepter(Range)(Range r)
  if (is(ElementType!Range == bool))
  {
  import std.array : array;
  import std.stdio : writeln;

  auto collected = r.array;
  writeln(collected);
  }

Or if you do this often, maybe something like this would do:

  enum RangeOfBools(T) = is(ElementType!T == bool) && 
isInputRange!T;


  void mapAccepter(Range)(Range r) if (RangeOfBools!Range)
  {
  import std.array : array;
  import std.stdio : writeln;

  auto collected = r.array;
  writeln(collected);
  }


Thanks for the detailed reply. So maybe it is just my perspective 
coming from other languages that I was expecting a class i.e. 
having some interface based way of dealing with the return type. 
But I guess isInputRange checks for structural equality rather 
than class and doesn't encode that into any interfaces in std 
library normally?


Would the second solution of declaring a template constraint like 
that be considering strange/out of place in D? e.g. do people 
normally try and declare the template constraints on a function 
or just rely on compile time failure from to instantiate template.


Re: How add "version.txt" Version File by Command Line or by resources.res using dmd.exe

2019-12-09 Thread rumbu via Digitalmars-d-learn

On Sunday, 8 December 2019 at 20:50:05 UTC, Marcone wrote:

I want to add version to my program.
I have configurated my version file "version.txt",  but I dont 
know how link this file to my program. If Need spec file, 
please send the exemple code of spec. Or is is possible add 
version file by dmd command line or resources. Thank you.


Your version.txt file is python specific, it will not work in D.

You have 2 options:
- create a .res file in Visual Studio and edit it by adding a 
VERSIONINFO resource.
- create a .rc file in any text editor adding a VERSIONINFO 
resource; compile it to .res using rc yourfile.rc


Pass the .res file to the dmd compiler in the command line.


Re: GC.collect inflating memory usage?

2019-12-09 Thread cc via Digitalmars-d-learn

On Sunday, 8 December 2019 at 17:49:09 UTC, Rainer Schuetze wrote:


Seems like a bug introduced in dmd 2.086, I've created a 
bugzilla issue: https://issues.dlang.org/show_bug.cgi?id=20438


I suspect there is something broken with respect to the 
free-lists inside the GC when manually freeing memory :-/




Fixed in stable for the next point-release.


Cool, thanks for the update.