Re: Template to retrieve compile-time enum member from run-time enum member?

2018-04-28 Thread Timoses via Digitalmars-d-learn

On Friday, 27 April 2018 at 14:33:36 UTC, Alex wrote:

On Friday, 27 April 2018 at 13:43:47 UTC, Timoses wrote:


`instantiateWith` gets called in three variations (menum.A, 
menum.B and menum.C). This causes instantiateWith to return 
TempStruct for each case of Temp...


However, I was under the impression that a templated function 
will exist multiple (in this case 3) times, so the return type 
should be allowed to be different?!


I think, because the enum value is a runtime parameter for 
instantiateWith, only a single variation of it exists. And this 
cannot have different return types.

So... "alias Fn" and "T" stay the same. The value of x varies.

https://run.dlang.io/is/jX4Ybh
states the same.


Ah yes, thanks. I see it now.


Re: Create variable for RedBlackTree range

2018-04-28 Thread Gerald via Digitalmars-d-learn
On Saturday, 28 April 2018 at 17:20:46 UTC, Jonathan M Davis 
wrote:
On Saturday, April 28, 2018 16:36:41 Gerald via 
Digitalmars-d-learn wrote:

[...]


In general, you just use auto, but that's not going to work if 
you can't directly initialize the variable. In that case, the 
solution is typeof. e.g. something like


typeof(prompPosition[]) range;


[...]


If you mean the interfaces from std.range.interfaces, I don't 
think that anything in Phobos uses them except for that module, 
and I expect that very little range-based code in general uses 
them. Ranges are almost always structs. There are rare cases 
where those interfaces make sense, but ranges in general don't 
use them. Rather, range-based code is almost always templated.


- Jonathan M Davis


Thanks for the quick reply and the pointer in the right 
direction, I ended up using the ReturnType template to make it 
work:


ReturnType!(promptPosition.lowerBound) range;



Re: Create variable for RedBlackTree range

2018-04-28 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, April 28, 2018 16:36:41 Gerald via Digitalmars-d-learn wrote:
> What is the appropriate way to create a variable for the range
> returned by RedBlackTree lowerBound and upperBound. For example,
> given this code:
>
> ```
> RedBlackTree!long promptPosition = redBlackTree!long();
>
> long row = to!long(vte.getVadjustment().getValue());
> RBRange!(RBNode!long*) range;
> if (direction < 0) {
>  range = promptPosition.lowerBound(row);
>  if (range.empty) result = lower;
>  else result = range.back;
> } else {
>  range = promptPosition.upperBound(row);
>  if (range.empty) result = upper;
>  else result = range.front();
> }
> if (result >= lower) {
>  vte.getVadjustment.setValue(to!double(result));
> } else {
>  promptPosition.remove(range);
> }
> ```
>
> The second line where I declare the range variable as
> RBRange!(RBNode!long*) the compiler complains with the following
> warning:
>
> Deprecation: std.container.rbtree.RBRange(N) is not visible from
> module terminal

In general, you just use auto, but that's not going to work if you can't
directly initialize the variable. In that case, the solution is typeof. e.g.
something like

typeof(prompPosition[]) range;

> Which makes sense since RBRange is a private struct. However I
> cannot use the normal range interfaces here either (ForwardRange,
> BiDirectionalRange, etc) since it complains about RBRange not
> being able to cast to them.

If you mean the interfaces from std.range.interfaces, I don't think that
anything in Phobos uses them except for that module, and I expect that very
little range-based code in general uses them. Ranges are almost always
structs. There are rare cases where those interfaces make sense, but ranges
in general don't use them. Rather, range-based code is almost always
templated.

- Jonathan M Davis



Create variable for RedBlackTree range

2018-04-28 Thread Gerald via Digitalmars-d-learn
What is the appropriate way to create a variable for the range 
returned by RedBlackTree lowerBound and upperBound. For example, 
given this code:


```
RedBlackTree!long promptPosition = redBlackTree!long();

long row = to!long(vte.getVadjustment().getValue());
RBRange!(RBNode!long*) range;
if (direction < 0) {
range = promptPosition.lowerBound(row);
if (range.empty) result = lower;
else result = range.back;
} else {
range = promptPosition.upperBound(row);
if (range.empty) result = upper;
else result = range.front();
}
if (result >= lower) {
vte.getVadjustment.setValue(to!double(result));
} else {
promptPosition.remove(range);
}
```

