Re: Is there a way to not escape slashes when parsing JSON?

2022-02-21 Thread bachmeier via Digitalmars-d-learn

On Tuesday, 22 February 2022 at 00:44:58 UTC, jmh530 wrote:

On Tuesday, 22 February 2022 at 00:36:38 UTC, bachmeier wrote:

[snip]

Yes. std.random is another. I gave up out on the current one. 
Luckily I already had external libraries for that before I 
started using D.


Have you tried mir.random?


I haven't done much with it. I already have what I need, so not 
much motivation to use anything else. The main advantage of 
std.random is that it eliminates an external dependency.


Re: Is there a way to not escape slashes when parsing JSON?

2022-02-21 Thread jmh530 via Digitalmars-d-learn

On Tuesday, 22 February 2022 at 00:36:38 UTC, bachmeier wrote:

[snip]

Yes. std.random is another. I gave up out on the current one. 
Luckily I already had external libraries for that before I 
started using D.


Have you tried mir.random?


Re: Is there a way to not escape slashes when parsing JSON?

2022-02-21 Thread bachmeier via Digitalmars-d-learn

On Monday, 21 February 2022 at 22:58:17 UTC, Ali Çehreli wrote:

On 2/21/22 09:34, bachmeier wrote:

> I may have to look for an alternative
> JSON library for D. std.json is not the most fun independent
of this issue.

std.json is a very good module. At work, we had to write 
additional code to cover its defficiencies.


Looking forward to versioning in Phobos so that we can get to 
better implementations of many old modules...


Ali


Yes. std.random is another. I gave up out on the current one. 
Luckily I already had external libraries for that before I 
started using D.


Re: Offline D documentation/tutorial

2022-02-21 Thread LorenDB via Digitalmars-d-learn
On Wednesday, 16 February 2022 at 20:07:09 UTC, Christian Köstlin 
wrote:
1. I was pointed by Seb to https://devdocs.io/d/ which offers 
an offline mode via html5.


Thanks, that looks promising!




Re: Is there a way to not escape slashes when parsing JSON?

2022-02-21 Thread Ali Çehreli via Digitalmars-d-learn

On 2/21/22 14:58, Ali Çehreli wrote:

> std.json is a very good module.

Correction: std.json is NOT a very good module.

Ali



Re: how to return map(fn)

2022-02-21 Thread Ali Çehreli via Digitalmars-d-learn

On 2/21/22 12:44, steve wrote:

> I had a look at the source code for map but it seems to return a private
> struct, which doesn't help.

What map and other Phobos algorithms return are called Voldemort types 
for the reason you state: They are unmentionable. The usual way is to 
return 'auto' and the template system will take care of the rest:


auto mapped(alias f, Range) (Range range) {
  import std.algorithm : map;
  return range.map!f();
}

void main() {
  auto r = [1, 2].mapped!(i => 10 * i);

  import std.algorithm;
  assert(r.equal([10, 20]));
}

And I am sure your function is more capable than being the equivalent as 
map. ;)


Ali



Re: SumType alias can't be deduced?

2022-02-21 Thread Emmanuelle via Digitalmars-d-learn

On Monday, 21 February 2022 at 20:18:46 UTC, Paul Backus wrote:
This is a long-standing limitation of the D compiler's template 
argument deduction: it cannot "see through" `alias` templates 
to deduce the underlying type.


Oh, that’s an unfortunate limitation but at least there’s a 
workaround. Thank you!


Re: Is there a way to not escape slashes when parsing JSON?

2022-02-21 Thread bauss via Digitalmars-d-learn

On Monday, 21 February 2022 at 15:13:52 UTC, Kagamin wrote:

On Monday, 21 February 2022 at 09:04:06 UTC, bauss wrote:
Why are we even escaping them by default, it should be the 
other way around, that slashes are only escaped if you ask for 
it; that's how it literally is in almost every JSON library.


Really? I always see escaped slashes in JSON, e.g. wikipedia 
does this, but everything else too.


I'm going to assume that JS is probably the most used language 
for JSON, since it originated from it, so a small demonstration 
will show you that even JS defaults to not escaping slashes:


```
let o = { a: "/path/to/something" };
console.log(JSON.stringify(o));
```

Output:

```
{"a":"/path/to/something"}
```


