Re: Big Oversight with readln?

2017-02-23 Thread Mike Parker via Digitalmars-d-learn

On Friday, 24 February 2017 at 07:38:04 UTC, Mike Parker wrote:
On Friday, 24 February 2017 at 04:22:17 UTC, Jonathan Marler 
wrote:




I discovered the .capacity property of arrays.  I don't know 
why I've never seen this but it looks like this is how readln 
is recovering this seemingly lost peice of data.  This does 
have an odd consequence though, if you pass a slice into 
readln it will read past the end of it if the underlying 
buffer is larger.  This might be something worth adding to the 
documentation.


Also I'm not completely sure how .capacity works, I assume it 
has to look up this information in the memory management 
metadata.  Any enlightenment on this subject is appreciated. 
It also says it's a O(log(n)) operation so I'm guessing it's 
looking it up in some sort of binary tree data structure.


https://dlang.org/phobos/object.html#.capacity


And although it never mentions capacity directly (capacity may 
not have been available when it was written, I can't recall), the 
How It Works section of Steven's array article gives the gist of 
it.


https://dlang.org/d-array-article.html


Re: Big Oversight with readln?

2017-02-23 Thread Mike Parker via Digitalmars-d-learn
On Friday, 24 February 2017 at 04:22:17 UTC, Jonathan Marler 
wrote:




I discovered the .capacity property of arrays.  I don't know 
why I've never seen this but it looks like this is how readln 
is recovering this seemingly lost peice of data.  This does 
have an odd consequence though, if you pass a slice into readln 
it will read past the end of it if the underlying buffer is 
larger.  This might be something worth adding to the 
documentation.


Also I'm not completely sure how .capacity works, I assume it 
has to look up this information in the memory management 
metadata.  Any enlightenment on this subject is appreciated. It 
also says it's a O(log(n)) operation so I'm guessing it's 
looking it up in some sort of binary tree data structure.


https://dlang.org/phobos/object.html#.capacity


Re: RAII

2017-02-23 Thread ketmar via Digitalmars-d-learn

Arun Chandrasekaran wrote:


On Thursday, 23 February 2017 at 09:57:09 UTC, ketmar wrote:

Thanks for your help. NRVO looks interesting. However this may not be 
RAII after all. Or may be too much of C++ has spoiled me. I am not 
familiar enough with D to appreciate/question the language design 
choice. I'll accept this and move forward and see how my code 
evolves. Nevertheless, I'm still learning something new. :)


also, why structs at all? there are another ways! ;-)

 void doWithLock (scope void delegate () action) {
   lockSomething();
   scope(exit) unlockSomething();
   action();
 }

no, wait, this is not so limiting as it may look!

 void foo () {
   int i = 42;
   doWithLock({
 writeln("i=", i); // yay, 42!
   });
 }

this looks *almost* like `synchronized(obj) { ... }`! and if we'll ever 
press D devs to allow us to omit "()" when the only argument is 
arg-less lambda... it will be indistinguishable from built-in! ;-)


Re: RAII

2017-02-23 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Thursday, 23 February 2017 at 21:05:48 UTC, cym13 wrote:
It reminds me of 
https://w0rp.com/blog/post/an-raii-constructor-by-another-name-is-just-as-sweet/ which isn't what you want but may be interesting anyway.


It is interesting, indeed, thanks.


Re: RAII

2017-02-23 Thread Arun Chandrasekaran via Digitalmars-d-learn

On Thursday, 23 February 2017 at 09:57:09 UTC, ketmar wrote:

Arun Chandrasekaran wrote:


I'm trying to write an RAII wrapper on Linux.

I understand struct in D doesn't have default constructor (for 
.init reasons).

I don't want to use `scope`.

Is there an elegant way to achieve this in D?
why not static method or free function that returns struct? due 
to NRVO[0] it won't even be copied.


 auto lock = MyWrapper();

`MyWrapper()` may return voldemort type, so user won't create 
your struct accidentally.



[0] https://dlang.org/glossary.html#nrvo


Thanks for your help. NRVO looks interesting. However this may 
not be RAII after all. Or may be too much of C++ has spoiled me. 
I am not familiar enough with D to appreciate/question the 
language design choice. I'll accept this and move forward and see 
how my code evolves. Nevertheless, I'm still learning something 
new. :)


Re: Big Oversight with readln?

2017-02-23 Thread Jonathan Marler via Digitalmars-d-learn
On Friday, 24 February 2017 at 03:45:35 UTC, Nick Sabalausky 
(Abscissa) wrote:

On 02/23/2017 09:43 PM, Jonathan Marler wrote:
I can't figure out how to make use of the full capacity of 
buffers that
are allocated by readln.  Take the example code from the 
documentation:


 // Read lines from $(D stdin) and count words

 void main()
 {
 char[] buf;
 size_t words = 0;

 while (!stdin.eof)
 {
 char[] line = buf;
 stdin.readln(line);
 if (line.length > buf.length)
 buf = line;

 words += line.split.length;
 }

 writeln(words);
 }

When buf is not large enough to hold the line, readln will 
allocate a
new buffer to accomodate and this example shows how you can 
save that
new buffer to reuse the next time.  The problem is that the 
capacity of
the new buffer is nowhere to be found. readln only returns the 
line that
was read which is only a slice of the buffer that was 
allocated.  The
next time that readln is called, it will not read past the 
slice even if
the capacity of the buffer it allocated was larger.  This will 
cause a
new allocation/copy every time you read a line that was larger 
than all
the previous lines, even if a previous allocation was already 
large
enough. This seems like a big oversight to me, I must be 
missing

something right?


I don't think that problem is actually occurring:

Let's step through the code, and suppose you're reading the 
following four lines of text:


12345
123456789
123
1234567

Starting out, buf.length is 0. When reading the first line, the 
buffer isn't big enough for 5, so readln allocates returns new 
buffer of length 5. That is more than buf.length (0), so the 
new buffer becomes the new buf.


Second line, again, buf (length 5) isn't big enough for 9, so 
readln allocates a new buffer length 9. That's more than the 
old one (5), so again your code sets buf to the larger new 
buffer (length 9).


Third line: buf (length 9) can definitely hold length 3, so 
readln does not allocate. The new slice returned (length 3) is 
NOT longer than buf (still length 9), so buf is NOT set to the 
slice returned by readln. So buf REMAINS length 9.


Fourth line: buf (still length 9) can definitely hold length 7, 
so readln does not allocate.


