How to get the type of a derived class in a method of its base class?

2017-02-18 Thread Max Samukha via Digitalmars-d-learn

class A {
this(T = this)() {
static assert(is(T == B));
}
}

class B {
}

auto b = new B;

Here, T becomes A, which may be reasonable but is completely 
useless. Is there a way to obtain the type of the class (or class 
instance reference) the method is called on?


Re: Converting multiple inheritance code into C ++ for D language

2017-02-18 Thread Jean Cesar via Digitalmars-d-learn

On Saturday, 18 February 2017 at 19:45:45 UTC, biozic wrote:

On Saturday, 18 February 2017 at 19:05:14 UTC, Jean Cesar wrote:
This is exactly what I want this code I did to understand how 
would apply multiple inheritance in D, C # also process using 
interfaces but the difference from C # to D is that C # 
already in the base class you have to define it as interface. 
..


OK, but I guess you are aware that in this code, using 
interfaces and the pseudo-multiple-inheritance is pointless! 
You could just ditch them and use regular methods :)


Yes this code is useless because it will serve as a hello world 
using multiple inheritance or else I study the language in what 
it supports and does not support to have examples where I can 
base myself and have an idea of how to do just to even understand 
how to implement something like this .


Re: Hello, folks! Newbie to D, have some questions!

2017-02-18 Thread Seb via Digitalmars-d-learn

On Saturday, 18 February 2017 at 21:09:20 UTC, ag0aep6g wrote:
5. Supposing I devote the time and energy and get up to speed 
on D, would the core language team be welcoming if I feel like 
I can contribute?


Absolutely. Anyone is welcome to contribute. D is very much a 
volunteer effort. Also don't hesitate to point out (or even 
fix) any stumbling blocks you may encounter when starting out.


I can't add more to this than two pointers:

https://wiki.dlang.org/Starting_as_a_Contributor
https://wiki.dlang.org/Get_involved


Re: scope with if

2017-02-18 Thread Moritz Maxeiner via Digitalmars-d-learn

On Friday, 17 February 2017 at 20:06:19 UTC, berni wrote:

I wonder if it's possible to do something like this:


import std.stdio;

void main(string[] args)
{
   if (args[1]=="a")
   {
   write("A");
   scope (exit) write("B");
   }

   write("C");
}


I expected the output to be ACB not ABC.


Scope guards are documented here[1][2] and that example behaves 
according to the spec. You can reach what I understood to be your 
objective by implementing the desired functionality on top of a 
scope guard, though:


---
import std.stdio;

void main(string[] args)
{
void delegate()[] finalizers;
scope (exit) foreach (onExit; finalizers) onExit();

if (args.length >= 2 && args[1] == "a")
{
writeln("A");
finalizers ~= { writeln("B"); };
}

writeln("C");
}
---

Keep the following in mind, though[2]:
A scope(exit) or scope(success) statement may not exit with a 
throw, goto, break, continue, or return; nor may it be entered 
with a goto.

i.e. those finalizers must not throw.

On Friday, 17 February 2017 at 20:06:19 UTC, berni wrote:
I understand, that the scope ends at the end of the if, but I 
wonder, if it's possible to have a "conditional scope" or 
something like this.


As shown in the example above, if you want functionality that is 
not provided by the default scope guard behaviour you'll have to 
implement it on top of them.


[1] https://tour.dlang.org/tour/en/gems/scope-guards
[2] https://dlang.org/spec/statement.html#ScopeGuardStatement



Re: Hello, folks! Newbie to D, have some questions!

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

I'm new here too (never heard of D before 2017).


c). The whole community seems infused with both the Feminism/SJW


I didn't tried out Rust, but that would draw me away too. 
(Incidentally it was a comment on alternatives for Rust, that 
pointed me to D.)


2. I am also curious as to what would be the best path for a 
complete beginner to D to learn it effectively?


I started with the online version of the book of Ali Çehreli but 
after a while I decided to buy it and was impressed on its size 
(more than 700 pages!). Meanwhile I'm halfway through.


At the same time I'm working on a project of mine, which I just 
started writing in C++ last december, because I couldn't find a 
better language and thought I had to bite the bullet. Meanwhile 
it's completely rewritten in D (but two lines of C code that I 
need to use a C-libraray). Whenever I came across a new concept 
in the book I tried to refactor that project using this concept.


This approach worked very well for me. (And I appreciate this 
Learn-forum, because else I'd not dare to ask my seemingly silly 
questions.)


You wrote:

... area thoroughly!The introspection ...


I just realised, how much I'm thinking in D allready when I saw 
this: At first glance I wondered, what this thoroughly-template 
is about... ;-)




Re: Hello, folks! Newbie to D, have some questions!

2017-02-18 Thread Moritz Maxeiner via Digitalmars-d-learn

On Saturday, 18 February 2017 at 20:15:55 UTC, timmyjose wrote:
My rudimentary knowledge of the D ecosystem tells me that there 
is a GC in D, but that can be turned off. Is this correct?


Technically yes; you will lose core functionality, though, if you 
do.
I don't have the complete list at hand, but e.g. dynamic and 
associative arrays are one of the things you won't be able to use 
without the GC IIRC. If you use the reference compiler (dmd), you 
can use the flag `-vgc` to be shown all the GC allocations in a D 
program.