Re: Is there a way to not escape slashes when parsing JSON?

2022-02-21 Thread Ali Çehreli via Digitalmars-d-learn

On 2/21/22 09:34, bachmeier wrote:

> I may have to look for an alternative
> JSON library for D. std.json is not the most fun independent of this 
issue.


std.json is a very good module. At work, we had to write additional code 
to cover its defficiencies.


Looking forward to versioning in Phobos so that we can get to better 
implementations of many old modules...


Ali



Re: Is there a way to not escape slashes when parsing JSON?

2022-02-21 Thread bachmeier via Digitalmars-d-learn

On Monday, 21 February 2022 at 17:50:56 UTC, bachmeier wrote:

I looked at the source for `parseJSON` and I see references 
only to `JSONOptions.strictParsing` and 
`JSONOptions.specialFloatLiterals`. I may be missing something, 
but I don't see any option to iterating over every element and 
unescaping manually.


So it looks like `.toString(JSONOptions.doNotEscapeSlashes)` is 
the correct way to do what I need:


```
import std.conv, std.json, std.stdio;

void main() {
auto json = parseJSON(`{"a": "path/file"}`);
writeln(json["a"].toString);
writeln(json["a"].to!string);
writeln(json["a"].toString(JSONOptions.doNotEscapeSlashes));
}
```

I was using to!string to be consistent with my other D code, and 
thought that would be okay since toString returns the same value. 
What I didn't realize is that I might need to send options to 
toString.


how to return map(fn)

2022-02-21 Thread steve via Digitalmars-d-learn

following this example in the documentation of map:

```
import std.algorithm.comparison : equal;
import std.conv : to;

alias stringize = map!(to!string);
assert(equal(stringize([ 1, 2, 3, 4 ]), [ "1", "2", "3", "4" ]));
```


I would like to write a function that takes as its parameter a 
function and returns something like the alias in the example. 
i.e. something like:



```
float[] function(float[]) get_map(alias f){
   return map!(f)
}
```
I had a look at the source code for map but it seems to return a 
private struct, which doesn't help. Is there any way to get this 
to work?


Re: SumType alias can't be deduced?

2022-02-21 Thread Paul Backus via Digitalmars-d-learn

On Monday, 21 February 2022 at 18:43:18 UTC, Emmanuelle wrote:

If you run this, the compiler should emit this error:

```d
onlineapp.d(14): Error: template `onlineapp.foobar` cannot 
deduce function from argument types `!()(SumType!(int, Unit))`
onlineapp.d(8):Candidate is: `foobar(T)(Option!T 
option)`

```

If you change `foobar`’s parameter’s type from `Option!T` to 
`SumType!(T, Unit)`, it  compiles without error.


This is a long-standing limitation of the D compiler's template 
argument deduction: it cannot "see through" `alias` templates to 
deduce the underlying type.


The enhancement request to make this work was submitted in 2008: 
https://issues.dlang.org/show_bug.cgi?id=1807


Unfortunately, getting the compiler to handle this correctly is 
much harder than it looks, and nobody has managed to do it yet, 
though several have tried.


As a workaround, you can define your `Optional` type using a 
struct instead of an `alias`:


```d
import std.sumtype;

struct Unit {}
struct Option(T) {
SumType!(T, Unit) data;
this(U, this This)(U value)
{
data = value;
}
alias data this;
}

void foobar(T)(Option!T option) {}

void main() {
foobar(Option!int(123));
}
```


Re: Differences between lambda function and regular functions in higher order functions

2022-02-21 Thread steve via Digitalmars-d-learn
thanks a lot both! Yes I'm aware that map exists already. This 
was didactic. I had tried to find out whether lambdas generate 
function pointers but also couldn't figure that one out :D


SumType alias can't be deduced?

2022-02-21 Thread Emmanuelle via Digitalmars-d-learn

See https://run.dlang.io/is/hNaSFh:

```d
import std.sumtype;

struct Unit {}
alias Option(T) = SumType!(T, Unit);

void foobar(T)(Option!T option) {}

void main() {
foobar(Option!int(123));
}
```

If you run this, the compiler should emit this error:

```d
onlineapp.d(14): Error: template `onlineapp.foobar` cannot deduce 
function from argument types `!()(SumType!(int, Unit))`

onlineapp.d(8):Candidate is: `foobar(T)(Option!T option)`
```

