Re: Real OOP with D

2015-08-17 Thread BBasile via Digitalmars-d-learn

On Monday, 17 August 2015 at 05:57:52 UTC, Ozan wrote:

Hi

Working with objectoriented concepts results often in large 
trees of related classes. Every instance of a class knows his 
methods and data.  An example like following would work:


import std.stdio;
class Family { }
class Dad : Family { void greeting() { writeln("I'm dad"); } }
class Boy : Family { void greeting() { writeln("I'm daddy's 
boy"); } }

void main() {
writeln("Father and son");
Dad father = new Dad;
Family son = new Boy;
father.greeting;
son.greeting;
}

The critical point is using a variable of type Family for an 
instance of Boy. Class Family covers the greeting method of 
Boy. In real OOP that would not be a problem, because the 
access point of view starts with the object. In D, it starts 
with the class definition.


Is there any way to get real OOP with D?

Regards,  Ozan


Can you name an OOP oriented language that allows this ? Your 
example is eroneous OOP.
The 2 other answers you 've got (the first using an interface and 
the second using an abstract class) are valid OOP.


One of the fundamental concept OOP is that a function defined in 
a class exists also  in its subclasses. So how do you expect 
`greeting()` to exist in Family if it's only defined in its 
sub-classes ?


You can verify that with the 'Liskov substitution principle' 
(https://en.wikipedia.org/wiki/Liskov_substitution_principle).

Actually your sample violates this principle.


Re: How to provide this arg or functor for algorithm?

2015-08-17 Thread anonymous via Digitalmars-d-learn

On Sunday, 16 August 2015 at 23:05:42 UTC, Ali Çehreli wrote:

// Now the type of d is a template parameter
@nogc auto func(Func)(uint[] arr, Func d)
{
return arr.map!(d);
}


Huh. I think func being a template is the key here. When the 
original code is put in a template, it works too (with 2.068):



void func()() @nogc
{
import std.algorithm;
uint[3] arr = [1,2,3];
uint context = 2;
auto r = arr[].map!(delegate(value) { return value * context; 
});

}
void main()
{
func();
}




Re: How to provide this arg or functor for algorithm?

2015-08-17 Thread thedeemon via Digitalmars-d-learn

On Monday, 17 August 2015 at 09:51:47 UTC, anonymous wrote:
Huh. I think func being a template is the key here. When the 
original code is put in a template, it works too (with 2.068):


Nope, it "works" only because "r" is unreferenced and gets thrown 
out. Just try using r.front there, for example, and the error 
returns.




Re: Theoretical Best Practices

2015-08-17 Thread Steven Schveighoffer via Digitalmars-d-learn

On 8/14/15 6:25 PM, DarthCthulhu wrote:

On Friday, 14 August 2015 at 12:40:08 UTC, Steven Schveighoffer wrote:


I would do it this way:

// at module level
debug(logging) {
Logger logger;
static this() { logger = new Logger;}
}



By 'module level', I assume you mean in the module that defines the
Logger class? An 'import debug.logger' or somesuch would then give all
relevant modules access, correct?


I mean, in global scope (which is defined as being part of the module). 
Not inside a class/struct/function.



Is the compiler smart enough to compile out all the symbols associated
with the logger if it is never instantiated?


If it's never instantiated, and it's a template, then it will not be 
compiled in.


If it's not a template, it could potentially make it into the binary.

-Steve


Re: How to provide this arg or functor for algorithm?

2015-08-17 Thread anonymous via Digitalmars-d-learn

On Monday, 17 August 2015 at 10:28:33 UTC, thedeemon wrote:
Nope, it "works" only because "r" is unreferenced and gets 
thrown out. Just try using r.front there, for example, and the 
error returns.


You're right, it falls short.

But I think r not being referenced is not exactly it. Using front 
in Ali's func breaks it in the same way. And returning r from 
func works.


Wait, returning r from func works? Yes:


auto func()(uint[] arr, uint context) @nogc
{
import std.algorithm;
auto r = arr[].map!(delegate(value) { return value * context; 
});

return r;
}
void main() @nogc
{
uint[3] arr = [1,2,3];
uint context = 2;
auto r = func(arr[], context);

import std.algorithm: equal;
import std.range: only;
assert(equal(r, only(2, 4, 6)));
}


Is that supposed to compile? A closure is needed for the 
delegate, isn't it? With @nogc, where is the closure stored? This 
looks like an accepts-invalid bug to me.


It doesn't compile when func is not a template. So maybe the 
check is broken for templates.


It also doesn't compile with 2.067, so this may be a regression.

Coming back to Ali's code, here's a version that shows that func 
doesn't need template parameters, but it needs to be a template:



import std.stdio;
import std.algorithm;

struct Caller
{
uint method(uint value) pure @nogc {
return _context * value;
}

uint _context;
}

auto func()(uint[] arr, uint delegate(uint) pure @nogc d) @nogc
{
return arr.map!(d);
}

void main() @nogc
{
uint[3] arr = [1,2,3];
uint context = 2;
auto c = Caller(context);
auto d = &c.method;

auto r = func(arr[], d);

import std.algorithm: equal;
import std.range: only;
assert(equal(r, only(2, 4, 6)));
}


I think this relies on the same discrepancy as the problematic 
code above.


std.net.curl

2015-08-17 Thread tired_eyes via Digitalmars-d-learn

Hi,
I'm trying to compile this trivial example of std.net.curl:

// app.d
import std.stdio;
import std.net.curl;

void main() {
auto content = get("dlang.org");
}

Hovewer, "dmd app.d" spits a whole bunch of strange error 
messages:


/usr/lib64/libphobos2.a(curl.o): In function 
`_D3std3net4curl4HTTP21_sharedStaticCtor1520FZv':

std/net/curl.d:(.text._D3std3net4curl4HTTP21_sharedStaticCtor1520FZv+0xf): 
undefined reference to `curl_version_info'
/usr/lib64/libphobos2.a(curl.o): In function 
`_D3std3net4curl4Curl21_sharedStaticCtor1522FZv':

std/net/curl.d:(.text._D3std3net4curl4Curl21_sharedStaticCtor1522FZv+0xa): 
undefined reference to `curl_global_init'
/usr/lib64/libphobos2.a(curl.o): In function 
`_D3std3net4curl4Curl21_sharedStaticDtor1523FZv':

std/net/curl.d:(.text._D3std3net4curl4Curl21_sharedStaticDtor1523FZv+0x5): 
undefined reference to `curl_global_cleanup'
/usr/lib64/libphobos2.a(curl_1ca7_192.o): In function 
`_D3std3net4curl4HTTP4Impl6__dtorMFZv':
std/net/curl.d:(.text._D3std3net4curl4HTTP4Impl6__dtorMFZv+0x19): 
undefined reference to `curl_slist_free_all'
/usr/lib64/libphobos2.a(curl_1cca_ea.o): In function 
`_D3std3net4curl3FTP4Impl6__dtorMFZv':
std/net/curl.d:(.text._D3std3net4curl3FTP4Impl6__dtorMFZv+0x18): 
undefined reference to `curl_slist_free_all'
/usr/lib64/libphobos2.a(curl_1cca_ea.o): In function 
`_D3std3net4curl3FTP3dupMFZS3std3net4curl3FTP':

std/net/curl.d:(.text._D3std3net4curl3FTP3dupMFZS3std3net4curl3FTP+0xab): 
undefined reference to `curl_slist_append'
/usr/lib64/libphobos2.a(curl_1cca_ea.o): In function 
`_D3std3net4curl3FTP13clearCommandsMFZv':

std/net/curl.d:(.text._D3std3net4curl3FTP13clearCommandsMFZv+0x20): undefined 
reference to `curl_slist_free_all'
/usr/lib64/libphobos2.a(curl_1cca_ea.o): In function 
`_D3std3net4curl3FTP10addCommandMFAxaZv':

std/net/curl.d:(.text._D3std3net4curl3FTP10addCommandMFAxaZv+0x67): undefined 
reference to `curl_slist_append'
/usr/lib64/libphobos2.a(curl_1ccf_432.o): In function 
`_D3std3net4curl4Curl10initializeMFZv':
std/net/curl.d:(.text._D3std3net4curl4Curl10initializeMFZv+0x3d): 
undefined reference to `curl_easy_init'
/usr/lib64/libphobos2.a(curl_1cd0_149.o): In function 
`_D3std3net4curl4Curl3dupMFZS3std3net4curl4Curl':

std/net/curl.d:(.text._D3std3net4curl4Curl3dupMFZS3std3net4curl4Curl+0x25): 
undefined reference to `curl_easy_duphandle'
/usr/lib64/libphobos2.a(curl_1cd4_37c.o): In function 
`_D3std3net4curl4Curl8shutdownMFZv':
std/net/curl.d:(.text._D3std3net4curl4Curl8shutdownMFZv+0x1a): 
undefined reference to `curl_easy_cleanup'
/usr/lib64/libphobos2.a(curl_1cd6_14c.o): In function 
`_D3std3net4curl4Curl3setMFE3etc1c4curl10CurlOptionAxaZv':

std/net/curl.d:(.text._D3std3net4curl4Curl3setMFE3etc1c4curl10CurlOptionAxaZv+0x5d):
 undefined reference to `curl_easy_setopt'
/usr/lib64/libphobos2.a(curl_1cd7_14c.o): In function 
`_D3std3net4curl4Curl3setMFE3etc1c4curl10CurlOptionlZv':

std/net/curl.d:(.text._D3std3net4curl4Curl3setMFE3etc1c4curl10CurlOptionlZv+0x2e):
 undefined reference to `curl_easy_setopt'
/usr/lib64/libphobos2.a(curl_1cd8_14c.o): In function 
`_D3std3net4curl4Curl3setMFE3etc1c4curl10CurlOptionPvZv':

std/net/curl.d:(.text._D3std3net4curl4Curl3setMFE3etc1c4curl10CurlOptionPvZv+0x2e):
 undefined reference to `curl_easy_setopt'
/usr/lib64/libphobos2.a(curl_1cd9_207.o): In function 
`_D3std3net4curl4Curl5clearMFE3etc1c4curl10CurlOptionZv':

std/net/curl.d:(.text._D3std3net4curl4Curl5clearMFE3etc1c4curl10CurlOptionZv+0x28):
 undefined reference to `curl_easy_setopt'
/usr/lib64/libphobos2.a(curl_1cda_67c.o): In function 
`_D3std3net4curl4Curl16clearIfSupportedMFE3etc1c4curl10CurlOptionZv':

std/net/curl.d:(.text._D3std3net4curl4Curl16clearIfSupportedMFE3etc1c4curl10CurlOptionZv+0x28):
 undefined reference to `curl_easy_setopt'
/usr/lib64/libphobos2.a(curl_1cdb_2fb.o): In function 
`_D3std3net4curl4Curl7performMFE3std8typecons41__T4FlagVAyaa12_7468726f774f6e4572726f72Z4FlagZi':

std/net/curl.d:(.text._D3std3net4curl4Curl7performMFE3std8typecons41__T4FlagVAyaa12_7468726f774f6e4572726f72Z4FlagZi+0x22):
 undefined reference to `curl_easy_perform'
/usr/lib64/libphobos2.a(curl_1cb0_65b.o): In function 
`_D3std3net4curl4HTTP16addRequestHeaderMFAxaAxaZv':

std/net/curl.d:(.text._D3std3net4curl4HTTP16addRequestHeaderMFAxaAxaZv+0x11b): 
undefined reference to `curl_slist_append'
/usr/lib64/libphobos2.a(curl_1cd2_4a1.o): In function 
`_D3std3net4curl4Curl11errorStringMFiZAya':

std/net/curl.d:(.text._D3std3net4curl4Curl11errorStringMFiZAya+0x11): undefined 
reference to `curl_easy_strerror'
collect2: error: ld returned 1 exit status
--- errorlevel 1

DMD 2.068.0, openSUSE 13.2 x64
I'm absolutely sure that I have licurl installed. "zypper info 
libcurl4" shows me that it's version is 7.43.0-2.1


Re: std.net.curl

2015-08-17 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 17 August 2015 at 12:52:37 UTC, tired_eyes wrote:
Hovewer, "dmd app.d" spits a whole bunch of strange error 
messages:


try "dmd -lcurl app.d" and see if that helps.


Re: std.net.curl

2015-08-17 Thread tired_eyes via Digitalmars-d-learn

On Monday, 17 August 2015 at 12:58:54 UTC, Adam D. Ruppe wrote:

On Monday, 17 August 2015 at 12:52:37 UTC, tired_eyes wrote:
Hovewer, "dmd app.d" spits a whole bunch of strange error 
messages:


try "dmd -lcurl app.d" and see if that helps.


Error: unrecognized switch '-lcurl'


Re: std.net.curl

2015-08-17 Thread yawniek via Digitalmars-d-learn

On Monday, 17 August 2015 at 13:04:31 UTC, tired_eyes wrote:

On Monday, 17 August 2015 at 12:58:54 UTC, Adam D. Ruppe wrote:

On Monday, 17 August 2015 at 12:52:37 UTC, tired_eyes wrote:
Hovewer, "dmd app.d" spits a whole bunch of strange error 
messages:


try "dmd -lcurl app.d" and see if that helps.


Error: unrecognized switch '-lcurl'


do you use dub? if so, did you add
"libs": ["curl"]
to your dub.json?

if that doesn't help, please post the output of curl-config.
e.g.
curl-config --libs --built-shared --prefix --static-libs



Re: std.net.curl

2015-08-17 Thread anonymous via Digitalmars-d-learn

On Monday, 17 August 2015 at 12:58:54 UTC, Adam D. Ruppe wrote:

On Monday, 17 August 2015 at 12:52:37 UTC, tired_eyes wrote:
Hovewer, "dmd app.d" spits a whole bunch of strange error 
messages:


try "dmd -lcurl app.d" and see if that helps.


DMD does not accept -lcurl. From dmd --help:
 -Llinkerflag   pass linkerflag to link
ergo
 dmd -L-lcurl app.d

on my machine (64Bit linux mint 17.2 with libcurl4-openssl-dev 
installed) -L-lcurl reduces the number of linker errors but the 
example still fails. I run

dmd -c app.d
dmd -L-lcurl app.o -v

and figured out that the linker is invoked (on my machine) with
 gcc a.o -o a -m64 -lcurl -L/usr/lib/x86_64-linux-gnu -Xlinker 
--export-dynamic -l:libphobos2.a -lpthread -lm -lrt
If I change the order of the libraries (move -lcurl to the end), 
it links. Used commands:

dmd -c app.d
gcc app.o -o app -m64  -L/usr/lib/x86_64-linux-gnu -Xlinker 
--export-dynamic -l:libphobos2.a -lpthread -lm -lrt -lcurl
(the later may be different on OpenSUSE (?) - better check with 
dmd -v ...)


Re: std.net.curl

2015-08-17 Thread anonymous via Digitalmars-d-learn

On Monday, 17 August 2015 at 13:13:48 UTC, anonymous wrote:


and figured out that the linker is invoked (on my machine) with
 gcc a.o -o a -m64 -lcurl -L/usr/lib/x86_64-linux-gnu -Xlinker


correct (I named the example programm a.d instead of app.d):
gcc app.o -o app -m64 -lcurl -L/usr/lib/x86_64-linux-gnu -Xlinker

This issue is already known:
 https://issues.dlang.org/show_bug.cgi?id=12572
and
 https://issues.dlang.org/show_bug.cgi?id=7044


Re: Pointers to Dynamic Arrays

2015-08-17 Thread Brandon Ragland via Digitalmars-d-learn

On Monday, 17 August 2015 at 03:07:26 UTC, Adam D. Ruppe wrote:
On Monday, 17 August 2015 at 02:45:22 UTC, Brandon Ragland 
wrote:

[...]


Short answer: pointers to slices are usually a mistake, you 
probably don't actually want it, but rather should be using a 
regular slice instead.



[...]


Here, for example, you've accidentally escaped slice land and 
are unto unchecked pointer arithmetic.


Since file is declared char[]* instead of char[], indexing it 
works C style: it at the index as an offset from the base 
char[].


In other words, it works more as if you wrote `char** file` in 
C. (still not identically, a D slice is different than a C 
char*, but same idea).


The above will only really work fine for index 0. Anything else 
would be a wild pointer. If this didn't crash on you, you just 
got kinda lucky with the index.


The append compiles because dereferencing a `char[]*` yields a 
`char[]`, which D can append normally.




[...]


This errors for the same reason the top one succeeded: what's 
pointed to by a char[]* is char[], not char. So you are trying 
to compare a string on the left hand side to an individual 
character on the right hand side.


In other words, what your error message told :)


[...]


char[] in D is like:

struct char_array {
size_t length;
char* ptr;
}

in C. Since there's already a pointer in there, you typically 
don't want the address of this struct, you just want to pass it 
right down by value and let the pointer be copied (it still 
points to the same actual data).


BTW you can access those .length and .ptr values in D:

char[] slice;
char* a = slice.ptr; // works

The only time you'd actually want a char[]* in D is if you need 
to write back to the original *variable* once you append or 
shrink it. (The contents are fine, they can be modified through 
the slice with no special effort.)



Bottom line again is char[] in D is like char* in C. So char[]* 
in D is more like char** in C.


Thanks Adam.

I keep getting myself confused with D, when I'm thinking in C 
land. Breaking it down like that is extremely helpful.


Going off what you're saying, this signature:

method(char[] file)

Would only copy the _pointer_, as this is similar to the C style 
struct you mentioned:


struct char_array {
size_t len;
char* index;
}

If that is true, than passing it as _char[] file_ makes the most 
sense to me. A pointer copy doesn't hurt as bad as an array copy, 
of say, 100Kibibytes...


-Brandon


Re: std.net.curl

2015-08-17 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 17 August 2015 at 13:04:31 UTC, tired_eyes wrote:

Error: unrecognized switch '-lcurl'


ooh I'm sorry, should have been `dmd -L-lcurl yourprogram.d`


Re: std.net.curl

2015-08-17 Thread tired_eyes via Digitalmars-d-learn

On Monday, 17 August 2015 at 13:26:29 UTC, Adam D. Ruppe wrote:

On Monday, 17 August 2015 at 13:04:31 UTC, tired_eyes wrote:

Error: unrecognized switch '-lcurl'


ooh I'm sorry, should have been `dmd -L-lcurl yourprogram.d`


Yes, it did the trick.



Re: Pointers to Dynamic Arrays

2015-08-17 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 17 August 2015 at 13:27:19 UTC, Brandon Ragland wrote:
If that is true, than passing it as _char[] file_ makes the 
most sense to me. A pointer copy doesn't hurt as bad as an 
array copy, of say, 100Kibibytes...


Right.

Knowing this helps to explain a lot btw:

char[] foo;

void func(char[] arg) {

}

func(foo);


In that scenario, foo's data is not copied, just a pointer to it 
is. Very fast operation.


arg[0] = 'a';

Since it is a pointer, writing to the arg will also write to foo.

BUT:

arg = "a new thing";

This will NOT be seen outside. It wrote a new pointer to the 
local variable arg's pointer, so now arg and foo point to 
different data.


Similarly:

arg ~= 'a';

will not be seen outside. The pointer might change which is not 
seen outside, and the length will change, which is also not seen.



So char[] arg in D is similar to char* arg_ptr, size_t length_ptr 
in C - changing them inside the function just modifies local 
variables, not the outer variable. Writing *through the pointer 
will be seen outside though since they talk to the same data.





Similarly, slicing in D is just a pointer assignment:

char[] a = b[3 .. 4];

That's like in C:

a.ptr = b.ptr + 3;
a.length = 4-3;


Just basic arithmetic and assignment, no data copy going on.






This is all talking about T[] slices. Static arrays, in the form 
of T[constant], are different:




ubyte[4] static_data;

void slice_test(ubyte[] slice) {}

void static_test(ubyte[4] copy) {}


slice_test(static_data[]); // passes a pointer to the data

static_test(static_data); // passes a copy.




So T[] is always talking about pointer+length, but T[n] is always 
talking about a block of memory which is copied around to other 
T[n].


Putting the [] at the end of it, like I did with slice_test, 
fetches a pointer instead of copying. It is how you convert a 
static memory chunk to a pointer.




There's a few more things but you should understand it now, apply 
the low level principles to it and it all should make sense.


More reading:
http://dlang.org/d-array-article.html
http://dlang.org/arrays.html


Compiletime Vs Runtime bencmarks

2015-08-17 Thread D_Learner via Digitalmars-d-learn
Hello everyone . I need advice on my first D-project . I have 
uploaded it at :-


https://bitbucket.org/mrjohns/matcher/downloads

IDEA : Benchmarking of 3 runtime algorithms and comparing them to 
their compile-time variants. The only difference between these is 
that for the compile time-ones, the lookup tables (i.e. Arrays 
bmBc, bmGs, and suffixes ) must be computed at compile time( I 
currently rely on CTFE ) . While for the runtime-ones the lookup 
tables are computed on runtime.


NB : The pattern matching algorithms themselves need not be 
executed  at compile-time, only the lookup tables.Having stated 
this the algorithms which run on known( compile-time computed) 
tables must be faster than the ones which have to compute them at 
runtime.


My results seem to show something different, only the first   
pair(
BM_Runtime and BM_Compile-time)   yields  admissible results, the 
other two pair give higher execution time for the compile-time 
variants. I think am missing something here. Please help.


Current Results  for the pattern="GCAGAGAG"  are as below :-

BM_Runtime  = 366 hnsecs position=   513
BM_Compile-time = 294 hnsecs position   =513

BMH_Runtime = 174 hnsecs position=   513
BMH_Compile-time= 261 hnsecs position=   513

AG_Run-time = 258 hnsecsposition=   513
AG_Compile-time = 268 hnsecsposition=   513


Running the code : dmd -J.  matcher.d inputs.d  rtime_pre.d 
ctime_pre.d && numactl --physcpubind=0 ./matcher



I would appreciate your suggestions.

Regards,


Re: Compiletime Vs Runtime bencmarks

2015-08-17 Thread Edwin van Leeuwen via Digitalmars-d-learn

On Monday, 17 August 2015 at 14:43:35 UTC, D_Learner wrote:
Hello everyone . I need advice on my first D-project . I have 
uploaded it at :-


Current Results  for the pattern="GCAGAGAG"  are as below :-

BM_Runtime  = 366 hnsecs position=   513
BM_Compile-time = 294 hnsecs position   =513

BMH_Runtime = 174 hnsecs position=   513
BMH_Compile-time= 261 hnsecs position=   513

AG_Run-time = 258 hnsecsposition=   513
AG_Compile-time = 268 hnsecsposition=   513


Running the code : dmd -J.  matcher.d inputs.d  rtime_pre.d 
ctime_pre.d && numactl --physcpubind=0 ./matcher




Hi,

What happens if you run each algorithm many (say 10) times. 
The current times seem to short to be reliable (variation in 
runtimes would be too great).


Regards, Edwin


A couple questions about a simple project

2015-08-17 Thread Andre Polykanine via Digitalmars-d-learn
Hi everyone,
I'm  new to D (I'm learning it by reading the great online book by Ali
Çehreli  -  thank  you  very  much for it, sir!), and, more than that,
programming  is  my hobby, so please bear with me if I'm asking stupid
questions.
I've  made  a  toy  project  which  is  a small command-line XML files
validator:
https://github.com/Oire/dxv/
and I have a couple questions about it:
1.  I'm  using std.getopt but don't know how to make it display a help
message if no options are present at all:
D:\repos\git\dxv\dxv.exe
(nothing   happens   but   I   would  like it to show the help as with
--help switch)
2. As you can see, I check whether the file to validate can be read. I
tried both `try...catch` and `enforce` (current version:
`string  s  =  enforce(cast(string)std.file.read(f),  "Unable  to read
file");`
),  but  this  very  exception  for  some reason can be caught only in
`main()`.
What am I missing?
Thanks!
  

-- 
With best regards from Ukraine,
Andre
Skype: Francophile
Twitter: @m_elensule; Facebook: menelion
My blog: http://menelion.oire.org/




Re: A couple questions about a simple project

2015-08-17 Thread John Colvin via Digitalmars-d-learn

On Monday, 17 August 2015 at 15:05:56 UTC, Andre Polykanine wrote:

Hi everyone,
I'm  new to D (I'm learning it by reading the great online book 
by Ali
Çehreli  -  thank  you  very  much for it, sir!), and, more 
than that,
programming  is  my hobby, so please bear with me if I'm asking 
stupid

