std.getopt and std.datetime

2017-05-12 Thread Russel Winder via Digitalmars-d-learn
Is there a canonical, idiomatic way of processing std.datetime objects
using std.getopt?

Currently, I am suffering:

/usr/include/d/std/getopt.d(921): Error: static assert  "Dunno how to deal with 
type SysTime*"

which on the one hand is understandable, albeit dreadful English, but
then I suppose it is American not English, as std.getopt is only going
to process builtin types, but it is very annoying.

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

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


Re: Access specifiers and visibility

2017-05-12 Thread Andrew Edwards via Digitalmars-d-learn

On Thursday, 11 May 2017 at 04:35:22 UTC, Jesse Phillips wrote:

On Wednesday, 10 May 2017 at 13:29:40 UTC, Andrew Edwards wrote:
On Wednesday, 10 May 2017 at 13:13:46 UTC, Jesse Phillips 
wrote:
On Wednesday, 10 May 2017 at 01:42:47 UTC, Andrew Edwards 
wrote:
Attempting to update a git repo to current D, I encounter 
the following deprecation messages:


src/glwtf/signals.d-mixin-256(256,2): Deprecation: 
glwtf.input.BaseGLFWEventHandler._on_key_down is not visible 
from module glwtf.signals


Thanks,
Andrew


This comes from:
http://dlang.org/changelog/2.071.0.html#dip22

The module glwtf.signals needs to import glwtf.input. One of 
the other imports was contaminating the namespace.


I actually attempted solve the issue with selective import as 
such::


import glwtf.input : BaseGLFWEventHandler;

but it did not work. Importing the entire module does not work 
either.


I'm not sure the library you're using but this like the one:
https://github.com/Dav1dde/glwtf/blob/master/glwtf/input.d#L163

This code says that the function is protected.

https://github.com/Dav1dde/glwtf/blob/master/glwtf/signals.d#L254

This line is probably mixing in a call to that function into a 
struct


https://github.com/Dav1dde/glwtf/blob/master/glwtf/signals.d#L229

But that struct doesn't inherit from the class so it can't 
access protected fields.


That's not the same library but the exact same implementation. 
The one I'm using (https://github.com/d-gamedev-team/dimgui) 
borrows its from the deimos bindings.


So since the a struct cannot inherit from a class and vice versa, 
then the entire approach taken by the library must be 
reconsidered going forward. A little over my head at this point.


Thanks for the assist.


Re: How to avoid throwing an exceptions for a built-in function?

2017-05-12 Thread Mike B Johnson via Digitalmars-d-learn

On Thursday, 11 May 2017 at 16:07:22 UTC, k-five wrote:
On Wednesday, 10 May 2017 at 21:19:21 UTC, Stanislav Blinov 
wrote:

On Wednesday, 10 May 2017 at 15:35:24 UTC, k-five wrote:

On Wednesday, 10 May 2017 at 14:27:46 UTC, Stanislav Blinov

---
I don't understand. If you don't want to take care of 
exceptions, then you just don't do anything, simply call 
to!int(str).


Well I did that, but when the string is a valid type like: "10" 
there is no problems. But when the string is not valid, like: 
"abc", then to! function throws an exception.


Why I do not want to take care of that? Because I just need the 
value, if the string is valid, otherwise no matter what the 
value of string is.


First I just wrote:
index = to!int( user_apply[ 4 ] );

And this code is a part of a command-line program and the user 
may enter anything. So, for a valid string:

./program '10'   // okey

but for:
./program 'non-numerical' // throws an exception an 10 lines of 
error appear on the screen( console )


I just want to silent this exception. Of course it is useful 
for handling when someone wants to. But in my code I no need to 
handle it. So I want to silent that, without using 
try{}catch(){} block. I just wondered about try-catch and I 
want to know may there would be a better way instead of a dummy 
try-catch block.


Thanks for replying and mentioning. And I am sorry, since I an 
new in English Writing, if you got confuse.