If you change `foobar`’s parameter’s type from `Option!T` to 
`SumType!(T, Unit)`, it  compiles without error.


Surely this is not intended? Is this a compiler bug or am I doing 
something wrong?


Cheers!



Re: Is there a way to not escape slashes when parsing JSON?

2022-02-21 Thread bachmeier via Digitalmars-d-learn

On Monday, 21 February 2022 at 17:32:23 UTC, bachmeier wrote:
On Monday, 21 February 2022 at 04:02:23 UTC, Steven 
Schveighoffer wrote:

On Monday, 21 February 2022 at 03:42:55 UTC, bachmeier wrote:

I tried this

```d
import std.json, std.stdio;

void main() {
writeln(parseJSON(`{"a": "path/file"}`, 
JSONOptions.doNotEscapeSlashes));

}
```

but the output is

```
{"a":"path\/file"}
```

Is there a way to avoid the escaping of the forward slash? Is 
there some reason I should want to escape the forward slash?


The options are applied on parsing or output but do not stay 
with the item! So just because you parsed without allowing 
escapes on slashes doesn't mean the output will use that 
option.


2 ways I found:

```d
// 1. allocate a string to display
writeln(parseJson(...).toString(JSONOptions.doNotEscapeSlashes));
// 2. wrap so you can hook the output range version of toString
struct NoEscapeJson
{
JSONValue json;
void toString(Out)(Out outputrange) const
{
json.toString(outputrange, 
JSONOptions.doNotEscapeSlashes);

}
}
writeln(NoEscapeJson(parseJson(...)));
```

-Steve


I've had a chance to look into this. The documentation for 
`parseJSON` says:


```
JSONOptions options 	enable decoding string representations of 
NaN/Inf as float values

```

which looks to me as if there's no way to apply 
`doNotEscapeSlashes` while parsing. Your approach works if the 
goal is to print out the JSON as it was passed in. I don't see 
any way to work with the parsed JSON data. If I want to later 
work with element "a" and do something with "path/file", the 
value will always be "path\/file". AFAICT, I'd have to manually 
unescape every element.


I looked at the source for `parseJSON` and I see references only 
to `JSONOptions.strictParsing` and 
`JSONOptions.specialFloatLiterals`. I may be missing something, 
but I don't see any option to iterating over every element and 
unescaping manually.


Re: Is there a way to not escape slashes when parsing JSON?

2022-02-21 Thread bachmeier via Digitalmars-d-learn

On Monday, 21 February 2022 at 09:04:06 UTC, bauss wrote:

Why are we even escaping them by default, it should be the 
other way around, that slashes are only escaped if you ask for 
it; that's how it literally is in almost every JSON library.


Escaping slashes as a default is a huge mistake IMHO.


I seldom work with JSON data, but I may have to look for an 
alternative JSON library for D. std.json is not the most fun 
independent of this issue.


Re: Is there a way to not escape slashes when parsing JSON?

2022-02-21 Thread bachmeier via Digitalmars-d-learn
On Monday, 21 February 2022 at 04:02:23 UTC, Steven Schveighoffer 
wrote:

On Monday, 21 February 2022 at 03:42:55 UTC, bachmeier wrote:

I tried this

```d
import std.json, std.stdio;

void main() {
writeln(parseJSON(`{"a": "path/file"}`, 
JSONOptions.doNotEscapeSlashes));

}
```

but the output is

```
{"a":"path\/file"}
```

Is there a way to avoid the escaping of the forward slash? Is 
there some reason I should want to escape the forward slash?


The options are applied on parsing or output but do not stay 
with the item! So just because you parsed without allowing 
escapes on slashes doesn't mean the output will use that option.


2 ways I found:

```d
// 1. allocate a string to display
writeln(parseJson(...).toString(JSONOptions.doNotEscapeSlashes));
// 2. wrap so you can hook the output range version of toString
struct NoEscapeJson
{
JSONValue json;
void toString(Out)(Out outputrange) const
{
json.toString(outputrange, 
JSONOptions.doNotEscapeSlashes);

}
}
writeln(NoEscapeJson(parseJson(...)));
```

-Steve