questions.
I've  made  a  toy  project  which  is  a small command-line 
XML files

validator:
https://github.com/Oire/dxv/
and I have a couple questions about it:
1.  I'm  using std.getopt but don't know how to make it display 
a help

message if no options are present at all:
D:\repos\git\dxv\dxv.exe
(nothing   happens   but   I   would  like it to show the help 
as with

--help switch)


Check if args has changed length after calling getopt, if not 
then it didn't find any options.


2. As you can see, I check whether the file to validate can be 
read. I

tried both `try...catch` and `enforce` (current version:
`string  s  =  enforce(cast(string)std.file.read(f),  "Unable  
to read

file");`
),  but  this  very  exception  for  some reason can be caught 
only in

`main()`.
What am I missing?
Thanks!


string s;
try
{
s = cast(string)std.file.read(f);
}
catch (FileException e)
{
// do whatever you want.
}



Re: Compiletime Vs Runtime bencmarks

2015-08-17 Thread D_Learner via Digitalmars-d-learn
On Monday, 17 August 2015 at 14:52:18 UTC, Edwin van Leeuwen 
wrote:

On Monday, 17 August 2015 at 14:43:35 UTC, D_Learner wrote:
Hello everyone . I need advice on my first D-project . I have 
uploaded it at :-


Current Results  for the pattern="GCAGAGAG"  are as below :-

