Re: Pass lambda into template

2018-09-03 Thread vit via Digitalmars-d-learn

On Monday, 3 September 2018 at 09:09:44 UTC, Andrey wrote:

Hello,

Here is a code with comments: https://run.dlang.io/is/BNl2Up.

I don't understand how to pass lambda into template.
I get an error:
onlineapp.d(18): Error: template instance `qwerty!((i) => "arg" 
~ i.to!string ~ "[0] == '?'", "||")` cannot use local __lambda1 
as parameter to non-global template qwerty(alias mapper, alias 
delimiter)


Try this:

if(mixin(qwerty!((int i) => "arg" ~ i.to!string ~ "[0] == '?'", 
"||")))


Re: assumeNoGC works but can't get an assumePure to work

2018-09-03 Thread Paul Backus via Digitalmars-d-learn

On Monday, 3 September 2018 at 22:07:10 UTC, aliak wrote:

Why does it work with nogc but not with pure?

Cheers,
- Ali


You can't define an impure function inside a pure unittest. If 
you move `modify` outside the unittest block, and change the 
argument from a lambda to a function pointer, it works:


https://run.dlang.io/is/xRS75H


assumeNoGC works but can't get an assumePure to work

2018-09-03 Thread aliak via Digitalmars-d-learn
In another thread [0] this function can be used to call non nogc 
code from nogc code


import std.traits;

auto assumeNoGC(T)(T t) {
enum attrs = functionAttributes!T | FunctionAttribute.nogc;
return cast(SetFunctionAttributes!(T, functionLinkage!T, 
attrs)) t;

}

And then you can use it like:

@nogc unittest {
  auto allocate() {
return [1];
  }
  assumeNoGC({allocate;})();
}

So I tried to the same with pure, wrote assumePure and changed 
the attribute to FunctionAttribute.pure_, but that doesn't seem 
to be treated the same:


pure unittest {
  static int thing = 3;
  void modify() {
thing = 4;
  }
  assumePure({modify;})();
}

Ye get: pure function modify cannot access mutable static data 
thing


Why does it work with nogc but not with pure?

Cheers,
- Ali

[0]: 
https://forum.dlang.org/thread/awalwokejtywzkxgd...@forum.dlang.org


Re: Pass lambda into template

2018-09-03 Thread Paul Backus via Digitalmars-d-learn

On Monday, 3 September 2018 at 09:09:44 UTC, Andrey wrote:

Hello,

Here is a code with comments: https://run.dlang.io/is/BNl2Up.

I don't understand how to pass lambda into template.
I get an error:
onlineapp.d(18): Error: template instance `qwerty!((i) => "arg" 
~ i.to!string ~ "[0] == '?'", "||")` cannot use local __lambda1 
as parameter to non-global template qwerty(alias mapper, alias 
delimiter)


Congratulations, you've run into one of D's oldest and most 
annoying known bugs, issue 5710: 
https://issues.dlang.org/show_bug.cgi?id=5710


The solution is to define `qwerty` outside of `exec`, and add an 
additional parameter to pass in `count`: 
https://run.dlang.io/is/VTeizm


Process in parallel and output result to stdout theread-safely

2018-09-03 Thread Dr.No via Digitalmars-d-learn
so I'im doing an expansive operation with a file, to try speed 
up, i switch to using parallel but keeping in the otuput printing 
thread-safe. But for some reason, even using synchonized, it 
doesn't work as expected, for example, it output multiples 
results on same time, as in the example below.


the code:


stdout.flush();
foreach(string fn; parallel(files))
{
auto res = doSomething(fn);
synchronized
{
   stdout.writefln("outjson = %s", res.serializeToJson);
   stdout.flush();
}

}



the expeced output is like that (one per line):


outjson = {"barCode":"1","ade":"1"}
outjson = {"barCode":"2","ade":"2"}
outjson = {"barCode":"3","ade":"3"}

// and so on...