I've had a chance to look into this. The documentation for 
`parseJSON` says:


```
JSONOptions options 	enable decoding string representations of 
NaN/Inf as float values

```

which looks to me as if there's no way to apply 
`doNotEscapeSlashes` while parsing. Your approach works if the 
goal is to print out the JSON as it was passed in. I don't see 
any way to work with the parsed JSON data. If I want to later 
work with element "a" and do something with "path/file", the 
value will always be "path\/file". AFAICT, I'd have to manually 
unescape every element.


Re: DDoc Reference Links point to /docs/docs instead of /docs?

2022-02-21 Thread Vijay Nayar via Digitalmars-d-learn

On Monday, 21 February 2022 at 16:58:43 UTC, Adam D Ruppe wrote:

On Monday, 21 February 2022 at 15:35:41 UTC, Vijay Nayar wrote:

I'm a bit surprised I've never heard of `adrdox` before now.


yeah i don't advertise much. it is what runs on my dpldocs.info 
website though which auto-generates docs for dub packages.


Regarding ddoc, should I submit a bug report? If so, is ddoc 
considered part of the compiler?


Yeah, it is part of dmd, so you can file on issues.dlang.org if 
you want.


Bug reported here: https://issues.dlang.org/show_bug.cgi?id=22803


Re: DDoc Reference Links point to /docs/docs instead of /docs?

2022-02-21 Thread Adam D Ruppe via Digitalmars-d-learn

On Monday, 21 February 2022 at 15:35:41 UTC, Vijay Nayar wrote:

I'm a bit surprised I've never heard of `adrdox` before now.


yeah i don't advertise much. it is what runs on my dpldocs.info 
website though which auto-generates docs for dub packages.


Regarding ddoc, should I submit a bug report? If so, is ddoc 
considered part of the compiler?


Yeah, it is part of dmd, so you can file on issues.dlang.org if 
you want.


Re: DDoc Reference Links point to /docs/docs instead of /docs?

2022-02-21 Thread Vijay Nayar via Digitalmars-d-learn

On Monday, 21 February 2022 at 13:18:01 UTC, Adam D Ruppe wrote:
tbh ddoc is pretty bad, you should try my `dub run adrdox` 
instead which also creates html but its links actually work.


I gave it a try and I must say that the documentation is 
formatted in a very good way, and as you said, all the links 
work. I'm a bit surprised I've never heard of `adrdox` before 
now. Thank you kindly for that!


Regarding ddoc, should I submit a bug report? If so, is ddoc 
considered part of the compiler?


Re: Is there a way to not escape slashes when parsing JSON?

2022-02-21 Thread Kagamin via Digitalmars-d-learn

On Monday, 21 February 2022 at 09:04:06 UTC, bauss wrote:
Why are we even escaping them by default, it should be the 
other way around, that slashes are only escaped if you ask for 
it; that's how it literally is in almost every JSON library.


Really? I always see escaped slashes in JSON, e.g. wikipedia does 
this, but everything else too.


Re: DDoc Reference Links point to /docs/docs instead of /docs?

2022-02-21 Thread Adam D Ruppe via Digitalmars-d-learn
tbh ddoc is pretty bad, you should try my `dub run adrdox` 
instead which also creates html but its links actually work.


Re: Why writeln can't be converted to nothrow with just catching of StdioException

2022-02-21 Thread bauss via Digitalmars-d-learn

On Monday, 21 February 2022 at 10:57:07 UTC, Basile B. wrote:

On Monday, 21 February 2022 at 10:53:56 UTC, Basile B. wrote:

On Monday, 21 February 2022 at 10:49:13 UTC, partypooper wrote:
Do I completely not understand what is `nothrow` or why I 
can't make function nothrow with just catching StdioException?


This doesn't work
```d
nothrow void hello() {
  try {
writeln("Hello, World!")
  } catch (StdioException) {}
}
```
This doest work
```d
nothrow void hello() {
  try {
writeln("Hello, World!")
  } catch (Exception) {}
}
```


I believe it's because it can throw ConvException as well ;)


However you're totally right to open a discussion, the 
documentation is innacurate:


in https://dlang.org/phobos/std_stdio.html#.writeln

just StdioException is mentioned ;)


What if we could do ex. this:

```d
__traits(possibleExceptions, writeln);
```

Which would give all exceptions a function could possibly throw.

That way we could build our try/catch based on that.

For nothrow it should return nothing of course, for extern 
functions that have no source code available it should only 
return "Exception" of course.


Just a thought I just had.


Re: Why writeln can't be converted to nothrow with just catching of StdioException

2022-02-21 Thread partypooper via Digitalmars-d-learn

On Monday, 21 February 2022 at 11:21:52 UTC, Mike Parker wrote:

On Monday, 21 February 2022 at 11:07:55 UTC, Basile B. wrote:

[...]


This has nothing to do with which exceptions types a function 
throws. The compiler doesn't dig into that. You have to catch 
`Exception`.


```D
import std.stdio;

class MyException : Exception {
this(string msg) { super(msg); }
}

void throwing(int x) {
if(x > 2) throw new MyException("Derp!");
else writeln(x);
}

void noThrowing() nothrow {
try {
throwing(10);
}
catch(MyException me) {}
}

void main()
{
noThrowing();
}
```

```
onlineapp.d(14): Error: function `onlineapp.throwing` is not 
`nothrow`
onlineapp.d(12): Error: `nothrow` function 
`onlineapp.noThrowing` may throw

```


Thank You for detailed explanation.


Re: Why writeln can't be converted to nothrow with just catching of StdioException

2022-02-21 Thread Basile B. via Digitalmars-d-learn

On Monday, 21 February 2022 at 11:12:38 UTC, partypooper wrote:

On Monday, 21 February 2022 at 11:07:55 UTC, Basile B. wrote:
Yeah there must be another one then. Something actionnable is 
the documentation.


What about Mike Parker answer?


if nothrow fails that's because things are checked. We could 
imagine a second flow analysis leading to better error messages. 
You see, instead of the generic error message you get now.


"If it fails, let's see why"


Re: Crosscompiling LDC's druntime for Android on Windows

2022-02-21 Thread kinke via Digitalmars-d-learn

On Monday, 21 February 2022 at 00:24:54 UTC, Fry wrote:
I'm following the azure pipeline's commands for how it's being 
built here:

https://github.com/ldc-developers/ldc/blob/master/.azure-pipelines/2-posix-build_cross_android.yml#L64


You can check the CI logs for the expanded cmdlines, e.g., from 
https://dev.azure.com/ldc-developers/ldc/_build/results?buildId=3281=logs=0dd91bf4-5270-5f6c-2450-fbc036057a53=0dd91bf4-5270-5f6c-2450-fbc036057a53=c2436f57-6a96-592e-95de-a459be0e607b:


ldc-build-runtime --ninja -j 2 
'--dFlags=-fvisibility=hidden;-mtriple=aarch64--linux-android;-gcc=/home/vsts/work/1/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang' '--targetSystem=Android;Linux;UNIX' --ldcSrcDir=/home/vsts/work/1/s ANDROID_ABI=arm64-v8a ANDROID_NATIVE_API_LEVEL=21 ANDROID_STL=c++_static CMAKE_CROSSCOMPILING=True LDC_INSTALL_LLVM_RUNTIME_LIBS_OS=android LDC_INSTALL_LLVM_RUNTIME_LIBS_ARCH=aarch64-android CMAKE_TOOLCHAIN_FILE=/home/vsts/work/1/android-ndk-r21e/build/cmake/android.toolchain.cmake


DDoc Reference Links point to /docs/docs instead of /docs?

2022-02-21 Thread Vijay Nayar via Digitalmars-d-learn

Greetings everyone,