BM_Runtime  = 366 hnsecs position=   513
BM_Compile-time = 294 hnsecs position   =513

BMH_Runtime = 174 hnsecs position=   513
BMH_Compile-time= 261 hnsecs position=   513

AG_Run-time = 258 hnsecsposition=   513
AG_Compile-time = 268 hnsecsposition=   513


Running the code : dmd -J.  matcher.d inputs.d  rtime_pre.d 
ctime_pre.d && numactl --physcpubind=0 ./matcher




Hi,

What happens if you run each algorithm many (say 10) times. 
The current times seem to short to be reliable (variation in 
runtimes would be too great).


Regards, Edwin


Thyanks, I havent tried that.  So it seems you suggest I must 
fucus on average times. Will try it , the above results are from 
a single run (no looping), but after executing the program 
several times I realised that the 1st two seem  ok(relatively 
stable results for same pattern), but the other pair give higher 
performance for the run-time versions.


Re: A couple questions about a simple project

2015-08-17 Thread FreeSlave via Digitalmars-d-learn

On Monday, 17 August 2015 at 15:05:56 UTC, Andre Polykanine wrote:

Hi everyone,
I'm  new to D (I'm learning it by reading the great online book 
by Ali
Çehreli  -  thank  you  very  much for it, sir!), and, more 
than that,
programming  is  my hobby, so please bear with me if I'm asking 
stupid