You are not making a lot of sense:

1. Exception do bubble up, so you don't need to "handle" 
exceptions at the call site if you don't want to. The whole point 
of exceptions is do effectively do what you want.


2. You say that you don't have to deal with it in your code but 
you are trying to deal with it now.



You are not clear on exactly what you are trying to accomplish.

Can you be more specific on why you do not want to add a 
try/catch/if-else/ifThrown/etc? Is it because you don't need to 
handle it in your own usage? If that is true, will others ever 
use it?


Basically all you have to do is ask yourself this:

"Do I need to EVER handle the case where the data is invalid?"

If you do(e.g., other users will use your code) then you better 
implement some type of exception handling. Else all you have to 
do is always put in the right data(not good because it might bite 
you in the ass one day).


If you just want less noise when the program crashes, just put a 
try/catch block in main and catch all generic exceptions and exit 
with a simple error message like "There was an ERROR, I do not 
know why...".








Re: As many thanks As possible to who crates D and UFCS feature

2017-05-12 Thread Nicholas Wilson via Digitalmars-d-learn

On Friday, 12 May 2017 at 21:26:01 UTC, Bastiaan Veelo wrote:

On Friday, 12 May 2017 at 15:24:52 UTC, k-five wrote:
A full version that I just added to my gitgub: 
https://github.com/k-five/dren


You may like getopt[1] for command line argument parsing.

https://dlang.org/phobos/std_getopt.html


see also
https://blog.thecybershadow.net/2014/08/05/ae-utils-funopt/
https://github.com/CyberShadow/ae/blob/master/utils/funopt.d



Re: Lookahead in unittest

2017-05-12 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, May 12, 2017 at 05:23:23PM -0400, Steven Schveighoffer via 
Digitalmars-d-learn wrote:
[...]
> Note, you can achieve what you want with version(unittest):
> 
> version(unittest)
> {
>class A { B b; }
>class B { }
> }
> 
> unittest
> {
>// use A and B here
> }
[...]

This advice, unfortunately, needs to be tempered with caution about
namespace pollution and accidental dependency of things outside
unittests on things inside a version(unittest) block.  There's also the
issue of library code introducing extraneous import dependencies that
are really only necessary for unittesting, but get pulled in anyway
because user code happens to compile with -unittest.


T

-- 
"Real programmers can write assembly code in any language. :-)" -- Larry Wall


Re: Lookahead in unittest

2017-05-12 Thread pineapple via Digitalmars-d-learn
On Friday, 12 May 2017 at 21:23:23 UTC, Steven Schveighoffer 
wrote:

Note, you can achieve what you want with version(unittest):


Please prefer `private version(unittest){...}` if the module 
might be imported by someone else's code, as to not pollute it 
with unneeded symbols




Re: As many thanks As possible to who crates D and UFCS feature

2017-05-12 Thread Bastiaan Veelo via Digitalmars-d-learn

On Friday, 12 May 2017 at 15:24:52 UTC, k-five wrote:
A full version that I just added to my gitgub: 
https://github.com/k-five/dren


You may like getopt[1] for command line argument parsing.

https://dlang.org/phobos/std_getopt.html


Re: Lookahead in unittest

2017-05-12 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/10/17 12:53 PM, Raiderium wrote:

On Wednesday, 10 May 2017 at 16:32:11 UTC, Adam D. Ruppe wrote:

On Wednesday, 10 May 2017 at 16:09:06 UTC, Raiderium wrote:

I can't figure out if this is intended behaviour.


It is. A unittest is a function, and in functions, all declarations
must be defined before used (just like local variables).

Sometimes, you can wrap it in a struct:

unittest {
  struct Decls {
// put your decls here
  }

  with(Decls()) {
   // call funcs here
  }
}


Ah. I wasn't aware class declarations within functions (including
unittest) were sensitive to their order, so that's something I've
learned today. :)