You're looking at this from the apps perspective and forgetting 
about what readln is doing under the hood.  It can't know how big 
the next line is going to be before it reads it so it's going 
guess how much to allocate.  If you look at the implementation in 
(http://github.com/dlang/phobos/blob/master/std/stdio.d), you can 
see it doubles the size of the current buffer and adds some more 
for good measure (on line 4479 as of writing this).


So in your example after it reads the first line, its going to 
allocate an initial buffer of some size, maybe 200 or so, then 
eventually returns a slice of the first 5 characters into that 
buffer.  When it reads the second line it can't use the rest of 
that initial buffer because the size of the buffer is gone, so it 
has to allocate a new buffer. At least that's what I thought 
until I found what I was missing!


I discovered the .capacity property of arrays.  I don't know why 
I've never seen this but it looks like this is how readln is 
recovering this seemingly lost peice of data.  This does have an 
odd consequence though, if you pass a slice into readln it will 
read past the end of it if the underlying buffer is larger.  This 
might be something worth adding to the documentation.


Also I'm not completely sure how .capacity works, I assume it has 
to look up this information in the memory management metadata.  
Any enlightenment on this subject is appreciated. It also says 
it's a O(log(n)) operation so I'm guessing it's looking it up in 
some sort of binary tree data structure.





Re: ddoc: Can I escape a colon?

2017-02-23 Thread Nick Sabalausky (Abscissa) via Digitalmars-d-learn

On 02/23/2017 10:36 PM, Adam D. Ruppe wrote:

On Friday, 24 February 2017 at 02:50:27 UTC, Nick Sabalausky (Abscissa)
wrote:

What I'd kinda like to do is put together a D doc generator that uses,
uhh, probably markdown.


My dpldocs.info generator continues to progress and I'm almost ready to
call it beta and let other people use it.

It's syntax is a hybrid of ddoc and markdown. Take a look at the sample
page: http://dpldocs.info/experimental-docs/test.html


Oh, nice. The $(MATH stuff is especially cool (although unlikely to be 
useful in my libs - damn! I wanna put that to use!).


Looking forward to seeing that hit beta (and beyond) and trying that out.



Re: Big Oversight with readln?

2017-02-23 Thread Nick Sabalausky (Abscissa) via Digitalmars-d-learn

On 02/23/2017 09:43 PM, Jonathan Marler wrote:

I can't figure out how to make use of the full capacity of buffers that
are allocated by readln.  Take the example code from the documentation:

 // Read lines from $(D stdin) and count words

 void main()
 {
 char[] buf;
 size_t words = 0;

 while (!stdin.eof)
 {
 char[] line = buf;
 stdin.readln(line);
 if (line.length > buf.length)
 buf = line;

 words += line.split.length;
 }

 writeln(words);
 }

When buf is not large enough to hold the line, readln will allocate a
new buffer to accomodate and this example shows how you can save that
new buffer to reuse the next time.  The problem is that the capacity of
the new buffer is nowhere to be found. readln only returns the line that
was read which is only a slice of the buffer that was allocated.  The
next time that readln is called, it will not read past the slice even if
the capacity of the buffer it allocated was larger.  This will cause a
new allocation/copy every time you read a line that was larger than all
the previous lines, even if a previous allocation was already large
enough. This seems like a big oversight to me, I must be missing
something right?


I don't think that problem is actually occurring:

Let's step through the code, and suppose you're reading the following 
four lines of text:


12345
123456789
123
1234567

Starting out, buf.length is 0. When reading the first line, the buffer 
isn't big enough for 5, so readln allocates returns new buffer of length 
5. That is more than buf.length (0), so the new buffer becomes the new buf.


Second line, again, buf (length 5) isn't big enough for 9, so readln 
allocates a new buffer length 9. That's more than the old one (5), so 
again your code sets buf to the larger new buffer (length 9).


Third line: buf (length 9) can definitely hold length 3, so readln does 
not allocate. The new slice returned (length 3) is NOT longer than buf 
(still length 9), so buf is NOT set to the slice returned by readln. So 
buf REMAINS length 9.


Fourth line: buf (still length 9) can definitely hold length 7, so 
readln does not allocate.




Re: ddoc: Can I escape a colon?

2017-02-23 Thread Adam D. Ruppe via Digitalmars-d-learn
On Friday, 24 February 2017 at 02:50:27 UTC, Nick Sabalausky 
(Abscissa) wrote:
What I'd kinda like to do is put together a D doc generator 
that uses, uhh, probably markdown.


My dpldocs.info generator continues to progress and I'm almost 
ready to call it beta and let other people use it.


It's syntax is a hybrid of ddoc and markdown. Take a look at the 
sample page: http://dpldocs.info/experimental-docs/test.html


Though I don't support user-defined macros, it just has a 
hardcoded list of common ones to support Phobos source, and then 
a bunch of "magic macros" and a few special syntaxes for really 
common things.


Like for links, I went with a Wikipedia-inspired [url|text] 
bracket. (or if it is just a url, you don't need to bracket it at 
all). You can also mention a name there, and it searches your 
imported namespaces to look it up.


import std.range;
/// See [isOutputRange] for more.
void foo() {}


That [isOutputRange] will look it up inside std.range and link 
straight to std.range.primitives.isOutputRange.


Or you can do like [http://dlang.org|The official D Website] and 
it will turn that into the natural http://dlang.org>The 
official D Website for you.


There's a few more syntax things you can see in that sample link 
above and a few more I have planned, but I find it is already 
really pretty usable. And then, of course, it has enough 
knowledge of D to make even complex functions look pretty 
readable:


http://dpldocs.info/experimental-docs/std.algorithm.sorting.sort.html

http://dpldocs.info/experimental-docs/arsd.simpledisplay.SimpleWindow.this.1.html

and so on.


Re: Debugging D applications from VS code with webfreak.debug

2017-02-23 Thread Jerry via Digitalmars-d-learn
You can use the C++ plugin, which provides a debugger. Just make 
sure you aren't using optlink, I don't think it generates 
compatible files. Also you might need to use "-gc" which 
generates debug names to be in C format.


https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools

You might also need to enable breakpoints anywhere in VS code 
user setting file.


Re: ddoc: Can I escape a colon?

2017-02-23 Thread Nick Sabalausky (Abscissa) via Digitalmars-d-learn

On 02/23/2017 04:34 PM, Ali Çehreli wrote:


(None of the following is tested.)

a) Try using the following macro:

 COLON = 

And then use "Note$(COLON) Blah".



Thanks, that works.


c) Another option:

 NOTE = Note $0

Then use "$(NOTE Blah)"


Actually, that's more or less what I was already doing anyway (for a 
changelog):


ENHANCE  = $(LI $(B Enhancement:) $0)
CHANGE   = $(LI $(B Change:) $0)
FIXED= $(LI $(B Fixed:) $0)