questions.
I've  made  a  toy  project  which  is  a small command-line 
XML files

validator:
https://github.com/Oire/dxv/
and I have a couple questions about it:
1.  I'm  using std.getopt but don't know how to make it display 
a help

message if no options are present at all:
D:\repos\git\dxv\dxv.exe
(nothing   happens   but   I   would  like it to show the help 
as with

--help switch)
2. As you can see, I check whether the file to validate can be 
read. I

tried both `try...catch` and `enforce` (current version:
`string  s  =  enforce(cast(string)std.file.read(f),  "Unable  
to read

file");`
),  but  this  very  exception  for  some reason can be caught 
only in

`main()`.
What am I missing?
Thanks!


1. getopt modifies args array leaving not processed arguments in 
it, i.e. name of the program and positional arguments (those 
without leading - or --). If there're no command line arguments 
given, args array will contain the only one element - the name of 
executable. Therefore you can check for args.length == 1 after 
processing by getopt.


2. catch can handle only exception of type specified in its 
argument and derived. std.file.read throws FileException on fail, 
while you catch only CheckException. To cover all cases you can 
catch any exception by using Exception type (it's the base class 
for all exception classes), or write two catch-statements in a 
row for both FileException and CheckException.


You don't need enforce here, unless you want to check if 
std.file.read returns null slice.