The second line where I declare the range variable as 
RBRange!(RBNode!long*) the compiler complains with the following 
warning:


Deprecation: std.container.rbtree.RBRange(N) is not visible from 
module terminal


Which makes sense since RBRange is a private struct. However I 
cannot use the normal range interfaces here either (ForwardRange, 
BiDirectionalRange, etc) since it complains about RBRange not 
being able to cast to them.


Re: E-mail attachment with unwanted characters

2018-04-28 Thread Vino.B via Digitalmars-d-learn

On Friday, 27 April 2018 at 18:20:46 UTC, Adam D. Ruppe wrote:

On Friday, 27 April 2018 at 17:57:26 UTC, Vino.B wrote:
headers.insert(to!string(Base64.encode(Content)) ~ 
".\r\n");

headers.insert("--" ~ boundary ~ ".");


what are those random dots for?


Hi Adam,

 Thank you very much, after removing the dot the unwanted 
characters disappeared, The earlier program (as function) is 
working as expected without any issue, but if I change the 
program from function to Classes, then the programing is 
executing without any errors, able to get the attachment wihout 
any unwanted characters, but not able to get the body text, tries 
passing the body text as Array!sting and normal string, even then 
the body message is not appearing when I receive the mails.


Code:
import std.array: join;
import std.base64: Base64;
import std.container.array;
import std.conv : to;
import std.file: read, getSize;
import std.format : format;
import std.net.curl;
import std.path : baseName;
import std.uuid: randomUUID;
pragma(lib, "curl");

class EmailMessage {

static string Boundary;
static this() { Boundary = randomUUID().toString(); }
string From, Subject, cid, Filename, msg, crlf = "\r\n";
Array!string To, Body, headers, attach;
int Size;

void Attachment (string Filename) { attach ~= Filename; }

string BuildMail () {
string[] tos;
foreach (e; To) { tos ~= e; }
headers.insert("From: " ~ From );
headers.insert("To: " ~ join(tos, ","));
headers.insert("Subject: " ~ Subject);
headers.insert("MIME-Version: 1.0");
headers.insert(format("Content-Type: multipart/alternative; 
boundary=\"%s\"\r\n", Boundary));

headers.insert("--" ~ Boundary);
headers.insert("Content-Type: text/plain; charset=utf-8");
headers ~ Body; //Array!string does not work
headers.insert(Body);   //string does not work
headers.insert("--" ~ Boundary);
headers.insert("Content-Type: text/plain");
headers ~ ((cid !is null) ? "Content-ID: <" ~ cid ~ ">" : "");
headers.insert("Content-Transfer-Encoding: base64");
foreach (File; attach) {
string Fname = baseName(File);
ubyte[] Content = cast(ubyte[])read(File);
Size = to!int(getSize(File) + Body.length);

headers.insert("Content-Disposition: attachment; 
filename=\"" ~ Fname ~ "\"");

headers.insert(crlf);
headers.insert(to!string(Base64.encode(Content)) ~ ".\r\n");
}

headers.insert("--" ~ Boundary);
msg.reserve(Size);
foreach(header; headers) { msg ~= header ~ "\r\n"; }
if(msg.length > 0) { msg ~= "\r\n";}
return(msg);
}

void Send(string server) {
const(char)[][] allRecipients;
foreach (e; To) { allRecipients ~= e; }
auto smtp = SMTP(server);
smtp.mailTo(allRecipients);
smtp.mailFrom = From;
smtp.message = BuildMail();
smtp.perform();
}
 }

void main () {
string Filename = "D:\\DScript\\Test.txt";
Array!string To, Body;
To.insert("us...@ask.com");
To.insert("us...@ask.com");
Body.insert("This is Test1");
Body.insert("This is Test2");
auto message = new EmailMessage();
message.To = To;
message.From = "ad...@ask.com";
message.Subject = "My Subject";
message.Body ~= Body;   //Array!string does not work
mesagae.Body = "Test Body"; //string does not work
message.Attachment = Filename;
message.Send = "smtp://ask.com";
}

From,
Vino.B


Re: Idiomatic way to add examples to dub package

