Re: opIndex vs. opSlice for empty slices
On Friday, 22 May 2015 at 06:28:16 UTC, Ali Çehreli wrote: http://ddili.org/ders/d.en/operator_overloading.html#ix_operator_overloading.opSlice http://ddili.org/ders/d.en/templates_more.html#ix_templates_more.overloading,%20operator Ali Thanks, Ali. I've actually looked over that already and it's part of the reason I postes the question in the first place. The docs indicate that opIndex can be used for the empty slice on a single-dimensional array, but your text says it's used for multi-dimensional slicing and opSlice is for the empty, single dimensional form. Then, in the thread linked above, H.S. Teoh's post seems to side with the docs. Additionally, there's the bit in the docs that I missed earlier about the non-templated opSlice being kept for backward compatibility, but the DoubleEndedQueue in your first link use the non-templated opSlice rather than the templated form that H.S. Teoh's post demonstrated. I've only recently started digging into the different sources of documentation on D to understand the why of things and, in the process, have managed to confuse myself about topics I thought I understood. I'm finding it hard to discern which source is correct and why.
Re: opIndex vs. opSlice for empty slices
On Friday, 22 May 2015 at 05:49:17 UTC, weaselcat wrote: http://forum.dlang.org/thread/luadir$t0g$1...@digitalmars.com#post-luadir:24t0g:241:40digitalmars.com Thanks!
Re: opIndex vs. opSlice for empty slices
On 05/21/2015 11:20 PM, Mike Parker wrote: On Friday, 22 May 2015 at 05:49:17 UTC, weaselcat wrote: http://forum.dlang.org/thread/luadir$t0g$1...@digitalmars.com#post-luadir:24t0g:241:40digitalmars.com Thanks! For convenience, here are the two sections that cover my understanding of this topic: http://ddili.org/ders/d.en/operator_overloading.html#ix_operator_overloading.opSlice http://ddili.org/ders/d.en/templates_more.html#ix_templates_more.overloading,%20operator Ali
Re: ImplicitConversionTargets opposite
On Thursday, 21 May 2015 at 21:49:55 UTC, Freddy wrote: std.traits has ImplicitConversionTargets. Is there any template that returns the types that can implicty convert to T? I doubt that, because it's an open set (alias this, inheritance). However, it should be possible to iterate over all types in a given module, and select only those that are implicitly convertible to your type.
What happens when you launch a D application ?
On SO[1] I got next answer: What happens when you launch a D application ? The entry point is a C main inside the runtime, which initialize it (the runtime), including module constructor, run the unittest (if you've compiled with -unittest), then call your main (which name is _Dmain - useful to know if you want to set a breakpoint with GDB). When Vibe.d's main is called, it parses command line argument, an optional config file, and finally, starts the event loop. Any code that wish to run once the event loop has started should use runTask and similar, or createTimer. They should not call the code directly from the static constructor (it's actually one of the most common mistake when starting with Vibe.d). Could you explain what mean C main inside the runtime. I thought that is only one main is possible. And why it's named *С* main D is not C-translated language. Same question is about _Dmain -- what is it? If I will call this() before main? What it will be? Will it run before main? [1] http://stackoverflow.com/questions/30302161/cant-connect-to-mysql-mariadb-database-from-vibed-app
Re: Distinguish recursive Templates
Timon Gehr wrote: template getDepth(T){ static if(is(T==Set!S,S)) enum getDepth=1+getDepth!S; else enum getDepth=0; } Thx. Seems that I have to relearn a lot. -manfred
Re: Distinguish recursive Templates
Matt Kline wrote: isn't making any use of the template argument T Correct. I do not know how to use `T' to determine the recursion depth of the template---and I want no further parameter. -manfred
Re: Distinguish recursive Templates
On 05/23/2015 12:12 AM, Manfred Nowak wrote: Matt Kline wrote: isn't making any use of the template argument T Correct. I do not know how to use `T' to determine the recursion depth of the template---and I want no further parameter. -manfred import std.stdio, std.range, std.algorithm; template getDepth(T){ static if(is(T==Set!S,S)) enum getDepth=1+getDepth!S; else enum getDepth=0; } class Set(T){ override string toString(){ enum r=Set.repeat.take(getDepth!Set).join; return r; } } void main(){ auto s0=new Set!uint; writeln(s0); // writes Set auto s1=new Set!(Set!uint); writeln(s1); // writes SetSet }
Distinguish recursive Templates
How can one determine the recursion depth for templated types? Example code: import std.stdio; class Set(T){ override string toString(){ return Set; } } void main(){ auto s0= new Set!uint; writeln( s0); // writes Set auto s1= new Set!(Set!uint); writeln( s1); // should write SetSet }
Re: Python's features, which requires D
On Friday, 22 May 2015 at 05:31:38 UTC, Ali Çehreli wrote: Here is my attempt: import std.stdio; import std.algorithm; import std.conv; import std.range; void main() { // Replace 'none' with 'all' to activate. version (none) { const n = 5; auto a = stdin .byLine .map!(l = l.splitter.map!(to!int).array) .take(n); writeln(a); writeln(-); } { const n = 6; auto a = iota(n) .map!(i = chain([2].replicate(i), [1], [0].replicate(n - i - 1))); writefln(%(%(%s %)\n%), a); writeln(-); } { const x = [ 1, 2, 3, 4, 5, 6 ]; writeln(x.stride(2)); writeln(x.dropOne.stride(2)); writeln(-); } { // The internet does not need another fizz buzz. :p } } Ali Yes, it looks pretty good :) Thanks. But... It seems to me that D lacks features that allow you to write function readln/readln.strip/readln.split just inside the lambda. stdin.byLine is a good feature, but it captures the entire input stream, which I should handle all or take lambdas function take(n) n lines and then still handle all of these lines right away. Ie stdin.byline used everywhere is not always convenient! For example, the code in Python looks quite natural: a = [[int(j) for j in input().split()] for i in range(n)] About D-code, I can not say: auto a = stdin .byLine .map!(l = l.splitter.map!(to!int).array) .take(n); I can call the map with the existing array: import std.stdio, std.algorithm, std.conv, std.array; void main() { auto a = [1, 2, 3]; auto b = a.map!(c = c ~ readln.split.map!(to!int).array).array; writeln(b); } - http://rextester.com/MBUMHI13858 But I can not call the readln n times without a map: import std.stdio, std.conv, std.algorithm, std.array, std.range; void main() { auto a = [1, 2, 3]; auto b = [readln.split.map!(to!int).array].take(3); writeln(b); } - http://rextester.com/KCJ9346 Ie readln function cycle is needed for, another lambda or revised map! Is there a function in Phobos?
Re: Python's features, which requires D
By the way, Python has deepDup :) http://rextester.com/KBFA82886
is expression with static if matches wrong type
[code] import std.stdio; public void setMarker(M)( size_t markerIndex, M markerValue ) if( is(M: ulong) || is(M: long) || is(M: uint) || is(M: int) || is(M: ushort) || is(M: short) || is(M: ubyte) || is(M: byte) || is(M: char) || is(bool) || is(M: double) || is(M: float) ) { static if( is(M: ulong) || is(M: long) ){ std.stdio.writeln(Here 1: , typeid(M)); } else static if( is(M: uint) || is(M: int) ){ std.stdio.writeln(Here 2: , typeid(M)); } else static if( is(M: ushort) || is(M: short) ){ std.stdio.writeln(Here 3: , typeid(M)); } else static if( is(M: ubyte) || is(M: byte) || is(M: char) || is(bool) ){ std.stdio.writeln(Here 4: , typeid(M)); } } void main() { setMarker( 0, cast(ubyte)5 ); } [/code] Result: Here 1: ubyte Compiler and OS: DMD 2.067.1 on Ubuntu 14.04 64-bit Expected: Here 4: ubyte is(my expectation) wrong?
Re: is expression with static if matches wrong type
On Saturday, 23 May 2015 at 04:35:55 UTC, tcak wrote: [code] import std.stdio; public void setMarker(M)( size_t markerIndex, M markerValue ) if( is(M: ulong) || is(M: long) || is(M: uint) || is(M: int) || is(M: ushort) || is(M: short) || is(M: ubyte) || is(M: byte) || is(M: char) || is(bool) || is(M: double) || is(M: float) ) { static if( is(M: ulong) || is(M: long) ){ std.stdio.writeln(Here 1: , typeid(M)); } else static if( is(M: uint) || is(M: int) ){ std.stdio.writeln(Here 2: , typeid(M)); } else static if( is(M: ushort) || is(M: short) ){ std.stdio.writeln(Here 3: , typeid(M)); } else static if( is(M: ubyte) || is(M: byte) || is(M: char) || is(bool) ){ std.stdio.writeln(Here 4: , typeid(M)); } } void main() { setMarker( 0, cast(ubyte)5 ); } [/code] Result: Here 1: ubyte Compiler and OS: DMD 2.067.1 on Ubuntu 14.04 64-bit Expected: Here 4: ubyte is(my expectation) wrong? I have found my mistake. is(M: type) matches if M can be converted to type automatically. Thus, it matches to ulong. I changed it to is(M == type) and it works correctly now.
Re: Distinguish recursive Templates
On Friday, 22 May 2015 at 21:13:50 UTC, Manfred Nowak wrote: How can one determine the recursion depth for templated types? Example code: import std.stdio; class Set(T){ override string toString(){ return Set; } } void main(){ auto s0= new Set!uint; writeln( s0); // writes Set auto s1= new Set!(Set!uint); writeln( s1); // should write SetSet } Why should the last line write SetSet? (It doesn't.) toString isn't making any use of the template argument T, so no recursion occurs.
Re: ImplicitConversionTargets opposite
On Thursday, 21 May 2015 at 21:49:55 UTC, Freddy wrote: std.traits has ImplicitConversionTargets. Is there any template that returns the types that can implicty convert to T? You can write something like this: --- import std.stdio; import std.traits; import std.typetuple; void main(string[] args) { template PossibleType(From) { private alias All = TypeTuple!(byte,ubyte,short,ushort,int,uint); private alias Convertible(To) = isImplicitlyConvertible!(From,To); public alias PossibleType = Filter!(Convertible, All); } writeln((PossibleType!int).stringof); writeln((PossibleType!short).stringof); } --- which outputs: --- (int, uint) (short, ushort, int, uint) --- The idea is to reduce the list of all the possible types. It's easy because the high-order functional functions are implemented for the type list. `All` could be a template parameter, and more filled, but to keep the example simple here it's not.
std.parallelism and multidimensional arrays
I have a code which does a lot of work on 2D/3D arrays, for which I use the 2.066 multidimensional slicing syntax through a fork of the Unstandard package [1]. Many times the order of operations doesn't matter and I thought I would give the parallelism module a try to try and get some easy speedups (I also use MPI, but that has some additional overhead). The way I currently have my foreach loops set up, p is a size_t[2], the payload of the array v is double[9] and the array is indexed directly with a size_t[2] array and all works fine: foreach(immutable p, ref v, arr) { double[9] stuff; arr[p] = stuff; } If I naively try foreach(immutable p, ref v, parallel(arr)) { ... } I first get errors of the type Error: foreach: cannot make v ref. I do not understand where that particular problem comes from, but I can possibly live without the ref, so I went for foreach(immutable p, v, parallel(arr)) { ... } Which gets me Error: no [] operator overload for type (complicated templated type of some wrapper struct I have for arr). I'm guessing it doesn't like that there is no such thing as a simple one-dimensional slicing operation for a multidimensional array? Should I define an opSlice function that takes the usual two size_t arguments for the upper and lower bounds and doesn't require a dimension template argument and somehow map this to my underlying two-dimensional array? Will it need an opIndex function that takes only takes a single size_t as well? Or is this just taking the simple parallel(...) too far and should I try to put something together myself using lower-level constructs? Any hints would be appreciated! [1] http://code.dlang.org/packages/unstandard
Re: What happens when you launch a D application ?
On Friday, 22 May 2015 at 06:36:27 UTC, Suliman wrote: On SO[1] I got next answer: What happens when you launch a D application ? The entry point is a C main inside the runtime, which initialize it (the runtime), including module constructor, run the unittest (if you've compiled with -unittest), then call your main (which name is _Dmain - useful to know if you want to set a breakpoint with GDB). When Vibe.d's main is called, it parses command line argument, an optional config file, and finally, starts the event loop. Any code that wish to run once the event loop has started should use runTask and similar, or createTimer. They should not call the code directly from the static constructor (it's actually one of the most common mistake when starting with Vibe.d). Could you explain what mean C main inside the runtime. I thought that is only one main is possible. And why it's named *С* main D is not C-translated language. Same question is about _Dmain -- what is it? If I will call this() before main? What it will be? Will it run before main? [1] http://stackoverflow.com/questions/30302161/cant-connect-to-mysql-mariadb-database-from-vibed-app Glossing over a lot of the detail: _Dmain is the symbol that is generated in the object file for the function called main in your source code. A symbol main is also generated, which is where the OS starts the whole program*. It sets up the runtime and module constructors etc. and then calls _Dmain. *It's sometimes called the C main because it's equivalent to main in C.
Re: What happens when you launch a D application ?
On Friday, 22 May 2015 at 11:13:43 UTC, Suliman wrote: Am I right understand that that: 1. every App start from main() 2. Dmain is function that run after main is started and it's run GC, unit-tests and so on? Not really, it depends what you mean by main, the function called main that you write in your source code or the function called main that ends up in the actual program? Here's an example: int a; static this() { a = 4; } void main() // { a = 5; } Conceptually speaking, the compiler turns this in to: int a; extern(C) int main() { a = 4; return _Dmain(); } int _Dmain() { a = 5; return 0; } I think that in practice it's more like this: int a; void staticConstructor1() { a = 4; } //actually in druntime int _d_run_main(int function() sourceCodeMain) { staticConstructor1(); // and all the other necessary setup // for the program and runtime sourceCodeMain(); } extern(C) int main() { _d_run_main(_Dmain); } int _Dmain { a = 5; return 0; }
Re: What happens when you launch a D application ?
On Friday, 22 May 2015 at 11:51:01 UTC, John Colvin wrote: On Friday, 22 May 2015 at 11:13:43 UTC, Suliman wrote: Am I right understand that that: 1. every App start from main() 2. Dmain is function that run after main is started and it's run GC, unit-tests and so on? Not really, it depends what you mean by main, the function called main that you write in your source code or the function called main that ends up in the actual program? Here's an example: int a; static this() { a = 4; } void main() // { a = 5; } Conceptually speaking, the compiler turns this in to: int a; extern(C) int main() { a = 4; return _Dmain(); } int _Dmain() { a = 5; return 0; } I think that in practice it's more like this: int a; void staticConstructor1() { a = 4; } //actually in druntime int _d_run_main(int function() sourceCodeMain) { staticConstructor1(); // and all the other necessary setup // for the program and runtime sourceCodeMain(); } extern(C) int main() { _d_run_main(_Dmain); } int _Dmain { a = 5; return 0; } Give or take a few return statements, not that it matters.
Re: ImplicitConversionTargets opposite
the line warping on this forum deserve a big slap in the face...
Re: What happens when you launch a D application ?
Am I right understand that that: 1. every App start from main() 2. Dmain is function that run after main is started and it's run GC, unit-tests and so on?
Re: What happens when you launch a D application ?
Really hard to understand... So what what would call at first ? extern(C) int main() or int _Dmain()
Re: What happens when you launch a D application ?
On Friday, 22 May 2015 at 13:26:32 UTC, Suliman wrote: So what what would call at first ? The operating system starts the program Then the extern(C) int main() gets called. Then the _Dmain gets called.
What is a mutable method?
I know there is mutable variables, but what is a mutable method? Error message says mutable method project.mariadb.connector.ver2p1.resultset.ResultSetColumn.info is not callable using a const object.
Re: What is a mutable method?
On Friday, 22 May 2015 at 12:12:46 UTC, tcak wrote: I know there is mutable variables, but what is a mutable method? Error message says mutable method project.mariadb.connector.ver2p1.resultset.ResultSetColumn.info is not callable using a const object. The method that can change the state of the object. This kind of error says that you try to call non-const method on const object, which is prohibited. You can call only the methods marked as const on the const object.
Re: What is a mutable method?
On 5/22/15 8:12 AM, tcak wrote: I know there is mutable variables, but what is a mutable method? Error message says mutable method project.mariadb.connector.ver2p1.resultset.ResultSetColumn.info is not callable using a const object. English grammar nit: I would say mutating instead of mutable. -Steve
Re: What is a mutable method?
On Friday, May 22, 2015 12:12:45 tcak via Digitalmars-d-learn wrote: I know there is mutable variables, but what is a mutable method? Error message says mutable method project.mariadb.connector.ver2p1.resultset.ResultSetColumn.info is not callable using a const object. It's a method / member function which is not const or immutable. e.g. struct MyStruct { void foo() {} // This method is mutable void bar() const {} void baz() immutable {} } What it's really indicating is the constness of the object's this point is inside the function. So, you can only call a mutable method on a mutable object, because otherwise, you'd be converting a const or immutable reference or pointer to mutable, which would violate the type system. mutable and immutable can be converted to const, but mutable and const can't be converted to immutable, and const and immutable can't be converted to mutable. So, in your case, it sounds like you're trying to call a mutable method on a const object, which won't work. The method needs to be const, since the object is const. - Jonathan M Davis
Re: opIndex vs. opSlice for empty slices
On 5/22/15 1:49 AM, weaselcat wrote: On Friday, 22 May 2015 at 05:47:28 UTC, Mike Parker wrote: I've always used opSlice to produce empty slices, but having recently read the documentation at [1], I see this: To overload a[], simply define opIndex with no parameters: And no mention that opSlice can fill the same role. Am I right to infer that we should prefer opIndex over opSlice for this? If so, what's the rationale? [1] http://dlang.org/operatoroverloading.html#slice http://forum.dlang.org/thread/luadir$t0g$1...@digitalmars.com#post-luadir:24t0g:241:40digitalmars.com This is news to me, and very cool! -Steve
Re: What happens when you launch a D application ?
On 5/22/15 2:36 AM, Suliman wrote: On SO[1] I got next answer: What happens when you launch a D application ? The entry point is a C main inside the runtime, which initialize it (the runtime), including module constructor, run the unittest (if you've compiled with -unittest), then call your main (which name is _Dmain - useful to know if you want to set a breakpoint with GDB). When Vibe.d's main is called, it parses command line argument, an optional config file, and finally, starts the event loop. Any code that wish to run once the event loop has started should use runTask and similar, or createTimer. They should not call the code directly from the static constructor (it's actually one of the most common mistake when starting with Vibe.d). Could you explain what mean C main inside the runtime. I thought that is only one main is possible. And why it's named *С* main D is not C-translated language. Same question is about _Dmain -- what is it? If I will call this() before main? What it will be? Will it run before main? Druntime defines the main function that C runtime will call. C runtime intializes its own things (e.g. stdin/stdout/stderr) and then calls C main. Druntime's version of C main then intializes all things that D needs (and there's quite a bit) not in any specific order: - set up the main thread - initialize the GC - detect module constructor/destructor cycles - run module constructors (both shared and thread-local) - run unittests if enabled - run your main function (defined in D as 'main') After main exits, it unwinds all this stuff. FYI, I didn't realize this (but just figured it out), C main *used* to be in druntime, but it's now generated by the compiler. See here: https://github.com/D-Programming-Language/dmd/blob/master/src/mars.c#L236 The function it calls (_d_run_main) is now the entry point into druntime, and it is here: https://github.com/D-Programming-Language/druntime/blob/master/src/rt/dmain2.d#L290 It can give you more clues as to how it works. -Steve
Re: std.parallelism and multidimensional arrays
On Friday, 22 May 2015 at 10:54:36 UTC, Stefan Frijters wrote: I have a code which does a lot of work on 2D/3D arrays, for which I use the 2.066 multidimensional slicing syntax through a fork of the Unstandard package [1]. Many times the order of operations doesn't matter and I thought I would give the parallelism module a try to try and get some easy speedups (I also use MPI, but that has some additional overhead). The way I currently have my foreach loops set up, p is a size_t[2], the payload of the array v is double[9] and the array is indexed directly with a size_t[2] array and all works fine: foreach(immutable p, ref v, arr) { double[9] stuff; arr[p] = stuff; } If I naively try foreach(immutable p, ref v, parallel(arr)) { ... } I first get errors of the type Error: foreach: cannot make v ref. I do not understand where that particular problem comes from, but I can possibly live without the ref, so I went for foreach(immutable p, v, parallel(arr)) { ... } Which gets me Error: no [] operator overload for type (complicated templated type of some wrapper struct I have for arr). I'm guessing it doesn't like that there is no such thing as a simple one-dimensional slicing operation for a multidimensional array? Should I define an opSlice function that takes the usual two size_t arguments for the upper and lower bounds and doesn't require a dimension template argument and somehow map this to my underlying two-dimensional array? Will it need an opIndex function that takes only takes a single size_t as well? Or is this just taking the simple parallel(...) too far and should I try to put something together myself using lower-level constructs? Any hints would be appreciated! [1] http://code.dlang.org/packages/unstandard I'd define a flatten range adaptor that presents n-dimensional ranges as 1d range that traverses the original array indices lexicographically, if that makes sense for your app.
Differences between C++11 atomics and core.atomics?
There's very little writing about D's core.atomics(TDPL seems to very barely cover them, I assume it's because it's aging compared to the library.) Is it safe to assume that they behave similarly to C++11's atomics?