I tried the with(Decls()) syntax and it worked perfectly, thanks Adam.
I'd been haphazardly nesting unittest{} blocks within the struct, and it
felt less than sanitary.

For full disclosure, the test I'm writing needs to create a reference
cycle (as in, class B holding a reference to A), and it works properly
if the classes are declared at module/class/struct level, but then
either the class names pollute the module (which is just eww) or they're
nested within a class/struct, which leads me to the current situation.

Consider my problem solved :) Thanks again Stefan and Adam for the replies.


Note, you can achieve what you want with version(unittest):

version(unittest)
{
   class A { B b; }
   class B { }
}

unittest
{
   // use A and B here
}

-Steve


Re: As many thanks As possible to who crates D and UFCS feature

2017-05-12 Thread Bastiaan Veelo via Digitalmars-d-learn

On Friday, 12 May 2017 at 15:24:52 UTC, k-five wrote:

On Friday, 12 May 2017 at 11:10:01 UTC, k-five wrote:
I was waiting for a stable version of C++17 ( standard library 
) to add some features of fileSystem in C++17 to my program 
that wants to iterate through all files in a directory 
recursively.


I was thinking how could I do for implementing that and add it 
to my program.


Now after starting to learn D ( nearby 2 weeks so far ). I can 
do it in 6 lines!


void main( string[] args ){

	string[] all_file_name =  dirEntries( ".", SpanMode.depth, 
false )
 .filter!( file => !file.name.matchFirst( regex( args[ 
1 ] ) ).empty() )
 .filter!( file => ( args[ 2 ] == "-f" || args[ 2 ] == 
"-d"  ? ( args[ 2 ] == "-f" ? !file.isDir : !file.isFile ) : ( 
!file.isSymlink ) ) )

 .map!( file => file.name )
 .array;
foreach( string item; all_file_name ) writeln( item );

}

./bin-file '[A-Z]$' -f   ---> print all files that are matched 
against [A-Z]$


./bin-file '[A-Z]$' -d   ---> print all directory that are 
matched against [A-Z]$


./bin-file '[A-Z]$' "anything-else"  ---> print both files and 
directory that are matched against [A-Z]$


I am so happy since after more than one year practicing in C++ 
and putting a collection more than 2000 examples of C++ on my 
github, I was not sure I could do it in 6 lines.


May it is a Spam but I think it is worth it.


--

May it has worth it to be an example on how great D is, in 
somewhere like, in the tour section or std.file or std.regex to 
attract others.


A full version that I just added to my gitgub: 
https://github.com/k-five/dren


Is it safe to say that these 40 lines of D do the same as your 
324 lines of C++ [1]? This, and your comments on the difficulties 
of building renrem [2] versus doing "rdmd", and the steepness of 
the learning curve (1 year C++ vs 2 weeks D), and the 
productivity (2 hours D vs ?? C++) I think are plenty material 
for a nice little blog.


Mike Parker runs the D blog, and I think he might be interested. 
No need to worry about the english language, you are safe with 
Mike. I'll see if I can get you his attention.


[1] https://github.com/k-five/renrem
[2] https://github.com/k-five/renrem/blob/master/src/README.md
[3] https://dlang.org/blog/


Re: How to avoid throwing an exceptions for a built-in function?

2017-05-12 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, May 12, 2017 at 12:47:04PM +0200, ag0aep6g via Digitalmars-d-learn 
wrote:
> On 05/12/2017 10:32 AM, k-five wrote:
> > Interesting! I was worried about performance and for that I did not
> > want to use try-catch.
> > So (isNumberic) is a good solution.
> > http://dlang.org/phobos/std_traits.html#isNumeric
> 
> If you're doing this for speed, you better be benchmarking. Exceptions
> are slow when they're thrown. But if no exception is being thrown,
> try-catch is probably faster than checking isNumeric beforehand.