But the section interpretation there wasn't playing nice with newer 
versions of ddox. Using that "COLON = " trick fixed it though (and 
does indeed appear to work outside of these macros, too.





Re: ddoc: Can I escape a colon?

2017-02-23 Thread Nick Sabalausky (Abscissa) via Digitalmars-d-learn

On 02/23/2017 04:49 PM, H. S. Teoh via Digitalmars-d-learn wrote:


I'm becoming more and more convinced that software that tries to be
smart ends up being even dumber than before.


I've been convinced of that for a long time ;) Not just software either. 
Anything. My car does idiotic "smart" junk that irritates the crap out 
of me and makes me wish my 1997 Prizm hadn't finally rusted itself to 
pieces. K.I.S.S.




Re: ddoc: Can I escape a colon?

2017-02-23 Thread Nick Sabalausky (Abscissa) via Digitalmars-d-learn

On 02/23/2017 04:51 PM, Adam D. Ruppe wrote:

On Thursday, 23 February 2017 at 21:39:11 UTC, H. S. Teoh

Apparently COLON is defined to be ':' in the default ddoc macros, so
you needn't define it yourself.


Oh yeah.

Still, barf.


Luckily in my case, the "Word:" part is already generated inside a ddoc 
macro, so it'll be just as well hidden.


What I'd kinda like to do is put together a D doc generator that uses, 
uhh, probably markdown. But still with some sort of basic (but 
better-looking than ddoc) macro system, because link URLs can still 
REALLY clutter up even markdown source and make it unreadable (which is 
why I actually ended up converting the changelog for at least one of my 
projects from markdown to ddox.) Not a high enough priority for me now 
though :(




Big Oversight with readln?

2017-02-23 Thread Jonathan Marler via Digitalmars-d-learn
I can't figure out how to make use of the full capacity of 
buffers that are allocated by readln.  Take the example code from 
the documentation:


// Read lines from $(D stdin) and count words

void main()
{
char[] buf;
size_t words = 0;

while (!stdin.eof)
{
char[] line = buf;
stdin.readln(line);
if (line.length > buf.length)
buf = line;

words += line.split.length;
}

writeln(words);
}

When buf is not large enough to hold the line, readln will 
allocate a new buffer to accomodate and this example shows how 
you can save that new buffer to reuse the next time.  The problem 
is that the capacity of the new buffer is nowhere to be found. 
readln only returns the line that was read which is only a slice 
of the buffer that was allocated.  The next time that readln is 
called, it will not read past the slice even if the capacity of 
the buffer it allocated was larger.  This will cause a new 
allocation/copy every time you read a line that was larger than 
all the previous lines, even if a previous allocation was already 
large enough. This seems like a big oversight to me, I must be 
missing something right?


Re: ddoc: Can I escape a colon?

2017-02-23 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Feb 23, 2017 at 01:39:11PM -0800, H. S. Teoh via Digitalmars-d-learn 
wrote:
[...]
> Nah, that's overkill. This works:
> 
>   Note$(COLON) blah blah blah
> 
> Apparently COLON is defined to be ':' in the default ddoc macros, so
> you needn't define it yourself.
[...]

Bah, I was wrong, you *do* need to manually define COLON yourself. :-(

I'm becoming more and more convinced that software that tries to be
smart ends up being even dumber than before.  Ddoc trying to
automagically turn text into HTML is an example. :-(


T

-- 
Never ascribe to malice that which is adequately explained by incompetence. -- 
Napoleon Bonaparte


Re: ddoc: Can I escape a colon?

2017-02-23 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 23 February 2017 at 21:39:11 UTC, H. S. Teoh
Apparently COLON is defined to be ':' in the default ddoc 
macros, so you needn't define it yourself.


Oh yeah.

Still, barf.


Re: ddoc: Can I escape a colon?

2017-02-23 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Feb 23, 2017 at 09:35:41PM +, Adam D. Ruppe via Digitalmars-d-learn 
wrote:
> On Thursday, 23 February 2017 at 21:17:33 UTC, H. S. Teoh wrote:
> > _Note: Blah blabbety blah
> 
> Nope.
> 
> Ddoc considers [A-Za-z_]+: to be a section header. You can trick it by
> doing something like
> 
> /++
> Note: ass
> +/
[...]

Nah, that's overkill. This works:

Note$(COLON) blah blah blah

Apparently COLON is defined to be ':' in the default ddoc macros, so you
needn't define it yourself.


T

-- 
2+2=4. 2*2=4. 2^2=4. Therefore, +, *, and ^ are the same operation.


Re: ddoc: Can I escape a colon?

2017-02-23 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 23 February 2017 at 21:17:33 UTC, H. S. Teoh wrote:

_Note: Blah blabbety blah


Nope.

Ddoc considers [A-Za-z_]+: to be a section header. You can trick 
it by doing something like


/++
Note: ass
+/

Yes, the html entity for a space. That trick's ddoc's stupid 
parser while being folded into surrounding whitespace by a html 
engine.


Of course, it breaks if the output is anything but html... and is 
just stupid


Which leads me to the best solution: never use ddoc again.


Re: ddoc: Can I escape a colon?

2017-02-23 Thread Ali Çehreli via Digitalmars-d-learn

On 02/23/2017 01:04 PM, Nick Sabalausky (Abscissa) wrote:

Suppose I want ddoc output to include this line:

--
Note: Blah blabbety blah
--

But the colon causes "Note" to be considered a section header. Is there
a way to escape the ":" so that it's displayed as expected, but doesn't
trigger a section?


DDoc has the following syntax as in "$ should be replaced with " at

  http://dlang.org/spec/ddoc.html

(None of the following is tested.)

a) Try using the following macro:

COLON = 

And then use "Note$(COLON) Blah".

b) Perhaps easier:

COLON = 
NOTE = Note$(COLON)

Then use "$(NOTE) Blah"

c) Another option:

NOTE = Note $0

Then use "$(NOTE Blah)"

Ali



Re: ddoc: Can I escape a colon?

2017-02-23 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Feb 23, 2017 at 04:04:39PM -0500, Nick Sabalausky (Abscissa) via 
Digitalmars-d-learn wrote:
> Suppose I want ddoc output to include this line:
> 
> --
> Note: Blah blabbety blah
> --
> 
> But the colon causes "Note" to be considered a section header. Is
> there a way to escape the ":" so that it's displayed as expected, but
> doesn't trigger a section?

Try:

_Note: Blah blabbety blah

?

Generally, whenever ddoc does something you don't like, sticking an
underscore in front of the offending word is often the solution.  (It's
sorta like Windows and the 3-finger salute, but I digress. ;-))