Re: How to provide this arg or functor for algorithm?

2015-08-17 Thread thedeemon via Digitalmars-d-learn

On Monday, 17 August 2015 at 12:38:05 UTC, anonymous wrote:

auto func()(uint[] arr, uint delegate(uint) pure @nogc d) @nogc
{
return arr.map!(d);
}

void main() @nogc
{
uint[3] arr = [1,2,3];
uint context = 2;
auto c = Caller(context);
auto d = &c.method;

auto r = func(arr[], d);

import std.algorithm: equal;
import std.range: only;
assert(equal(r, only(2, 4, 6)));
}


I've just checked with my runtime GC hook. Here the call to 
func() allocates 12 bytes via gc_malloc, and it's the same for a 
4-elements array, so it's not for the array itself, it's for a 
closure, I think.


Re: How to provide this arg or functor for algorithm?

2015-08-17 Thread thedeemon via Digitalmars-d-learn

On Monday, 17 August 2015 at 16:18:50 UTC, thedeemon wrote:
I've just checked with my runtime GC hook. Here the call to 
func() allocates 12 bytes via gc_malloc, and it's the same for 
a 4-elements array, so it's not for the array itself, it's for 
a closure, I think.


Also, compiling with -vgc says nothing, compiler (2.068) seems to 
miss this allocation.