Yes, when it comes to performance-related issues, always profile,
profile, profile. (Or benchmark, benchmark, benchmark. Anything that
gives you actual measurements rather than subjective judgment calls.)
Far too often, what we think will perform poorly is actually nowhere
near the real bottleneck in the program, and we end up wasting too much
time "optimizing" something that doesn't even make a noticeable
difference in the end. Whereas, using a profiler early on will help you
zero in on the real bottlenecks, and you could potentially make huge
performance savings with much less effort.


T

-- 
Beware of bugs in the above code; I have only proved it correct, not tried it. 
-- Donald Knuth


Re: As many thanks As possible to who crates D and UFCS feature

2017-05-12 Thread k-five via Digitalmars-d-learn

On Friday, 12 May 2017 at 11:10:01 UTC, k-five wrote:
I was waiting for a stable version of C++17 ( standard library 
) to add some features of fileSystem in C++17 to my program 
that wants to iterate through all files in a directory 
recursively.


I was thinking how could I do for implementing that and add it 
to my program.


Now after starting to learn D ( nearby 2 weeks so far ). I can 
do it in 6 lines!


void main( string[] args ){

	string[] all_file_name =  dirEntries( ".", SpanMode.depth, 
false )
 .filter!( file => !file.name.matchFirst( regex( args[ 
1 ] ) ).empty() )
 .filter!( file => ( args[ 2 ] == "-f" || args[ 2 ] == 
"-d"  ? ( args[ 2 ] == "-f" ? !file.isDir : !file.isFile ) : ( 
!file.isSymlink ) ) )

 .map!( file => file.name )
 .array;
foreach( string item; all_file_name ) writeln( item );

}

./bin-file '[A-Z]$' -f   ---> print all files that are matched 
against [A-Z]$


./bin-file '[A-Z]$' -d   ---> print all directory that are 
matched against [A-Z]$


./bin-file '[A-Z]$' "anything-else"  ---> print both files and 
directory that are matched against [A-Z]$


I am so happy since after more than one year practicing in C++ 
and putting a collection more than 2000 examples of C++ on my 
github, I was not sure I could do it in 6 lines.


May it is a Spam but I think it is worth it.


--

May it has worth it to be an example on how great D is, in 
somewhere like, in the tour section or std.file or std.regex to 
attract others.


A full version that I just added to my gitgub: 
https://github.com/k-five/dren


Re: Processing a gzipped csv-file by line-by-line

2017-05-12 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/11/17 8:18 PM, H. S. Teoh via Digitalmars-d-learn wrote:

On Wed, May 10, 2017 at 11:40:08PM +, Jesse Phillips via 
Digitalmars-d-learn wrote:



If you can get the zip to decompress into a range of dchar then
std.csv will work with it. It is by far not the fastest, but much
speed is lost since it supports input ranges and doesn't specialize on
any other range type.


I actually spent some time today to look into whether fastcsv can
possibly be made to work with general input ranges as long as they
support slicing... and immediately ran into the infamous autodecoding
issue: strings are not random-access ranges because of autodecoding, so
it would require either extensive code surgery to make it work, or ugly
hacks to bypass autodecoding.  I'm quite tempted to attempt the latter,
in fact, but not now since it's getting busier at work and I don't have
that much free time to spend on a major refactoring of fastcsv.


Yeah, iopipe treats char[] as a random-access sliceable range :) 
Autodecoding gets annoying if you want to do anything fancy (like 
chain(somestr, someotherstr))



Alternatively, I could possibly hack together a version of fastcsv that
took a range of const(char)[] as input (rather than a single string), so
that, in theory, it could handle arbitrarily large input files as long
as the caller can provide a range of data blocks, e.g., File.byChunk, or
in this particular case, a range of decompressed data blocks from
whatever decompressor is used to extract the data.  As long as you
consume the individual rows without storing references to them
indefinitely (don't try to make an array of the entire dataset),
fastcsv's optimizations should still work, since unreferenced blocks
will eventually get cleaned up by the GC when memory runs low.


I'm interested in getting a fast CSV parser built on top of iopipe. I 
may fork your code and see if I can get it to work. Since you already 
work on arrays, it should be quite simple, since arrays are also iopipes 
by default.


-Steve


Re: Processing a gzipped csv-file by line-by-line

2017-05-12 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/10/17 7:17 PM, Nicholas Wilson wrote:

On Wednesday, 10 May 2017 at 22:20:52 UTC, Nordlöw wrote:

What's fastest way to on-the-fly-decompress and process a gzipped
csv-fil line by line?

Is it possible to combine

http://dlang.org/phobos/std_zlib.html

with some stream variant of

File(path).byLineFast

?


I suggest you take a look at Steven's iopipe (also watch his Dconf
presentation). should be very simple.


