Re: Why tuples are not ranges?

2018-06-28 Thread Alex via Digitalmars-d-learn

On Thursday, 28 June 2018 at 19:02:51 UTC, Ali Çehreli wrote:

On 06/28/2018 11:08 AM, Mr.Bingo wrote:

> Thanks, why not add the ability to pass through ranges and
arrays and
> add it to phobos?

Makes sense. It needs an enhancement request at 
http://issues.dlang.org/, a good implementation, and a pull 
request. :)


Ali


Wouldn't this be weird from the semantic view?

Assume, I define a tuple with named fields "key" and "value", 
where a popFront is defined.

Now, I use popFront.
Is the "key" field still available? Why? Or, why not?
What happens, if I name a field of a tuple "front"? Is it 
available after using popFront? And where does it point to?


I mean, I use something similar in my own project, but doesn't 
one has to make a clear distinction between a container and a 
slice of this container? Or should static arrays be unified in 
the same manner?


Re: errnoEnforce: which imports?

2018-06-28 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, June 29, 2018 01:25:39 kdevel via Digitalmars-d-learn wrote:
> In https://dlang.org/phobos/std_exception.html#errnoEnforce this
> example is shown:
>
> ---
> auto f = errnoEnforce(fopen("data.txt"));
> auto line = readln(f);
> enforce(line.length); // expect a non-empty line
> ---
>
> I added
>
> import std.stdio;
> import std.exception;
>
> and get an error message which reminds me of C++:
>
> zz.d(8): Deprecation: std.stdio.fopen(R1, R2)(R1 name, R2 mode =
> "r") if ((isInputRange!R1 && isSomeChar!(ElementEncodingType!R1)
>
> || isSomeString!R1) && (isInputRange!R2 &&
>
> isSomeChar!(ElementEncodingType!R2) || isSomeString!R2)) is not
> visible from module zz
> zz.d(8): Error: function std.stdio.fopen!(string, string).fopen
> is not accessible from module zz
> zz.d(9): Error: template std.stdio.readln cannot deduce function
> from argument types !()(shared(_IO_FILE)*), candidates are:
> [...]/dmd2/linux/bin64/../../src/phobos/std/stdio.d(4068):
> std.stdio.readln(S = string)(dchar terminator = '\x0a') if
> (isSomeString!S)
> [...]/dmd2/linux/bin64/../../src/phobos/std/stdio.d(4102):
> std.stdio.readln(C)(ref C[] buf, dchar terminator = '\x0a') if
> (isSomeChar!C && is(Unqual!C == C) && !is(C == enum))
> [...]/dmd2/linux/bin64/../../src/phobos/std/stdio.d(4109):
> std.stdio.readln(C, R)(ref C[] buf, R terminator) if
> (isSomeChar!C && is(Unqual!C == C) && !is(C == enum) &&
> isBidirectionalRange!R && is(typeof(terminator.front ==
> (dchar).init)))
> make: *** [zz.o] Error 1
>
> What's wrong here?

It looks like that example is using a private function from std.stdio which
wraps core.stdc.stdio.fopen. It also passes a FILE* to readln, which is
completely wrong. readln as a free function works with std.stdio.stdin and
does not accept a FILE*, whereas readln on std.stdio.File obviously operates
on File, since it's a member function on File. You could use
core.stdc.stdio.fopen to get a FILE and pass it to File's constructor, and
then call readln on the file, but really, that example needs to be
completely redone.

The error message for fopen stems from the fact that it's private, and that
message is longer than it would be otherwise because of an import bug that
was previously fixed but generates a deprecation message for now so as not
to immediately break code. The error messages after that have to do with the
compiler showing you the various overloads of readln that don't match FILE*.

- Jonathan M Davis



Re: Create D binding for C struct containing templated class

2018-06-28 Thread evilrat via Digitalmars-d-learn

On Thursday, 28 June 2018 at 18:43:11 UTC, Andre Pany wrote:

Hi,

I try to write a binding for the GRPC core. There is a struct 
which has some ugly fields:

(https://github.com/grpc/grpc/blob/master/src/core/lib/surface/call.cc#L229)

struct grpc_call {
  gpr_refcount ext_ref;
  gpr_arena* arena;
  ...
  
grpc_core::ManualConstructor 
sending_stream;
  grpc_core::OrphanablePtr 
receiving_stream;



ManualConstructor is defined as:
https://github.com/grpc/grpc/blob/50cecca24d4826dea4595b514a332fca5783074b/src/core/lib/gprpp/manual_constructor.h#L168

template 
class ManualConstructor {
 public:
  Type* get() { return reinterpret_cast(_); }
  const Type* get() const { return reinterpret_castType*>(_); }



My C / C++ knowledge is very limited and I also do not know how 
much D matured regarding C / C++ integration. Is it possible to 
write a binding in D for these constructs or isn't it supported 
by D?


Kind regards
André


No, unfortunately D cannot interface with C templates, only C++ 
templates. But just by coincidence this is C++ source code (and 
so the templates).



So it is possible but there is few catches.
- template functions/methods could be inlined by compiler, which 
means you may (or may not) have to implement it in D (either 
copy-paste and adapt, or make own ABI compatible implementation)


- most of the methods in this particular template is purely C++ 
specific convenience helpers, all of this isn't strictly needed 
in D, just for example operator-> could simply be replaced with 
alias this.


- the only data member in this template is the 
std::aligned_storage, which you can probably ignore completely 
and instead just use fixed-size ubyte[] array with align, again 
just make struct with alias this and align its content same way 
as aligned_storage does.



And much more-more other things to keep in mind.


But the most important thing, are you absolutely sure this is a 
part of the public API and not the implementation specific 
internals? Because normally you don't #include anything other 
than .h* files, this is a source file, and it is not even sits in 
the include folder.


errnoEnforce: which imports?

2018-06-28 Thread kdevel via Digitalmars-d-learn
In https://dlang.org/phobos/std_exception.html#errnoEnforce this 
example is shown:


---
auto f = errnoEnforce(fopen("data.txt"));
auto line = readln(f);
enforce(line.length); // expect a non-empty line
---

I added

   import std.stdio;
   import std.exception;

and get an error message which reminds me of C++:

zz.d(8): Deprecation: std.stdio.fopen(R1, R2)(R1 name, R2 mode = 
"r") if ((isInputRange!R1 && isSomeChar!(ElementEncodingType!R1) 
|| isSomeString!R1) && (isInputRange!R2 && 
isSomeChar!(ElementEncodingType!R2) || isSomeString!R2)) is not 
visible from module zz
zz.d(8): Error: function std.stdio.fopen!(string, string).fopen 
is not accessible from module zz
zz.d(9): Error: template std.stdio.readln cannot deduce function 
from argument types !()(shared(_IO_FILE)*), candidates are:
[...]/dmd2/linux/bin64/../../src/phobos/std/stdio.d(4068):
std.stdio.readln(S = string)(dchar terminator = '\x0a') if 
(isSomeString!S)
[...]/dmd2/linux/bin64/../../src/phobos/std/stdio.d(4102):
std.stdio.readln(C)(ref C[] buf, dchar terminator = '\x0a') if 
(isSomeChar!C && is(Unqual!C == C) && !is(C == enum))
[...]/dmd2/linux/bin64/../../src/phobos/std/stdio.d(4109):
std.stdio.readln(C, R)(ref C[] buf, R terminator) if 
(isSomeChar!C && is(Unqual!C == C) && !is(C == enum) && 
isBidirectionalRange!R && is(typeof(terminator.front == 
(dchar).init)))

make: *** [zz.o] Error 1

What's wrong here?


Re: Nullable!T with T of class type

2018-06-28 Thread Jordan Wilson via Digitalmars-d-learn

On Thursday, 28 June 2018 at 19:22:38 UTC, Jonathan M Davis wrote:
On Thursday, June 28, 2018 18:10:07 kdevel via 
Digitalmars-d-learn wrote:
On Tuesday, 26 June 2018 at 21:54:49 UTC, Jonathan M Davis 
wrote:
> [H]onestly, I don't understand why folks keep trying to put 
> nullable types in Nullable in non-generic code.


How do you signify that a struct member of class type is 
optional?


Structs aren't nullable, so wrapping them in a Nullable makes 
perfect sense. Whether they happen to be on the stack or 
members of another type is irrelevant to that. It's wrapping 
types like pointers and class references in a Nullable that's 
an odd thing to do - the types where someone might ask why the 
extra bool is necessary in the Nullable. Wrapping them in a 
Nullable makes sense in generic code, because the code isn't 
written specifically for them, but something like 
Nullable!MyClass in non-generic code is pointless IMHO, because 
a class reference is already nullable.


- Jonathan M Davis


Reading inpput from a csv file, and the value could either be 
blank, na, or numeric.  Nullable!MyClass could be used to 
represent 3 states in your programming logic.
I'm not saying this is the best way to represent ternary states, 
but it's not unreasonable.


Jordan Wilson


Re: Nullable!T with T of class type

2018-06-28 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, June 28, 2018 19:45:52 kdevel via Digitalmars-d-learn wrote:
> On Thursday, 28 June 2018 at 19:22:38 UTC, Jonathan M Davis wrote:
> > Nullable makes sense in generic code, because the code isn't
> > written specifically for them, but something like
> > Nullable!MyClass in non-generic code is pointless IMHO, because
> > a class reference is already nullable.
>
> It is already technically nullable. But how do you signify to the
> reader of the code that a class member in a struct or in a class
> may intentionally (not purely technically) be null?
>
> If I had written
>
>  class R {
>
> S s;
>
>  }
>
> with S being a class. The reader of the code cannot spot if s is
> optional. In my code I could not have declared S as a struct
> since it implements an interface.

That's something that you have to worry about with all code involving
classes. Putting Nullable on a class reference does seem like it would make
it clear that you want it to be nullable, but it doesn't really, and the
lack of nullable doesn't mean that it can't purposefully be null. If
Nullable couldn't contain a null, or it didn't have a separate bool for
null, then maybe it would help clarify, but that would result in subtle bugs
in generic code whenever null is involved, and there are cases where code
purposefully uses Nullable to indicate that the variable has not been
initialized yet but where null is considered a reasonable value for the
type. So, the fact that Nullable is there doesn't really clarify matters. It
just opens up the question of what happens with null.

In my experience, it's usually pretty clear from the API or design of a
piece of code whether it's intended to accept null or not (usually, the
answer is no) and that folks document what happens if null is provided if
it's supposed to be accepted, but regardless, any case where it's not clear
needs to be documented so that it is clear. Certainly, relying on Nullable
for that is going to be error-prone, because Nullable allows null and can
take advantage of the fact that isNull has nothing to do with the value of
null. So, if the intention is that a Nullable!MyClass never have a value of
null, then that needs to be documented, essentially defeating the purpose of
putting it in a Nullable in the first place. At that point, it would just be
more user-friendly to let it be null and document it as such than to use
Nullable and require that someone provide an uninitialized or cleared
Nullable instead of just using null.

Now, some folks do use Nullable to try and handle whether a class reference
is null, but given that it doesn't really solve the problem of what happens
with null and still requires documenting the intent, I think that it's more
misleading than enlightening to use Nullable on class references. If you're
going to wrap class references to try and indicate whether they can be null
or not, it makes far more sense to wrap a class reference in a wrapper
intended to indicate that it _can't_ be null than one that's intended to
indicate that it can be.

- Jonathan M Davis



Re: Nullable!T with T of class type

2018-06-28 Thread aliak via Digitalmars-d-learn

On Thursday, 28 June 2018 at 18:10:07 UTC, kdevel wrote:
On Tuesday, 26 June 2018 at 21:54:49 UTC, Jonathan M Davis 
wrote:
[H]onestly, I don't understand why folks keep trying to put 
nullable types in Nullable in non-generic code.


How do you signify that a struct member of class type is 
optional?


So there're no optional type in D (ala Swift, Kotlin, Scala, 
etc).  There are a number of workarounds you can use to achieve 
the same type of behavior.


You can use ranges to denote "some" value or no value (empty 
range). But it's a bit inconvenient and the APIs to get that 
running say nothing about intent.


You can create a lightweight Maybe type: 
https://stackoverflow.com/questions/27241908/maybe-types-in-d


I've implemented an optional type as well that includes safe 
dispatching, but it's not @nogc right now -> 
https://github.com/aliak00/optional


I think there's another optional type on dub somewhere as well.

But using Nullable!T just defers the problem that optional solves:

if (nullable.isNull) {
  nullable.get.doSomething();
}

vs:

if (ptr !is null) {
  ptr.doSomething();
}

meh...

It's more of a tool to allow you to give any type nullability 
semantics.


Cheers,
- Ali


Re: Nullable!T with T of class type

2018-06-28 Thread kdevel via Digitalmars-d-learn

On Thursday, 28 June 2018 at 19:22:38 UTC, Jonathan M Davis wrote:
Nullable makes sense in generic code, because the code isn't 
written specifically for them, but something like 
Nullable!MyClass in non-generic code is pointless IMHO, because 
a class reference is already nullable.


It is already technically nullable. But how do you signify to the 
reader of the code that a class member in a struct or in a class 
may intentionally (not purely technically) be null?


If I had written

class R {
   :
   S s;
   :
}

with S being a class. The reader of the code cannot spot if s is 
optional. In my code I could not have declared S as a struct 
since it implements an interface.


Re: Nullable!T with T of class type

2018-06-28 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, June 28, 2018 18:10:07 kdevel via Digitalmars-d-learn wrote:
> On Tuesday, 26 June 2018 at 21:54:49 UTC, Jonathan M Davis wrote:
> > [H]onestly, I don't understand why folks keep trying to put
> > nullable types in Nullable in non-generic code.
>
> How do you signify that a struct member of class type is optional?

Structs aren't nullable, so wrapping them in a Nullable makes perfect sense.
Whether they happen to be on the stack or members of another type is
irrelevant to that. It's wrapping types like pointers and class references
in a Nullable that's an odd thing to do - the types where someone might ask
why the extra bool is necessary in the Nullable. Wrapping them in a Nullable
makes sense in generic code, because the code isn't written specifically for
them, but something like Nullable!MyClass in non-generic code is pointless
IMHO, because a class reference is already nullable.

- Jonathan M Davis



Re: Why tuples are not ranges?

2018-06-28 Thread Ali Çehreli via Digitalmars-d-learn

On 06/28/2018 11:08 AM, Mr.Bingo wrote:

> Thanks, why not add the ability to pass through ranges and arrays and
> add it to phobos?

Makes sense. It needs an enhancement request at 
http://issues.dlang.org/, a good implementation, and a pull request. :)


Ali



Create D binding for C struct containing templated class

2018-06-28 Thread Andre Pany via Digitalmars-d-learn

Hi,

I try to write a binding for the GRPC core. There is a struct 
which has some ugly fields:

(https://github.com/grpc/grpc/blob/master/src/core/lib/surface/call.cc#L229)

struct grpc_call {
  gpr_refcount ext_ref;
  gpr_arena* arena;
  ...
  grpc_core::ManualConstructor 
sending_stream;
  grpc_core::OrphanablePtr 
receiving_stream;



ManualConstructor is defined as:
https://github.com/grpc/grpc/blob/50cecca24d4826dea4595b514a332fca5783074b/src/core/lib/gprpp/manual_constructor.h#L168

template 
class ManualConstructor {
 public:
  Type* get() { return reinterpret_cast(_); }
  const Type* get() const { return reinterpret_castType*>(_); }



My C / C++ knowledge is very limited and I also do not know how 
much D matured regarding C / C++ integration. Is it possible to 
write a binding in D for these constructs or isn't it supported 
by D?


Kind regards
André




Re: Nullable!T with T of class type

2018-06-28 Thread kdevel via Digitalmars-d-learn

On Tuesday, 26 June 2018 at 21:54:49 UTC, Jonathan M Davis wrote:
[H]onestly, I don't understand why folks keep trying to put 
nullable types in Nullable in non-generic code.


How do you signify that a struct member of class type is optional?


Re: Why tuples are not ranges?

2018-06-28 Thread Meta via Digitalmars-d-learn

On Thursday, 28 June 2018 at 17:00:37 UTC, Mr.Bingo wrote:
I mean, if you think about it, the memory layout of a tuple is 
sequential types:


T1
T2
...

So, to popFront a tuple is just changing the starting offset.


You're right; it can definitely be done.

struct TupleRange(T...)
{
size_t index;
Tuple!T store;

@property length()
{
assert(index <= store.length);
return store.length - index;
}

Algebraic!T front()
{
assert(length > 0);
return typeof(return)(store[index]);
}

void popFront()
{
assert(length > 0);
index++;
}
}


Re: Why tuples are not ranges?

2018-06-28 Thread Mr.Bingo via Digitalmars-d-learn

On Thursday, 28 June 2018 at 18:03:09 UTC, Ali Çehreli wrote:

On 06/28/2018 10:00 AM, Mr.Bingo wrote:

> But is this going to be optimized?

Not our job! :o)

> That is, a tuple is a range!

Similar to the array-slice distinction, tuple is a container, 
needing its range.


> It is clearly easy to see if a tuple is empty, to get the
front,

Ok.

> and to
> pop the front and return a new tuple with n - 1 elements,
which is
> really just the tuple(a sliced tuple, say) with the first
member hidden.

That would be special for tuples because popFront does not 
return a new range (and definitely not with a new type) but 
mutates the existing one.


Here is a quick and dirty library solution:

// TODO: Template constraints
auto rangified(T)(T t) {
import std.traits : CommonType;
import std.conv : to;

alias ElementType = CommonType!(T.tupleof);

struct Range {
size_t i;

bool empty() {
return i >= t.length;
}

ElementType front() {
final switch (i) {
static foreach (j; 0 .. t.length) {
case j:
return t[j].to!ElementType;
}
}
}

void popFront() {
++i;
}

enum length = t.length;

// TODO: save(), opIndex(), etc.
}

return Range();
}

unittest {
import std.typecons : tuple;
import std.stdio : writefln;
import std.range : ElementType;

auto t = tuple(5, 3.5, false);
auto r = t.rangified;

writefln("%s elements of '%s': %(%s, %)",
 r.length, ElementType!(typeof(r)).stringof, r);
}

void main() {
}

Prints

3 elements of 'double': 5, 3.5, 0

Ali


Thanks, why not add the ability to pass through ranges and arrays 
and add it to phobos?


Re: Why tuples are not ranges?

2018-06-28 Thread Ali Çehreli via Digitalmars-d-learn

On 06/28/2018 10:00 AM, Mr.Bingo wrote:

> But is this going to be optimized?

Not our job! :o)

> That is, a tuple is a range!

Similar to the array-slice distinction, tuple is a container, needing 
its range.


> It is clearly easy to see if a tuple is empty, to get the front,

Ok.

> and to
> pop the front and return a new tuple with n - 1 elements, which is
> really just the tuple(a sliced tuple, say) with the first member hidden.

That would be special for tuples because popFront does not return a new 
range (and definitely not with a new type) but mutates the existing one.


Here is a quick and dirty library solution:

// TODO: Template constraints
auto rangified(T)(T t) {
import std.traits : CommonType;
import std.conv : to;

alias ElementType = CommonType!(T.tupleof);

struct Range {
size_t i;

bool empty() {
return i >= t.length;
}

ElementType front() {
final switch (i) {
static foreach (j; 0 .. t.length) {
case j:
return t[j].to!ElementType;
}
}
}

void popFront() {
++i;
}

enum length = t.length;

// TODO: save(), opIndex(), etc.
}

return Range();
}

unittest {
import std.typecons : tuple;
import std.stdio : writefln;
import std.range : ElementType;

auto t = tuple(5, 3.5, false);
auto r = t.rangified;

writefln("%s elements of '%s': %(%s, %)",
 r.length, ElementType!(typeof(r)).stringof, r);
}

void main() {
}

Prints

3 elements of 'double': 5, 3.5, 0

Ali



Re: Why tuples are not ranges?

2018-06-28 Thread Mr.Bingo via Digitalmars-d-learn

On Thursday, 28 June 2018 at 16:02:59 UTC, Alex wrote:

On Thursday, 28 June 2018 at 14:35:33 UTC, Mr.Bingo wrote:

Seems like it would unify things quite a bit.


Yeah... this is, because you can't popFront on a tuple, as the 
amount of entries is fixed. You can, however, popFront on every 
range.


But as Timoses wrote you can easily make a range out of a 
tuple, for example by slicing:


´´´
import std.typecons, std.range, std.array, std.algorithm, 
std.stdio;


void main()
{
auto t = tuple(3,4,5,6);
auto m = [t[]];
writeln(m.map!(a => 3*a).sum());
}
´´´


But is this going to be optimized?

BTW, surely the tuple has a popFront! Just pop the last element 
and returns a new tuple.


That is, a tuple is a range! Just because it doesn't implement 
the proper functions doesn't mean it can't do so.


It is clearly easy to see if a tuple is empty, to get the front, 
and to pop the front and return a new tuple with n - 1 elements, 
which is really just the tuple(a sliced tuple, say) with the 
first member hidden.


Since a tuple is fixed at compile time you can "virtually" pop 
the elements, it doesn't mean that a tuple is not a range.


So, maybe for it to work the compiler needs to be able to slice 
tuples efficiently(not convert to dynamic arrays).


This is moot if the compiler can realize that it can do most of 
the work at compile time but I'm not so sure it can.


I mean, if you think about it, the memory layout of a tuple is 
sequential types:


T1
T2
...

So, to popFront a tuple is just changing the starting offset.



Re: Why tuples are not ranges?

2018-06-28 Thread Alex via Digitalmars-d-learn

On Thursday, 28 June 2018 at 14:35:33 UTC, Mr.Bingo wrote:

Seems like it would unify things quite a bit.


Yeah... this is, because you can't popFront on a tuple, as the 
amount of entries is fixed. You can, however, popFront on every 
range.


But as Timoses wrote you can easily make a range out of a tuple, 
for example by slicing:


´´´
import std.typecons, std.range, std.array, std.algorithm, 
std.stdio;


void main()
{
auto t = tuple(3,4,5,6);
auto m = [t[]];
writeln(m.map!(a => 3*a).sum());
}
´´´


Re: Why tuples are not ranges?

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

On Thursday, 28 June 2018 at 14:35:33 UTC, Mr.Bingo wrote:

Seems like it would unify things quite a bit.

import std.typecons, std.range, std.array, std.algorithm, 
std.stdio;


void main()
{
auto t = tuple(3,4,5,6);
//auto t = [3,4,5,6];

writeln(t.map!(a => 3*a).sum());


}


You could make a range out of tuples. This seems to work:

import std.typecons, std.range, std.array, std.algorithm, 
std.stdio;


void main()
{
auto t = tuple(3,4,5,6);
auto m = t.expand.only;
writeln(m.map!(a => 3*a).sum());
}

Tuple.expand will expand the elements when calling a function (in 
this case only from std.range).

https://dlang.org/library/std/typecons/tuple.expand.html


Re: E-mail attachment with scrambled text.

2018-06-28 Thread vino.B via Digitalmars-d-learn

On Thursday, 28 June 2018 at 12:36:11 UTC, Simen Kjærås wrote:

On Thursday, 28 June 2018 at 11:46:31 UTC, vino.B wrote:

Output in Linux
Server Details**
Server Name : 1 IP: 1XX
Server Name : 2 IP: 2XX
Server Name : 3 IP: 3XX


The output in Windows(Email attachment) all are in single line

Server Details**Server Name : 1
  IP: 1XXServer Name : 2 IP: 2XX Server Name : 
3 IP: 3XX




Looks to be an issue with newlines. In linux, a newline is 
simply \n. In Windows it's \r\n, and some Windows programs get 
confused when they just see a \n, notably notepad. Notepad++ 
and basically any other editor will handle Unix newlines 
correctly.


--
  Simen


Hi Simen,

 Thank you very much, after replacing all the '\n' with '\r\n' it 
resolved 99% of the formatting issue expect for the below 
function, can you help me on the same.


auto getAvailableDiskSpace(Array!string UtilDrive, File logF) {
auto result = ["/usr/bin/df", "-h", UtilDrive, 
"--output=target,size,used,avail,pcent"].execute;

enforce(result.status == 0);
logF.writeln(result.output);

}

The output of the above code is as below(Single line)

Mounted on  Size  Used Avail Use%/backup 3.0T  2.6T  393G  87%


From,
Vino.B






Re: Getting Source code from complied code.

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

On Thursday, 28 June 2018 at 08:01:42 UTC, vino.B wrote:

Hi All,

  Request your help on how to get the source code, i wrote a 
program named clean.d, complied it and by mistake deleted the 
source code(clean.d), so can we get back the source using the 
complied program(clean), if yes, can you any one help on the 
same.



From,
Vino.B


Use a versioning system and push important changes as soon as 
you're done with smth to a remote location ; ). E.g. Git


Why tuples are not ranges?

2018-06-28 Thread Mr.Bingo via Digitalmars-d-learn

Seems like it would unify things quite a bit.

import std.typecons, std.range, std.array, std.algorithm, 
std.stdio;


void main()
{
auto t = tuple(3,4,5,6);
//auto t = [3,4,5,6];

writeln(t.map!(a => 3*a).sum());


}


Re: Getting Source code from complied code.

2018-06-28 Thread Steven Schveighoffer via Digitalmars-d-learn

On 6/28/18 4:33 AM, vino.B wrote:

Hi Basile,

  Thank you very much, luckily the file was backed up by the backup 
scheduler, so was able to restore the same.




I was going to suggest, next time use a backup, but you already have. 
Amazing how nice it is when you realize that has happened :)


I once had 2 weeks worth of work gone to a hard drive crash, because 
there was no backup. My company actually paid to have the drive restored 
because it was cheaper than recreating the project.


Definitely learned my lesson then.

-Steve


Re: -dip1000 @safe scope wrapper

2018-06-28 Thread Chris M. via Digitalmars-d-learn

On Thursday, 28 June 2018 at 13:29:58 UTC, vit wrote:

Hello,
Is it possible to create scope wrapper initialized by non 
default constructor with scope parameter?

something like this:

struct Wrapper{
int* p;

static Wrapper create(scope return int* p)@safe{
Wrapper w;
w.p = p;
return w;
}

/++ This doesn't work:
this(scope return int* p)@safe scope{
this.p = p;
}+/
}

void main()@safe{
scope int i;
scope Wrapper w1 = Wrapper.create();
scope Wrapper w2 = Wrapper();
}


I'm not sure why create works in this case either. I get the 
following when I uncomment the constructor


test.d(12): Error: scope variable p assigned to this with longer 
lifetime


Is there a subtle difference I'm missing?




-dip1000 @safe scope wrapper

2018-06-28 Thread vit via Digitalmars-d-learn

Hello,
Is it possible to create scope wrapper initialized by non default 
constructor with scope parameter?

something like this:

struct Wrapper{
int* p;

static Wrapper create(scope return int* p)@safe{
Wrapper w;
w.p = p;
return w;
}

/++ This doesn't work:
this(scope return int* p)@safe scope{
this.p = p;
}+/
}

void main()@safe{
scope int i;
scope Wrapper w1 = Wrapper.create();
scope Wrapper w2 = Wrapper();
}



Re: E-mail attachment with scrambled text.

2018-06-28 Thread Simen Kjærås via Digitalmars-d-learn

On Thursday, 28 June 2018 at 11:46:31 UTC, vino.B wrote:

Output in Linux
Server Details**
Server Name : 1 IP: 1XX
Server Name : 2 IP: 2XX
Server Name : 3 IP: 3XX


The output in Windows(Email attachment) all are in single line

Server Details**Server Name : 1
  IP: 1XXServer Name : 2 IP: 2XX Server Name : 
3 IP: 3XX




Looks to be an issue with newlines. In linux, a newline is simply 
\n. In Windows it's \r\n, and some Windows programs get confused 
when they just see a \n, notably notepad. Notepad++ and basically 
any other editor will handle Unix newlines correctly.


--
  Simen


E-mail attachment with scrambled text.

2018-06-28 Thread vino.B via Digitalmars-d-learn

Hi All,

  Request your help, i have a D code which generates a log file 
with below text, in Linux, when i send this log file(text file) 
as an mail attachment the text in the attachment are scrambled so 
request your help on this.


Tried the below Options (no luck):
Content-Type: text/plain
Content-Transfer-Encoding: base64

Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64

Output in Linux
Server Details**
Server Name : 1 IP: 1XX
Server Name : 2 IP: 2XX
Server Name : 3 IP: 3XX


The output in Windows(Email attachment) all are in single line

Server Details**Server Name : 1   
  IP: 1XXServer Name : 2 IP: 2XX Server Name : 
3 IP: 3XX




From,
Vino.B


Re: turn range into tuple ?

2018-06-28 Thread Flaze07 via Digitalmars-d-learn

On Thursday, 28 June 2018 at 09:47:07 UTC, Jonathan M Davis wrote:
On Thursday, June 28, 2018 09:26:10 Flaze07 via 
Digitalmars-d-learn wrote:

On Thursday, 28 June 2018 at 08:52:33 UTC, Simen Kjærås wrote:
>   [...]

what about during runtime ?


Ranges in general have an arbitrary length, whereas tuples have 
a fixed length that is known at compile time. So, it really 
doesn't make sense to convert a range to a tuple. You can 
create function that takes the first x number of elements of a 
range (probably throwing if the range is too short) and create 
a tuple from those elements, but it would be a bit of a pain to 
do, and it would generally be a pretty weird thing to do. A 
dynamic array would make a lot more sense than a tuple.


- Jonathan M Davis


ok gotcha


Re: turn range into tuple ?

2018-06-28 Thread Flaze07 via Digitalmars-d-learn

On Thursday, 28 June 2018 at 09:42:54 UTC, Flaze07 wrote:

On Thursday, 28 June 2018 at 09:38:36 UTC, Stefan Koch wrote:

On Thursday, 28 June 2018 at 09:26:10 UTC, Flaze07 wrote:

On Thursday, 28 June 2018 at 08:52:33 UTC, Simen Kjærås wrote:

On Thursday, 28 June 2018 at 08:36:54 UTC, Flaze07 wrote:
is there some sort of ways to turn range into tuple ? ( an 
array preferably )

e.g
uint[] arr = [ 10, 20, 30 ];
auto tup = rangeToTup( arr );
assert( tup[ 0 ] == 10 );
assert( tup[ 1 ] == 20 );
assert( tup[ 2 ] == 30 );


https://dlang.org/phobos/std_meta#aliasSeqOf

--
  Simen


what about during runtime ?


Tuples are compile-time entities.
However if you just want an array use std.range.array.


that's interesting, thanks


the reason I am asking is that I want to do something similar to 
this code in python :

a, b, c = input().split(' ')
because I feel like reading with readln is easier than readf


Re: turn range into tuple ?

2018-06-28 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, June 28, 2018 09:26:10 Flaze07 via Digitalmars-d-learn wrote:
> On Thursday, 28 June 2018 at 08:52:33 UTC, Simen Kjærås wrote:
> > On Thursday, 28 June 2018 at 08:36:54 UTC, Flaze07 wrote:
> >> is there some sort of ways to turn range into tuple ? ( an
> >> array preferably )
> >> e.g
> >> uint[] arr = [ 10, 20, 30 ];
> >> auto tup = rangeToTup( arr );
> >> assert( tup[ 0 ] == 10 );
> >> assert( tup[ 1 ] == 20 );
> >> assert( tup[ 2 ] == 30 );
> >
> > https://dlang.org/phobos/std_meta#aliasSeqOf
> >
> > --
> >
> >   Simen
>
> what about during runtime ?

Ranges in general have an arbitrary length, whereas tuples have a fixed
length that is known at compile time. So, it really doesn't make sense to
convert a range to a tuple. You can create function that takes the first x
number of elements of a range (probably throwing if the range is too short)
and create a tuple from those elements, but it would be a bit of a pain to
do, and it would generally be a pretty weird thing to do. A dynamic array
would make a lot more sense than a tuple.

- Jonathan M Davis




Re: turn range into tuple ?

2018-06-28 Thread Flaze07 via Digitalmars-d-learn

On Thursday, 28 June 2018 at 09:38:36 UTC, Stefan Koch wrote:

On Thursday, 28 June 2018 at 09:26:10 UTC, Flaze07 wrote:

On Thursday, 28 June 2018 at 08:52:33 UTC, Simen Kjærås wrote:

On Thursday, 28 June 2018 at 08:36:54 UTC, Flaze07 wrote:
is there some sort of ways to turn range into tuple ? ( an 
array preferably )

e.g
uint[] arr = [ 10, 20, 30 ];
auto tup = rangeToTup( arr );
assert( tup[ 0 ] == 10 );
assert( tup[ 1 ] == 20 );
assert( tup[ 2 ] == 30 );


https://dlang.org/phobos/std_meta#aliasSeqOf

--
  Simen


what about during runtime ?


Tuples are compile-time entities.
However if you just want an array use std.range.array.


that's interesting, thanks


Re: turn range into tuple ?

2018-06-28 Thread Stefan Koch via Digitalmars-d-learn

On Thursday, 28 June 2018 at 09:26:10 UTC, Flaze07 wrote:

On Thursday, 28 June 2018 at 08:52:33 UTC, Simen Kjærås wrote:

On Thursday, 28 June 2018 at 08:36:54 UTC, Flaze07 wrote:
is there some sort of ways to turn range into tuple ? ( an 
array preferably )

e.g
uint[] arr = [ 10, 20, 30 ];
auto tup = rangeToTup( arr );
assert( tup[ 0 ] == 10 );
assert( tup[ 1 ] == 20 );
assert( tup[ 2 ] == 30 );


https://dlang.org/phobos/std_meta#aliasSeqOf

--
  Simen


what about during runtime ?


Tuples are compile-time entities.
However if you just want an array use std.range.array.


Re: turn range into tuple ?

2018-06-28 Thread Flaze07 via Digitalmars-d-learn

On Thursday, 28 June 2018 at 08:52:33 UTC, Simen Kjærås wrote:

On Thursday, 28 June 2018 at 08:36:54 UTC, Flaze07 wrote:
is there some sort of ways to turn range into tuple ? ( an 
array preferably )

e.g
uint[] arr = [ 10, 20, 30 ];
auto tup = rangeToTup( arr );
assert( tup[ 0 ] == 10 );
assert( tup[ 1 ] == 20 );
assert( tup[ 2 ] == 30 );


https://dlang.org/phobos/std_meta#aliasSeqOf

--
  Simen


what about during runtime ?


Re: turn range into tuple ?

2018-06-28 Thread Stefan Koch via Digitalmars-d-learn

On Thursday, 28 June 2018 at 08:36:54 UTC, Flaze07 wrote:
is there some sort of ways to turn range into tuple ? ( an 
array preferably )

e.g
uint[] arr = [ 10, 20, 30 ];
auto tup = rangeToTup( arr );
assert( tup[ 0 ] == 10 );
assert( tup[ 1 ] == 20 );
assert( tup[ 2 ] == 30 );


I think you are looking for `aliasSeqOf` in std.meta


Re: turn range into tuple ?

2018-06-28 Thread Simen Kjærås via Digitalmars-d-learn

On Thursday, 28 June 2018 at 08:36:54 UTC, Flaze07 wrote:
is there some sort of ways to turn range into tuple ? ( an 
array preferably )

e.g
uint[] arr = [ 10, 20, 30 ];
auto tup = rangeToTup( arr );
assert( tup[ 0 ] == 10 );
assert( tup[ 1 ] == 20 );
assert( tup[ 2 ] == 30 );


https://dlang.org/phobos/std_meta#aliasSeqOf

--
  Simen


turn range into tuple ?

2018-06-28 Thread Flaze07 via Digitalmars-d-learn
is there some sort of ways to turn range into tuple ? ( an array 
preferably )

e.g
uint[] arr = [ 10, 20, 30 ];
auto tup = rangeToTup( arr );
assert( tup[ 0 ] == 10 );
assert( tup[ 1 ] == 20 );
assert( tup[ 2 ] == 30 );


Re: Getting Source code from complied code.

2018-06-28 Thread vino.B via Digitalmars-d-learn

On Thursday, 28 June 2018 at 08:21:20 UTC, Basile B. wrote:

On Thursday, 28 June 2018 at 08:01:42 UTC, vino.B wrote:

Hi All,

  Request your help on how to get the source code, i wrote a 
program named clean.d, complied it and by mistake deleted the 
source code(clean.d), so can we get back the source using the 
complied program(clean), if yes, can you any one help on the 
same.



From,
Vino.B


You cant. You can disasm that's all (with Hopper or IDA Free 7).


Hi Basile,

 Thank you very much, luckily the file was backed up by the 
backup scheduler, so was able to restore the same.


From,
Vino.B


Re: Getting Source code from complied code.

2018-06-28 Thread Basile B. via Digitalmars-d-learn

On Thursday, 28 June 2018 at 08:01:42 UTC, vino.B wrote:

Hi All,

  Request your help on how to get the source code, i wrote a 
program named clean.d, complied it and by mistake deleted the 
source code(clean.d), so can we get back the source using the 
complied program(clean), if yes, can you any one help on the 
same.



From,
Vino.B


You cant. You can disasm that's all (with Hopper or IDA Free 7).


Getting Source code from complied code.

2018-06-28 Thread vino.B via Digitalmars-d-learn

Hi All,

  Request your help on how to get the source code, i wrote a 
program named clean.d, complied it and by mistake deleted the 
source code(clean.d), so can we get back the source using the 
complied program(clean), if yes, can you any one help on the same.



From,
Vino.B