2018-04-28 Thread bauss via Digitalmars-d-learn

On Saturday, 28 April 2018 at 15:11:31 UTC, FreeSlave wrote:

On Friday, 27 April 2018 at 12:37:04 UTC, Basile B. wrote:

On Thursday, 26 April 2018 at 18:16:01 UTC, FreeSlave wrote:
Most dub packages are libraries and should provide runnable 
examples.

What's the current idiomatic way to add examples?


IMO the most simple way (and the best too) is to put single 
file packages in the example folder, so that an example can 
just be run with DUB like that:


`dub example1.d`.

Other good point is that you can specify that the dependency 
to the main package is local, like here: 
https://github.com/BBasile/kheops/blob/master/runnable/actions_window.d#L5.


And finally the example folder is neither polluted with 
sub-folders nor with sub.selection JSON, FTW.


That's probably the best option. No need for using subPackages, 
nor for creating dub.json per every example. And no --root 
option is needed.


The difference is however with most vibe.d examples, they're each 
separate applications.


Re: Idiomatic way to add examples to dub package

2018-04-28 Thread FreeSlave via Digitalmars-d-learn

On Friday, 27 April 2018 at 12:37:04 UTC, Basile B. wrote:

On Thursday, 26 April 2018 at 18:16:01 UTC, FreeSlave wrote:
Most dub packages are libraries and should provide runnable 
examples.

What's the current idiomatic way to add examples?


IMO the most simple way (and the best too) is to put single 
file packages in the example folder, so that an example can 
just be run with DUB like that:


`dub example1.d`.

Other good point is that you can specify that the dependency to 
the main package is local, like here: 
https://github.com/BBasile/kheops/blob/master/runnable/actions_window.d#L5.


And finally the example folder is neither polluted with 
sub-folders nor with sub.selection JSON, FTW.


That's probably the best option. No need for using subPackages, 
nor for creating dub.json per every example. And no --root option 
is needed.


Re: Virtual table in class without overloading

2018-04-28 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 28 April 2018 at 11:52:50 UTC, Jonathan M Davis 
wrote:
unless you're declaring the type as a class purely so that it's 
forced to be a reference type.


You can do a struct as a reference type too - just make it a 
pimpl handle.


Re: Virtual table in class without overloading

2018-04-28 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, April 28, 2018 11:04:56 Andrey via Digitalmars-d-learn wrote:
> Hello,
> I have some questions about virtual table in classes.
> Example 1:
>
> class Test
> {
>  void someMethod() { ... }
>  int anotherMethod { ... }
> }
>
> Will this class have a vtable?
>
> Example 2:
> class Test2 : Test
> {
>  void thirdMethod() { ... }
> }
>
> Will this class have a vtable?
>
> In C++ these classes don't have any vtables. What about D? In D
> all methods are virtual by default in classes. Will complier
> optimize and remove table of virtual functions because in these
> examples we see no method overloading.
>
> Thanks.

Object has virtual functions, so it's not possible to ever remove the
virtual table from a D class.

Also, even if Object didn't have virtual functions, I don't think that it
would matter, because the compiler has no way of knowing whether other code
derives from those classes and overrides those member functions (since that
would require whole program optimtization). As such, it has to treat them as
virtual. It can't devirtualize them.

If you want a class' public member function to be non-virtual, then mark it
with final. That won't get rid of the virtual table, but it will
devirtualize that function as long as its not overriding a base class
function.

Though honestly, I'm not sure why you'd even want a class without a virtual
table unless you're declaring the type as a class purely so that it's forced
to be a reference type. Otherwise, just use a struct. D structs never have
virtual tables, because they have no inheritance. Classes in D are mostly
pointless if you don't want virtual functions.

- Jonathan M Davis



Virtual table in class without overloading

2018-04-28 Thread Andrey via Digitalmars-d-learn

Hello,
I have some questions about virtual table in classes.
Example 1:

class Test
{
void someMethod() { ... }
int anotherMethod { ... }
}

Will this class have a vtable?

Example 2:
class Test2 : Test
{
void thirdMethod() { ... }
}

Will this class have a vtable?

In C++ these classes don't have any vtables. What about D? In D 
all methods are virtual by default in classes. Will complier 
optimize and remove table of virtual functions because in these 
examples we see no method overloading.


Thanks.