Re: Weird error message.

2015-08-17 Thread Kagamin via Digitalmars-d-learn

On Sunday, 16 August 2015 at 21:32:10 UTC, Warwick wrote:
Dont know what to make of this, I pretty much get it every 
other time I call rdmd. It'll alternate between running fine 
and then giving me this error...

Any ideas?


rdmd creates an executable and runs it immediately. If you have 
an antivirus, it may not complete its job in this rather short 
timeframe. Just run it again, AFAIK, rdmd caches the executable 
from the last compilation.


On Monday, 17 August 2015 at 00:00:11 UTC, Jonathan M Davis wrote:

Personally, I wish that Windows didn't have file locks... :(


My guess, it's the executable file, which is opened by another 
process, if it's not completely written, running it would give 
even more funny results :)


Re: Weird error message.

2015-08-17 Thread Kagamin via Digitalmars-d-learn

On Monday, 17 August 2015 at 03:14:24 UTC, BBasile wrote:
It's locked unless it's specified during the call to 
`CreateFile()` that the file can be shared for reading/writing 
(FILE_SHARE_READ / FILE_SHARE_WRITE).


And the executable file being run must not be shared for writing, 
because it's mapped.


Re: A couple questions about a simple project

2015-08-17 Thread Andre Polykanine via Digitalmars-d-learn
Hello John,

Yes, but this doesn't work, either.
Now I have this (that was my first variant, btw):

string s;
try {
s = cast(string)std.file.read(f);
try {
check(s);
if (this.verbose) {
output(format("%s: 
validation passed!", f));
}
} catch(CheckException e) {
// This is output when a file is invalid, that works
output(format("Failed to 
validate %s: %s", f, e.toString()));
}
} catch(Exception exc) {
//  And  that  isn't  output,  I  see  just  the  stacktrace as if the
exception was not caught at all
output(format("Error reading %s: %s", 
f, exc.msg));
}
}

To  catch  the  exception,  I  need  to  wrap  the  validate() call in
`try...catch` in `main()`. Why so?
Thanks!
-- 
With best regards from Ukraine,
Andre
Skype: Francophile
Twitter: @m_elensule; Facebook: menelion
My blog: http://menelion.oire.org/


 Original message 
From: John Colvin via Digitalmars-d-learn 
To: digitalmars-d-learn@puremagic.com
Date created: , 6:17:38 PM
Subject: A couple questions about a simple project


  On Monday, 17 August 2015 at 15:05:56 UTC, Andre Polykanine wrote:
> Hi everyone,
> I'm  new to D (I'm learning it by reading the great online book 
> by Ali
> Çehreli  -  thank  you  very  much for it, sir!), and, more 
> than that,
> programming  is  my hobby, so please bear with me if I'm asking 
> stupid
> questions.
> I've  made  a  toy  project  which  is  a small command-line 
> XML files
> validator:
> https://github.com/Oire/dxv/
> and I have a couple questions about it:
> 1.  I'm  using std.getopt but don't know how to make it display 
> a help
> message if no options are present at all:
> D:\repos\git\dxv\dxv.exe
> (nothing   happens   but   I   would  like it to show the help 
> as with
> --help switch)

Check if args has changed length after calling getopt, if not 
then it didn't find any options.

> 2. As you can see, I check whether the file to validate can be 
> read. I
> tried both `try...catch` and `enforce` (current version:
> `string  s  =  enforce(cast(string)std.file.read(f),  "Unable  
> to read
> file");`
> ),  but  this  very  exception  for  some reason can be caught 
> only in
> `main()`.
> What am I missing?
> Thanks!

string s;
try
{
 s = cast(string)std.file.read(f);
}
catch (FileException e)
{
 // do whatever you want.
}




Re: A couple questions about a simple project

2015-08-17 Thread Ali Çehreli via Digitalmars-d-learn

On 08/17/2015 10:08 AM, Andre Polykanine via Digitalmars-d-learn wrote:

>  string s;
>  try {
>  s = 
cast(string)std.file.read(f);

>  try {
>  check(s);
>  if (this.verbose) {
> 
output(format("%s: validation passed!", f));

>  }
>  } catch(CheckException e) {
> // This is output when a file is invalid, that works
> 
output(format("Failed to validate %s: %s", f, e.toString()));

>  }
>  } catch(Exception exc) {
> //  And  that  isn't  output,  I  see  just  the  stacktrace as if the
> exception was not caught at all
>  output(format("Error reading 
%s: %s", f, exc.msg));

>  }
>  }

Regardless of whether it is the best solution here, you can have 
multiple catch blocks for a single try:


try {
// ...

} catch(CheckException e) {
// ...

} catch(Exception exc) {
// ...
}

> To  catch  the  exception,  I  need  to  wrap  the  validate() call in
> `try...catch` in `main()`. Why so?

The reason is you don't try to make use of the provided file name until 
validate() starts executing. If it's important that the program notify 
the user right away, then you can call std.file.exists() to make sure 
that the file is there. Still, it does not try to open so it can't know 
whether the file will be readable later.


The funny thing is, even though the upfront check sees a file as being 
present, it may disappear when it is being used or it may become unreadable.


A better solution may be to open the files first and then pass File 
objects to validate().


> Thanks!

And thank you very much for the kind words! :)

Ali



Re: Compiletime Vs Runtime bencmarks

2015-08-17 Thread D_Learner via Digitalmars-d-learn
On Monday, 17 August 2015 at 14:52:18 UTC, Edwin van Leeuwen 
wrote:

On Monday, 17 August 2015 at 14:43:35 UTC, D_Learner wrote:
Hello everyone . I need advice on my first D-project . I have 
uploaded it at :-


Current Results  for the pattern="GCAGAGAG"  are as below :-

BM_Runtime  = 366 hnsecs position=   513
BM_Compile-time = 294 hnsecs position   =513

BMH_Runtime = 174 hnsecs position=   513
BMH_Compile-time= 261 hnsecs position=   513

AG_Run-time = 258 hnsecsposition=   513
AG_Compile-time = 268 hnsecsposition=   513


Running the code : dmd -J.  matcher.d inputs.d  rtime_pre.d 
ctime_pre.d && numactl --physcpubind=0 ./matcher




Hi,

What happens if you run each algorithm many (say 10) times. 
The current times seem to short to be reliable (variation in 
runtimes would be too great).


Regards, Edwin


The surprisingly, the D-profiler gives plausible results:-
Algorithm1
2921   int rtime_pre.bm_rmatch (runtime )
2122   int ctime_pre.bm_cmatch  (compiletime )

1317int rtime_pre.bmh_rmatch  (runtime )
1099int ctime_pre.bmh_cmatch   (compiletime )


3959 int rtime_pre.ag_rmatch (runtime )
2688 pure int ctime_pre.ag_cmatch   (compiletime )

This suggests that my timer design has some flaw ;)



Re: How to provide this arg or functor for algorithm?

2015-08-17 Thread anonymous via Digitalmars-d-learn

On Monday, 17 August 2015 at 16:21:16 UTC, thedeemon wrote:

On Monday, 17 August 2015 at 16:18:50 UTC, thedeemon wrote:
I've just checked with my runtime GC hook. Here the call to 
func() allocates 12 bytes via gc_malloc, and it's the same for 
a 4-elements array, so it's not for the array itself, it's for 
a closure, I think.


Also, compiling with -vgc says nothing, compiler (2.068) seems 
to miss this allocation.


Thanks for confirming.

It seems to be a known issue that the compiler doesn't recognize 
the @nogc violation by the closure:

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

That's not a regression, though. 2.067 rejecting the code has 
something to do with this assert:

https://github.com/D-Programming-Language/phobos/blob/v2.067.1/std/algorithm/iteration.d#L453
 (code is the same for 2.068).

I don't know if that assert should trigger a @nogc violation or 
not. But anyway, the real issue is 14771, as far as I can tell.


Getting a TypeTuple of a Template's Arguments

2015-08-17 Thread Meta via Digitalmars-d-learn
For functions, we have std.traits.ParameterTypeTuple. Is there 
any equivalent functionality for templates?


Re: Compiletime Vs Runtime bencmarks

2015-08-17 Thread John Colvin via Digitalmars-d-learn

On Monday, 17 August 2015 at 17:48:22 UTC, D_Learner wrote:
On Monday, 17 August 2015 at 14:52:18 UTC, Edwin van Leeuwen 
wrote:

[...]


The surprisingly, the D-profiler gives plausible results:-
Algorithm1
2921   int rtime_pre.bm_rmatch (runtime )
2122   int ctime_pre.bm_cmatch  (compiletime )

1317int rtime_pre.bmh_rmatch  (runtime )
1099int ctime_pre.bmh_cmatch   (compiletime )


3959 int rtime_pre.ag_rmatch (runtime )
2688 pure int ctime_pre.ag_cmatch   (compiletime )

This suggests that my timer design has some flaw ;)


