Re: weird exception on windows

2017-12-18 Thread unleashy via Digitalmars-d-learn
On Friday, 15 December 2017 at 21:56:48 UTC, Steven Schveighoffer 
wrote:

On 12/15/17 10:08 AM, Kagamin wrote:

Maybe this https://issues.dlang.org/show_bug.cgi?id=18084


Thanks for looking into this. I created a PR to fix.

Szabo, can you please try with this patch and see if it fixes 
your issue?


https://github.com/dlang/phobos/pull/5932

-Steve


I created the original issue in Szabo's post. I applied your fix, 
but nothing changed—the test program still crashes with the same 
exception :/


What gives?


Re: Treating a slice as an InputRange

2017-11-15 Thread unleashy via Digitalmars-d-learn

Thanks for the insights everyone, it really helped!

I actually discovered that it wasn't working because one of the 
parsing functions used `std.range.take` and, since I was giving 
it a slice, `take` decided to save the fwdrange instead of 
mutating it. I realised the `take` call was 100% useless, so I 
removed it and it works perfectly now, and refactored to be more 
idiomatic :)


On Wednesday, 15 November 2017 at 21:02:35 UTC, Jonathan M Davis 
wrote:
If you specifically want a function to accept a range and 
mutate it without returning it, then it should take its 
argument by ref. Having it take auto ref is actually quite odd, 
since that means that the behavior can depend on whether an 
lvalue or rvalue is passed in.


I was under the impression that templated parameters needed `auto 
ref` to work as `ref` properly. Good to know that's not true.


On Wednesday, 15 November 2017 at 21:11:16 UTC, Steven 
Schveighoffer wrote:

I'd model your functions after the std.conv.parse functions:

https://dlang.org/phobos/std_conv.html#parse

This always takes the range by reference, and takes the type to 
parse via a template parameter (more idiomatic D to have 
parse!int than parseInt).


Yes, that is much more idiomatic than what I was going for. I've 
been writing some Java, so I guess it got to my head :)


On Wednesday, 15 November 2017 at 21:14:04 UTC, Ali Çehreli wrote:

That should work:

import std.range;

T read(T, Range)(auto ref Range range) if (isInputRange!Range) {
   range.popFront();
   return 42;
}

unittest {
   ubyte[] slice = [ 1, 2 ];
   read!int(slice);
   assert(slice == [2]);
}

void main() {
}


That is exactly what I was looking for, thanks!


Treating a slice as an InputRange

2017-11-15 Thread unleashy via Digitalmars-d-learn

Hello,

I'm writing a small parser for a specific binary format. The 
format is parsed by way of a sequence of functions, each 
deserializing a small portion of the format into a D type such as 
int, double, string, etc., operating on a single InputRange. The 
problem is, if I pass a slice to each of these functions, the 
slice doesn't get mutated by the popFront function used in the 
functions, so something like this:


ubyte[] slice;
...
int a = readInt(slice);
double b = readDouble(slice);

Ends up failing, because readInt properly reads an int, but the 
slice is not mutated "outside" of itself, which means readDouble 
will not read from where readInt stopped, and instead read from 
the start of the slice, and failing because there is an int 
encoded there and not a double. Both functions' signatures are 
like this:


T readXXX(Range)(auto ref Range range) if (isInputRange!Range)

So my question is, is there a way to treat a slice strictly as an 
InputRange, so that it is mutated no matter what? Or is there 
another way to do what I'm trying to do?


I've worked around it using a "wrapper" InputRange struct, but I 
feel like there must be another way.


Thanks!


Re: Undefined symbol for, apparently, valid code?

2017-07-06 Thread unleashy via Digitalmars-d-learn

On Thursday, 6 July 2017 at 16:16:17 UTC, H. S. Teoh wrote:
Which version of the compiler are you using?  I just tested on 
the latest dmd git HEAD, and it (correctly) tells me that it's 
illegal to override a non-virtual function.  I'm surprised you 
got your code to compile at all.


`dmd --version` outputs:

---
DMD32 D Compiler v2.074.1
Copyright (c) 1999-2017 by Digital Mars written by Walter Bright
---

I downloaded it 3 days ago, for Windows. It's not git HEAD, but 
it's the one available for download in the homepage. Thankfully, 
it seems fixed for the next version then?






Re: Undefined symbol for, apparently, valid code?

2017-07-06 Thread unleashy via Digitalmars-d-learn

On Thursday, 6 July 2017 at 06:48:57 UTC, rikki cattermole wrote:

Templates+classes = require function body.

Why? Templated methods are not virtual, they are final and 
cannot be inherited (so its a little strange that the override 
is valid).


Ah well. I would've expected a compiler error, not an obscure 
linker error, but as Arafel said:


Finally, have also in mind that if the function had been 
declared abstract (as it arguably should), a compile-time error 
would have been generated [2].


Maybe it was an error on my part for not declaring the function 
as abstract? My view was that the abstract attribute on a class 
marks all its members as virtual unless they have a body, which 
is how it works in, say, Java.


Still, kinda odd that the linker is the one to call me out, and 
not the compiler. Pretty unexpected.




Undefined symbol for, apparently, valid code?

2017-07-06 Thread unleashy via Digitalmars-d-learn

Hello. I am trying to compile this:

---
module asd.asd;

abstract class Asd
{
void opCall(Args...)(Args args);
}

@system unittest
{
class Foo : Asd
{
override void opCall(Args...)(Args args)
{
/* nothing */
}
}

Asd a = new Foo();

a(1, 2, 3);
}
---

This file is under source/asd/asd.d and I'm compiling with `dmd 
-unittest -main -ofasd.exe source\asd\asd.d -m32 -g` under 
Windows x64. For some reason, after the successful compilation 
step, the linker then complains:


---
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
asd.obj(asd)
 Error 42: Symbol Undefined 
_D3asd3asd3Asd17__T6opCallTiTiTiZ6opCallMFiiiZv

Error: linker exited with status 1
---

Am I doing something wrong, or is this a linker bug?

Thanks!


Re: Interfacing with C - calling member function of D struct from C?

2017-06-25 Thread unleashy via Digitalmars-d-learn

On Sunday, 25 June 2017 at 02:09:53 UTC, Adam D. Ruppe wrote:

On Sunday, 25 June 2017 at 02:05:35 UTC, unleashy wrote:

How would I call `addToBar` from C code?


You don't. Instead write it like:

struct Foo
{
int bar;
}

extern(C) void addToBar(Foo* foo, int what) {
   foo.bar += what;
}


Then define it in C the same way and you call it the normal way 
from C.


But from D, you can UFCS call it:

Foo* foo = new Foo();
foo.addToBar(5); // cool


though I'd prolly just call it from D the same way you do from 
C too.


Thank you, this is what I suspected :)


Interfacing with C - calling member function of D struct from C?

2017-06-24 Thread unleashy via Digitalmars-d-learn

Hello! If I have a D struct like:

struct Foo
{
int bar;

void addToBar(int what) {
bar += what;
}
}

How would I call `addToBar` from C code? Would I need to put the 
`addToBar` function outside of the struct and mark it as `extern 
(C)` and in normal D code take advantage of UFCS or is there some 
magic C incantation?


I've scoured the forums and other places for anything about this 
but couldn't find any information whatsoever... so yeah. (or my 
Google-fu is terrible)


Thanks!