On Saturday, 18 February 2017 at 20:15:55 UTC, timmyjose wrote:
Also, some threads online mention that if we do turn off GC, 
some of the core std libraries may not fully work. Is this 
presumption also correct?


Yes. Everything in Phobos that uses features depending on the GC 
won't work anymore.


On Saturday, 18 February 2017 at 20:15:55 UTC, timmyjose wrote:
In this regard, I am curious to know if I would face any issues 
(with my intent in mind), or will I do just fine?


If you don't turn the GC off you should be fine. The GC will - 
AFAIK - only perform a collection cycle as a result of an 
allocation call to it, so you can avoid slow collection cycles 
without turning it off.


On Saturday, 18 February 2017 at 20:15:55 UTC, timmyjose wrote:
If you could share your experiences and domains of use, that 
would also be very helpful for me.


I mostly use D for writing tools for my own use that have to 
interact with C APIs.


On Saturday, 18 February 2017 at 20:15:55 UTC, timmyjose wrote:
Secondly, how stable is the language and how fast is the pace 
of development on D?


The parts of the language I need are pretty stable, but I don't 
think I use even half of what the language offers (D is very 
complex).
Regarding speed, you can see the numbers (git tags) for yourself 
here[0].


On Saturday, 18 February 2017 at 20:15:55 UTC, timmyjose wrote:
2. I am also curious as to what would be the best path for a 
complete beginner to D to learn it effectively?


That's usually not something someone can tell you, since every 
person learns differently.
Personally, when I started with D (back in D1 days) I read the 
articles about it and then just tried writing tools in it, so I 
suggest reading these[1]


On Saturday, 18 February 2017 at 20:15:55 UTC, timmyjose wrote:
I am a relatively fast learner (and I learn better by context, 
as in, some core unifying idea described and then elucidated 
through big examples instead of learning in bits and pieces).


I'd describe D's unifying idea as "allow people to write complex, 
native software without all the C/C++ insanity". Though D comes 
with it's own share of insanity, of course.


On Saturday, 18 February 2017 at 20:15:55 UTC, timmyjose wrote:
Are there any books/video tutorials that you would recommend 
(aside from this site itself).


I personally would not recommend books at the very start of 
learning a language (if one is already proficient with native 
programming in general), but only after one has already gotten 
comfortable with it and is looking for a comprehensive overview.

Regardless, I've heard good things about two books[2][3].
Since I loathe video tutorials I can't add anything on that point.

On Saturday, 18 February 2017 at 20:15:55 UTC, timmyjose wrote:
3. Are there some small-scale Open Source projects that you 
would recommend to peruse to get a feel for and learn idiomatic 
D?


Technically there's no such thing as idiomatic D as D is 
multi-paradigm. You can see some sensible idioms here[4], but no, 
I would not recommend reading anyone's D code just to get a 
feeling for "idiomatic D".


On Saturday, 18 February 2017 at 20:15:55 UTC, timmyjose wrote:
4. I have heard good reports of D's metaprogramming 
capabilities (ironically enough, primarily from a thread on the 
Rust user group),


This doesn't surprise me, honestly, since Rust's (compile time) 
metaprogramming capabilities are below D's and from my experience 
people in both communities are well aware of that. There are 
threads on Reddit about this topic if you have the time to dig 
them up. D's advanced compile time features are one of the main 
reasons I'm unlikely to switch to anything else for my tools (in 
my experience there is no other native programming language that 
let's me get things done as fast - in terms of development time - 
as D).


On Saturday, 18 February 2017 at 20:15:55 UTC, timmyjose wrote:
and coming from a Common Lisp (and some  Racket) background, I 
am deeply interested in this aspect. Are  D macros as powerful 
as Lisp macros? Are they semantically similar (for instance, I 
found Rust's macros are quite similar to Racket's)?


D does not have macros, it has compile time function 
execution[5], templates[6],

 mixins[7], and template mixins[8].

On Saturday, 18 February 2017 at 20:15:55 UTC, timmyjose wrote:
5. Supposing I devote the time and energy and get up to speed 
on D, would the core language team be welcoming if I feel li

Re: Hello, folks! Newbie to D, have some questions!

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

timmyjose wrote:
Thanks for the very comprehensive response! I think most of my doubts 
are cleared now. You're right though that I'm probably worrying too 
much about GC with my current use case.


i can tell you that i'm doing things like, for example, ZX Spectrum 
emulator and hobbyst videogames (everything in D, even low-level gfx), 
i never really cared about "avoiding GC", and i can maintain solid 
35/50/60 FPS (depending of my needs) with my code.


i.e. that "GC-phobia" (i'm not talking about you specifiallly, of 
course, sorry) is mostly based on nothing. as GC will never fire "on 
it's own", and you can control it, avoiding GC is not unnecessary 
(except some very special cases, of course ;-). the key tech here (as 
usual) is to not allocate in tight loops, plan your memory discipline 
and such. nothing new for people used to systems languages. ;-)


sure, you can stop worrying about that and use D as some kind of 
scripting language too, and still have all the features like type 
checking. for things like IRC or email client i absolutely don't care 
about allocations (i.e. doing it left and right) and just letting GC to 
do it's work. my usual IRC client uptime is several monthes (after that 
it runs out of memory, but this is 'cause it never does any cleanup on 
it's data, keeping all logs and db in memory. i am too lazy to finish 
it. note that is it not a GC fault, it is my code. ;-).