Yeah, this should work and be quite fast:

import iopipe.zip;
import iopipe.textpipe;
import iopipe.bufpipe;
import iopipe.stream;

foreach(line; openDev(path).bufd.unzip.decodeText.byLineRange)

I think that was actually one of my slide examples.

-Steve


Re: As many thanks As possible to who crates D and UFCS feature

2017-05-12 Thread k-five via Digitalmars-d-learn

On Friday, 12 May 2017 at 12:56:50 UTC, drug wrote:

12.05.2017 14:58, k-five пишет:

On Friday, 12 May 2017 at 11:41:57 UTC, cym13 wrote:

On Friday, 12 May 2017 at 11:10:01 UTC, k-five wrote:

---

also .each!writeln should be possible

-

Yes. Worked. Thanks



Re: As many thanks As possible to who crates D and UFCS feature

2017-05-12 Thread drug via Digitalmars-d-learn

12.05.2017 14:58, k-five пишет:

On Friday, 12 May 2017 at 11:41:57 UTC, cym13 wrote:

On Friday, 12 May 2017 at 11:10:01 UTC, k-five wrote:

---


Shorter:

void main( string[] args ){
dirEntries( ".", SpanMode.depth, false )
 .filter!( file => !file.name.matchFirst( regex( args[ 1 ] )
).empty() )
 .filter!( file => ( args[ 2 ] == "-f" || args[ 2 ] == "-d"  ?
( args[ 2 ] == "-f" ? !file.isDir : !file.isFile ) : ( !file.isSymlink
) ) )
 .map!( file => file.name )
 .each!(string item => writeln( item ));
}

It's more memory efficient too because at no point the actual list is
stored.

-

Thanks and the correct syntax for each! is, passing a lambda. So the:

 .each!(string item => writeln( item ));

is an error:
temp.d(15): Error: found 'item' when expecting ')' following template
argument list ...

and should be:
.each!( ( string item )  => writeln( item ) );


also .each!writeln should be possible


Re: As many thanks As possible to who crates D and UFCS feature

2017-05-12 Thread Bastiaan Veelo via Digitalmars-d-learn

On Friday, 12 May 2017 at 11:10:01 UTC, k-five wrote:
I was waiting for a stable version of C++17 ( standard library 
) to add some features of fileSystem in C++17 to my program 
that wants to iterate through all files in a directory 
recursively.


I was thinking how could I do for implementing that and add it 
to my program.


Now after starting to learn D ( nearby 2 weeks so far ). I can 
do it in 6 lines!


Thumbs up, nice post!

Bastiaan.



Re: As many thanks As possible to who crates D and UFCS feature

2017-05-12 Thread cym13 via Digitalmars-d-learn

On Friday, 12 May 2017 at 11:58:23 UTC, k-five wrote:

On Friday, 12 May 2017 at 11:41:57 UTC, cym13 wrote:

[...]

---

[...]

-

Thanks and the correct syntax for each! is, passing a lambda. 
So the:

[...]

is an error:
temp.d(15): Error: found 'item' when expecting ')' following 
template argument list ...


and should be:
.each!( ( string item )  => writeln( item ) );


Ah, yeah, my bad, I should have try compiling it instead of 
answering directly ;)


Re: As many thanks As possible to who crates D and UFCS feature

