Re: How to override impure function from pure function
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
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
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
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
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
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
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
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
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
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()
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
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
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
Re: Error and Exception chaining
(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: > 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. 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. The following program causes a chain of exceptions: import std.stdio; import std.string; import std.range; class TestException : Exception { this(string msg) { const fullMmsg = format("%s: %s",typeof(this).stringof, msg); writefln("throwing '%s'", fullMmsg); super(fullMmsg); } } class TestError : Error { this(string msg) { const fullMmsg = format("%s: %s",typeof(this).stringof, msg); writefln("throwing '%s'", fullMmsg); super(fullMmsg); } } // Causes an exception chain where the node at index errorIndex is an // Error (others are all Exceptions). void causeExceptionChain(size_t chainLength, size_t errorIndex) { void throws(size_t n) { scope (exit) { const msg = format("%s", n); if (n == errorIndex) { throw new TestError(msg); } else { throw new TestException(msg); } } if (n != 0) { // Redundant 'return' keyword due to // https://issues.dlang.org/show_bug.cgi?id=16960 return throws(n - 1); } } throws(chainLength - 1); } void main() { try { // -1 would mean "no Error in the chain". Change this to a // number between 0 and 4 (inclusive) then you will realize // that the Exception below will not be caught. size_t errorIndex = -1; causeExceptionChain(5, errorIndex); } catch (Exception original) { writefln("Caught"); // Unrelated: If you're not familiar with the curly braces in // the for loop, see the Note under "The sections of the for // loop" section at http://ddili.org/ders/d.en/for.html for ({ size_t i; Throwable ex = original; } ex; ex = ex.next, ++i) { writeln(" ".replicate(i), ex.msg); } } } throwing 'TestException: 0' throwing 'TestException: 1' throwing 'TestException: 2' throwing 'TestException: 3' throwing 'TestException: 4' Caught TestException: 0 TestException: 1 TestException: 2 TestException: 3 TestException: 4 Make errorIndex something other than -1 e.g. 3 and you will see that the Exception cannot be caught. size_t errorIndex = 3; You get the usual stack trace of an uncaught exception. throwing 'TestException: 0' throwing 'TestException: 1' throwing 'TestException: 2' throwing 'TestError: 3' throwing 'TestException: 4' deneme.TestError@(0): TestError: 3 [...] Then, replace Exception with Error in the catch clause, and you will see that Error is again caught. (Side note: Errors are not supposed to be caught by programs because the whole state of the program is in question.) catch (Error original) {// <-- Now Error // ... } You will see that Error is in its own chain. It will contain just Error 3 and Error 4: throwing 'TestException: 0' throwing 'TestException: 1' throwing 'TestException: 2' throwing 'TestError: 3' throwing 'TestException: 4' Caught TestError: 3 TestException: 4 Now you would hope to get the original bypassed Exception chain with the following code writefln("The bypassed exception was '%s'", original.bypassedException); But bypassedException member of Error is always null. Bug? Thank you, Ali
Error and Exception chaining
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.
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.
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.
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
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?
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()
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
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
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
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
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?
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
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
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?
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 ()
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