Re: Hello, folks! Newbie to D, have some questions!

2017-02-18 Thread Daniel Kozak via Digitalmars-d-learn

Dne 18.2.2017 v 21:15 timmyjose via Digitalmars-d-learn napsal(a):


Hello folks,

I am interested in learning D (just starting out, did a few of the 
exercises on the D tour), and had some questions before I decide to 
jump right in. My questions are genuinely motivated by my experiences 
and expectations, so please forgive me if some questions don't come 
across as well as my intentions!


1. I have some experience with both C and C++, and have been learning 
Rust for a while, but a few things put me off about the whole business -


a). The core language appears to be simple enough, but becomes 
increasingly complex as I begin writing larger programs.


b). The whole ownership system is easy to understand, but the APIs 
become very complicated and unwieldy, and more time appears to be 
spent on understanding and ensuring that memory is being used 
correctly than on the core program logic.


c). The whole community seems infused with both the Feminism/SJW (I 
don't care about those communities, but it feels weird having a 
programming community get sidetracked by all that bullshit), and too 
much of Ruby-on-Rails culture (probably started with Steve Klabnik) so 
that it doesn't feel like any real systems programmers are focusing on 
that language, and finally, d). The whole language feels like a bit of 
patchwork of random ideas, and also the whole "safety" and "no 
segfaults" guarantees seem to have lesser and lesser RoI as time goes by.


Sorry for the rant, I didn't realise I was quite that frustrated! 
That's just to give some background about me and my recent 
experiences! :D


In that regard, I suppose I'll get a better feel of the community here 
as I interact more, but I have high hopes that it'll be much more 
technical than purely social!

Hi, welcome in D community


2. I am more interested in learning D as a pure systems programming 
language so that I can develop my own tools (not looking to develop an 
OS, just some grep-scale tools to start off with). In that regard, I 
have a few concerns about the GC. My rudimentary knowledge of the D 
ecosystem tells me that there is a GC in D, but that can be turned 
off. Is this correct? Also, some threads online mention that if we do 
turn off GC, some of the core std libraries may not fully work. Is 
this presumption also correct? In this regard, I am curious to know if 
I would face any issues (with my intent in mind), or will I do just 
fine? If you could share your experiences and domains of use, that 
would also be very helpful for me




Yes, by default D use GC. And yes there is a some part of D standard 
library which uses GC. But it is something you can avoid if you want. I 
am using D for many years and for almost anything and never have issue 
with GC.


Secondly, how stable is the language and how fast is the pace of 
development on D?


Again, sorry for my ignorance if I have been wrong-footed on some (or 
all) points.


D stability is good, really good, for many of us too good :P. I have 
been using D for many years (five or six). And right now there is a big 
effort to never break anything until it makes really sense.


OTOH D development is quite fast. So there are many improvements with 
every release





2. I am also curious as to what would be the best path for a complete 
beginner to D to learn it effectively? I am a relatively fast learner 
(and I learn better by context, as in, some core unifying idea 
described and then elucidated through big examples instead of learning 
in bits and pieces). How did you folks learn D? I'm sure hearing your 
experiences would be helpful too. Are there any books/video tutorials 
that you would recommend (aside from this site itself).
I can't help here because I am using D for a long time, so I do not 
remember how I have learned it.


3. Are there some small-scale Open Source projects that you would 
recommend to peruse to get a feel for and learn idiomatic D?


It is maybe not small-scale but idiomatic D code is in phobos itself.



4. I have heard good reports of D's metaprogramming capabilities 
(ironically enough, primarily from a thread on the Rust user group), 
and coming from a Common Lisp (and some Racket) background, I am 
deeply interested in this aspect. Are D macros as powerful as Lisp 
macros? Are they semantically similar (for instance, I found Rust's 
macros are quite similar to Racket's)?
I do not know Lisp macros, but AFAIK there are not semantically similar. 
OTOH D metaprogramming is really powerful and there has been some 
proposals to improve that https://wiki.dlang.org/DIP50


5. Supposing I devote the time and energy and get up to speed on D, 
would the core language team be welcoming if I feel like I can 
contribute?


That's all off the top of my head at the moment. Perhaps I'll have 
more questions as I read the responses. Thanks in advance!


Cheers.






Re: Hello, folks! Newbie to D, have some questions!

2017-02-18 Thread timmyjose via Digitalmars-d-learn

On Saturday, 18 February 2017 at 21:27:55 UTC, sarn wrote:

On Saturday, 18 February 2017 at 20:15:55 UTC, timmyjose wrote:

[...]


Hi :)


[...]


Okay, yes, it's easy to turn off or control the GC.  It's also 
easy to control memory allocation in general (unlike, say, 
Java, where it's practically impossible to do anything without 
writing "new").


