Re: Why a template with Nullable does not compile?

2019-03-11 Thread Victor Porton via Digitalmars-d-learn

On Tuesday, 12 March 2019 at 05:14:21 UTC, Victor Porton wrote:

Why does this not compile?

import std.typecons;

template FieldInfo(T, Nullable!T default_) {
}

/usr/lib/ldc/x86_64-linux-gnu/include/d/std/typecons.d(2570,17): Error: `alias 
T = T;` cannot alias itself, use a qualified name to create an overload set
/usr/lib/ldc/x86_64-linux-gnu/include/d/std/typecons.d(3291,17): Error: `alias 
T = T;` cannot alias itself, use a qualified name to create an overload set


LDC - the LLVM D compiler (1.11.0):
  based on DMD v2.081.2 and LLVM 6.0.1



Why a template with Nullable does not compile?

2019-03-11 Thread Victor Porton via Digitalmars-d-learn

Why does this not compile?

import std.typecons;

template FieldInfo(T, Nullable!T default_) {
}

/usr/lib/ldc/x86_64-linux-gnu/include/d/std/typecons.d(2570,17): 
Error: `alias T = T;` cannot alias itself, use a qualified name 
to create an overload set
/usr/lib/ldc/x86_64-linux-gnu/include/d/std/typecons.d(3291,17): 
Error: `alias T = T;` cannot alias itself, use a qualified name 
to create an overload set




Re: Easiest way to display images

2019-03-11 Thread Murilo via Digitalmars-d-learn

Hi Adam, how do I set the color of the SimpleWindow background?


Re: Why does D language do not support BigDecimal type?

2019-03-11 Thread Heromyth via Digitalmars-d-learn

On Monday, 11 March 2019 at 15:23:34 UTC, BoQsc wrote:
There is Money datatype that can be provided by using a third 
party package: https://code.dlang.org/packages/money


But that's only for money, what about math?
Why such fundamental as BigDecimal is still not included into 
the D language itself?

There is BigInt.

If it is unavoidable to use Floating point, how can I quickly 
and simply understand the rules of using float to make the 
least error, or should I just find a third party package for 
that as well?



There is an article on that, but it is not that straight 
forward:

https://dlang.org/articles/d-floating-point.html

Basically any thing that I find on Google, that include 
explaining floating point are badly written and hard to 
understand for the outsider lacking ability to understand 
advanced concepts.


We have being porting one from Java. See 
https://github.com/huntlabs/hunt/blob/master/source/hunt/math/BigDecimal.d.


It's so sad that many methods are still commented out.


ggplotd Fixed ratio between x and y axes

2019-03-11 Thread kerdemdemir via Digitalmars-d-learn
How can I configure a fixed ratio between x and y axes in ggplotd 
?


I easily found what I am looking for in ggplot which ggplotd 
inspires a lot.

http://www.cookbook-r.com/Graphs/Axes_(ggplot2)/

But ggplotd documentation does not have any info about that. Even 
I go through the source code spend around half a hour I couldn't 
figure out how to achieve:


sp <- ggplot(dat, aes(xval, yval)) + geom_point()
sp + coord_fixed(ratio=1/3)

Erdem


inout auto ref escaping a reference to parameter, only errors with vibe Json type, or if inout is there.

2019-03-11 Thread aliak via Digitalmars-d-learn

Hi,

I have an error that says "Error: returning `match1(opt)` escapes 
a reference to parameter `opt`, perhaps annotate with `return`".


The code is here: https://run.dlang.io/is/ESZDW4 (It's copied at 
the end of this post as well)


1) If you remove the inout from line 11. It works.
2) If you *do not* call match2 and call match1 instead, it also 
works.
3) If you return something other than vibe's Json type, it works 
(afaict)


Am I using inout wrong? Why is it only happen with Json type so 
far. If I return any other random struct it works fine. And why 
does it work if I don't go through the "match2" template? Any 
help would be much appreciated.


---

template match1(handlers...) {
auto ref match1(T)(inout auto ref Optional!T opt) { // remove 
inout, it works

if (opt.empty) {
return  handlers[1]();
} else {
return  handlers[0](opt.front);
}
}
}

