Re: How can you read and understand the source of *naryFun in functional.d?
I would like to thank you for that such a great explanation of the Template-based programming in that unary example. I think it's great you have uploaded that to the wiki4d, and it's definite will help a lot of people that come from other common languages background (like C, C# and Python) that don't support full templating, to understand that design pattern. And with that close tracking to the functional.d code, I thing but not sure that we've found some bugs. I will do some testing later today, and if I will see what I think to be wrong, I'll update. and again, Thanks
Re: General unicode category
On 01/29/2011 09:26 PM, Tomek Sowiński wrote: How can I get the general unicode category (Lu, Nd, Pc, etc.) of a dchar? std.uni contains barely anything useful. DUnicode has such functionality: https://bitbucket.org/stephan/dunicode/src Watch inside unicodedata.d, search for general category. Denis -- _ vita es estrany spir.wikidot.com
Re: pragma(msg,
On Sun, Jan 30, 2011 at 00:26, Ellery Newcomer ellery-newco...@utulsa.edu wrote: code: template tct(T1,T2){ string tct = T1.stringof ~ ~ T2.stringof ~ typeof(true?T1.init:T2.init).stringof; } pragma(msg, tct!(shared(const(int))*, const(int*))); result: tct why? Because the inner string is not a CT constant. Put an 'enum' before. Philippe
Re: General unicode category
spir spir napisał: DUnicode has such functionality: https://bitbucket.org/stephan/dunicode/src Watch inside unicodedata.d, search for general category. Thanks. Any word of moving some of it into Phobos? It's jarring to see a Unicode-compliant language have so few tools to work with the standard. -- Tomek
Re: How can you read and understand the source of *naryFun in functional.d?
On Sun, Jan 30, 2011 at 12:03, Tom t...@gmail.com wrote: I would like to thank you for that such a great explanation of the Template-based programming in that unary example. I think it's great you have uploaded that to the wiki4d, and it's definite will help a lot of people that come from other common languages background (like C, C# and Python) that don't support full templating, to understand that design pattern. And with that close tracking to the functional.d code, I thing but not sure that we've found some bugs. I will do some testing later today, and if I will see what I think to be wrong, I'll update. If anyone is interested, I coded a n-args version of unaryFun/binaryFun called naryFun. If you use 'a', 'b', ... as args names, it can automatically determine the templated function arity. So naryFun!a + b * c - sin(d-a) is a 4-args template function. It's there: http://svn.dsource.org/projects/dranges/trunk/dranges/docs/functional.html (look for naryFun at the bottom) code is here: http://dsource.org/projects/dranges/browser/trunk/dranges/functional.d It could be simpler now: when I did it 18 months ago, CTFE wasn't so powerful. The looping templates can now easily be done with a simple foreach. This has been on my todo list for quite some time... Philippe
C# interop
Hi, I am trying to combine C# and D, to get what is to me the best of both worlds; large libraries and excellent toolchain, with to the metal programming. Currently, I am doing this by means of C#/CLI/C++ interop, but I got reminded as to why I hate C++ so much after spending a few days trying to get something as simple as ranges to work. But the interop works smooth; no complaints there. I am using visual D, if it matters any. What would be the best way to go about this? The D documentation is kindof cryptic to a non-professional like me. It doesnt seem as if I will be able to use D classes inside CLI, so I think that means I might as well ditch it altogether and just use Pinvoke? Any suggestions are appreciated! Regards, Eelco
Re: common types + type modifiers
On 2011-01-29 20:09:11 -0500, Ellery Newcomer ellery-newco...@utulsa.edu said: trying to wrap my head around shared,const,immutable (not inout yet, though someday I'll need to figure out what it's supposed to do) Currently, dmd doesn't exhibit a lot of consistency wrt the above, so bear with me as I question every ing thing it does. my [erroneous?] understanding of the modifiers: shared - many threads can see lack of shared - one thread can see plus some difference or other in physical layout (of which I am mostly ignorant) immutable - nobody can mutate lack of immutable - anybody can mutate const - either immutable or mutable - you can't mutate because you don't know which, but you also can't rely on it not mutating shared(immutable(T)) == immutable(T) because who care who can see it if nobody can mutate it otherwise, shared(T) != T where T is not shared or immutable because T might be mutable and then it matters who can see it and when What is the common type of two types with the same base T but different modifiers? My [erroneous?] [generic] reasoning: (notation - i under T1 means T1 == immutable(T), etc) T1 :T2 - ResultT i c cfollows from my defs above i m cditto c m cditto I think this is what dmd does. seems obvious. Everything is correct up to here. what about shared? shared(U) : U - ?? where U is not shared or immutable the result is some sort of a 'maybe shared' - it definitely can't be U. Can it be shared(U) ? I'm not sure about the semantics of tls, but it seems like that would violate U's contract that only one thread can see [and mutate] it. So it seems to me that there is no common type of shared(U) and U. Indeed, U and shared(U) have no common type. unless it's acceptable to just make the mutability const. which would give: csm cs csc cs msm cs msc cs No, that doesn't work. There is no common type between shared and non-shared. otherwise, its a shared/shared or immutable/shared pair, so the result type is shared, and the mutability is the same as for unshared/unshared. which, come to think of it, will always be const. So the result type will be const shared(T) So that's my generic reasoning, which doesn't take the semantics of subtypes, ease of use, etc, into account. here are some cases which I suspect are wrong: const(int) : int - int const(int) : shared(int) - int and shared(const(int*)) : const(int*) - const(int)* 1) why can we do away with the constness of the pointer? 2) I think we should not do away with the sharedness of the pointer, let alone of the element (shared is transitive too, isn't it? it has to be..) About the constness of the pointer, you should only be able to do this when you make a copy of the pointer, as in: const(int*) a; const(int)* b = a; There is no const violation here because pointer 'b' is a new variable, distinct of 'a'. The data it points to however is the same, so the 'int' must stay const. That said, shared(const(int*)) and const(int*) do not have a common type since one is shared and the other is not. -- Michel Fortin michel.for...@michelf.com http://michelf.com/
Re: C# interop
Eelco Hoogendoorn hoogendoorn.ee...@gmail.com wrote: Hi, I am trying to combine C# and D, to get what is to me the best of both worlds; large libraries and excellent toolchain, with to the metal programming. Currently, I am doing this by means of C#/CLI/C++ interop, but I got reminded as to why I hate C++ so much after spending a few days trying to get something as simple as ranges to work. But the interop works smooth; no complaints there. I am using visual D, if it matters any. What would be the best way to go about this? The D documentation is kindof cryptic to a non-professional like me. It doesnt seem as if I will be able to use D classes inside CLI, so I think that means I might as well ditch it altogether and just use Pinvoke? Any suggestions are appreciated! There is at least one attempt to make a D compiler for .net, you might want to give that a try: http://dnet.codeplex.com/ -- Simen
Re: common types + type modifiers
Michel Fortin michel.for...@michelf.com wrote: unless it's acceptable to just make the mutability const. which would give: csm cs csc cs msm cs msc cs No, that doesn't work. There is no common type between shared and non-shared. It's also worth add why this is so: shared guarantees that reads and writes do not overlap. This means all references to an instance must type it as either shared or non-shared. -- Simen
Re: C# interop
As Simen said, there was an attempt to create an IL backend but it hasn't been updated since Aug 2009 and even it compiled you probably wouldn't have much fun with it. You could try to use a C bridge. But even then, I don't see the benefit.
Re: How can you read and understand the source of *naryFun in functional.d?
On 1/30/11, Philippe Sigaud philippe.sig...@gmail.com wrote: If anyone is interested, I coded a n-args version of unaryFun/binaryFun called naryFun. If you use 'a', 'b', ... as args names, it can automatically determine the templated function arity. Is this going in the next release? There's an nary template in std.functional right now but it's commented out. It does look like an older attempt that probably didn't work.
Re: common types + type modifiers
On 01/30/2011 09:47 AM, Michel Fortin wrote: On 2011-01-29 20:09:11 -0500, Ellery Newcomer ellery-newco...@utulsa.edu said: trying to wrap my head around shared,const,immutable (not inout yet, though someday I'll need to figure out what it's supposed to do) Currently, dmd doesn't exhibit a lot of consistency wrt the above, so bear with me as I question every ing thing it does. my [erroneous?] understanding of the modifiers: shared - many threads can see lack of shared - one thread can see plus some difference or other in physical layout (of which I am mostly ignorant) immutable - nobody can mutate lack of immutable - anybody can mutate const - either immutable or mutable - you can't mutate because you don't know which, but you also can't rely on it not mutating shared(immutable(T)) == immutable(T) because who care who can see it if nobody can mutate it otherwise, shared(T) != T where T is not shared or immutable because T might be mutable and then it matters who can see it and when What is the common type of two types with the same base T but different modifiers? My [erroneous?] [generic] reasoning: (notation - i under T1 means T1 == immutable(T), etc) T1 : T2 - ResultT i c c follows from my defs above i m c ditto c m c ditto I think this is what dmd does. seems obvious. Everything is correct up to here. what about shared? shared(U) : U - ?? where U is not shared or immutable the result is some sort of a 'maybe shared' - it definitely can't be U. Can it be shared(U) ? I'm not sure about the semantics of tls, but it seems like that would violate U's contract that only one thread can see [and mutate] it. So it seems to me that there is no common type of shared(U) and U. Indeed, U and shared(U) have no common type. unless it's acceptable to just make the mutability const. which would give: cs m cs cs c cs ms m cs ms c cs No, that doesn't work. There is no common type between shared and non-shared. otherwise, its a shared/shared or immutable/shared pair, so the result type is shared, and the mutability is the same as for unshared/unshared. which, come to think of it, will always be const. So the result type will be const shared(T) So that's my generic reasoning, which doesn't take the semantics of subtypes, ease of use, etc, into account. here are some cases which I suspect are wrong: const(int) : int - int const(int) : shared(int) - int and shared(const(int*)) : const(int*) - const(int)* 1) why can we do away with the constness of the pointer? 2) I think we should not do away with the sharedness of the pointer, let alone of the element (shared is transitive too, isn't it? it has to be..) About the constness of the pointer, you should only be able to do this when you make a copy of the pointer, as in: const(int*) a; const(int)* b = a; There is no const violation here because pointer 'b' is a new variable, distinct of 'a'. The data it points to however is the same, so the 'int' must stay const. That said, shared(const(int*)) and const(int*) do not have a common type since one is shared and the other is not. Ah, thank you. That clears things up a bit. how do you safely convert from a shared type to a non shared type or vice versa? a deep copy inside a critical section?
Threads fibers
Hey guys, I already posted a thread in the wrong section (digitalmars.D instead of digitalmars.D.learn) - sorry for that. I'm looking for a solution to suspend/ interrupt threads which are sleeping. In the last few minutes I figured out some things I didn't understand exactly. I tested thread and fibers from the core.thread-package. My first test-code is the following: import std.stdio; import core.thread; a testInstance; class a { void writeTest() { writeln(test); } } void main(string[] args) { testInstance = new a(); Thread t = new Thread(threadFunc); t.start(); Thread.yield(); // give the thread a chance to call threadFunc() } void threadFunc() { writeln(testInstance is null); } The result is: true which means that testInstance of type a is null - but I already created a instance and if I write writeln(testInstance is null); after Thread.yield(); in the main, it says false which means testInstance is a valid instance of the class a. - Why does threadFunc() says true, when testInstance should be a valid instance of a? Next question: When I extend my threadFunc()... like the following: void threadFunc() { writeln(testInstance is null); Thread.sleep(milliseconds(10_000)); } ... is there any chance to interrupt the Thread.sleep-command or to suspend the thread? As I know, the join()-method does wait until the thread is finished, but does not interrupt the sleep()-command. I hope anyone can help and know how I can do this all. ... sorry for double posting in digitalmars.d! Thanks in advance!
Re: Threads fibers
Nrgyzer nrgy...@gmail.com wrote: The result is: true which means that testInstance of type a is null - but I already created a instance and if I write writeln(testInstance is null); after Thread.yield(); in the main, it says false which means testInstance is a valid instance of the class a. - Why does threadFunc() says true, when testInstance should be a valid instance of a? The default storage in D is in TLS, that is, changes in one thread will not be visible to others. If instead you mark your class a as 'shared class a', it works the way you'd expect it to. ... is there any chance to interrupt the Thread.sleep-command or to suspend the thread? As I know, the join()-method does wait until the thread is finished, but does not interrupt the sleep()-command. I think the best way to do this would be using std.concurrency, and passing it a message. Not sure, though. -- Simen
Re: Threads fibers
Nrgyzer nrgy...@gmail.com wrote: The result is: true which means that testInstance of type a is null - but I already created a instance and if I write writeln(testInstance is null); after Thread.yield(); in the main, it says false which means testInstance is a valid instance of the class a. - Why does threadFunc() says true, when testInstance should be a valid instance of a? The default storage in D is in TLS, that is, changes in one thread will not be visible to others. If instead you mark your class a as 'shared class a', it works the way you'd expect it to. ... is there any chance to interrupt the Thread.sleep-command or to suspend the thread? As I know, the join()-method does wait until the thread is finished, but does not interrupt the sleep()-command. I think the best way to do this would be using std.concurrency, and passing it a message. Not sure, though. Thanks, marking a as shared class works :)... I already used threads in D1 but there as I just know - since 2.030 I need shared- decleration.
Re: C# interop
Given that I explicitly stated my interest in to the metal programming, I dont think the .NET implementation will do me any good, no matter how perfectly implemented it is.
Re: pragma(msg,
On Sun, Jan 30, 2011 at 19:15, Ellery Newcomer ellery-newco...@utulsa.edu wrote: doh. when I did something like enum string s = tct!(int,int); it just took it and didn't complain that it didn't know the value at compile time. I think in this case, your asking for an enum string, er, 'propagates' the compile-time-ness to tct!(int,int). Everything is calculated at CT, because it's doable. But when you used pragma(msg, someString), the compiler doesn't know someString can be entirely known at CT. So, there is a difference between pragma(msg, tct!(T,U)); and enum string s = tct!(T,U)); pragma(msg, s); But if you indicate to the compiler you want the result of tct to be a CT string, everything is resolved. So the easiest way is to put enum string tct = ... inside the template. I spent some time building a CT regex engine in D. Having the match/replace results shown with pragma instructions is fun, but I had horrible moments trying to understand the interplay between compile-time et runtime... Philippe
Re: common types + type modifiers
On 2011-01-30 13:23:16 -0500, Ellery Newcomer ellery-newco...@utulsa.edu said: Ah, thank you. That clears things up a bit. how do you safely convert from a shared type to a non shared type or vice versa? a deep copy inside a critical section? A copy? Yes. A critical section? Not really. Shared is about atomic operations, things that the processor can do atomically without locking. They're used mostly to provide sequential consistency in our multi-core world where multiple cores can see a different value for the same memory address at one given time due to out-of-sync caches. Atomic ops forces the caches to synchronize but are slower. You can't mix atomic operations with OS-level locking because they don't respect each other (a lock won't prevent an atomic read from occurring). Also, atomic ops are generally limited to word-sized data structures, sometime double-word. So on a 32-bit processor that's a 32-bit or 64-bit value (assuming the value is properly aligned). If you want to use locking (a critical section in Windows parlance), you should put your data in a synchronized class, then share that class with other threads. If you don't use locking, you have at best double-word granularity. -- Michel Fortin michel.for...@michelf.com http://michelf.com/
Re: pragma(msg,
On 01/30/2011 03:47 PM, Philippe Sigaud wrote: On Sun, Jan 30, 2011 at 19:15, Ellery Newcomer ellery-newco...@utulsa.edu wrote: doh. when I did something like enum string s = tct!(int,int); it just took it and didn't complain that it didn't know the value at compile time. I think in this case, your asking for an enum string, er, 'propagates' the compile-time-ness to tct!(int,int). Everything is calculated at CT, because it's doable. But when you used pragma(msg, someString), the compiler doesn't know someString can be entirely known at CT. actually, it didn't calculate it and if I passed it to a function call, then it would complain that it didn't know the value
Re: Nested function declarations
dennis luehring dl.so...@gmx.net writes: They're useful for testing: unittest { int foo(); static assert (is(ReturnType!foo == int)); } and else? is it worth? Don't class function declarations have the same issue? You can declare but all you'll get is a link error. Unless there some way like C++ to provide a definition elsewhere. class C { int foo(); } void main() { } superarray_error.obj(superarray_error) Error 42: Symbol Undefined _D16superarray_error1C3fooMFZi --- errorlevel 1
Why does * cause my tiny regextester program to crash?
I wrote this little program to test for regular expression matches. I compiled it with in Windows with DMD 2.051 through Visual Studio 2010 with Visual D. It crashes if regexbuf is just the single character, *. Why? Shouldn't it match the entire string? Visual Studio's debug output is this: First-chance exception at 0x76fde124 in regextester.exe: 0xE0440001: 0xe0440001. The program '[5492] regextester.exe: Native' has exited with code 1 (0x1). Also, why does it match an unlimited number of times on $ instead of just once? Is this a Phobos-specific issue, or are regular expressions supposed to do that? I mean, it doesn't match an unlimited times on h, for example. Mind you, I'm very new to both regular expressions and D. I'm also not an experienced programmer of anything else. I've spent years dabbling in the surface various programming languages without learning anything meaty. I've learned syntax mostly. My debug build is here: http://lex.clansfx.co.uk/projects/regextester.exe Here's the source code: import std.stdio, std.regex; void main() { char[] regexbuf; char[] teststring; while(1) { write(test string: ); std.stdio.readln(teststring); teststring.length=teststring.length-1; while(teststring.length0) { uint i=0; write(regex input: ); std.stdio.readln(regexbuf); regexbuf.length=regexbuf.length-1; if(regexbuf.length0) foreach(m; match(teststring, regex(regexbuf))) { i++; writefln(Match number %s: %s[%s]%s,i,m.pre,m.hit,m.post); if(i = 50) { writefln(There have been %s matches. I'm breaking for safety.,i); break; } } } } }
Re: Why does * cause my tiny regextester program to crash?
Alex Folland Wrote: I wrote this little program to test for regular expression matches. I compiled it with in Windows with DMD 2.051 through Visual Studio 2010 with Visual D. It crashes if regexbuf is just the single character, *. Why? Shouldn't it match the entire string? While it would be best to give your example data. The regular expression for matching all data is .* * represents a repeating something of zero or more. . represents anything So * just makes no sense.
Re: Why does * cause my tiny regextester program to crash?
On Mon, 31 Jan 2011 03:57:44 +0200, Alex Folland lexlex...@gmail.com wrote: I wrote this little program to test for regular expression matches. I compiled it with in Windows with DMD 2.051 through Visual Studio 2010 with Visual D. It crashes if regexbuf is just the single character, *. Why? Shouldn't it match the entire string? * in regular expressions means 0 or more instances of the previous entity: http://www.regular-expressions.info/repeat.html It doesn't make sense at the start of an expression. .* is the regexp that matches anything[1]. std.regex probably can't handle invalid regexps very well. Note that std.regex is a new module that intends to replace the older std.regexp, but still has some problems. Also, why does it match an unlimited number of times on $ instead of just once? Looks like another std.regex bug. My debug build is here: http://lex.clansfx.co.uk/projects/regextester.exe A note for the future: compiled executables aren't very useful when source is available, especially considering many people here don't use Windows. [1]: A dot in a regular expression may not match newlines, depending on the implementation and search options. -- Best regards, Vladimirmailto:vladi...@thecybershadow.net
Re: Why does * cause my tiny regextester program to crash?
On 2011-01-30 21:47, Vladimir Panteleev wrote: On Mon, 31 Jan 2011 03:57:44 +0200, Alex Folland lexlex...@gmail.com wrote: I wrote this little program to test for regular expression matches. I compiled it with in Windows with DMD 2.051 through Visual Studio 2010 with Visual D. It crashes if regexbuf is just the single character, *. Why? Shouldn't it match the entire string? * in regular expressions means 0 or more instances of the previous entity: http://www.regular-expressions.info/repeat.html It doesn't make sense at the start of an expression. .* is the regexp that matches anything[1]. std.regex probably can't handle invalid regexps very well. Note that std.regex is a new module that intends to replace the older std.regexp, but still has some problems. Okay, so that particular regex is invalid. Yeah, it still shouldn't crash. You're right. How should I prevent my program from crashing without fixing std.regex (code I definitely don't trust myself to touch)? Would the Scope statement be useful? I still can't figure out exactly what it does. I tried using scope(exit)writeln(Bad regex.); just before my foreach loop, but it still crashes. I then tried changing exit to failure, but that didn't help either; same behavior. Am I using scope wrong? Also, why does it match an unlimited number of times on $ instead of just once? Looks like another std.regex bug. I thought it through and decided that it might not be std.regex' bug. I mean, there's no way m could have an unlimited number of elements for foreach to loop through, right? Actually, it probably is std.regex' bug. Though, all of this doesn't really matter since nobody uses just $ as a regex, since it'd match an obvious point in any input. I bet Andrei would still be irked by it if he knew though. My debug build is here: http://lex.clansfx.co.uk/projects/regextester.exe A note for the future: compiled executables aren't very useful when source is available, especially considering many people here don't use Windows. Right. [1]: A dot in a regular expression may not match newlines, depending on the implementation and search options. Thanks for the extra info. :)
Re: StoppingPolicy.longest never stop?
bearophile wrote: tsukikage: Why this zip not stop after iterated through the longest range? thanks! http://d.puremagic.com/issues/show_bug.cgi?id=5506 Bye, bearophile Thank you!