Also, yes, a lot of the standard library doesn't work if you do 
that.  A lot does work, but a lot doesn't.  The biggest blocker 
is the use of exceptions, which currently rely on GC (though 
there's interest in changing that).


But I think the real answer to your question is in this thread:
https://forum.dlang.org/thread/o6c9tj$2bdp$1...@digitalmars.com
(Silicon Valley D Meetup - January 26, 2017 - "High Performance 
Tools in D" by Jon Degenhardt)



[...]


When I first started using D about four years ago, it was easy 
to hit compiler bugs and basic things that didn't work.  It 
don't find that happens much nowadays when I'm doing everyday 
programming.


There's plenty of new stuff happening, like escape analysis, 
but the foundation is getting pretty good.  I think the biggest 
gap is the number of libraries compared to, say, Python, but 
personally I'm happy binding to C libraries, and there are 
plenty of them.



[...]


Some people have written tutorials.  It sounds like you're 
already experienced with programming, so the fastest way is 
probably to just dive in.  Get the basics from a small 
tutorial, then pick a small project (or some practice 
programming problems) and start coding with the standard 
library docs on hand :)



[...]


Lisp macros let you rewrite features at the interpreter level.  
Walter Bright has explicitly said he doesn't like that kind of 
macro (I don't think he even likes the C preprocessor's macros).


D's metaprogramming is more constrained in that sense, but it's 
powerful at code generation (see templates and the "mixin" 
keyword), compile-time code execution, and compile-time 
introspection.  Compile-time introspection is one of my 
favourite features.  If, for example, you need an array of all 
the names of single argument methods (or whatever) from a 
class, you can get it.


Take a look at ctRegex in the standard library for a great 
example of what can be done.



[...]


I'm not the core team, but I'm confident the answer is yes :)


Wow! That was an excellent response. Thank you!

I'll be sure to check out the thread that you linked in detail (a 
lot of it went over my head, but I'm sure it'll all make more 
sense soon). I also managed to dig out the YouTube link from 
there. :-)


Also, thanks for sharing your experience. It really does help. I 
was a bit apprehensive because these days rather than the effort, 
I'm more concerned with the time invested (who isn't, right?), 
and reading that your experience with D helps put me at ease.


About metaprogramming, yes, that is one part that I'm really 
interested in, and I would love to explore that area 
thoroughly!The introspection example is pretty cool! I'm pretty 
sure I'll have tons of questions once I get going with D, and the 
community has been very welcoming so far! :-)


Re: Hello, folks! Newbie to D, have some questions!

2017-02-18 Thread sarn via Digitalmars-d-learn

On Saturday, 18 February 2017 at 21:09:20 UTC, ag0aep6g wrote:
Also, some threads online mention that if we do turn off GC, 
some of the core std libraries may not fully work. Is this 
presumption also correct?


Yes. Whenever a std function returns a new string or some such 
it's going to be GC-allocated.


This particular problem isn't so bad as it might sound because D 
string functions are based on ranges.


Re: Hello, folks! Newbie to D, have some questions!

2017-02-18 Thread sarn via Digitalmars-d-learn

On Saturday, 18 February 2017 at 20:15:55 UTC, timmyjose wrote:

Hello folks,


Hi :)

2. I am more interested in learning D as a pure systems 
programming language so that I can develop my own tools (not 
looking to develop an OS, just some grep-scale tools to start 
off with). In that regard, I have a few concerns about the GC. 
My rudimentary knowledge of the D ecosystem tells me that there 
is a GC in D, but that can be turned off. Is this correct? 
Also, some threads online mention that if we do turn off GC, 
some of the core std libraries may not fully work. Is this 
presumption also correct?


Okay, yes, it's easy to turn off or control the GC.  It's also 
easy to control memory allocation in general (unlike, say, Java, 
where it's practically impossible to do anything without writing 
"new").


Also, yes, a lot of the standard library doesn't work if you do 
that.  A lot does work, but a lot doesn't.  The biggest blocker 
is the use of exceptions, which currently rely on GC (though 
there's interest in changing that).


But I think the real answer to your question is in this thread:
https://forum.dlang.org/thread/o6c9tj$2bdp$1...@digitalmars.com
(Silicon Valley D Meetup - January 26, 2017 - "High Performance 
Tools in D" by Jon Degenhardt)


In this regard, I am curious to know if I would face any issues 
(with my intent in mind), or will I do just fine? If you could 
share your experiences and domains of use, that would also be 
very helpful for me. Secondly, how stable is the language and 
how fast is the pace of development on D?


When I first started using D about four years ago, it was easy to 
hit compiler bugs and basic things that didn't work.  It don't 
find that happens much nowadays when I'm doing everyday 
programming.


There's plenty of new stuff happening, like escape analysis, but 
the foundation is getting pretty good.  I think the biggest gap 
is the number of libraries compared to, say, Python, but 
personally I'm happy binding to C libraries, and there are plenty 
of them.


2. I am also curious as to what would be the best path for a 
complete beginner to D to learn it effectively? I am a 
relatively fast learner (and I learn better by context, as in, 
some core unifying idea described and then elucidated through 
big examples instead of learning in bits and pieces). How did 
you folks learn D? I'm sure hearing your experiences would be 
helpful too. Are there any books/video tutorials that you would 
recommend (aside from this site itself).


Some people have written tutorials.  It sounds like you're 
already experienced with programming, so the fastest way is 
probably to just dive in.  Get the basics from a small tutorial, 
then pick a small project (or some practice programming problems) 
and start coding with the standard library docs on hand :)


4. I have heard good reports of D's metaprogramming 
capabilities (ironically enough, primarily from a thread on the 
Rust user group), and coming from a Common Lisp (and some 
Racket) background, I am deeply interested in this aspect. Are 
D macros as powerful as Lisp macros? Are they semantically 
similar (for instance, I found Rust's macros are quite similar 
to Racket's)?


Lisp macros let you rewrite features at the interpreter level.  
Walter Bright has explicitly said he doesn't like that kind of 
macro (I don't think he even likes the C preprocessor's macros).


D's metaprogramming is more constrained in that sense, but it's 
powerful at code generation (see templates and the "mixin" 
keyword), compile-time code execution, and compile-time 
introspection.  Compile-time introspection is one of my favourite 
features.  If, for example, you need an array of all the names of 
single argument methods (or whatever) from a class, you can get 
it.


Take a look at ctRegex in the standard library for a great 
example of what can be done.


5. Supposing I devote the time and energy and get up to speed 
on D, would the core language team be welcoming if I feel like 
I can contribute?


I'm not the core team, but I'm confident the answer is yes :)


Re: Hello, folks! Newbie to D, have some questions!

2017-02-18 Thread timmyjose via Digitalmars-d-learn
Thanks for the very comprehensive response! I think most of my 
doubts are cleared now. You're right though that I'm probably 
worrying too much about GC with my current use case. Also thanks 
for the links - they should also come in very handy indeed.


I managed to find some book recommendations as well on the site. 
I've decided to start out with what appears to be the most 
approachable of them - Programming in D by Ceherli.


D doesn't have macros. D has templates like C++, string mixins 
(insert a statically >know/generated string as D code), and CTFE 
(Compile Time Function Evaluation, to >programmatically generate 
static stuff).


Ah, I see! Thanks for clarifying that although CTFE as you 
mentioned it seems to match my specific interest.


I look forward to learning D and being able to contribute some 
day! :-)