T
-- 
Right now I'm having amnesia and deja vu at the same time. I think I've 
forgotten this before.


Re: RAII

2017-02-23 Thread cym13 via Digitalmars-d-learn
On Thursday, 23 February 2017 at 09:52:26 UTC, Arun 
Chandrasekaran wrote:

I'm trying to write an RAII wrapper on Linux.

I understand struct in D doesn't have default constructor (for 
.init reasons).

I don't want to use `scope`.

Is there an elegant way to achieve this in D?

```
import core.sys.posix.pthread;
import core.sys.posix.sys.types;

/// Makes pthread_mutexattr_t cleanup easy when using exceptions
struct mutexattr_wrapper
{
/// Constructor
this(bool _)
{
if (pthread_mutexattr_init(_attr) != 0 ||
pthread_mutexattr_setpshared(_attr, 
PTHREAD_PROCESS_SHARED)!= 0)

  // may be I will add few more methods here
  throw custom_exception("pthread_mutexattr_ 
failed");

}

/// Destructor
~this() {  pthread_mutexattr_destroy(_attr);  }

/// This allows using mutexattr_wrapper as 
pthread_mutexattr_t

alias m_attr this;

pthread_mutexattr_t m_attr;
}
```


It reminds me of 
https://w0rp.com/blog/post/an-raii-constructor-by-another-name-is-just-as-sweet/ which isn't what you want but may be interesting anyway.


ddoc: Can I escape a colon?

2017-02-23 Thread Nick Sabalausky (Abscissa) via Digitalmars-d-learn

Suppose I want ddoc output to include this line:

--
Note: Blah blabbety blah
--

But the colon causes "Note" to be considered a section header. Is there 
a way to escape the ":" so that it's displayed as expected, but doesn't 
trigger a section?


Re: simple static if / traits question...

2017-02-23 Thread Patrick Schluter via Digitalmars-d-learn
On Thursday, 23 February 2017 at 18:35:29 UTC, Profile Anaysis 
wrote:

[...]


option 1 is the one I was shooting for. does the static if 
(audio) just check for the existence of audio, or does it also 
check to see if audio is true as well?




Yes, but it checks at compile time. So the code will be 
evaluated by the compiler and if audio is true, it will only 
compile in the code in the if block.


e.g,

static if (audio) { do something }

will be identical, in the binary, to

do something

if audio is true.

The static if is an if statement and works like any ordinary if 
statement, but since you are using static(known at compile 
time) information then static if can actually be 
computed/evaluated at compile time(since all the inputs are 
know and cannot be changed)


Or to make it simple

 static if (audio) { do somethng }

is the same thing as

 #if audio
   do something
 #endif

in C and C++.


Re: Checking, whether string contains only ascii.

2017-02-23 Thread berni via Digitalmars-d-learn

On Thursday, 23 February 2017 at 17:44:05 UTC, HeiHon wrote:

Generally postscript files may contain binary data.
Think of included images or font data.
So in postscript files there should normally be no utf-8 
encoded text, but binary data are quite usual.

Think of postscript files as a sequence of ubytes.


As far as I know, images and font data have to be in clean7bit 
too (they are not human readable though). But postscript files 
can contain preview images, which can be binary. I know about 
this. I just tried to keep my question simple -- and actually I'm 
only testing part of the postscript file, where I know, that 
binary data must not occur.


Re: RAII

2017-02-23 Thread kinke via Digitalmars-d-learn

On Thursday, 23 February 2017 at 18:46:58 UTC, kinke wrote:

A constructor is just a factory function with a special name...


Wrt. the special name, that's obviously extremely useful for 
generic templates (containers etc.).


Re: RAII