I have a question regarding the use of [relative 
links](https://dlang.org/spec/ddoc.html#reference_links) in DDoc. 
According to the specification, you can include a reference to an 
object that is in scope using square brackets, e.g. `[Object]`.


One of my current projects is to add support to 
[Avro](https://avro.apache.org/docs/current/index.html) in D, and 
I encountered something unusual. Consider the following code 
snippets.


```d
// File: source/avro/parser.d
module avro.parser;
class Parser {
  /// Builds a [Schema] using a path to a ".avsc" file.
  public Schema parseFile(string fileName) {
// ...
  }
```

```d
// File: source/avro/schema.d
module avro.schema;
class Schema {
  // ...
}
```

When I build the documentation using `dub build -b docs`, which 
creates a bunch of individual `.html` files under the `docs` 
folder, but there is no `index.html` or anything else. I start by 
browsing to `file:///home/vnayar/projects/avro-d/docs/parser.html`


The documentation for the `Parser::parseFile` creates a link like 
so:

```html


  
  
  
Builds a class="code">Schema using a path to a ".avsc" file.

  

```

However, when I click the `Schema` link in my browser, the 
relative link of `docs/schema.html` actually is relative to the 
current file, thus, it takes me to 
`file:///home/vnayar/projects/avro-d/docs/docs/schema.html#Schema`. Because the folder `docs/docs` does not exist, I just get a file-not-found error.


Am I using DDocs incorrectly?


Re: Why writeln can't be converted to nothrow with just catching of StdioException

2022-02-21 Thread Mike Parker via Digitalmars-d-learn

On Monday, 21 February 2022 at 11:07:55 UTC, Basile B. wrote:


Yeah there must be another one then. Something actionnable is 
the documentation.


This has nothing to do with which exceptions types a function 
throws. The compiler doesn't dig into that. You have to catch 
`Exception`.


```D
import std.stdio;

class MyException : Exception {
this(string msg) { super(msg); }
}

void throwing(int x) {
if(x > 2) throw new MyException("Derp!");
else writeln(x);
}

void noThrowing() nothrow {
try {
throwing(10);
}
catch(MyException me) {}
}

void main()
{
noThrowing();
}
```

```
onlineapp.d(14): Error: function `onlineapp.throwing` is not 
`nothrow`
onlineapp.d(12): Error: `nothrow` function `onlineapp.noThrowing` 
may throw

```


Re: Why writeln can't be converted to nothrow with just catching of StdioException

2022-02-21 Thread Mike Parker via Digitalmars-d-learn

On Monday, 21 February 2022 at 11:11:28 UTC, partypooper wrote:

So with such behavior there is no reason at all to make make 
function nothrow, if it uses throw functions in its body?


I'm not sure what you mean. If a function throws an exception, it 
can't be nothrow.


And as much as I already know compliler can deduce and 
automatically adds nothrow to all functions which do not throw 
exceptions. Right?


Function attribute inference is done in specific circumstances, 
but not for all functions. See 
https://dlang.org/spec/function.html#function-attribute-inference.


Re: Why writeln can't be converted to nothrow with just catching of StdioException

2022-02-21 Thread partypooper via Digitalmars-d-learn

On Monday, 21 February 2022 at 11:07:55 UTC, Basile B. wrote:
Yeah there must be another one then. Something actionnable is 
the documentation.


What about Mike Parker answer?


Re: Why writeln can't be converted to nothrow with just catching of StdioException

2022-02-21 Thread partypooper via Digitalmars-d-learn

On Monday, 21 February 2022 at 11:04:46 UTC, Mike Parker wrote:

On Monday, 21 February 2022 at 10:49:13 UTC, partypooper wrote:
Do I completely not understand what is `nothrow` or why I 
can't make function nothrow with just catching StdioException?


D does not have checked exceptions like Java, so the compiler 
doesn't have anyway to verify that any function you call won't 
throw a given exception. You have to catch `Exception` to 
satisfy the `nothrow` requirement.


Oh, that's explain it.
So with such behavior there is no reason at all to make make 
function nothrow, if it uses throw functions in its body? And as 
much as I already know compliler can deduce and automatically 
adds nothrow to all functions which do not throw exceptions. 
Right?


Re: Why writeln can't be converted to nothrow with just catching of StdioException

2022-02-21 Thread Basile B. via Digitalmars-d-learn

On Monday, 21 February 2022 at 11:05:42 UTC, partypooper wrote:

On Monday, 21 February 2022 at 10:58:26 UTC, Basile B. wrote:

more likely UTFException actually


Additionaly catching UTF and Conv exceptions doesn't help.


Yeah there must be another one then. Something actionnable is the 
documentation.


Re: Why writeln can't be converted to nothrow with just catching of StdioException

2022-02-21 Thread partypooper via Digitalmars-d-learn

On Monday, 21 February 2022 at 10:58:26 UTC, Basile B. wrote:

more likely UTFException actually


Additionaly catching UTF and Conv exceptions doesn't help.




Re: Why writeln can't be converted to nothrow with just catching of StdioException

2022-02-21 Thread Mike Parker via Digitalmars-d-learn

On Monday, 21 February 2022 at 10:49:13 UTC, partypooper wrote:
Do I completely not understand what is `nothrow` or why I can't 
make function nothrow with just catching StdioException?


D does not have checked exceptions like Java, so the compiler 
doesn't have anyway to verify that any function you call won't 
throw a given exception. You have to catch `Exception` to satisfy 
the `nothrow` requirement.


Re: Why writeln can't be converted to nothrow with just catching of StdioException

2022-02-21 Thread Basile B. via Digitalmars-d-learn

On Monday, 21 February 2022 at 10:49:13 UTC, partypooper wrote:
Do I completely not understand what is `nothrow` or why I can't 
make function nothrow with just catching StdioException?


This doesn't work
```d
nothrow void hello() {
  try {
writeln("Hello, World!")
  } catch (StdioException) {}
}
```
This doest work
```d
nothrow void hello() {
  try {
writeln("Hello, World!")
  } catch (Exception) {}
}
```


https://issues.dlang.org/show_bug.cgi?id=22800


Re: Differences between lambda function and regular functions in higher order functions

2022-02-21 Thread Stanislav Blinov via Digitalmars-d-learn

On Monday, 21 February 2022 at 10:04:16 UTC, steve wrote:
I am trying to implement a simple map function. I found code to 
do this in another post but it only seems to work with lambda 
functions and I do not understand why. Any help would be 
greatly appreciated


```
import std.stdio;

T[] map_vals(T,S)(scope T function(S) f, S[] a){
auto b = new T[a.length];
foreach(i,x;a) b[i] = f(x);
return b;
}
```



As partypooper says, with that singature it'll only work if you 
pass function pointer (whereas a lambda converts to one). 
Alternatively (and how it is typically done in i.e. D's standard 
library), you can pass your callable as a compile-time argument. 
This also has an advantage of supporting UFCS, as shown in this 
example:


```d
import std.stdio;

// original, needs a function pointer
T[] map_vals(T,S)(scope T function(S) f, S[] a){
auto b = new T[a.length];
foreach(i,x;a) b[i] = f(x);
return b;
}

// Takes the callable as a compile-time argument
auto map_vals(alias f,S)(S[] a)
if (is(typeof(f(a[0]
{
alias T = typeof(f(a[0]));
auto b = new T[a.length];
foreach (i, ref x; a) b[i] = f(x);
return b;
}

auto timestwo(float x) {
return 2*x;
}

void main(){
float[] my_array = [1., 2., 3.];
auto ff = (float x)=>2*x;

// This works
writeln(map_vals(ff, my_array));

// this works with pointer to timestwo
writeln(map_vals(, my_array));

// and this works by just passing the symbol name,
// also note UFCS syntax:
my_array.map_vals!timestwo.writeln;

// as does this:
my_array.map_vals!ff.writeln;

// and this:
my_array.map_vals!(x => 2*x).writeln;
}
```

Note that `map` already exists in Phobos 
(https://dlang.org/phobos/std_algorithm_iteration.html#map), and 
that one makes a lazy range and doesn't allocate.


Re: Why writeln can't be converted to nothrow with just catching of StdioException

2022-02-21 Thread Basile B. via Digitalmars-d-learn

On Monday, 21 February 2022 at 10:53:56 UTC, Basile B. wrote:

On Monday, 21 February 2022 at 10:49:13 UTC, partypooper wrote:
Do I completely not understand what is `nothrow` or why I 
can't make function nothrow with just catching StdioException?


This doesn't work
```d
nothrow void hello() {
  try {
writeln("Hello, World!")
  } catch (StdioException) {}
}
```
This doest work
```d
nothrow void hello() {
  try {
writeln("Hello, World!")
  } catch (Exception) {}
}
```


I believe it's because it can throw ConvException as well ;)


more likely UTFException actually


Re: Why writeln can't be converted to nothrow with just catching of StdioException

2022-02-21 Thread Basile B. via Digitalmars-d-learn

On Monday, 21 February 2022 at 10:53:56 UTC, Basile B. wrote:

On Monday, 21 February 2022 at 10:49:13 UTC, partypooper wrote:
Do I completely not understand what is `nothrow` or why I 
can't make function nothrow with just catching StdioException?


This doesn't work
```d
nothrow void hello() {
  try {
writeln("Hello, World!")
  } catch (StdioException) {}
}
```
This doest work
```d
nothrow void hello() {
  try {
writeln("Hello, World!")
  } catch (Exception) {}
}
```


I believe it's because it can throw ConvException as well ;)


However you're totally right to open a discussion, the 
documentation is innacurate:


in https://dlang.org/phobos/std_stdio.html#.writeln

just StdioException is mentioned ;)


Re: Why writeln can't be converted to nothrow with just catching of StdioException

2022-02-21 Thread Basile B. via Digitalmars-d-learn

On Monday, 21 February 2022 at 10:49:13 UTC, partypooper wrote:
Do I completely not understand what is `nothrow` or why I can't 
make function nothrow with just catching StdioException?


This doesn't work
```d
nothrow void hello() {
  try {
writeln("Hello, World!")
  } catch (StdioException) {}
}
```
This doest work
```d
nothrow void hello() {
  try {
writeln("Hello, World!")
  } catch (Exception) {}
}
```


I believe it's because it can throw ConvException as well ;)


Why writeln can't be converted to nothrow with just catching of StdioException

2022-02-21 Thread partypooper via Digitalmars-d-learn
Do I completely not understand what is `nothrow` or why I can't 
make function nothrow with just catching StdioException?


This doesn't work
```d
nothrow void hello() {
  try {
writeln("Hello, World!")
  } catch (StdioException) {}
}
```
This doest work
```d
nothrow void hello() {
  try {
writeln("Hello, World!")
  } catch (Exception) {}
}
```


Re: Differences between lambda function and regular functions in higher order functions

2022-02-21 Thread partypooper via Digitalmars-d-learn

On Monday, 21 February 2022 at 10:04:16 UTC, steve wrote:
I am trying to implement a simple map function. I found code to 
do this in another post but it only seems to work with lambda 
functions and I do not understand why. Any help would be 
greatly appreciated


```
import std.stdio;

T[] map_vals(T,S)(scope T function(S) f, S[] a){
auto b = new T[a.length];
foreach(i,x;a) b[i] = f(x);
return b;
}


auto timestwo(float x) {
return 2*x;
}

void main(){
float[] my_array = [1., 2., 3.];
auto ff = (float x)=>2*x;

// This works
writeln(map_vals(ff, my_array));

// this does not
// writeln(map_vals(timestwo, my_array));
}
```


I guess because your function parameter is actually a pointer to 
a function. ff is a pointer to anonymous function. timestwo is 
not. This should work


```d
writeln(map_vals(, my_array));
```




Differences between lambda function and regular functions in higher order functions

2022-02-21 Thread steve via Digitalmars-d-learn
I am trying to implement a simple map function. I found code to 
do this in another post but it only seems to work with lambda 
functions and I do not understand why. Any help would be greatly 
appreciated


```
import std.stdio;

T[] map_vals(T,S)(scope T function(S) f, S[] a){
auto b = new T[a.length];
foreach(i,x;a) b[i] = f(x);
return b;
}


auto timestwo(float x) {
return 2*x;
}

void main(){
float[] my_array = [1., 2., 3.];
auto ff = (float x)=>2*x;

// This works
writeln(map_vals(ff, my_array));

// this does not
// writeln(map_vals(timestwo, my_array));
}
```


Re: Is there a way to not escape slashes when parsing JSON?

2022-02-21 Thread bauss via Digitalmars-d-learn

On Monday, 21 February 2022 at 03:42:55 UTC, bachmeier wrote:

I tried this

```
import std.json, std.stdio;

void main() {
writeln(parseJSON(`{"a": "path/file"}`, 
JSONOptions.doNotEscapeSlashes));

}
```

but the output is

```
{"a":"path\/file"}
```

Is there a way to avoid the escaping of the forward slash? Is 
there some reason I should want to escape the forward slash?


Why are we even escaping them by default, it should be the other 
way around, that slashes are only escaped if you ask for it; 
that's how it literally is in almost every JSON library.


Escaping slashes as a default is a huge mistake IMHO.