On Saturday, 18 February 2017 at 21:09:20 UTC, ag0aep6g wrote:

On Saturday, 18 February 2017 at 20:15:55 UTC, timmyjose wrote:

[...]


There is a GC, but you can avoid the features that use it. 
There's a function attribute for that: @nogc [1]. It forbids 
GC-manages allocations. The GC is still there, but it won't do 
anything because you're not triggering it.


You can also turn automatic collections off (GC.disable [2]). 
There's no need for that when all your code is @nogc, though, 
because collections are triggered by allocations.


As for getting rid of the GC entirely (for saving space, I 
guess), I think that's more involved. May require changes to 
druntime. Shouldn't be necessary most of the time.



[...]


Yes. Whenever a std function returns a new string or some such 
it's going to be GC-allocated. There's an experimental module 
for custom allocators [3], but the rest of the library doesn't 
make use of it, yet. When a std function uses the GC, the 
compiler won't let you call it from @nogc code.



[...]


I don't think you're going to run into much trouble when making 
"grep-scale tools".


[...]

[...]


D doesn't have macros. D has templates like C++, string mixins 
(insert a statically know/generated string as D code), and CTFE 
(Compile Time Function Evaluation, to programmatically generate 
static stuff).



[...]


Can't answer this, because I'm not familiar enough with those 
languages.



[...]


Absolutely. Anyone is welcome to contribute. D is very much a 
volunteer effort. Also don't hesitate to point out (or even 
fix) any stumbling blocks you may encounter when starting out.


[1] https://dlang.org/spec/attribute.html#nogc
[2] https://dlang.org/phobos/core_memory.html#.GC.disable
[3] https://dlang.org/phobos/std_experimental_allocator.html
[4] http://ddili.org/ders/d.en/index.html
[5] https://tour.dlang.org/




Re: Hello, folks! Newbie to D, have some questions!

2017-02-18 Thread ag0aep6g via Digitalmars-d-learn

On Saturday, 18 February 2017 at 20:15:55 UTC, timmyjose wrote:
2. I am more interested in learning D as a pure systems 
programming language so that I can develop my own tools (not 
looking to develop an OS, just some grep-scale tools to start 
off with). In that regard, I have a few concerns about the GC. 
My rudimentary knowledge of the D ecosystem tells me that there 
is a GC in D, but that can be turned off. Is this correct?


There is a GC, but you can avoid the features that use it. 
There's a function attribute for that: @nogc [1]. It forbids 
GC-manages allocations. The GC is still there, but it won't do 
anything because you're not triggering it.


You can also turn automatic collections off (GC.disable [2]). 
There's no need for that when all your code is @nogc, though, 
because collections are triggered by allocations.


As for getting rid of the GC entirely (for saving space, I 
guess), I think that's more involved. May require changes to 
druntime. Shouldn't be necessary most of the time.


Also, some threads online mention that if we do turn off GC, 
some of the core std libraries may not fully work. Is this 
presumption also correct?


Yes. Whenever a std function returns a new string or some such 
it's going to be GC-allocated. There's an experimental module for 
custom allocators [3], but the rest of the library doesn't make 
use of it, yet. When a std function uses the GC, the compiler 
won't let you call it from @nogc code.


In this regard, I am curious to know if I would face any issues 
(with my intent in mind), or will I do just fine?


I don't think you're going to run into much trouble when making 
"grep-scale tools".


[...]
4. I have heard good reports of D's metaprogramming 
capabilities (ironically enough, primarily from a thread on the 
Rust user group), and coming from a Common Lisp (and some 
Racket) background, I am deeply interested in this aspect. Are 
D macros as powerful as Lisp macros?


D doesn't have macros. D has templates like C++, string mixins 
(insert a statically know/generated string as D code), and CTFE 
(Compile Time Function Evaluation, to programmatically generate 
static stuff).