But it in the middle of output, I got output like this:

outjson = {"barCode":"20","ade":"20"}♪◙outjson = 
{"barCode":"X21","ade":"21"}


also there's that extra ♪◙ character. Thos sounds memory 
violation somewhere.
This only happens when using parallel. Any guess what's possibily 
happeing?


Re: Structures and CTFE

2018-09-03 Thread Stefan Koch via Digitalmars-d-learn

On Monday, 3 September 2018 at 15:08:57 UTC, agorkvmh wrote:

On Monday, 3 September 2018 at 15:00:33 UTC, Alex wrote:

On Monday, 3 September 2018 at 14:00:23 UTC, agorkvmh wrote:

[...]


Yes. Put a pragma where you static assert for Foo(1).pos 
equality with 2:


[...]


Thanks, by the way, the autocomplete suggests me '__ctfeWrite', 
what is it?


__ctfeWrite was an idea which never got implemented ...
it should be a no-op  iirc.


Re: Structures and CTFE

2018-09-03 Thread agorkvmh via Digitalmars-d-learn

On Monday, 3 September 2018 at 15:00:33 UTC, Alex wrote:

On Monday, 3 September 2018 at 14:00:23 UTC, agorkvmh wrote:

[...]


Yes. Put a pragma where you static assert for Foo(1).pos 
equality with 2:


[...]


Thanks, by the way, the autocomplete suggests me '__ctfeWrite', 
what is it?


Re: Structures and CTFE

2018-09-03 Thread Alex via Digitalmars-d-learn

On Monday, 3 September 2018 at 14:00:23 UTC, agorkvmh wrote:

There is a way to do print the two values at compile time?


Yes. Put a pragma where you static assert for Foo(1).pos equality 
with 2:


--
static assert(Foo(1).pos == 2);
pragma(msg, Foo(1).pos);

struct Foo
{
this(int i)
{
static assert(this.init.pos == 1);
advance();
}

size_t pos = 1;

void advance()
{
pragma(msg, pos);
pos = pos + 1;
pragma(msg, pos);
}
}

void main(){}
--

What's the best way to debug a CTFE function?


There is none.

From the Dlang tour:
"CTFE is a mechanism which allows the compiler to execute 
functions at compile time. There is no special set of the D 
language necessary to use this feature - whenever a function just 
depends on compile time known values the D compiler might decide 
to interpret it during compilation."

https://tour.dlang.org/tour/en/gems/compile-time-function-evaluation-ctfe

As no specifics for CTFE writing exist, no specifics for CTFE 
testing exist.


So, the way to choose is the one you did: by putting static 
asserts at the places where you want to assert, that something 
has to have specific values.
I also added another one, before the initialization of the 
struct. I.e., before calling the advance function.


Re: Structures and CTFE

2018-09-03 Thread agorkvmh via Digitalmars-d-learn

On Monday, 3 September 2018 at 13:52:24 UTC, bauss wrote:

On Monday, 3 September 2018 at 13:39:25 UTC, agorkvmh wrote:

[...]


It prints 1, because pragma(msg) is called when the compiler is 
analyzing the code through the semantic process and not when 
the body of the function is executed during CTFE.


And since "pos = pos+1" is a runtime construct (technically) 
then the expression is ignored, unless the function is called 
during CTFE, but by the time the function is called during CTFE 
then the pragma(msg) has already been executed.


Thank you for your quick answer.

There is a way to do print the two values at compile time?
What's the best way to debug a CTFE function?




Re: anyway to debug nogc code with writeln?

2018-09-03 Thread aliak via Digitalmars-d-learn

On Saturday, 1 September 2018 at 22:38:46 UTC, Ali Çehreli wrote:

You can strip off any attribute with SetFunctionAttributes:

import std.stdio;