template match2(handlers...) {
auto match2(T)(auto ref Optional!T opt) {
return match1!handlers(opt);
}
}

void main() {
some(1)
.match2!( // use match1, it works
(int i) {return Json(1);}, // return anything else, 
it works

() {return Json(1);}
)
.writeln;
}

Cheers,
- Ali


Re: How are (Static) Libraries with Templates Compiled?

2019-03-11 Thread jmh530 via Digitalmars-d-learn

On Monday, 11 March 2019 at 20:11:37 UTC, ag0aep6g wrote:

On Monday, 11 March 2019 at 19:53:53 UTC, jmh530 wrote:
So what information is in the static library that allows this 
to take place?


None. The information is in in the source or interface file 
(.d/.di). Templates can't be compiled before they're 
instantiated.


Ah, so you need the .lib files and the .di files to get it work.


Re: How are (Static) Libraries with Templates Compiled?

2019-03-11 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Mar 11, 2019 at 07:53:53PM +, jmh530 via Digitalmars-d-learn wrote:
> Suppose I have a file with a simple templated function. I compile that
> to a static library. I then compile another file that uses the library
> into an executable. When compiled to the static library, you don't
> know in advance what types it will call on the templated function, so
> that must be resolved when compiling the executable. So what
> information is in the static library that allows this to take place?
> Is the library just a simple collection of object files?

Templates are never compiled into any object code.  Only template
instantiations are.

This is why you cannot elide the template body from a .di file --
because in order to instantiate the template, the compiler must know the
full definition of the template.  Unlike functions, where you can just
declare the function signature, and the body can be an opaque binary
blob that's only supplied in a precompiled object/library file.


T

-- 
Famous last words: I wonder what will happen if I do *this*...


Re: How are (Static) Libraries with Templates Compiled?

2019-03-11 Thread ag0aep6g via Digitalmars-d-learn

On Monday, 11 March 2019 at 19:53:53 UTC, jmh530 wrote:
So what information is in the static library that allows this 
to take place?


None. The information is in in the source or interface file 
(.d/.di). Templates can't be compiled before they're instantiated.


How are (Static) Libraries with Templates Compiled?

2019-03-11 Thread jmh530 via Digitalmars-d-learn
Suppose I have a file with a simple templated function. I compile 
that to a static library. I then compile another file that uses 
the library into an executable. When compiled to the static 
library, you don't know in advance what types it will call on the 
templated function, so that must be resolved when compiling the 
executable. So what information is in the static library that 
allows this to take place? Is the library just a simple 
collection of object files?


Re: Array of byLineCopy ranges behaves as they are byLine

2019-03-11 Thread Paul Backus via Digitalmars-d-learn

On Monday, 11 March 2019 at 17:33:31 UTC, HaraldZealot wrote:
Ah yes, I forget about laziness of `map`. BTW, I have found 
other solution, which is more fit to my initial intention.


```d
ReturnType!(std.stdio.File.byLineCopy!(char, 
immutable(char)))[] ranges;

foreach(filename; args[1 .. $])
{
ranges ~= File(filename, "r").byLineCopy;
}
```


An easier way to do this is to use the library function 
`std.array.array`:


auto ranges = args[1..$].map!(a => File(a, "r").byLineCopy).array;


Re: Array of byLineCopy ranges behaves as they are byLine

2019-03-11 Thread HaraldZealot via Digitalmars-d-learn

On Monday, 11 March 2019 at 17:04:56 UTC, ag0aep6g wrote:


To avoid the re-evaluation, assign `ranges[0]` to a variable 
before using it:


auto lines = ranges[0];
writeln(lines.front);
writeln(lines.front);
writeln(lines.front);

That should print the same line three times.


Ah yes, I forget about laziness of `map`. BTW, I have found other 
solution, which is more fit to my initial intention.


```d
ReturnType!(std.stdio.File.byLineCopy!(char, immutable(char)))[] 
ranges;

foreach(filename; args[1 .. $])
{
ranges ~= File(filename, "r").byLineCopy;
}
```