2017-02-23 Thread kinke via Digitalmars-d-learn
On Thursday, 23 February 2017 at 14:24:14 UTC, Adam D. Ruppe 
wrote:

On Thursday, 23 February 2017 at 10:48:38 UTC, kinke wrote:
That's not elegant. You need a factory function for each type 
containing one of these structs then.


A constructor is just a factory function with a special name... 
it is almost equal amount of work.


Sorry but this is just completely wrong. RAII is all about me NOT 
having to call special functions all over the place.


struct S
{
this() { /* initialize */ }
}

S[123] myArray; // hooray, no need to invoke a factory in a loop

struct Container
{
S s;
// hooray, implicit this() automatically calls s.this()
}


Re: Debugging D applications from VS code with webfreak.debug

2017-02-23 Thread FR via Digitalmars-d-learn

On Thursday, 23 February 2017 at 17:54:09 UTC, FR wrote:
gdb is in my path, I can run it from the command line. When I 
run 'gdb test.exe' (test.exe being the binary placed in my 
workspace folder), I get the error message "not in executable 
format: File format not recognized", whether I build as x86 or 
x86_64. Any further tips on where I could get a working gdb?


Nevermind on this one. Turns out something was off with the gdb 
from my MinGW installation. Got a new one from 
http://www.equation.com/servlet/equation.cmd?fa=gdb , placed it 
where it can be found and it runs. Yay!


However: I cannot seem to get breakpoints to work. When my 
executable is launched, the debug output says "No symbol table is 
loaded.  Use the "file" command.". Is there any special flag I 
need to set in my dub.json? Should I point the "target" and "cwd" 
in the launch.json anywhere but the executable that pops up in my 
${workspaceRoot} (e.g. one of the sub-folders of .dub/build)?




Re: simple static if / traits question...

2017-02-23 Thread Profile Anaysis via Digitalmars-d-learn

There are a few options:

1. static if(audio)
2. version(audio)
3. if (audio)