2017-05-12 Thread k-five via Digitalmars-d-learn

On Friday, 12 May 2017 at 11:41:57 UTC, cym13 wrote:

On Friday, 12 May 2017 at 11:10:01 UTC, k-five wrote:

---


Shorter:

void main( string[] args ){
dirEntries( ".", SpanMode.depth, false )
 .filter!( file => !file.name.matchFirst( regex( args[ 
1 ] ) ).empty() )
 .filter!( file => ( args[ 2 ] == "-f" || args[ 2 ] == 
"-d"  ? ( args[ 2 ] == "-f" ? !file.isDir : !file.isFile ) : ( 
!file.isSymlink ) ) )

 .map!( file => file.name )
 .each!(string item => writeln( item ));
}

It's more memory efficient too because at no point the actual 
list is stored.

-

Thanks and the correct syntax for each! is, passing a lambda. So 
the:

 .each!(string item => writeln( item ));

is an error:
temp.d(15): Error: found 'item' when expecting ')' following 
template argument list ...


and should be:
.each!( ( string item )  => writeln( item ) );


Re: As many thanks As possible to who crates D and UFCS feature

2017-05-12 Thread cym13 via Digitalmars-d-learn

On Friday, 12 May 2017 at 11:10:01 UTC, k-five wrote:
I was waiting for a stable version of C++17 ( standard library 
) to add some features of fileSystem in C++17 to my program 
that wants to iterate through all files in a directory 
recursively.


I was thinking how could I do for implementing that and add it 
to my program.


Now after starting to learn D ( nearby 2 weeks so far ). I can 
do it in 6 lines!


void main( string[] args ){

	string[] all_file_name =  dirEntries( ".", SpanMode.depth, 
false )
 .filter!( file => !file.name.matchFirst( regex( args[ 
1 ] ) ).empty() )
 .filter!( file => ( args[ 2 ] == "-f" || args[ 2 ] == 
"-d"  ? ( args[ 2 ] == "-f" ? !file.isDir : !file.isFile ) : ( 
!file.isSymlink ) ) )

 .map!( file => file.name )
 .array;
foreach( string item; all_file_name ) writeln( item );

}

./bin-file '[A-Z]$' -f   ---> print all files that are matched 
against [A-Z]$


./bin-file '[A-Z]$' -d   ---> print all directory that are 
matched against [A-Z]$


./bin-file '[A-Z]$' "anything-else"  ---> print both files and 
directory that are matched against [A-Z]$


I am so happy since after more than one year practicing in C++ 
and putting a collection more than 2000 examples of C++ on my 
github, I was not sure I could do it in 6 lines.


May it is a Spam but I think it is worth it.


Shorter:

void main( string[] args ){
dirEntries( ".", SpanMode.depth, false )
 .filter!( file => !file.name.matchFirst( regex( args[ 1 
] ) ).empty() )
 .filter!( file => ( args[ 2 ] == "-f" || args[ 2 ] == 
"-d"  ? ( args[ 2 ] == "-f" ? !file.isDir : !file.isFile ) : ( 
!file.isSymlink ) ) )

 .map!( file => file.name )
 .each!(string item => writeln( item ));
}

It's more memory efficient too because at no point the actual 
list is stored.


As many thanks As possible to who crates D and UFCS feature

2017-05-12 Thread k-five via Digitalmars-d-learn
I was waiting for a stable version of C++17 ( standard library ) 
to add some features of fileSystem in C++17 to my program that 
wants to iterate through all files in a directory recursively.


I was thinking how could I do for implementing that and add it to 
my program.


Now after starting to learn D ( nearby 2 weeks so far ). I can do 
it in 6 lines!


void main( string[] args ){

	string[] all_file_name =  dirEntries( ".", SpanMode.depth, false 
)
 .filter!( file => !file.name.matchFirst( regex( args[ 1 
] ) ).empty() )
 .filter!( file => ( args[ 2 ] == "-f" || args[ 2 ] == 
"-d"  ? ( args[ 2 ] == "-f" ? !file.isDir : !file.isFile ) : ( 
!file.isSymlink ) ) )

 .map!( file => file.name )
 .array;
foreach( string item; all_file_name ) writeln( item );

}

