Re: Defining an alias to an overloaded function

2020-01-20 Thread Boris Carvajal via Digitalmars-d-learn

On Monday, 20 January 2020 at 22:02:54 UTC, olvy wrote:
I'm learning D, and as an exercise, I'm trying to define a 
HashSet that would be a wrapper around an associative array 
with some dummy value type.


This seems to work:

...
struct RangeImpl(T) {
alias byKeyRetType = 
typeof(byKey!(int[T])((int[T]).init));

byKeyRetType keyRange;
this(ref int[T] d) {
keyRange = d.byKey;
}
bool empty() {
return keyRange.empty;
}
...
}

RangeImpl!T opSlice() {
return RangeImpl!T(_dict);
}
}

void main()
{
...
if (h[].all!(nn => nn < 5)) {
writeln("less than 5");
}



Re: Type Inference and Try Blocks

2020-01-20 Thread user1234 via Digitalmars-d-learn

On Monday, 20 January 2020 at 23:16:07 UTC, Henry Claesson wrote:
This isn't a D-specific "problem", but there may be D-specific 
solutions.
I have a function `doSomething()` that returns a Voldemort 
type, and this same function also throws. So, there's this:


try {
auto foo = doSomething();
} catch (AnException e) {
// Do stuff
}

The problem that I'm encountering is that I'd like, assuming no 
exception was thrown, to use foo outside the `try` (or 
`finally`) block to avoid nesting as any operations on `foo` 
from that point onward may also throw. Are there any constructs 
that act as alternatives to try/catch/finally so that I can do 
this?


(This issue could very well stem from poor design and not being 
familiar with programming using exceptions. So feel free to 
ignore.)


Thanks


The problem you have is not called type inference, it's called 
"scope".
In a particular scope you have symbols "S". In a sub scope you 
also have "S" but in the parent scope "S" are not existing 
anymore.




Re: Type Inference and Try Blocks

2020-01-20 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 20 January 2020 at 23:16:07 UTC, Henry Claesson wrote:
This isn't a D-specific "problem", but there may be D-specific 
solutions.
I have a function `doSomething()` that returns a Voldemort 
type, and this same function also throws. So, there's this:


try {
auto foo = doSomething();
} catch (AnException e) {
// Do stuff
}


I'd suggest just doing

try {
auto foo = doSomething();
// use foo right here!
} catch(AnException e) {
// do other stuff
} catch(OtherException e) {
   // reminder you can do this too btw
}


That is, put ALL the use of foo inside the one try block, don't 
keep nesting - only try/catch when you can specifically recover 
or add information to it at that particular point. If you can't 
do that, just keep going. try/catch over individual functions is 
often (though not always) poor design.


If you really do need the variable outside, you can do

typeof(doSomething(args...)) foo;
try {
  foo = doSomething()
}

too to declare the var outside. But first I'd try getting it all 
in that try block.


You can also do helper functions for some cases too btw

auto getfoo() {
   try return doSomething();
   catch(MyException e) { return alternative(); }
}

and remember you can define that right inside the function; 
nested functons rock.




Type Inference and Try Blocks

2020-01-20 Thread Henry Claesson via Digitalmars-d-learn
This isn't a D-specific "problem", but there may be D-specific 
solutions.
I have a function `doSomething()` that returns a Voldemort type, 
and this same function also throws. So, there's this:


try {
auto foo = doSomething();
} catch (AnException e) {
// Do stuff
}

The problem that I'm encountering is that I'd like, assuming no 
exception was thrown, to use foo outside the `try` (or `finally`) 
block to avoid nesting as any operations on `foo` from that point 
onward may also throw. Are there any constructs that act as 
alternatives to try/catch/finally so that I can do this?


(This issue could very well stem from poor design and not being 
familiar with programming using exceptions. So feel free to 
ignore.)


Thanks


Re: dub dustmite struggles

2020-01-20 Thread DanielG via Digitalmars-d-learn

On Monday, 20 January 2020 at 15:04:32 UTC, Andre Pany wrote:
In general dub Dustmite works fine, I used it several times. 
Maybe it has to do s.th. with your project structure. Please 
create a dub issue with an example zip.


I've created a minimal nested project with a similar structure 
(library with nested app project), and dub-dustmite works fine 
there. So it must be something peculiar to my real project.


Once the standalone dustmite finishes running, I'll spend some 
time manually reducing my project structure to something 
repeatable to file an issue with.


Thanks!






Defining an alias to an overloaded function

2020-01-20 Thread olvy via Digitalmars-d-learn
I'm learning D, and as an exercise, I'm trying to define a 
HashSet that would be a wrapper around an associative array with 
some dummy value type.


This worked fine until I've tried writing opSlice for this 
HashSet in terms of the byKey() function of the AA.  I defined my 
own internal type that wraps the one returned by byKey and 
returned it.  However I had to keep byKey's result inside my 
internal type, but I couldn't figure out how to name it properly 
without getting a compilation error.


The problem is that byKey is overloaded and but I can't figure 
out how to convince the compiler to disambiguate between the 
overloads.


Another lesser problem is that I have to write "opSlice" 
explicitly at the call site instead of using [].   But it might 
be that the compiler is confused due to failure of parsing the 
function.


My aim isn't building a HashSet, the point is learning, here 
specifically learning how to convince the compiler to use the 
correct byKey overload in this case.
I could also trivially use the AA's keys() function to implement 
opSlice.  But I like byKey since it exposes just an iterator, 
while keys() actually allocates an array which isn't necessary 
here.


I'm using dmd 2.089 on my machine.

My code is (also in https://wandbox.org/permlink/zXsf7sQiKDj6zjlJ)

import std;

struct HashSet(T) {
int[T] _dict;

bool add(T val) {
auto prev = val in _dict;
_dict[val] = 0;
return prev is null;
}

bool remove(T val) {
return _dict.remove(val);
}

bool opBinaryRight(string op)(T val) {
static if (op == "in") {
return (val in _dict) !is null;
} else {
static assert(false, "Operator " ~ op ~ " not 
implemented");

}
}

import std.traits;
import std.meta;
struct RangeImpl(T) {
// Next line has compilation error since byKey has 2 
overloads

alias byKeyAlias = Alias!(byKey!(int[T])(T.init));
ReturnType!(byKeyAlias) keyRange;
// alias byKeyAlias = byKey;
// ReturnType!(byKey!(int[T], T, int)) keyRange;
this(ref int[T] d) {
keyRange = d.byKey();
}
bool empty() const {
return keyRange.empty;
}
ref T front() {
return keyRange.front;
}
void popFront() {
keyRange.popFront();
}
}

// T[] opSlice(T)() {
// return _dict.keys;
// }

RangeImpl!T opSlice(T)() {
return RangeImpl!T(_dict);
}

}

void main()
{
HashSet!long h;
h.add(0);
h.add(1);
if (1 in h) {
writeln("it is");
}
if (h.opSlice!long.all!(nn => nn < 5)) {
writeln("less than 5");
}
}


Re: dub dustmite struggles

2020-01-20 Thread Andre Pany via Digitalmars-d-learn

On Monday, 20 January 2020 at 06:48:08 UTC, DanielG wrote:
I can't seem to figure out what dub's dustmite command is 
looking for with its regexes. No matter what I try - no matter 
how simple - the initial test fails.


I am able to run dustmite standalone just fine with the 
following test script:


cd example
dub 2>&1 | grep -F "ScrollView6__initZ+0xd8): undefined 
reference to \`internal'"


However, when I attempt using 'dub dustmite' with 
--linker-regex (or --linker-status, even), the initial test 
always fails. I've also tried simplifying the regex on the 
assumption that I'm not escaping things properly - to no avail.


Is it perhaps something to do with my project structure? My 
project is a library containing an /example subfolder, 
containing an application dub project, and that's where my 
linker error occurs, not in the library itself. So that's where 
I'm attempting to run dub dustmite as well.


In general dub Dustmite works fine, I used it several times. 
Maybe it has to do s.th. with your project structure. Please 
create a dub issue with an example zip.


But yes, it could take also with dub a lot of time.

One thing which caused me a lot of headaches, dmd is lies about 
the error message if coloured output is on. Then the error text 
lacks some characters.


Kind regards
Andre


Re: Specify dmd or ldc compiler and version in a json dub file?

2020-01-20 Thread Andre Pany via Digitalmars-d-learn

On Monday, 20 January 2020 at 11:54:43 UTC, Bastiaan Veelo wrote:
On Tuesday, 8 August 2017 at 09:17:02 UTC, data pulverizer 
wrote:
I would like to know how to specify dmd or ldc compiler and 
version in a json dub file.


Update: you can at least specify these in the toolchain 
requirements section: 
https://dub.pm/package-format-json.html#toolchain-requirements


I myself am looking for ways to satisfy these automatically, 
using a tool like dvm (https://code.dlang.org/packages/dvm) in 
preBuildCommands.


Bastiaan.


In the meantime, the dub settings json could also be placed in 
the project folder and has precedence. Although I would not 
recommend to distribute the file as it is meant for your local 
working computer.


Kind regards
Andre


Re: Specify dmd or ldc compiler and version in a json dub file?

2020-01-20 Thread Bastiaan Veelo via Digitalmars-d-learn

On Tuesday, 8 August 2017 at 09:17:02 UTC, data pulverizer wrote:
I would like to know how to specify dmd or ldc compiler and 
version in a json dub file.


Update: you can at least specify these in the toolchain 
requirements section: 
https://dub.pm/package-format-json.html#toolchain-requirements


I myself am looking for ways to satisfy these automatically, 
using a tool like dvm (https://code.dlang.org/packages/dvm) in 
preBuildCommands.


Bastiaan.