Re: Array of byLineCopy ranges behaves as they are byLine

2019-03-11 Thread ag0aep6g via Digitalmars-d-learn

On Monday, 11 March 2019 at 15:23:53 UTC, HaraldZealot wrote:

```d
File[] files;
foreach(filename; args[1 .. $])
{
files ~= File(filename, "r");
}

auto ranges = files.map!(a => a.byLineCopy);

writeln(ranges[0].front);
writeln(ranges[0].front);
writeln(ranges[0].front);
```
produces
```
1
2
3
```

[...]

What I'm doing wrong with `map`? Or is this a bug?


`map` is lazy in the sense that it (re-)evaluates the given 
function whenever you access an element. That means you're 
calling `byLineCopy` three times on the same file. Your code 
effectively does this:


writeln(files[0].byLineCopy.front);
writeln(files[0].byLineCopy.front);
writeln(files[0].byLineCopy.front);

The range created by `byLineCopy` immediately reads a line from 
the file to populate its `front`. So you're reading three lines 
from the file.


Strictly speaking, I don't think any of this qualifies as a bug. 
`map`'s behavior might be surprising, but it's deliberate, as far 
as I know.


To avoid the re-evaluation, assign `ranges[0]` to a variable 
before using it:


auto lines = ranges[0];
writeln(lines.front);
writeln(lines.front);
writeln(lines.front);

That should print the same line three times.


Array of byLineCopy ranges behaves as they are byLine

2019-03-11 Thread HaraldZealot via Digitalmars-d-learn
Let assume I have multiple files (like "file1", "file2" and so 
on). I need to iterate them simultaneously with some logic. I 
have tried to create random access range of ranges, however, it 
behaves not as expected. Let "file1" is

```
1
2
3
4
```

Then this code
```d
File[] files;
foreach(filename; args[1 .. $])
{
files ~= File(filename, "r");
}

auto ranges = files.map!(a => a.byLineCopy);

writeln(ranges[0].front);
writeln(ranges[0].front);
writeln(ranges[0].front);
```
produces
```
1
2
3
```
Even despite I'm using `byLineCopy`.

However, this code
```d
File[] files;
foreach(filename; args[1 .. $])
{
files ~= File(filename, "r");
}

auto range = files[0].byLineCopy;

writeln(range.front);
writeln(range.front);
writeln(range.front);
```
produses
```
1
1
1
```
as expected.

What I'm doing wrong with `map`? Or is this a bug?

dmd v2.085.0

P.S. I know that I can call `dup` on `front` and I'm going to do 
so as workaround. However, I'm curious why current situation is 
taking place.


Why does D language do not support BigDecimal type?

2019-03-11 Thread BoQsc via Digitalmars-d-learn
There is Money datatype that can be provided by using a third 
party package: https://code.dlang.org/packages/money


But that's only for money, what about math?
Why such fundamental as BigDecimal is still not included into the 
D language itself?

There is BigInt.

If it is unavoidable to use Floating point, how can I quickly and 
simply understand the rules of using float to make the least 
error, or should I just find a third party package for that as 
well?



There is an article on that, but it is not that straight forward:
https://dlang.org/articles/d-floating-point.html

Basically any thing that I find on Google, that include 
explaining floating point are badly written and hard to 
understand for the outsider lacking ability to understand 
advanced concepts.


Workaround for dub build-path problem

2019-03-11 Thread bitwise via Digitalmars-d-learn

https://github.com/dlang/dub/issues/658

As noted in the above issue, dub runs in the root project 
directory for all packages, including dependancies. So if any 
project aside from the root project includes a relative path in 
it's dub.json, that dub build will break, due to the incorrect 
working directory. I can't even use $PACKAGE_DIR to fix this 
because it doesn't work half the time (invalid identifier).


How do people deal with this?


Re: Distinguish float and integer types from string

2019-03-11 Thread XavierAP via Digitalmars-d-learn

On Monday, 11 March 2019 at 15:03:39 UTC, XavierAP wrote:


What compiler version are you using? I on the other hand was 
surprised that I needed the try-catch above, after having 
already checked isNumeric. The documentation claims that the 
conversion to int or long would truncate, but my compiler 
(v2.084.0) throws instead.


Of course now I realize that using try-catch I no longer need to 
check isNumeric... My design didn't use try-catch but I had to 
add it because std.conv:to behaves differently from the 
documentation:


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

Not sure if I need to update my DMD, or it's the documentation 
that's out of date, or something else is wrong.


Re: Distinguish float and integer types from string

2019-03-11 Thread XavierAP via Digitalmars-d-learn

On Saturday, 9 March 2019 at 18:11:09 UTC, Jacob Shtokolov wrote:


One of the task was to take a string from STDIN and detect its 
type.
There were a few options: Float, Integer, string and "something 
else" (which, I think, doesn't have any sense under the scope 
of the task).


Another std-based solution I came up with:

bool isInteger(string str)
{
if(str.isNumeric)
{
try { return str.to!long == str.to!real; }
catch(ConvException) { return false; }
}
else return false;
}

I tried to use std.conv.to and std.conv.parse, but found that 
they can't really do this. When I call `data.to!int`, the value 
of "123.45" will be converted to int!


What compiler version are you using? I on the other hand was 
surprised that I needed the try-catch above, after having already 
checked isNumeric. The documentation claims that the conversion 
to int or long would truncate, but my compiler (v2.084.0) throws 
instead.


Re: Distinguish float and integer types from string

2019-03-11 Thread Johann Lermer via Digitalmars-d-learn

On Saturday, 9 March 2019 at 18:11:09 UTC, Jacob Shtokolov wrote:
I tried to use std.conv.to and std.conv.parse, but found that 
they can't really do this. When I call `data.to!int`, the value 
of "123.45" will be converted to int!


Are you sure? This here works for me:

import std.stdio;
import std.string;
import std.conv;

void main ()
{
auto s = readln.strip;

try
{
int i = to!int (s);
writefln ("string is an int: %s", i);
}
catch(Exception e)
{
try
{
float f = to!float(s);
writefln ("string is a float: %s", f);
}
catch(Exception e)
{
writefln ("string is an neither an int nor a float: 
%s", s);

}
}
}


Re: Distinguish float and integer types from string

2019-03-11 Thread Soulsbane via Digitalmars-d-learn

On Saturday, 9 March 2019 at 18:11:09 UTC, Jacob Shtokolov wrote:

Hi,

Recently, I was trying to solve some funny coding challenges 
(https://www.techgig.com).
The questions were really simple, but I found it interesting 
because the website allows to use D.


One of the task was to take a string from STDIN and detect its 
type.
There were a few options: Float, Integer, string and "something 
else" (which, I think, doesn't have any sense under the scope 
of the task).


Anyway, I was struggling to find a built-in function to 
distinguish float and integer types from a string.


I came to the following solution:

```
import std.stdio;
import std.range;
import std.conv;
import std.string;
import std.format;

immutable msg = "This input is of type %s";

void main()
{
string type;
auto data = stdin.byLine.takeOne.front;

if (data.isNumeric) {
type = data.indexOf(".") >= 0 ? "Float" : "Integer";
}
else {
type = "string";
}

writeln(msg.format(type));
}
```

But I think that's ugly. The thing is that in PHP, for example, 
I would do that like this:


```
if (is_integer($data)) {
//...do smth
}
else if (is_float($data)) {
//...do smth
}
else {
//...do smth
}
```

I tried to use std.conv.to and std.conv.parse, but found that 
they can't really do this. When I call `data.to!int`, the value 
of "123.45" will be converted to int!


Is there any built-in way to detect these types?

Thanks!


Unless I'm missing something perhaps two functions like this:

bool isInteger(string value) pure nothrow @safe
{
import std.string : isNumeric;
	return (isNumeric(value) && value.count(".") == 0) ? true : 
false;

}

bool isDecimal(string value) pure nothrow @safe
{
import std.string : isNumeric;
	return (isNumeric(value) && value.count(".") == 1) ? true : 
false;

}