// Adapted from std.traits.SetFunctionAttributes documentation
import std.traits;
auto assumeNoGC(T)(T t)
if (isFunctionPointer!T || isDelegate!T)
{
enum attrs = functionAttributes!T | FunctionAttribute.nogc;
return cast(SetFunctionAttributes!(T, functionLinkage!T, 
attrs)) t;

}

void f(T)(auto ref T) {
writeln("yo");
}

@nogc void main() {
assumeNoGC(() => f(3));
// or
assumeNoGC( { writeln("yo"); });
}

Ali


Ah this works! Can define a debugWriteln then that can be used 
from anywhere without having to re attribute all the functions.


Thanks!


Re: DStep rocks [was Example of using C API from D?]

2018-09-03 Thread Russel Winder via Digitalmars-d-learn
On Mon, 2018-09-03 at 12:45 +, Andrea Fontana via Digitalmars-d-
learn wrote:
> […]
> 
> I use dpp to generate d code to import:
> - Create a temp.dpp file with #include inside
> - Run "d++ --preprocess-only temp.dpp"
> 
> Now you have your .d file exactly like in dstep.
> 

So where does this leave DStep if DPP is the next generation tool?

Of course I can create a dstep executable, but cannot yet create a d++
executable on Debian Sid. :-(

-- 
Russel.
===
Dr Russel Winder  t: +44 20 7585 2200
41 Buckmaster Roadm: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk



signature.asc
Description: This is a digitally signed message part


Re: Structures and CTFE

2018-09-03 Thread bauss via Digitalmars-d-learn

On Monday, 3 September 2018 at 13:39:25 UTC, agorkvmh wrote:

Hi all,

Running this:
---
static assert(Foo(1).pos == 2);


struct Foo
{
this(int i){ advance(); }

size_t pos =1;

void advance()
{
pragma(msg, pos);
pos = pos+1;
pragma(msg, pos);
}
}

dmd -o- -unittest source/pgs/parser.d
1LU
1LU
---

The static assert passes, but why the second pragma print '1'?

Thanks


It prints 1, because pragma(msg) is called when the compiler is 
analyzing the code through the semantic process and not when the 
body of the function is executed during CTFE.


And since "pos = pos+1" is a runtime construct (technically) then 
the expression is ignored, unless the function is called during 
CTFE, but by the time the function is called during CTFE then the 
pragma(msg) has already been executed.


Structures and CTFE

2018-09-03 Thread agorkvmh via Digitalmars-d-learn

Hi all,

Running this:
---
static assert(Foo(1).pos == 2);


struct Foo
{
this(int i){ advance(); }

size_t pos =1;

void advance()
{
pragma(msg, pos);
pos = pos+1;
pragma(msg, pos);
}
}

dmd -o- -unittest source/pgs/parser.d
1LU
1LU
---

The static assert passes, but why the second pragma print '1'?

Thanks



Re: Is there any reason to use non-ref foreach?

2018-09-03 Thread Andrea Fontana via Digitalmars-d-learn

On Friday, 31 August 2018 at 09:59:20 UTC, Dukc wrote:
For me, it seems that for generality you should always add ref 
into foreach loop variable. The reason is this:


One good reason:
https://forum.dlang.org/thread/dlhrrgvzmhladnphi...@forum.dlang.org




Re: Meson issue with -L--export-dynamic flag

2018-09-03 Thread Gerald via Digitalmars-d-learn
For anyone that wants to try to reproduce it, you can clone this 
repo and switch to the meson branch:


https://github.com/bilelmoussaoui/tilix


Meson issue with -L--export-dynamic flag

2018-09-03 Thread Gerald via Digitalmars-d-learn
Myself and some others are looking at replacing autotools in 
Tilix with meson for the various Linux distros to use when 
building and packaging the binary. However we are running into an 
issue with meson around the use of the "-L--export-dynamic" flag.


When compiling with meson using LDC and that flag the following 
errors are generated:


[66/66] Linking target tilix.
FAILED: tilix
ldc  -of tilix 'tilix@exe/source_secretc_secrettypes.d.o' 
'tilix@exe/source_secretc_secret.d.o' 'tilix@exe/source_app.d.o' 
'tilix@exe/source_x11_X.d.o' 'tilix@exe/source_x11_Xlib.d.o' 
'tilix@exe/source_secret_SchemaAttribute.d.o' 
'tilix@exe/source_secret_Item.d.o' 
'tilix@exe/source_secret_Schema.d.o' 
'tilix@exe/source_secret_Service.d.o' 
'tilix@exe/source_secret_Prompt.d.o' 
'tilix@exe/source_secret_Collection.d.o' 
'tilix@exe/source_secret_Secret.d.o' 
'tilix@exe/source_secret_Value.d.o' 
'tilix@exe/source_gx_util_array.d.o' 
'tilix@exe/source_gx_util_path.d.o' 
'tilix@exe/source_gx_util_string.d.o' 
'tilix@exe/source_gx_gtk_cairo.d.o' 
'tilix@exe/source_gx_gtk_clipboard.d.o' 
'tilix@exe/source_gx_gtk_x11.d.o' 
'tilix@exe/source_gx_gtk_resource.d.o' 
'tilix@exe/source_gx_gtk_vte.d.o' 
'tilix@exe/source_gx_gtk_actions.d.o' 
'tilix@exe/source_gx_gtk_threads.d.o' 
'tilix@exe/source_gx_gtk_dialog.d.o' 
'tilix@exe/source_gx_gtk_settings.d.o' 
'tilix@exe/source_gx_gtk_util.d.o' 
'tilix@exe/source_gx_gtk_color.d.o' 
'tilix@exe/source_gx_tilix_appwindow.d.o' 
'tilix@exe/source_gx_tilix_terminal_advpaste.d.o' 
'tilix@exe/source_gx_tilix_terminal_search.d.o' 
'tilix@exe/source_gx_tilix_terminal_regex.d.o' 
'tilix@exe/source_gx_tilix_terminal_actions.d.o' 
'tilix@exe/source_gx_tilix_terminal_activeprocess.d.o' 
'tilix@exe/source_gx_tilix_terminal_terminal.d.o' 
'tilix@exe/source_gx_tilix_terminal_layout.d.o' 
'tilix@exe/source_gx_tilix_terminal_password.d.o' 
'tilix@exe/source_gx_tilix_terminal_util.d.o' 
'tilix@exe/source_gx_tilix_terminal_exvte.d.o' 
'tilix@exe/source_gx_tilix_terminal_monitor.d.o' 
'tilix@exe/source_gx_tilix_sidebar.d.o' 
'tilix@exe/source_gx_tilix_customtitle.d.o' 
'tilix@exe/source_gx_tilix_prefeditor_bookmarkeditor.d.o' 
'tilix@exe/source_gx_tilix_prefeditor_profileeditor.d.o' 
'tilix@exe/source_gx_tilix_prefeditor_common.d.o' 
'tilix@exe/source_gx_tilix_prefeditor_prefdialog.d.o' 
'tilix@exe/source_gx_tilix_prefeditor_titleeditor.d.o' 
'tilix@exe/source_gx_tilix_prefeditor_advdialog.d.o' 
'tilix@exe/source_gx_tilix_preferences.d.o' 
'tilix@exe/source_gx_tilix_application.d.o' 
'tilix@exe/source_gx_tilix_shortcuts.d.o' 
'tilix@exe/source_gx_tilix_colorschemes.d.o' 
'tilix@exe/source_gx_tilix_session.d.o' 
'tilix@exe/source_gx_tilix_constants.d.o' 
'tilix@exe/source_gx_tilix_common.d.o' 
'tilix@exe/source_gx_tilix_cmdparams.d.o' 
'tilix@exe/source_gx_tilix_encoding.d.o' 
'tilix@exe/source_gx_tilix_closedialog.d.o' 
'tilix@exe/source_gx_tilix_bookmark_manager.d.o' 
'tilix@exe/source_gx_tilix_bookmark_bmchooser.d.o' 
'tilix@exe/source_gx_tilix_bookmark_bmeditor.d.o' 
'tilix@exe/source_gx_tilix_bookmark_bmtreeview.d.o' 
'tilix@exe/source_gx_i18n_l10n.d.o' -L-L/usr/lib// -L-lgtkd-3 
-L-ldl -L-lvted-3 -L-L/usr/lib// -L-lgtkd-3 -L-ldl -L-lX11
/usr/bin/ld: tilix@exe/source_gx_tilix_appwindow.d.o: in function 
`_D2gx5tilix6common__T12GenericEventTCQBjQBj7session7SessionZQBn11__xopEqualsFKxSQDaQDaQCx__TQCtTQCiZQDbKxQBaZb':
/tmp/tilix/build/../source/gx/tilix/common.d:28: undefined 
reference to 
`_D6object__T8__equalsTxDFC2gx5tilix7session7SessionZvTxQBgZQBvFNaNbNiNfAxQByQfZb'
/usr/bin/ld: tilix@exe/source_gx_tilix_terminal_terminal.d.o: in 
function 
`_D2gx5tilix6common__T12GenericEventTEQBjQBjQBg10ActionTypeTCQCgQCgQCd__T16CumulativeResultTbZQvZQCx11__xopEqualsFKxSQEkQEkQEh__TQEdTQDsTQCzZQEpKxQBeZb':
/tmp/tilix/build/../source/gx/tilix/common.d:28: undefined 
reference to 
`_D6object__T8__equalsTxDFE2gx5tilix6common10ActionTypeCQBdQBdQBa__T16CumulativeResultTbZQvZvTxQCtZQDiFNaNbNiNfAxQDlQfZb'
/usr/bin/ld: tilix@exe/source_gx_tilix_terminal_terminal.d.o: in 
function 
`_D2gx5tilix6common__T12GenericEventTAyaTQeTQhTQkZQBc11__xopEqualsFKxSQCpQCpQCm__TQCiTQBxTQCbTQCfTQCjZQDcKxQBmZb':
/tmp/tilix/build/../source/gx/tilix/common.d:28: undefined 
reference to 
`_D6object__T8__equalsTxDFAyaQdQfQhZvTxQpZQBdFNaNbNiNfAxQBgQfZb'

collect2: error: ld returned 1 exit status
Error: /usr/bin/gcc failed with status: 1
ninja: build stopped: subcommand failed.


Taking out the export-dynamic flag it all works fine. 
Unfortunately this flag is the default for the DMD compiler 
(which apparently exhibits the same behavior) as well as LDC on 
some distros like Arch. That flag looks pretty innocuous so I'm 
not sure why it is causing an issue.


I could use some help from someone more experienced with the 
compiler to help understand what is going on and if there is an 
issue with tilix, meson or the compiler itself. A full discussion 
of the issue can be viewed here:


https://github.com/gnunn1/tilix/issues/1502




Re: DStep rocks [was Example of using C API from D?]

2018-09-03 Thread Andrea Fontana via Digitalmars-d-learn

On Monday, 3 September 2018 at 10:50:17 UTC, Russel Winder wrote:
Interesting alternative to DStep. I came to D to avoid 
#include, but… I'll give it a whirl once I can get it compiled 
on Debian Sid. It seems the libclang-dev package does not 
install a libclang.so symbolic link, you have to be explicit 
about which version you want, e.g. libclang- 6.0.so on my 
Debian Sid installation.


I use dpp to generate d code to import:
- Create a temp.dpp file with #include inside
- Run "d++ --preprocess-only temp.dpp"

Now you have your .d file exactly like in dstep.

Andrea


Re: DStep rocks [was Example of using C API from D?]

2018-09-03 Thread Russel Winder via Digitalmars-d-learn
On Sun, 2018-09-02 at 21:54 +, Laeeth Isharc via Digitalmars-d-
learn wrote:
> On Sunday, 2 September 2018 at 17:49:45 UTC, Russel Winder wrote:
> > 
[…]
> > Now to work out how to make the project auto generate this D 
> > module so as to avoid having it in the repository, and 
> > potentially inconsistent with the platform in use.

Turns out this is easy with Meson and SCons. Dub is giving some issues.
:-(

> You could also look at dpp. That's worked for most things I tried 
> and was written in part to avoid the problem of macros changing 
> behaviour at build time.
> 
> Example here:
> 
> 
https://run.dlang.io/?compiler=dmd=%23include%20%0Avoid%20main()%20%7B%0A%20%20%20%20printf("Hello%20dpp.");%0A%7D
> 
> https://github.com/atilaneves/dpp

Interesting alternative to DStep. I came to D to avoid #include, but…
I'll give it a whirl once I can get it compiled on Debian Sid. It seems
the libclang-dev package does not install a libclang.so symbolic link,
you have to be explicit about which version you want, e.g. libclang-
6.0.so on my Debian Sid installation.

-- 
Russel.
===
Dr Russel Winder  t: +44 20 7585 2200
41 Buckmaster Roadm: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk



signature.asc
Description: This is a digitally signed message part


Re: Pass lambda into template

2018-09-03 Thread Andrea Fontana via Digitalmars-d-learn

On Monday, 3 September 2018 at 09:09:44 UTC, Andrey wrote:

Hello,

Here is a code with comments: https://run.dlang.io/is/BNl2Up.

I don't understand how to pass lambda into template.
I get an error:
onlineapp.d(18): Error: template instance `qwerty!((i) => "arg" 
~ i.to!string ~ "[0] == '?'", "||")` cannot use local __lambda1 
as parameter to non-global template qwerty(alias mapper, alias 
delimiter)


Not the best solution, but did you try to make querty template 
global adding a third parameter to pass count?


Andrea


Pass lambda into template

2018-09-03 Thread Andrey via Digitalmars-d-learn

Hello,

Here is a code with comments: https://run.dlang.io/is/BNl2Up.

I don't understand how to pass lambda into template.
I get an error:
onlineapp.d(18): Error: template instance `qwerty!((i) => "arg" 
~ i.to!string ~ "[0] == '?'", "||")` cannot use local __lambda1 
as parameter to non-global template qwerty(alias mapper, alias 
delimiter)


Re: Load entire file, as a char array.

2018-09-03 Thread rikki cattermole via Digitalmars-d-learn

On 03/09/2018 7:38 PM, Chris Katko wrote:

On Monday, 3 September 2018 at 06:28:38 UTC, bauss wrote:

On Monday, 3 September 2018 at 06:25:23 UTC, bauss wrote:

On Monday, 3 September 2018 at 03:19:39 UTC, Neia Neutuladh wrote:

On Monday, 3 September 2018 at 03:04:57 UTC, Chris Katko wrote:
This should be simple? All I want to do is load an entire file, and 
access individual bytes. The entire thing. I don't want to have 
know the file size before hand, or "guess" and have a "maximum 
size" buffer.


So far, all google searches for "dlang binary file read" end up not 
working for me.


Thank you.


http://dpldocs.info/experimental-docs/std.file.read.1.html

import std.file : read;
auto bytes = read("filename");

This gives you a void[], which you can cast to ubyte[] or char[] or 
whatever you need.


Or he could do readText() which returns a string, which in turn will 
give a proper char array when casted.


Actually ignore the casting thing, looking at readText it takes a 
template parameter.


So:

char[] a = readText!(char[])("filename");


Thanks, that works!

But... I'm so confused by D's fifty different string types.


Only three. string, wstring and dstring. They are all aliases for 
immutable(Char)[].



I can run .strip() on a char[]. But I can't run .replace('\n','?') ?


Replace is working on arrays, use " not '. There is a dedicated version 
for characters (tr).



So then I convert char[] to a temporary string and run replace on that.


import std.stdio;
import std.array;

void main()
{
char[] text = "123\nhi".dup;
text = text.replace("\n", "?");
text.writeln;
}


but then writefln("%s") doesn't accept strings! Only char[].

   char []t = cast(char[])(c[i-15 .. i+1]).strip();
   string s = text(t); //s.replace('\n','?')
   writefln(" - [%s]", s); // fail


Looks ok to me:

import std.stdio;

void main()
{
string s = "text";
writefln(" - [%s]", s);
}



Re: Load entire file, as a char array.

2018-09-03 Thread Chris Katko via Digitalmars-d-learn

On Monday, 3 September 2018 at 07:38:51 UTC, Chris Katko wrote:

On Monday, 3 September 2018 at 06:28:38 UTC, bauss wrote:

On Monday, 3 September 2018 at 06:25:23 UTC, bauss wrote:
On Monday, 3 September 2018 at 03:19:39 UTC, Neia Neutuladh 
wrote:
On Monday, 3 September 2018 at 03:04:57 UTC, Chris Katko 
wrote:
This should be simple? All I want to do is load an entire 
file, and access individual bytes. The entire thing. I 
don't want to have know the file size before hand, or 
"guess" and have a "maximum size" buffer.


So far, all google searches for "dlang binary file read" 
end up not working for me.


Thank you.


http://dpldocs.info/experimental-docs/std.file.read.1.html

import std.file : read;
auto bytes = read("filename");

This gives you a void[], which you can cast to ubyte[] or 
char[] or whatever you need.


Or he could do readText() which returns a string, which in 
turn will give a proper char array when casted.


Actually ignore the casting thing, looking at readText it 
takes a template parameter.


So:

char[] a = readText!(char[])("filename");


Thanks, that works!

But... I'm so confused by D's fifty different string types.

I can run .strip() on a char[]. But I can't run 
.replace('\n','?') ?


So then I convert char[] to a temporary string and run replace 
on that.


but then writefln("%s") doesn't accept strings! Only char[].

  char []t = cast(char[])(c[i-15 .. i+1]).strip();
  string s = text(t); //s.replace('\n','?')
  writefln(" - [%s]", s); // fail

main.d(89): Error: template std.array.replace cannot deduce 
function from argument types !()(char[], char, char), 
candidates are:
/usr/include/dmd/phobos/std/array.d(2122):
std.array.replace(E, R1, R2)(E[] subject, R1 from, R2 to) if 
(isDynamicArray!(E[]) && isForwardRange!R1 && isForwardRange!R2 
&& (hasLength!R2 || isSomeString!R2))
/usr/include/dmd/phobos/std/array.d(2255):
std.array.replace(T, Range)(T[] subject, size_t from, size_t 
to, Range stuff) if (isInputRange!Range && 
(is(ElementType!Range : T) || isSomeString!(T[]) && 
is(ElementType!Range : dchar)))



What's going on here?


WAIT! This is my fault (not that I was saying it was "D's" fault, 
just that I was  confused).


it's not replace '' ''. It's replace "" "".  For some reason, I 
must have been thinking it was per-character (which is what I'm 
doing) so I should be using single quotes.


So I CAN run .replace("","") on a char[], just as I can a string. 
And THANK GOODNESS because I thought one of the major advantages 
of D was being relatively orthogonal/type agnostic and if I was 
going to have to remember "X() runs only on Y" for 3+ different 
string types that would be a nightmare!


Re: Load entire file, as a char array.

2018-09-03 Thread Chris Katko via Digitalmars-d-learn

On Monday, 3 September 2018 at 06:28:38 UTC, bauss wrote:

On Monday, 3 September 2018 at 06:25:23 UTC, bauss wrote:
On Monday, 3 September 2018 at 03:19:39 UTC, Neia Neutuladh 
wrote:
On Monday, 3 September 2018 at 03:04:57 UTC, Chris Katko 
wrote:
This should be simple? All I want to do is load an entire 
file, and access individual bytes. The entire thing. I don't 
want to have know the file size before hand, or "guess" and 
have a "maximum size" buffer.


So far, all google searches for "dlang binary file read" end 
up not working for me.


Thank you.


http://dpldocs.info/experimental-docs/std.file.read.1.html

import std.file : read;
auto bytes = read("filename");

This gives you a void[], which you can cast to ubyte[] or 
char[] or whatever you need.


Or he could do readText() which returns a string, which in 
turn will give a proper char array when casted.


Actually ignore the casting thing, looking at readText it takes 
a template parameter.


So:

char[] a = readText!(char[])("filename");


Thanks, that works!

But... I'm so confused by D's fifty different string types.

I can run .strip() on a char[]. But I can't run 
.replace('\n','?') ?


So then I convert char[] to a temporary string and run replace on 
that.


but then writefln("%s") doesn't accept strings! Only char[].

  char []t = cast(char[])(c[i-15 .. i+1]).strip();
  string s = text(t); //s.replace('\n','?')
  writefln(" - [%s]", s); // fail

main.d(89): Error: template std.array.replace cannot deduce 
function from argument types !()(char[], char, char), candidates 
are:
/usr/include/dmd/phobos/std/array.d(2122):
std.array.replace(E, R1, R2)(E[] subject, R1 from, R2 to) if 
(isDynamicArray!(E[]) && isForwardRange!R1 && isForwardRange!R2 
&& (hasLength!R2 || isSomeString!R2))
/usr/include/dmd/phobos/std/array.d(2255):
std.array.replace(T, Range)(T[] subject, size_t from, size_t to, 
Range stuff) if (isInputRange!Range && (is(ElementType!Range : T) 
|| isSomeString!(T[]) && is(ElementType!Range : dchar)))



What's going on here?


Re: Load entire file, as a char array.

2018-09-03 Thread bauss via Digitalmars-d-learn

On Monday, 3 September 2018 at 03:19:39 UTC, Neia Neutuladh wrote:

On Monday, 3 September 2018 at 03:04:57 UTC, Chris Katko wrote:
This should be simple? All I want to do is load an entire 
file, and access individual bytes. The entire thing. I don't 
want to have know the file size before hand, or "guess" and 
have a "maximum size" buffer.


So far, all google searches for "dlang binary file read" end 
up not working for me.


Thank you.


http://dpldocs.info/experimental-docs/std.file.read.1.html

import std.file : read;
auto bytes = read("filename");

This gives you a void[], which you can cast to ubyte[] or 
char[] or whatever you need.


Or he could do readText() which returns a string, which in turn 
will give a proper char array when casted.


Re: Load entire file, as a char array.

2018-09-03 Thread bauss via Digitalmars-d-learn

On Monday, 3 September 2018 at 06:25:23 UTC, bauss wrote:
On Monday, 3 September 2018 at 03:19:39 UTC, Neia Neutuladh 
wrote:

On Monday, 3 September 2018 at 03:04:57 UTC, Chris Katko wrote:
This should be simple? All I want to do is load an entire 
file, and access individual bytes. The entire thing. I don't 
want to have know the file size before hand, or "guess" and 
have a "maximum size" buffer.


So far, all google searches for "dlang binary file read" end 
up not working for me.


Thank you.


http://dpldocs.info/experimental-docs/std.file.read.1.html

import std.file : read;
auto bytes = read("filename");

This gives you a void[], which you can cast to ubyte[] or 
char[] or whatever you need.


Or he could do readText() which returns a string, which in turn 
will give a proper char array when casted.


Actually ignore the casting thing, looking at readText it takes a 
template parameter.


So:

char[] a = readText!(char[])("filename");