std.datetime.StopWatch is the easiest way to do timing manually, 
or just use std.datetime.benchmark


Re: Getting a TypeTuple of a Template's Arguments

2015-08-17 Thread Ali Çehreli via Digitalmars-d-learn
Aside: With 2.068, std.typetuple and TypeTuple are renamed as std.meta 
and AliasSeq, respectively.


On 08/17/2015 02:23 PM, Meta wrote:> For functions, we have 
std.traits.ParameterTypeTuple. Is there any

> equivalent functionality for templates?

There is TemplateArgsOf for instances of templates but I don't know 
anything more meta :) than that:


import std.traits;

void foo(int i, alias f, T)()
{}

void main()
{
int bar(int) { return 0; }

alias f = foo!(42, bar, int);
pragma(msg, TemplateArgsOf!(f));
}

Prints

tuple(42, bar, (int))

Ali



unusual bare metal target: Amazon Dash

2015-08-17 Thread Laeeth Isharc via Digitalmars-d-learn
I don't know whether D can run on one, but from a quick look 
perhaps feasible.  Running D on something like this (perhaps it's 
underpowered, but looked to have similar spec to what people had 
been doing with related ARM cortex processors) would certainly 
make the point very vivid that it can be a bare metal programming 
language.


Only 1Mb of flash RAM for the program - is that enough?

https://learn.adafruit.com/dash-hacking-bare-metal-stm32-programming/programming
https://learn.adafruit.com/dash-hacking-bare-metal-stm32-programming/overview

The Amazon Dash button is a tiny device that orders products from 
Amazon.com at the press of a button.  It's designed to be put 
wherever you store consumeables like paper towels, trash bags, 
etc. so that you can easily order more when they run out.  The 
Dash is great at what it's designed to do, but did you know 
inside the Dash is a powerful ARM Cortex-M3 processor and WiFi 
module that are very similar to wireless development boards like 
the Particle Photon?  You'll even find there are easily 
accessible test pads on the Dash which allow you to reprogram its 
CPU and turn it into your own $5 internet button!  This guide 
will explore how to take apart the Dash and reprogram its CPU to 
run your own code.

...
The CPU is a STM32F205RG6 processor which is an ARM Cortex-M3 
that can run up to 120mhz and has 128 kilobytes of RAM and 1 
megabyte of flash memory for program storage.
The WiFi module is a BCM943362 module which in combination with 
the CPU make it a platform for Broadcom's WICED SDK.
There's a 16 megabit SPI flash ROM which is typically used in 
conjunction with the WICED SDK for storing application data.
An ADMP441 microphone is connected to the CPU and used by the 
Dash iOS application to configure the device using the speaker on 
a phone/tablet.

There's a single RGB LED and a button.



Re: unusual bare metal target: Amazon Dash

2015-08-17 Thread Rikki Cattermole via Digitalmars-d-learn