Are they semantically similar (for instance, I found Rust's 
macros are quite similar to Racket's)?


Can't answer this, because I'm not familiar enough with those 
languages.


5. Supposing I devote the time and energy and get up to speed 
on D, would the core language team be welcoming if I feel like 
I can contribute?


Absolutely. Anyone is welcome to contribute. D is very much a 
volunteer effort. Also don't hesitate to point out (or even fix) 
any stumbling blocks you may encounter when starting out.


[1] https://dlang.org/spec/attribute.html#nogc
[2] https://dlang.org/phobos/core_memory.html#.GC.disable
[3] https://dlang.org/phobos/std_experimental_allocator.html
[4] http://ddili.org/ders/d.en/index.html
[5] https://tour.dlang.org/


Re: scope with if

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

Just a note - I found something, that works:


import std.stdio;

void main(string[] args)
{
   immutable cond = args[1]=="a";
   if (cond) write("A");
   scope (exit) if (cond) write("B");

   write("C");
}


I'm using the immutable variable to avoid, that the condition 
changes later.


Hello, folks! Newbie to D, have some questions!

2017-02-18 Thread timmyjose via Digitalmars-d-learn

Hello folks,

I am interested in learning D (just starting out, did a few of 
the exercises on the D tour), and had some questions before I 
decide to jump right in. My questions are genuinely motivated by 
my experiences and expectations, so please forgive me if some 
questions don't come across as well as my intentions!


1. I have some experience with both C and C++, and have been 
learning Rust for a while, but a few things put me off about the 
whole business -


a). The core language appears to be simple enough, but becomes 
increasingly complex as I begin writing larger programs.


b). The whole ownership system is easy to understand, but the 
APIs become very complicated and unwieldy, and more time appears 
to be spent on understanding and ensuring that memory is being 
used correctly than on the core program logic.


c). The whole community seems infused with both the Feminism/SJW 
(I don't care about those communities, but it feels weird having 
a programming community get sidetracked by all that bullshit), 
and too much of Ruby-on-Rails culture (probably started with 
Steve Klabnik) so that it doesn't feel like any real systems 
programmers are focusing on that language, and finally, d). The 
whole language feels like a bit of patchwork of random ideas, and 
also the whole "safety" and "no segfaults" guarantees seem to 
have lesser and lesser RoI as time goes by.


Sorry for the rant, I didn't realise I was quite that frustrated! 
That's just to give some background about me and my recent 
experiences! :D


In that regard, I suppose I'll get a better feel of the community 
here as I interact more, but I have high hopes that it'll be much 
more technical than purely social!


2. I am more interested in learning D as a pure systems 
programming language so that I can develop my own tools (not 
looking to develop an OS, just some grep-scale tools to start off 
with). In that regard, I have a few concerns about the GC. My 
rudimentary knowledge of the D ecosystem tells me that there is a 
GC in D, but that can be turned off. Is this correct? Also, some 
threads online mention that if we do turn off GC, some of the 
core std libraries may not fully work. Is this presumption also 
correct?


In this regard, I am curious to know if I would face any issues 
(with my intent in mind), or will I do just fine? If you could 
share your experiences and domains of use, that would also be 
very helpful for me. Secondly, how stable is the language and how 
fast is the pace of development on D?


Again, sorry for my ignorance if I have been wrong-footed on some 
(or all) points.



2. I am also curious as to what would be the best path for a 
complete beginner to D to learn it effectively? I am a relatively 
fast learner (and I learn better by context, as in, some core 
unifying idea described and then elucidated through big examples 
instead of learning in bits and pieces). How did you folks learn 
D? I'm sure hearing your experiences would be helpful too. Are 
there any books/video tutorials that you would recommend (aside 
from this site itself).


3. Are there some small-scale Open Source projects that you would 
recommend to peruse to get a feel for and learn idiomatic D?


4. I have heard good reports of D's metaprogramming capabilities 
(ironically enough, primarily from a thread on the Rust user 
group), and coming from a Common Lisp (and some Racket) 
background, I am deeply interested in this aspect. Are D macros 
as powerful as Lisp macros? Are they semantically similar (for 
instance, I found Rust's macros are quite similar to Racket's)?


5. Supposing I devote the time and energy and get up to speed on 
D, would the core language team be welcoming if I feel like I can 
contribute?


That's all off the top of my head at the moment. Perhaps I'll 
have more questions as I read the responses. Thanks in advance!


Cheers.




Re: Converting multiple inheritance code into C ++ for D language

2017-02-18 Thread biozic via Digitalmars-d-learn

On Saturday, 18 February 2017 at 19:05:14 UTC, Jean Cesar wrote:
This is exactly what I want this code I did to understand how 
would apply multiple inheritance in D, C # also process using 
interfaces but the difference from C # to D is that C # already 
in the base class you have to define it as interface. ..


OK, but I guess you are aware that in this code, using interfaces 
and the pseudo-multiple-inheritance is pointless! You could just 
ditch them and use regular methods :)


Re: Converting multiple inheritance code into C ++ for D language

2017-02-18 Thread Jean Cesar via Digitalmars-d-learn

On Saturday, 18 February 2017 at 16:27:51 UTC, biozic wrote:

On Saturday, 18 February 2017 at 12:56:51 UTC, wiki wrote:

On Saturday, 18 February 2017 at 09:33:25 UTC, biozic wrote:
A mixin can be used to provide an base implementation for the 
methods of an interface, along with data members, so that you 
don't have to define it in every class that implements the 
interface.


An example : https://dpaste.dzfl.pl/b656851e5c51




I tried to use it in the same way but I did not understand 
correctly because to simulate, alias in this code I had 
already defined the classes as interfaces but I did not 
understand how these constructors should be declared for later 
use ..


There are multiple typos problems with your code. For me, the 
main problem would be that this code is using OOP the wrong 
way, but maybe this code doesn't represent what you actually 
want to do... Anyway, see a corrected version below.


import std.stdio;

class Test1
{
protected string _msg1;

this(string msg1)
{
_msg1 = msg1;
}
} // No semicolon

interface Test2
{
// Interfaces can't have data members

// This template could actually be out of the interface.
// I just put it here because it's more clear that it's 
related to Test2.

mixin template Impl()
{
protected string _msg2; // Data member is inside the 
template


// This function is not a constructor. Only the class 
implementing

// the interface will have one.
void thisTest2(string msg2)
{
_msg2 = msg2;
}
}
}

interface Test3
{
mixin template Impl()
{
protected string _msg3;
void thisTest3(string msg3)
{
_msg3 = msg3;
}
}
}

class Test4 : Test1, Test2, Test3
{
mixin Test2.Impl;
mixin Test3.Impl;

string _msg4;

this(string msg1, string msg2, string msg3, string msg4)
{
super(msg1);  // Calls the constructor of Test1
thisTest2(msg2); // Use interface Test2
thisTest3(msg3); // Use interface Test3
this._msg4 = msg4; // Test4 implementation
}

void show() // Don't use override here
{
writeln(_msg1, _msg2, _msg3, _msg4);
}
}

void main()
{
auto teste = new Test4("\n\tTeste1 ", "Teste2 ", "Teste3 ", 
"Teste4");

teste.show();
// No explicit return is required
}


This is exactly what I want this code I did to understand how 
would apply multiple inheritance in D, C # also process using 
interfaces but the difference from C # to D is that C # already 
in the base class you have to define it as interface. ..


Re: Converting multiple inheritance code into C ++ for D language

2017-02-18 Thread biozic via Digitalmars-d-learn

On Saturday, 18 February 2017 at 12:56:51 UTC, wiki wrote:

On Saturday, 18 February 2017 at 09:33:25 UTC, biozic wrote:
A mixin can be used to provide an base implementation for the 
methods of an interface, along with data members, so that you 
don't have to define it in every class that implements the 
interface.


An example : https://dpaste.dzfl.pl/b656851e5c51




I tried to use it in the same way but I did not understand 
correctly because to simulate, alias in this code I had already 
defined the classes as interfaces but I did not understand how 
these constructors should be declared for later use ..


There are multiple typos problems with your code. For me, the 
main problem would be that this code is using OOP the wrong way, 
but maybe this code doesn't represent what you actually want to 
do... Anyway, see a corrected version below.


import std.stdio;

class Test1
{
protected string _msg1;

this(string msg1)
{
_msg1 = msg1;
}
} // No semicolon

interface Test2
{
// Interfaces can't have data members

// This template could actually be out of the interface.
// I just put it here because it's more clear that it's 
related to Test2.

mixin template Impl()
{
protected string _msg2; // Data member is inside the 
template


// This function is not a constructor. Only the class 
implementing

// the interface will have one.
void thisTest2(string msg2)
{
_msg2 = msg2;
}
}
}

interface Test3
{
mixin template Impl()
{
protected string _msg3;
void thisTest3(string msg3)
{
_msg3 = msg3;
}
}
}

class Test4 : Test1, Test2, Test3
{
mixin Test2.Impl;
mixin Test3.Impl;

string _msg4;

this(string msg1, string msg2, string msg3, string msg4)
{
super(msg1);  // Calls the constructor of Test1
thisTest2(msg2); // Use interface Test2
thisTest3(msg3); // Use interface Test3
this._msg4 = msg4; // Test4 implementation
}

void show() // Don't use override here
{
writeln(_msg1, _msg2, _msg3, _msg4);
}
}

void main()
{
auto teste = new Test4("\n\tTeste1 ", "Teste2 ", "Teste3 ", 
"Teste4");

teste.show();
// No explicit return is required
}


Re: multi-dimensional arrays, not arrays of arrays

2017-02-18 Thread XavierAP via Digitalmars-d-learn

Nice, thanks! Will check it out


Re: Converting multiple inheritance code into C ++ for D language

2017-02-18 Thread wiki via Digitalmars-d-learn

On Saturday, 18 February 2017 at 09:33:25 UTC, biozic wrote:

On Friday, 17 February 2017 at 23:35:33 UTC, Jean Cesar wrote:
On Friday, 17 February 2017 at 23:31:41 UTC, Adam D. Ruppe 
wrote:

On Friday, 17 February 2017 at 23:11:25 UTC, Jean Cesar wrote:
so I changed the code to use interface but how would I do so 
I could use the constructor in the same way as such a C ++ 
code?


Interfaces + mixin templates give you something very similar 
to multiple inheritance. You can have named functions in the 
mixin templates that do the work of the constructor, then 
call them from the real constructor.