It looks like you are trying to create the version(audio) 
semantic(if exists then use, else don't).


Ultimately, though, if you are trying to make a binary that 
can either use audio or not depending on where it is ran, 
you'll have to stick to using actual run time variables and 
probably be a bit more organized about it.


option 1 is the one I was shooting for. does the static if 
(audio) just check for the existence of audio, or does it also 
check to see if audio is true as well?




Yes, but it checks at compile time. So the code will be evaluated 
by the compiler and if audio is true, it will only compile in the 
code in the if block.


e.g,

static if (audio) { do something }

will be identical, in the binary, to

do something

if audio is true.

The static if is an if statement and works like any ordinary if 
statement, but since you are using static(known at compile time) 
information then static if can actually be computed/evaluated at 
compile time(since all the inputs are know and cannot be changed)




Re: template parameter inference and introspection

2017-02-23 Thread Meta via Digitalmars-d-learn

On Thursday, 23 February 2017 at 18:21:51 UTC, Meta wrote:
On Thursday, 23 February 2017 at 16:01:44 UTC, John Colvin 
wrote:
Is there any way to get a reference/alias to the instantiation 
of a template function that would be called, given certain 
parameters? I.e. to get the result of whatever template 
parameter inference (and overload resolution) has occurred?


E.g. for some arbitrarily complex foo:

static assert(__traits(compiles, foo(3)));
alias fooWithInt = someMagic(foo(3));

so if foo was `void foo(T)(T t) {}` then `fooWithInt` would be 
`foo!int`, but if it was `void foo(Q = float, T = long)(T t)` 
then `fooWithInt` would be `foo!(float, int)`


I don't believe so, because foo(3) is a value (void), not a 
type like foo!int would be. You can't get it back after you've 
called the function. You would have to do something like:


alias fooWithInt = someMagic!foo(3);

Where someMagic constructs the alias to foo!int based on the 
type of arguments passed, or something like that.


A quick and rough example I threw together. Annoyingly, I can't 
figure out a way to tell the compiler that I want to printout the 
symbol of fooWithInt, not call it without parens. The best I can 
do is print out its type.


void foo(T)(T t) {}
void foo(Q = float, T = long)(T t) {}

alias Typeof(alias v) = typeof(v);

template getInstantiation(alias f, T...)
{
import std.meta;

alias getInstantiation = f!(staticMap!(Typeof, T));
}

alias fooWithInt = getInstantiation!(foo, 3);
alias fooWithLong = getInstantiation!(foo, 3L);

void main()
{
pragma(msg, typeof(fooWithInt));
pragma(msg, typeof(fooWithLong));
}


Re: template parameter inference and introspection

2017-02-23 Thread Meta via Digitalmars-d-learn

On Thursday, 23 February 2017 at 16:01:44 UTC, John Colvin wrote:
Is there any way to get a reference/alias to the instantiation 
of a template function that would be called, given certain 
parameters? I.e. to get the result of whatever template 
parameter inference (and overload resolution) has occurred?


E.g. for some arbitrarily complex foo:

static assert(__traits(compiles, foo(3)));
alias fooWithInt = someMagic(foo(3));

so if foo was `void foo(T)(T t) {}` then `fooWithInt` would be 
`foo!int`, but if it was `void foo(Q = float, T = long)(T t)` 
then `fooWithInt` would be `foo!(float, int)`


I don't believe so, because foo(3) is a value (void), not a type 
like foo!int would be. You can't get it back after you've called 
the function. You would have to do something like:


alias fooWithInt = someMagic!foo(3);

Where someMagic constructs the alias to foo!int based on the type 
of arguments passed, or something like that.


Re: Debugging D applications from VS code with webfreak.debug

2017-02-23 Thread FR via Digitalmars-d-learn

On Thursday, 23 February 2017 at 16:30:08 UTC, WebFreak001 wrote:
I don't know how to build mago-mi either, but you can obtain it 
from the bundle with dlangide 
https://github.com/buggins/dlangide/releases/download/v0.6.11/dlangide-v0_6_11-bin-win32_x86-magomi-v0_3_1.zip


Thanks, that got me somewhere. However, this executable stops 
working as soon as I run it from the command line. 
Double-clicking it from the explorer opens a gdb console. I added 
it to my path anyyhow, but clicking on debug in vscode with a 
launch.json with "type": "mago-mi" doesn't do anything.


With GDB it should just work though, if you can run `gdb` from 
the command line. If you can only run it through some MinGW 
command line version, try running vscode over the command line 
there


gdb is in my path, I can run it from the command line. When I run 
'gdb test.exe' (test.exe being the binary placed in my workspace 
folder), I get the error message "not in executable format: File 
format not recognized", whether I build as x86 or x86_64. Any 
further tips on where I could get a working gdb?


Re: Checking, whether string contains only ascii.

2017-02-23 Thread HeiHon via Digitalmars-d-learn

On Thursday, 23 February 2017 at 08:34:53 UTC, berni wrote:
On Wednesday, 22 February 2017 at 21:23:45 UTC, H. S. Teoh 
wrote:

enforce(!s.any!"a > 127");


Puh, it's lot's of possibilities to choose of, now... I thought 
of something like the foreach-loop but wasn't sure if that is 
correct for all utf encodings. All in all, I think I take the 
any-approach, because it feels a little bit more like looking 
at the string at a whole and I like to use enforce.


Thanks for all your answers!


All the examples given here are very nice.
But alas this will not work with postscript files as found in the 
wild.


In my program, I read a postscript file. Normal postscript 
files should only be composed of ascii characters, but one 
never knows what users give us. Therefore I'd like to make sure 
that the string the program read is only made up of ascii 
characters.


Generally postscript files may contain binary data.
Think of included images or font data.
So in postscript files there should normally be no utf-8 encoded 
text, but binary data are quite usual.

Think of postscript files as a sequence of ubytes.


Re: Debugging D applications from VS code with webfreak.debug

2017-02-23 Thread WebFreak001 via Digitalmars-d-learn

On Thursday, 23 February 2017 at 16:28:26 UTC, FR wrote:

Hi everyone,

as the subject says, I'm trying to get a debugger running with 
visual studio code on windows.
I have installed WebFreak001's code-d and debug extensions but 
fail to figure out how to install a working debugger. The gdb I 
have installed is part of a MinGW installation and complains 
about the file format of the executable, I'm unsure if there's 
soemthing wrong there.
I could not figure out how to obtain mago-mi to try that one, 
either.
I'd appreciate any help or pointers to resources on how to get 
this running, as I otherwise really like the workflow with 
code-d.


Cheers,

FR


I don't know how to build mago-mi either, but you can obtain it 
from the bundle with dlangide 
https://github.com/buggins/dlangide/releases/download/v0.6.11/dlangide-v0_6_11-bin-win32_x86-magomi-v0_3_1.zip


With GDB it should just work though, if you can run `gdb` from 
the command line. If you can only run it through some MinGW 
command line version, try running vscode over the command line 
there


template parameter inference and introspection

2017-02-23 Thread John Colvin via Digitalmars-d-learn
Is there any way to get a reference/alias to the instantiation of 
a template function that would be called, given certain 
parameters? I.e. to get the result of whatever template parameter 
inference (and overload resolution) has occurred?


E.g. for some arbitrarily complex foo:

static assert(__traits(compiles, foo(3)));
alias fooWithInt = someMagic(foo(3));

so if foo was `void foo(T)(T t) {}` then `fooWithInt` would be 
`foo!int`, but if it was `void foo(Q = float, T = long)(T t)` 
then `fooWithInt` would be `foo!(float, int)`


Re: RAII

2017-02-23 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 23 February 2017 at 10:48:38 UTC, kinke wrote:
That's not elegant. You need a factory function for each type 
containing one of these structs then.


A constructor is just a factory function with a special name... 
it is almost equal amount of work.


Re: RAII

2017-02-23 Thread Adrian Matoga via Digitalmars-d-learn
On Thursday, 23 February 2017 at 09:52:26 UTC, Arun 
Chandrasekaran wrote:

I'm trying to write an RAII wrapper on Linux.

I understand struct in D doesn't have default constructor (for 
.init reasons).