On 18/08/2015 1:32 p.m., Laeeth Isharc wrote:

I don't know whether D can run on one, but from a quick look perhaps
feasible.  Running D on something like this (perhaps it's underpowered,
but looked to have similar spec to what people had been doing with
related ARM cortex processors) would certainly make the point very vivid
that it can be a bare metal programming language.

Only 1Mb of flash RAM for the program - is that enough?

https://learn.adafruit.com/dash-hacking-bare-metal-stm32-programming/programming

https://learn.adafruit.com/dash-hacking-bare-metal-stm32-programming/overview


The Amazon Dash button is a tiny device that orders products from
Amazon.com at the press of a button.  It's designed to be put wherever
you store consumeables like paper towels, trash bags, etc. so that you
can easily order more when they run out.  The Dash is great at what it's
designed to do, but did you know inside the Dash is a powerful ARM
Cortex-M3 processor and WiFi module that are very similar to wireless
development boards like the Particle Photon?  You'll even find there are
easily accessible test pads on the Dash which allow you to reprogram its
CPU and turn it into your own $5 internet button!  This guide will
explore how to take apart the Dash and reprogram its CPU to run your own
code.
...
The CPU is a STM32F205RG6 processor which is an ARM Cortex-M3 that can
run up to 120mhz and has 128 kilobytes of RAM and 1 megabyte of flash
memory for program storage.
The WiFi module is a BCM943362 module which in combination with the CPU
make it a platform for Broadcom's WICED SDK.
There's a 16 megabit SPI flash ROM which is typically used in
conjunction with the WICED SDK for storing application data.
An ADMP441 microphone is connected to the CPU and used by the Dash iOS
application to configure the device using the speaker on a phone/tablet.
There's a single RGB LED and a button.


By what you are saying, I believe it should be doable.
Although I'm a little worried for the WiFi support. Do you need to 
include the code to drive it beyond wrap up some communication to it?


1mb flash should be enough to run D code on it. If you strip out a good 
percentage of druntime and definitely no Phobos.
Although you may be able to mark and use some of that 16mb flash rom as 
executable code storage. If that's so, you'll be in a good place to have 
more then 1mb. It would require some clever runtime linking tricks however.


I'm probably not the best person to go more in depth about it or the 
specific chips. So I won't. Most of my knowledge comes from reading what 
others says and talking with Jens Bauer.




Re: Real OOP with D

2015-08-17 Thread Ozan via Digitalmars-d-learn

On Monday, 17 August 2015 at 06:08:35 UTC, Ali Çehreli wrote:

On 08/16/2015 10:57 PM, Ozan wrote:


[...]


From the way you use it below, a better name for that class 
would be FamilyMember but I get the point.

Yes,  you're right.



[...]

> father.greeting;
> son.greeting;

What you mean is that the call above causes a compilation error:

Error: no property 'greeting' for type 'deneme.Family'

> }
>

[...]


My experience with OOP is limited to C++ and D. I think what 
you are describing is how dynamically typed languages work. It 
is impossible in compiled languages like C++ and D. When a type 
is used in an expression, the compiler ensures that the call is 
bound to a function.


> In D, it starts with the class definition.

Actually, 'interface' is a better fit in most cases:

interface FamilyMember
{
// ...
}

class Dad : FamilyMember
{
// ...
}


[...]


Ali


Interfaces are very helpful to avoid large inheritance trees in 
OOP. On the other hand,  there are not designed as workarounds 
for OOP implementation problems.
The kind of OOP in D is a classical way to handle it. Dynamic 
languages tries to close the gap between theoretical and 
practical OOP for the price of speed.
I think that Adam's jsvar idea is a great way to bring more 
flexibility into D. The same in OOP would be also great.


Regards,  Ozan



Re: Real OOP with D

2015-08-17 Thread Ozan via Digitalmars-d-learn

On Monday, 17 August 2015 at 06:10:38 UTC, Rikki Cattermole wrote:

On 17/08/2015 5:57 p.m., Ozan wrote:

Hi


[...]


import std.stdio;
abstract class Family { void greeting(); }
class Dad : Family { void greeting() { writeln("I'm dad"); } }
class Boy : Family { void greeting() { writeln("I'm daddy's 
boy"); } }


void main() {
Family dad = new Dad;
Family boy = new Boy;
dad.greeting;
boy.greeting;
}

I'm confused how this isn't real OOP?


Replace 'real' with 'theoretical' OOP. Every instance of a class 
(object) is like an independent black box. Every public message 
(method / function) of the class definition could be called when 
ever you want. Variables are like pointers to objects. With 
inheritance and overriding you're losing in class type 
implementations of OOP a lot of 'theoretical'  flexibility. I was 
asking for a possibility to avoid this.


Regards Ozan


Re: Real OOP with D

2015-08-17 Thread Ozan via Digitalmars-d-learn

On Monday, 17 August 2015 at 06:59:51 UTC, BBasile wrote:

On Monday, 17 August 2015 at 05:57:52 UTC, Ozan wrote:

Hi

[...]


Is there any way to get real OOP with D?

Regards,  Ozan


Can you name an OOP oriented language that allows this ? Your 
example is eroneous OOP.
The 2 other answers you 've got (the first using an interface 
and the second using an abstract class) are valid OOP.


One of the fundamental concept OOP is that a function defined 
in a class exists also  in its subclasses. So how do you expect 
`greeting()` to exist in Family if it's only defined in its 
sub-classes ?


You can verify that with the 'Liskov substitution principle' 
(https://en.wikipedia.org/wiki/Liskov_substitution_principle).

Actually your sample violates this principle.


Languages like Groovy or JavaScript (with the help of frameworks 
;-)

And I believe many more the newer ones.  But that's not the point.

And... This was not a criticism against D (... "bad D, has no 
understanding of OOP. Boahh"  ;-)
It was only a question about handling of a typical OOP problem in 
a class-typed implementation of OOP like D has. Thanks to every 
existing or new creative programming language, today we have so 
many other ways to solve our programming problems.


Regards Ozan