Yes I saw here that it uses interface to make multiple 
inheritance just like C#, but I did not understand what would 
this mixing?


A mixin can be used to provide an base implementation for the 
methods of an interface, along with data members, so that you 
don't have to define it in every class that implements the 
interface.


An example : https://dpaste.dzfl.pl/b656851e5c51




I tried to use it in the same way but I did not understand 
correctly because to simulate, alias in this code I had already 
defined the classes as interfaces but I did not understand how 
these constructors should be declared for later use ..


import std.stdio;
import std.string;

/**
* Source: Digital MArs D  
*
* Name: StringHerancaMulti.d  
*
* Concept Aplied: Herança Multipla usando Classe com 4 variáveis 
string   *
* Autor: Jean Zonta   
*

**/

class Test1
{
 protected string _msg1;

   this( string msg1 )
   {
 _msg1=msg1;
   }
};

interface Test2
{
   protected string _msg2;

 // Provide an overridable implementation
 mixin template Impl()
 {
   this( string msg2 )
   {
_msg2=msg2;
   }
 }
};

interface Test3
{
 protected string _msg3;

 // Provide an overridable implementation
 mixin template Impl()
 {
   this( string msg3 )
   {
_msg3=msg3;
   }
 }
};

class Test4: Test1, Test2, Test3
{
  mixin Test2.Impl;
  mixin Test3.Impl;

 string _msg4;

 this( string msg1, string msg2 , string msg3, string msg4 )
 {
  super(msg1,msg2,msg3);
  this._msg1 = msg1;
  this._msg2 = msg2;
  this._msg3 = msg3;
  this._msg4 = msg4;
 }

 override void show()
 {
  writeln(_msg1,_msg2,_msg3,_msg4);
 }
};

void main()
{
 Test4 teste = new Teste4("\n\tTeste1 ","Teste2 ","Teste3 
","Teste4");

 teste.show();
 return 0;
}



Re: multi-dimensional arrays, not arrays of arrays

2017-02-18 Thread Ilya Yaroshenko via Digitalmars-d-learn

On Saturday, 18 February 2017 at 10:37:21 UTC, XavierAP wrote:
Does D provide anything like this? Otherwise, was this ever 
considered and were reasons found not to have it?


They are implemented as part of the Mir project. We call them 
ndslices.


https://github.com/libmir/mir-algorithm
Docs: http://docs.algorithm.dlang.io/

See also other Mir projects at https://github.com/libmir.

std.experimental.ndslice is a deprecated version of mir.ndslice. 
std.experimental.ndslice provides only numpy like tensors. 
mir.ndslice provides all kinds of tensors. Sparse tensors can be 
found at https://github.com/libmir/mir


Best,
Ilya



multi-dimensional arrays, not arrays of arrays

2017-02-18 Thread XavierAP via Digitalmars-d-learn
Does D provide anything like this? Otherwise, was this ever 
considered and were reasons found not to have it?


I mean at least in C# (not talking about C/C++ at all) you can 
declare two kind of multi-dimensional arrays: T[][][] or T[,,]. 
The first is the same as the D ones, array of arrays of... 
(except the order of the indices isn't inverted in C# type 
declarations IIRC); but the second provides contiguous, 
rectangular truly multi-dimensional arrays supposedly with one 
single level of indirection. (Of course static arrays don't exist 
in C# just dynamic but that's an apart issue.)


Is there a reason why this feature would not be really desirable 
or possible? Does it really exist and I missed it in Andrei's 
book? As a matter of fact he states in chapter 4.3:


«On the minus side, "tall and thin" arrays with many rows and few 
columns incur a large size overhead as there's one array to keep 
per column. [...] Jagged arrays may have problems with efciency 
of access and cache friendliness. Each element access requires 
two indirections [...] going column-wise through a jagged array 
is a cache miss bonanza.»


It would look that well implemented [,,] arrays would fill some 
application gaps, even though arrays of arrays (jagged in 
practice or not) are elsewhere useful as well of course. In the 
former cases I would think about defining my own struct wrapping 
a flat array with an n-dimensional api such as return _buff[i*m + 
j]... but generic in type and maybe also in dimension.


Again any special reason why this isn't already provided in the 
language? Either a generic struct/class like that, or something 
deeper in the language that allows using the [] operator as 
transparently as with any other arrays (as in C#).

Thanks for any input.


Re: Converting multiple inheritance code into C ++ for D language

2017-02-18 Thread biozic via Digitalmars-d-learn

On Friday, 17 February 2017 at 23:35:33 UTC, Jean Cesar wrote:
On Friday, 17 February 2017 at 23:31:41 UTC, Adam D. Ruppe 
wrote:

On Friday, 17 February 2017 at 23:11:25 UTC, Jean Cesar wrote:
so I changed the code to use interface but how would I do so 
I could use the constructor in the same way as such a C ++ 
code?


Interfaces + mixin templates give you something very similar 
to multiple inheritance. You can have named functions in the 
mixin templates that do the work of the constructor, then call 
them from the real constructor.



Yes I saw here that it uses interface to make multiple 
inheritance just like C#, but I did not understand what would 
this mixing?


A mixin can be used to provide an base implementation for the 
methods of an interface, along with data members, so that you 
don't have to define it in every class that implements the 
interface.


An example : https://dpaste.dzfl.pl/b656851e5c51