./bin-file '[A-Z]$' -f   ---> print all files that are matched 
against [A-Z]$


./bin-file '[A-Z]$' -d   ---> print all directory that are 
matched against [A-Z]$


./bin-file '[A-Z]$' "anything-else"  ---> print both files and 
directory that are matched against [A-Z]$


I am so happy since after more than one year practicing in C++ 
and putting a collection more than 2000 examples of C++ on my 
github, I was not sure I could do it in 6 lines.


May it is a Spam but I think it is worth it.


Re: How to avoid throwing an exceptions for a built-in function?

2017-05-12 Thread ag0aep6g via Digitalmars-d-learn

On 05/12/2017 10:32 AM, k-five wrote:

Interesting! I was worried about performance and for that I did not want
to use try-catch.
So (isNumberic) is a good solution.
http://dlang.org/phobos/std_traits.html#isNumeric


If you're doing this for speed, you better be benchmarking. Exceptions 
are slow when they're thrown. But if no exception is being thrown, 
try-catch is probably faster than checking isNumeric beforehand.


Re: How to avoid throwing an exceptions for a built-in function?

2017-05-12 Thread k-five via Digitalmars-d-learn

On Friday, 12 May 2017 at 09:03:39 UTC, Jonathan M Davis wrote:
That's the wrong isNumeric. Unfortunately, both std.string and 
std.traits have an isNumeric. std.traits.isNumeric is an 
eponymous template that tests whether a type is an integral or 
floating point type, whereas std.string.isNumeric is a 
templated function which tests whether a string (or other range 
of characters) represents an integer or floating point literal. 
So, std.traits.isNumeric won't work at all for what you want, 
and std.string.isNumeric will sort of do what you want - the 
main caveat being that it will also accept floating point 
values, whereas to!int will not.


So, if you have

auto i = str.isNumeric ? to!int(str).ifThrown(0) : 0;

then it will work but will still throw (and then be caught by 
ifThrown) if str is a floating point value. Alternatively, you 
could check something like


auto i = str.all!isDigit ? to!int(str).ifThrown(0) : 0;

(where all is in std.algorithm and isDigit is in std.ascii), 
and that _almost_ wouldn't need ifThrown, letting you do


auto i = str.all!isDigit ? to!int(str) : 0;

except that str could be an integral value that wouldn't fit in 
an int, in which case to!int would throw. But 
std.string.isNumeric will work so long as you're willing to 
have have an exception be thrown and caught if the string is a 
floating point literal.


- Jonathan M Davis


-

Thank you for mentioning it. Since I had seen D-conference 2016 
in YouTube and it talked about isNumeric in std.traits, then I 
used std.traits. and does not worked as I wanted.


But std.string: isNumeric, worked.

string str = "string";
int index = str.isNumeric ? to!int( str ) : 0;
writeln( "index: ", index ); // 0 and without throwing any 
exceptions


Re: How to avoid throwing an exceptions for a built-in function?

2017-05-12 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, May 12, 2017 08:32:03 k-five via Digitalmars-d-learn wrote:
> On Thursday, 11 May 2017 at 19:59:55 UTC, Jordan Wilson wrote:
> > On Thursday, 11 May 2017 at 18:07:47 UTC, H. S. Teoh wrote:
> >> On Thu, May 11, 2017 at 05:55:03PM +, k-five via
> >>
> >> Digitalmars-d-learn wrote:
> >>> On Thursday, 11 May 2017 at 17:18:37 UTC, crimaniak wrote:
> >>> > On Wednesday, 10 May 2017 at 12:40:41 UTC, k-five wrote:
> >>> -
> >
> > This reason is why I sometimes use isNumeric if I have heaps of
> > strings I need to convert,  to reduce exceptions. So something
> > like:
> > int index = (str.isNumeric) ? to!int(str).ifThrown(0) : 0;
> >
> > Jordan
>
> --
> Interesting! I was worried about performance and for that I did
> not want to use try-catch.
> So (isNumberic) is a good solution.
> http://dlang.org/phobos/std_traits.html#isNumeric

