DMD Stdio Linker Oddities

2016-12-24 Thread Jake Pittis via Digitalmars-d-learn
I'm posting this in Learn because I'm assuming I've done 
something wrong rather than discovered a bug.


Running `dmd -main main.d` with the following 3 files produces 
the following linker error.


```
$ dmd -main main.d
Undefined symbols for architecture x86_64:
  "_D13linking_fails12__ModuleInfoZ", referenced from:
  _D4main12__ModuleInfoZ in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to 
see invocation)

Error: linker exited with status 1
```

```
// main.d
import linking_succeeds;
import linking_fails;
```

```
// linking_succeeds.d
import std.range;
```

```
// linking_fails.d
import std.stdio;
```

In the `linking_fails.d` file, if I replace the stdio import with 
`import std.algorithm`, running `dmd -main main.d` succeeds. 
Somehow stdio is causing the linker to fail.


Any idea what's going on or how to fix it? Thanks!

(DMD64 D Compiler v2.072.0, macOS Sierra 10.12)


Re: How to serialize a double.

2016-12-01 Thread Jake Pittis via Digitalmars-d-learn

On Thursday, 1 December 2016 at 07:13:45 UTC, Bauss wrote:

On Thursday, 1 December 2016 at 00:36:30 UTC, Jake Pittis wrote:

[...]


You could do something like below which will allow you to 
serialize any number.



import std.stdio : writeln;
import std.traits : isNumeric;

ubyte[] bytes(T)(T num) if (isNumeric!T) {
auto buf = new ubyte[T.sizeof];

(*cast(T*)(buf.ptr)) = num;

return buf;
}

T value(T)(ubyte[] buf) if (isNumeric!T) {
return (*cast(T*)(buf.ptr));
}


And example usage:

double foo = 3.14;

writeln(foo); // Prints 3.14

ubyte[] bar = foo.bytes;

writeln(bar); // Prints the bytes equal to 3.14

foo = bar.value!double;

writeln(foo); // Prints 3.14



Regarding the test assertion that failed. Turns out I had a bug 
in the test. (of course)

This last solution is very pretty. Thanks.

You folks are all so kind!


How to serialize a double.

2016-11-30 Thread Jake Pittis via Digitalmars-d-learn

How do I convert a double to a ubyte[]?

I've tried all sorts of things including converting the double to 
a ulong and trying to serialize the ulong. For example test 
bellow fails.



unittest {
double d = 3.14;
ulong l = *cast(ulong*)(&d);
double after = *cast(double*)(&l));
assert(after == d); // This fails.
}



Unable to convert a MapResult to an InputRange.

2016-09-18 Thread Jake Pittis via Digitalmars-d-learn


import std.stdio;
import std.array;
import std.algorithm;
import std.range;

InputRange!string keys(int[string] foo) {
  return foo.byPair.map!(p => p[0]);
}

void main() {
  int[string] foo;
  foo["lol"] = 1;
  foo["wat?"] = 2;
  keys(foo).each!(p => writeln(p));
}


The above code produces an `cannot implicitly convert expression 
(map(byPair(foo))) of type MapResult!(__lambda2, 
MapResult!(__lambda2, Result)) to 
std.range.interfaces.InputRange!string` error.


If I replace `InputRange!string` with `auto`, it compiles.

I'm intending to use InputRange!string as a return type in an 
interface so I can't use auto.



interface bar {
  InputRange!string keys();
}


I don't want to convert it to an array because of the allocation. 
Isn't the whole point of ranges to pass around a lazy interface 
without having to allocate any extra memory? Why can't I cast the 
output of map to an InputRange or something similar?


Thanks for the help. :) Phobos documentation is lovely but I've 
been finding errors similar to this to be quite frustrating.