I don't want to use `scope`.

Is there an elegant way to achieve this in D?



static opCall() is the way to emulate an argumentless struct 
constructor. You still need to call it explicitly, though:


```
import std.stdio : writefln;
struct Foo {
int a;
static Foo opCall() {
Foo foo;
foo.a = 42;
return foo;
}
~this() {
writefln("destroyed with a=%d", a);
}
@disable this(this);
@disable void opAssign(Foo);
}

void main()
{
auto foo1 = Foo(); // ok
Foo foo2;  // == Foo.init
}
```




Re: RAII

2017-02-23 Thread kinke via Digitalmars-d-learn

On Thursday, 23 February 2017 at 09:57:09 UTC, ketmar wrote:

Arun Chandrasekaran wrote:

Is there an elegant way to achieve this in D?
why not static method or free function that returns struct? due 
to NRVO[0] it won't even be copied.


That's not elegant. You need a factory function for each type 
containing one of these structs then.


Re: JustQuestion: Are 'D' had a browser library?

2017-02-23 Thread dummy via Digitalmars-d-learn

On Sunday, 19 February 2017 at 09:21:40 UTC, aberba wrote:

On Sunday, 19 February 2017 at 08:01:56 UTC, dummy wrote:

[...]


You can use any D lib with http GET support (Vibe.d[1]: 
download, requests[2]) at code.dlang.org. arsd.dom[3] has dom 
parsing support. or use any XML lib.


[1] http://code.dlang.org/packages/vibe-d
[2] http://code.dlang.org/packages/requests
[3] http://code.dlang.org/packages/arsd

The expiremental XML lib too has nice API 
(https://lodo1995.github.io/experimental.xml/std/experimental/xml/dom/Document.html)


I recommend requests and experimental DOM lib 
(http://code.dlang.org/packages/std-experimental-xml) to 
implement crawling.


'requests' looks like very useful! Thank you so much!


Re: RAII

2017-02-23 Thread ketmar via Digitalmars-d-learn

Arun Chandrasekaran wrote:


I'm trying to write an RAII wrapper on Linux.

I understand struct in D doesn't have default constructor (for .init 
reasons).

I don't want to use `scope`.

Is there an elegant way to achieve this in D?
why not static method or free function that returns struct? due to 
NRVO[0] it won't even be copied.


 auto lock = MyWrapper();

`MyWrapper()` may return voldemort type, so user won't create your 
struct accidentally.



[0] https://dlang.org/glossary.html#nrvo


RAII

2017-02-23 Thread Arun Chandrasekaran via Digitalmars-d-learn

I'm trying to write an RAII wrapper on Linux.

I understand struct in D doesn't have default constructor (for 
.init reasons).

I don't want to use `scope`.

Is there an elegant way to achieve this in D?

```
import core.sys.posix.pthread;
import core.sys.posix.sys.types;

/// Makes pthread_mutexattr_t cleanup easy when using exceptions
struct mutexattr_wrapper
{
/// Constructor
this(bool _)
{
if (pthread_mutexattr_init(_attr) != 0 ||
pthread_mutexattr_setpshared(_attr, 
PTHREAD_PROCESS_SHARED)!= 0)

  // may be I will add few more methods here
  throw custom_exception("pthread_mutexattr_ failed");
}

/// Destructor
~this() {  pthread_mutexattr_destroy(_attr);  }

/// This allows using mutexattr_wrapper as pthread_mutexattr_t
alias m_attr this;

pthread_mutexattr_t m_attr;
}
```


Re: Checking, whether string contains only ascii.

2017-02-23 Thread berni via Digitalmars-d-learn

On Wednesday, 22 February 2017 at 21:23:45 UTC, H. S. Teoh wrote:

enforce(!s.any!"a > 127");


Puh, it's lot's of possibilities to choose of, now... I thought 
of something like the foreach-loop but wasn't sure if that is 
correct for all utf encodings. All in all, I think I take the 
any-approach, because it feels a little bit more like looking at 
the string at a whole and I like to use enforce.


Thanks for all your answers!