Re: How to override impure function from pure function

2016-12-12 Thread Nicholas Wilson via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 05:13:01 UTC, Nikhil Jacob wrote:
I mistook the original statement to mean that an impure 
function can be called from a pure function with some manual 
overrides.


Thank you for the clarification.


Yeah you can't do that, except in a debug statement. You can 
however cheat with SetFunctionAttributes (from std.traits).


Re: How to override impure function from pure function

2016-12-12 Thread Nicholas Wilson via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 04:48:11 UTC, Nikhil Jacob wrote:
In the D spec for pure functions it says that a pure function 
can override


"can override an impure function, but an impure function cannot 
override a pure one"


Can anyone help me how to do this ?


what this means is

class Foo
{
   void foo() { ... }
}

class Bar : Foo
{
override void foo() pure { ... }
}

is allowed. but

int someglobal;
class Foo
{
   void foo() pure { ... }
}

class Bar : Foo
{
override void foo()  { someglobal = 42; }
}

in not.


Re: How to override impure function from pure function

2016-12-12 Thread Nikhil Jacob via Digitalmars-d-learn
On Tuesday, 13 December 2016 at 05:10:02 UTC, Nicholas Wilson 
wrote:
On Tuesday, 13 December 2016 at 04:48:11 UTC, Nikhil Jacob 
wrote:
In the D spec for pure functions it says that a pure function 
can override


"can override an impure function, but an impure function 
cannot override a pure one"


Can anyone help me how to do this ?


what this means is

class Foo
{
   void foo() { ... }
}

class Bar : Foo
{
override void foo() pure { ... }
}

is allowed. but

int someglobal;
class Foo
{
   void foo() pure { ... }
}

class Bar : Foo
{
override void foo()  { someglobal = 42; }
}

in not.


I mistook the original statement to mean that an impure function 
can be called from a pure function with some manual overrides.


Thank you for the clarification.


How to override impure function from pure function

2016-12-12 Thread Nikhil Jacob via Digitalmars-d-learn
In the D spec for pure functions it says that a pure function can 
override


"can override an impure function, but an impure function cannot 
override a pure one"


Can anyone help me how to do this ?


Re: Error and Exception chaining

2016-12-12 Thread Yuxuan Shui via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 00:33:58 UTC, Yuxuan Shui wrote:

On Monday, 12 December 2016 at 22:35:22 UTC, Yuxuan Shui wrote:

On Monday, 12 December 2016 at 22:13:59 UTC, Ali Çehreli wrote:

On 12/12/2016 02:08 PM, Yuxuan Shui wrote:
> [...]
wrote:
>> [...]
Error.bypassedException
>> [...]
mechanism,
>> [...]
Error."
>> [...]
Exception,
>> [...]
otherwise
>> [...]
original
>> [...]
is the Error.
> [...]
Exception to
> [...]

Currently yes. Now this line in my versatile :o) program:

if (n >= errorIndex) {

Two Errors are chained:

Caught
TestError: 3
 TestError: 4

However, there is the following ongoing thread claiming that 
it was a wrong decision:


  http://forum.dlang.org/post/o2n347$2i1g$1...@digitalmars.com

> [...]

From what I could graps from that much of documentation, yes, 
it seems to be a bug.


I did some testing and bypassedException is null in 2.072.1, 
but is not null in 2.070.2




Ali


The unwind process seem to stop one level too early, causing 
language_specific_data to be different, causing 
__dmd_personality_v0 to not chain exception into 
.bypassException.


OK. I think I figured it out. catch(Error x) won't catch 
TestException, so TestException 0,1,2 is not handled at main(). 
However TestError 3 (and TestExcepotion 4 which is chained to it) 
is handled at main(). And dmd won't put Throwables that are 
handled at different places together.


This behavior seems reasonable. So maybe this is actually a bug 
is LDC?


Re: arsd.cgi - maximum length of form post

2016-12-12 Thread Adam D. Ruppe via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 00:48:44 UTC, bachmeier wrote:
a range violation error core.exception.RangeError@test.d(109): 
Range violation


What's that line of your code too?


Re: arsd.cgi - maximum length of form post

2016-12-12 Thread Adam D. Ruppe via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 00:48:44 UTC, bachmeier wrote:

Is there a way to increase the maximum post size?


The second argument to GenericMain is the max content length, it 
has a default of 5,000,000.


http://dpldocs.info/experimental-docs/arsd.cgi.GenericMain.html

http://dpldocs.info/experimental-docs/source/arsd.cgi.d.html#L182


But that shouldn't be changing anything around 3000


arsd.cgi - maximum length of form post

2016-12-12 Thread bachmeier via Digitalmars-d-learn
I'm using arsd.cgi, and have a form set up to take input. I get a 
range violation error core.exception.RangeError@test.d(109): 
Range violation when using the embedded server. It appears to be 
because the input is too large (about 3900 characters). When I 
cut the input to 3000 characters, there is no error.


Is there a way to increase the maximum post size?



Re: Error and Exception chaining

2016-12-12 Thread Yuxuan Shui via Digitalmars-d-learn

On Monday, 12 December 2016 at 22:35:22 UTC, Yuxuan Shui wrote:

On Monday, 12 December 2016 at 22:13:59 UTC, Ali Çehreli wrote:

On 12/12/2016 02:08 PM, Yuxuan Shui wrote:
> [...]
wrote:
>> [...]
Error.bypassedException
>> [...]
mechanism,
>> [...]
Error."
>> [...]
Exception,
>> [...]
otherwise
>> [...]
original
>> [...]
is the Error.
> [...]
Exception to
> [...]

Currently yes. Now this line in my versatile :o) program:

if (n >= errorIndex) {

Two Errors are chained:

Caught
TestError: 3
 TestError: 4

However, there is the following ongoing thread claiming that 
it was a wrong decision:


  http://forum.dlang.org/post/o2n347$2i1g$1...@digitalmars.com

> [...]

From what I could graps from that much of documentation, yes, 
it seems to be a bug.


I did some testing and bypassedException is null in 2.072.1, 
but is not null in 2.070.2




Ali


The unwind process seem to stop one level too early, causing 
language_specific_data to be different, causing 
__dmd_personality_v0 to not chain exception into .bypassException.


Re: Error and Exception chaining

2016-12-12 Thread Yuxuan Shui via Digitalmars-d-learn

On Monday, 12 December 2016 at 22:13:59 UTC, Ali Çehreli wrote:

On 12/12/2016 02:08 PM, Yuxuan Shui wrote:
> [...]
wrote:
>> [...]
Error.bypassedException
>> [...]
mechanism,
>> [...]
Error."
>> [...]
Exception,
>> [...]
otherwise
>> [...]
original
>> [...]
is the Error.
> [...]
Exception to
> [...]

Currently yes. Now this line in my versatile :o) program:

if (n >= errorIndex) {

Two Errors are chained:

Caught
TestError: 3
 TestError: 4

However, there is the following ongoing thread claiming that it 
was a wrong decision:


  http://forum.dlang.org/post/o2n347$2i1g$1...@digitalmars.com

> [...]

From what I could graps from that much of documentation, yes, 
it seems to be a bug.


I did some testing and bypassedException is null in 2.072.1, but 
is not null in 2.070.2




Ali




Re: help on cartesianProduct()

2016-12-12 Thread Era Scarecrow via Digitalmars-d-learn

On Monday, 12 December 2016 at 14:27:07 UTC, Orut wrote:
The code works beautifully! Thank you very much. Will certainly 
acknowledge you as source of this code if this becomes of part 
of a bigger project. I hope others searching this forum would 
also discover this code.


 It could probably use some minor cleanup, documentation and 
proper unittests and constraints, moving to unsigned size_t; If 
front is used more than once than there's inefficiency with 
memory and remaking the array it builds (among other things). 
Could even make an opIndex for creating specific iterations of 
the results.


 But for it's simplicity I hope it does what you need. It really 
wasn't that hard to make.


Re: Error and Exception chaining

2016-12-12 Thread Ali Çehreli via Digitalmars-d-learn

On 12/12/2016 02:08 PM, Yuxuan Shui wrote:
> Thanks a lot for the explanation!
>
> On Monday, 12 December 2016 at 22:01:54 UTC, Ali Çehreli wrote:
>> (Note: Looks like there is a bug regarding Error.bypassedException
>> member. Would others please confirm.)
>>
>> On 12/12/2016 01:15 PM, Yuxuan Shui wrote:
>> > [...]
>> that Error
>> > [...]
>> vague, and I'm
>> > [...]
>>
>> You're referring to "[Errors] bypass the normal chaining mechanism,
>> such that the chain can only be caught by catching the first Error."
>> What it means is that an Error cannot be a collateral of an Exception,
>> hiding in its chain. So, when there is an Error that would otherwise
>> be a collateral of an Exception, you cannot catch the original
>> Exception. What is more importantly in-flight at that time is the Error.
>>
>
> But chaining Error to Error works just like chaining Exception to
> Exception?

Currently yes. Now this line in my versatile :o) program:

if (n >= errorIndex) {

Two Errors are chained:

Caught
TestError: 3
 TestError: 4

However, there is the following ongoing thread claiming that it was a 
wrong decision:


  http://forum.dlang.org/post/o2n347$2i1g$1...@digitalmars.com

>> But bypassedException member of Error is always null. Bug?
>
> Did I just randomly found a bug?

From what I could graps from that much of documentation, yes, it seems 
to be a bug.


Ali



Re: Error and Exception chaining

2016-12-12 Thread Yuxuan Shui via Digitalmars-d-learn

Thanks a lot for the explanation!

On Monday, 12 December 2016 at 22:01:54 UTC, Ali Çehreli wrote:
(Note: Looks like there is a bug regarding 
Error.bypassedException member. Would others please confirm.)


On 12/12/2016 01:15 PM, Yuxuan Shui wrote:
> [...]
that Error
> [...]
vague, and I'm
> [...]

You're referring to "[Errors] bypass the normal chaining 
mechanism, such that the chain can only be caught by catching 
the first Error." What it means is that an Error cannot be a 
collateral of an Exception, hiding in its chain. So, when there 
is an Error that would otherwise be a collateral of an 
Exception, you cannot catch the original Exception. What is 
more importantly in-flight at that time is the Error.




But chaining Error to Error works just like chaining Exception to 
Exception?




But bypassedException member of Error is always null. Bug?


Did I just randomly found a bug?



Thank you,
Ali




Error and Exception chaining

2016-12-12 Thread Yuxuan Shui via Digitalmars-d-learn
I read https://dlang.org/spec/statement.html, which told me that 
Error is different in the way it's chained. But that is pretty 
vague, and I'm still confused.


Can someone explain that using examples?

Thanks.


Re: Trying to understand something about UDAs.

2016-12-12 Thread Kevin Balbas via Digitalmars-d-learn

On Monday, 12 December 2016 at 19:56:27 UTC, Adam D. Ruppe wrote:

But the static if(is(UDA)) is probably what you actually want.


That's exactly what I was looking for, thanks.  I'm not sure I 
ever would've figured out that one on my own.


Re: Trying to understand something about UDAs.

2016-12-12 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 12 December 2016 at 19:37:42 UTC, Kevin Balbas wrote:
I'm trying to figure out how to test whether a UDA struct was 
initialized with arguments (and change the behavior of the 
surrounding code if it was.


Easy solution: test the type. @Attr is setting the type as a UDA, 
@Attr("test") is setting a *value* of type Attr.


static if(is(UDA)) {
  // UDA is a type, so it was @UDA
} else {
  // it is something else (do further tests to see if value, 
etc). For example, test is(typeof(UDA) == Attr) to see if it is a 
value of type Attr.

}


I can rationalize what the error means -- the compiler wasn't 
given a string so it requires a concrete object to get the 
member from.  What I don't understand is why *both* of those 
static ifs passed.  If it has the member and I can't get it, 
and if getting the member compiles yet at the same time it 
doesn't, I'm not sure how I can actually make this test work.


So you can get the member of a type, but not the value of that 
member. getMember itself succeeds - you can pass the result to 
further reflection things (test its type, offset, etc.), but you 
cannot get the value, which is what your code was trying to do 
there (that is like if you wrote `"foo";` in the middle of 
nowhere. It treats the string as an orphaned value).


Basically, getMember is legal, just not in the context you were 
trying it, thus the __traits(compiles) works but the next line 
doesn't.



But the static if(is(UDA)) is probably what you actually want.


Trying to understand something about UDAs.

2016-12-12 Thread Kevin Balbas via Digitalmars-d-learn
I'm trying to figure out how to test whether a UDA struct was 
initialized with arguments (and change the behavior of the 
surrounding code if it was.  While I was tinkering around with 
test files, I came up with this little gem:


import std.traits;

struct Attr
{
string str;
}

@Attr("test")
void foo(){}

@Attr
void bar(){}

void main()
{
alias UDA = getUDAs!(bar, Attr)[0];
static if (__traits(hasMember, UDA, "str"))
{
static if (__traits(compiles, __traits(getMember, UDA, 
"str")))

{
__traits(getMember, UDA, "str"); // Compile error! 
need 'this' for 'str' of type 'string'

}
}
}

I can rationalize what the error means -- the compiler wasn't 
given a string so it requires a concrete object to get the member 
from.  What I don't understand is why *both* of those static ifs 
passed.  If it has the member and I can't get it, and if getting 
the member compiles yet at the same time it doesn't, I'm not sure 
how I can actually make this test work.


It would make sense if the attribute over bar wasn't semantically 
valid, but I can use it in every other way *except* getting the 
string field out of it.


Am I missing something obvious here?


Re: arsd.dom appenChild method gives assertion message

2016-12-12 Thread Erdem via Digitalmars-d-learn

On Sunday, 11 December 2016 at 22:11:51 UTC, Adam D. Ruppe wrote:

On Sunday, 11 December 2016 at 17:52:29 UTC, Erdem wrote:

[...]


Try something like:

content.appendChild(firstElements[0].removeFromTree());


Thanks this method also works.

   foreach (element; firstElements)
   {
   element.removeFromTree();
   content.appendChild(element);
   }




Re: Sanitizing forms in vibe.d. How?

2016-12-12 Thread Bauss via Digitalmars-d-learn

On Monday, 12 December 2016 at 10:25:05 UTC, aberba wrote:
On Monday, 12 December 2016 at 00:42:54 UTC, Nicholas Wilson 
wrote:

On Sunday, 11 December 2016 at 18:30:54 UTC, aberba wrote:


You can enforce that the string that you receive is an email 
address with `isEmail` from `std.net.isemail`


Nice.

What sql library are you using? there is probably a function 
in that somewhere, that does sanitisation, or use prepared 
statements.


Will look into that. Currently planning to use mysql-lited (not 
sure which one is more capable though)




How about alternative to php strip_tags(), strip_slash() ?


With vibe.d I would definitely go with mysql-native instead since 
it's already compatible with it.


See: https://github.com/mysql-d/mysql-native

vibe.d and D in general doesn't suffer from the same things PHP 
does when it comes to sanitizing.


As long as you use prepared statements, then you won't suffer 
from it.


It's much safer to validate data, than sanitize it. That way you 
don't get garbage either.


Re: help on cartesianProduct()

2016-12-12 Thread Orut via Digitalmars-d-learn

On Sunday, 11 December 2016 at 23:07:16 UTC, Era Scarecrow wrote:
On Sunday, 11 December 2016 at 18:05:19 UTC, Era Scarecrow 
wrote:

On Sunday, 11 December 2016 at 16:34:38 UTC, Orut wrote:
I need to be able to vary the number of ranges to feed into 
cartesianProduct() at run time.


 Hmmm... what kind of ranges? Are they going to be arrays? Or 
something else?



Well, with the assumption of using arrays (since you kinda need 
a length for it to work) I've thrown together a quick struct 
that does the job. Not fully tested, but will do the same thing 
with a varying number of inputs.


[code]
struct MultiCart(T) {
T[][] data;
int iteration;
int max;

this(T[][] _d, int iter=0) {
data = _d;
iteration = iter;

max = 1;
foreach(a; _d) {
if (a.length)
max *= a.length;
}
}

T[] front() {
int i = iteration;
T[] val;

foreach(d; data) {
if (d.length) {
val ~= d[i % d.length];
i /= d.length;
}
}

return val;
}

void popFront() {
iteration++;
}

bool empty() {
return iteration >= max;
}
}

unittest {
import std.stdio;
alias CartInt = MultiCart!int;

int[] a=[1,2,3],b=[4,5],c=[6,7];
foreach(x; CartInt([a,b,c]))
writeln(x);

foreach(x; CartInt([a,b]))
writeln(x);
}
[/code]


The code works beautifully! Thank you very much. Will certainly 
acknowledge you as source of this code if this becomes of part of 
a bigger project. I hope others searching this forum would also 
discover this code.


Re: Check whether function/delegate uses any shared or global variables

2016-12-12 Thread Nikhil Jacob via Digitalmars-d-learn

On Monday, 12 December 2016 at 12:30:42 UTC, Jacob Carlborg wrote:

On 2016-12-12 12:15, Nicholas Wilson wrote:

there is the pure function attribute, how ever this still 
allows you to

use globals *if you pass them as parameters to the function*.


And it can access immutable global data.


Thank you all for the help


Re: Check whether function/delegate uses any shared or global variables

2016-12-12 Thread Jacob Carlborg via Digitalmars-d-learn

On 2016-12-12 12:15, Nicholas Wilson wrote:


there is the pure function attribute, how ever this still allows you to
use globals *if you pass them as parameters to the function*.


And it can access immutable global data.

--
/Jacob Carlborg


Re: Check whether function/delegate uses any shared or global variables

2016-12-12 Thread Nicholas Wilson via Digitalmars-d-learn

On Monday, 12 December 2016 at 11:37:04 UTC, Nikhil Jacob wrote:
On Monday, 12 December 2016 at 11:15:28 UTC, Nicholas Wilson 
wrote:
On Monday, 12 December 2016 at 11:02:21 UTC, Nikhil Jacob 
wrote:
Is there any way to check whether a function/delegate passed 
to a function uses any shared or global variables ?


I could not find any in std.traits.


there is the pure function attribute, how ever this still 
allows you to use globals *if you pass them as parameters to 
the function*.


see https://dlang.org/spec/function.html#pure-functions


Make sense.. I have two follow up questions

1. What about delegates ?
2. If a function is not explicitly declared as pure but 
satisfies the conditions of a pure function then can i check 
whether the function is pure using functionAttributes in 
std.traits.


1. they work the same. Delegates can have any of the function 
attributes that a function can, as well as scope, meaning the 
delegate will not be stored.


2. Yes and no. Template functions have their attributes inferred 
(but you can still annotate them manually, to make sure that the 
function is e.g. pure). Non-template functions don't have their 
attributes inferred.


Re: Check whether function/delegate uses any shared or global variables

2016-12-12 Thread Nikhil Jacob via Digitalmars-d-learn
On Monday, 12 December 2016 at 11:15:28 UTC, Nicholas Wilson 
wrote:

On Monday, 12 December 2016 at 11:02:21 UTC, Nikhil Jacob wrote:
Is there any way to check whether a function/delegate passed 
to a function uses any shared or global variables ?


I could not find any in std.traits.


there is the pure function attribute, how ever this still 
allows you to use globals *if you pass them as parameters to 
the function*.


see https://dlang.org/spec/function.html#pure-functions


Make sense.. I have two follow up questions

1. What about delegates ?
2. If a function is not explicitly declared as pure but satisfies 
the conditions of a pure function then can i check whether the 
function is pure using functionAttributes in std.traits.





Re: Sanitizing forms in vibe.d. How?

2016-12-12 Thread Nicholas Wilson via Digitalmars-d-learn

On Monday, 12 December 2016 at 10:25:05 UTC, aberba wrote:
On Monday, 12 December 2016 at 00:42:54 UTC, Nicholas Wilson 
wrote:

On Sunday, 11 December 2016 at 18:30:54 UTC, aberba wrote:


You can enforce that the string that you receive is an email 
address with `isEmail` from `std.net.isemail`


Nice.

What sql library are you using? there is probably a function 
in that somewhere, that does sanitisation, or use prepared 
statements.


Will look into that. Currently planning to use mysql-lited (not 
sure which one is more capable though)


All the bindings on code.dlang.org should be equally capable, 
however some may be easier to use and/or be DB specific (e.g. the 
Postges bindings)




How about alternative to php strip_tags(), strip_slash() ?


for strip_slash look for `replace` and friends in std.array

for strip_tags I would look for an xml library (e.g. arsd.dom) 
and parse it and then reprint it without the tags. There's 
probably a better way to do it though. I'm sure Adam Ruppe will 
be able to help you there.


Re: Check whether function/delegate uses any shared or global variables

2016-12-12 Thread Nicholas Wilson via Digitalmars-d-learn

On Monday, 12 December 2016 at 11:02:21 UTC, Nikhil Jacob wrote:
Is there any way to check whether a function/delegate passed to 
a function uses any shared or global variables ?


I could not find any in std.traits.


there is the pure function attribute, how ever this still allows 
you to use globals *if you pass them as parameters to the 
function*.


see https://dlang.org/spec/function.html#pure-functions




Check whether function/delegate uses any shared or global variables

2016-12-12 Thread Nikhil Jacob via Digitalmars-d-learn
Is there any way to check whether a function/delegate passed to a 
function uses any shared or global variables ?


I could not find any in std.traits.




Re: Sanitizing forms in vibe.d. How?

2016-12-12 Thread aberba via Digitalmars-d-learn
On Monday, 12 December 2016 at 00:42:54 UTC, Nicholas Wilson 
wrote:

On Sunday, 11 December 2016 at 18:30:54 UTC, aberba wrote:


You can enforce that the string that you receive is an email 
address with `isEmail` from `std.net.isemail`


Nice.

What sql library are you using? there is probably a function in 
that somewhere, that does sanitisation, or use prepared 
statements.


Will look into that. Currently planning to use mysql-lited (not 
sure which one is more capable though)




How about alternative to php strip_tags(), strip_slash() ?


Re: function is not callable using argument types ()

2016-12-12 Thread Bauss via Digitalmars-d-learn

On Saturday, 10 December 2016 at 08:41:56 UTC, Suliman wrote:

import std.stdio;
import std.concurrency;

void main()
{

void sp(int i)
{
receive((int i)
{
writeln("i: ", i);
});
}

auto r = new Generator!int(
{
foreach(i; 1 .. 10)
yield(i);
});

foreach(e;r)
{
		sp.send(e); //Error: function app.main.sp (int i) is not 
callable using argument types ()

}

}


What I am doing wrong? How "receive" can be named? Is it's 
method or what? Why it's without return type?


Read: https://dlang.org/spec/function.html#pseudo-member