That's the wrong isNumeric. Unfortunately, both std.string and std.traits
have an isNumeric. std.traits.isNumeric is an eponymous template that tests
whether a type is an integral or floating point type, whereas
std.string.isNumeric is a templated function which tests whether a string
(or other range of characters) represents an integer or floating point
literal. So, std.traits.isNumeric won't work at all for what you want, and
std.string.isNumeric will sort of do what you want - the main caveat being
that it will also accept floating point values, whereas to!int will not.

So, if you have

auto i = str.isNumeric ? to!int(str).ifThrown(0) : 0;

then it will work but will still throw (and then be caught by ifThrown) if
str is a floating point value. Alternatively, you could check something like

auto i = str.all!isDigit ? to!int(str).ifThrown(0) : 0;

(where all is in std.algorithm and isDigit is in std.ascii), and that
_almost_ wouldn't need ifThrown, letting you do

auto i = str.all!isDigit ? to!int(str) : 0;

except that str could be an integral value that wouldn't fit in an int, in
which case to!int would throw. But std.string.isNumeric will work so long as
you're willing to have have an exception be thrown and caught if the string
is a floating point literal.

- Jonathan M Davis



Re: How to avoid throwing an exceptions for a built-in function?

2017-05-12 Thread k-five via Digitalmars-d-learn

On Friday, 12 May 2017 at 08:32:03 UTC, k-five wrote:

On Thursday, 11 May 2017 at 19:59:55 UTC, Jordan Wilson wrote:

On Thursday, 11 May 2017 at 18:07:47 UTC, H. S. Teoh wrote:
On Thu, May 11, 2017 at 05:55:03PM +, k-five via 
Digitalmars-d-learn wrote:

On Thursday, 11 May 2017 at 17:18:37 UTC, crimaniak wrote:
> On Wednesday, 10 May 2017 at 12:40:41 UTC, k-five wrote:
-


This reason is why I sometimes use isNumeric if I have heaps 
of strings I need to convert,  to reduce exceptions. So 
something like:

int index = (str.isNumeric) ? to!int(str).ifThrown(0) : 0;

Jordan

--
Interesting! I was worried about performance and for that I did 
not want to use try-catch.

So (isNumberic) is a good solution.
http://dlang.org/phobos/std_traits.html#isNumeric



NOT a GOOD solution! Since it accept type and NOT variable:

so:

index = (str.isNumeric) ? to!int(str).ifThrown(0) : 0;

is:

temp.d(11): Error: template std.traits.isNumeric cannot deduce 
function from argument types !()(string), candidates are:


Re: How to avoid throwing an exceptions for a built-in function?

2017-05-12 Thread k-five via Digitalmars-d-learn

On Thursday, 11 May 2017 at 19:59:55 UTC, Jordan Wilson wrote:

On Thursday, 11 May 2017 at 18:07:47 UTC, H. S. Teoh wrote:
On Thu, May 11, 2017 at 05:55:03PM +, k-five via 
Digitalmars-d-learn wrote:

On Thursday, 11 May 2017 at 17:18:37 UTC, crimaniak wrote:
> On Wednesday, 10 May 2017 at 12:40:41 UTC, k-five wrote:
-


This reason is why I sometimes use isNumeric if I have heaps of 
strings I need to convert,  to reduce exceptions. So something 
like:

int index = (str.isNumeric) ? to!int(str).ifThrown(0) : 0;

Jordan

--
Interesting! I was worried about performance and for that I did 
not want to use try-catch.

So (isNumberic) is a good solution.
http://dlang.org/phobos/std_traits.html#isNumeric