Re: How would you dive into a big codebase
On Wednesday, 22 October 2014 at 01:21:19 UTC, Freddy wrote: Is there any advice/tips for reading medium/big D codebases? Somewhat D specific: I would consider an IDE/editor like Eclipse with DDT that can give an outline of the data structures & functions names in a source file to make the files easier to digest.
Re: How would you dive into a big codebase
On Wednesday, 22 October 2014 at 01:21:19 UTC, Freddy wrote: Is there any advice/tips for reading medium/big D codebases? I'm not aware of there being anything about D which makes it any different to dig into a large codebase than it is for any other language. The usual suggestion that I've heard given (without any particular language in mind) is to start by tracking down and fixing a bug in it, though if the codebase isn't too large for it, personally, I'd be highly tempted to just read through it all. I don't think very many people function that way though.
Re: Sorting array of string arrays by an element in the string array
On Wednesday, 22 October 2014 at 01:58:19 UTC, neal wrote: On Wednesday, 22 October 2014 at 01:02:17 UTC, bearophile wrote: neal: Anybody have any suggestions? Something like this, perhaps? data.sort!q{ a[4] > b[4] }; Bye, bearophile Hmmm.. Im getting some interesting results here. So when i put all of the populations in an area and used this code: sort!("a>b")(population); writeln("Top 5 population in 1993: "); for(int i = 0; i < 5;i++) writeln(population[i]); I get: Top 5 population in 1993: 1189550675 916529257 264493898 191658591 156810428 The problem there is that i lose the country that the population is associated with (the top one being China). When I run your code: data.sort!q{ a[4] > b[4] }; for(int i = 0; i < 5; i++) writeln(data[][]); I get this result for the top 5: ["Portugal", "1593140", "-668536", "1993", "9993683", "8"] ["Anguilla", "114", "-536", "1993", "9865", "10"] ["Malawi", "31745", "-25289", "1993", "9862531", "6"] ["Grenada", "2274", "-4890", "1993", "96908", "2"] ["Burkina Faso", "22613", "-38738", "1993", "9688261", "6"] Notice that the top populations arent correct. Wow sorry I just realized my mistake. the array is an array of strings! data.sort!q{ to!int(a[4]) > to!int(b[4]) }; This code fixes my problem! Thanks for the quick responses guys. you rock!
Re: bitmanip bigEndianToNative using a buffer slice?
On Wednesday, October 22, 2014 00:45:17 Lucas Burson via Digitalmars-d-learn wrote: I'm trying to create a primitive type given a specific buffer slice. I can place the uint into a sliced buffer but I'm getting compiler errors when using a slice to create the uint. Still new to Dlang and unfamiliar with the template system. How do I get this working? import std.bitmanip; int main() { size_t offset = 3; ubyte[10] buffer; buffer[offset..offset+4] = nativeToBigEndian!uint(cast(uint) 104387); // compiler error uint fromBuf = bigEndianToNative!uint(buffer[offset..offset+4]); return 0; } The compiler error: ./test.d(11): Error: template std.bitmanip.bigEndianToNative does not match any function template declaration. Candidates are: /usr/include/dmd/phobos/std/bitmanip.d(1689): std.bitmanip.bigEndianToNative(T, ulong n)(ubyte[n] val) if (canSwapEndianness!(T) && n == T.sizeof) ./test.d(11): Error: template std.bitmanip.bigEndianToNative(T, ulong n)(ubyte[n] val) if (canSwapEndianness!(T) && n == T.sizeof) cannot deduce template function from argument types !(uint)(ubyte[]) ./test.d(11): Error: template instance bigEndianToNative!(uint) errors instantiating template You can't just use a dynamic array as a static array, because they're distinct types. Slicing a static array gives you a dynamic one which refers to the static array's memory, but to convert a dynamic array to a static one, you have to cast it (which will do a copy). So, your code becomes something like import std.bitmanip; void main() { size_t offset = 3; ubyte[10] buffer; buffer[offset..offset+4] = nativeToBigEndian!uint(104387); // compiler error auto fromBuf = bigEndianToNative!uint(cast(ubyte[4])buffer[offset..offset+4]); } However, in this case, you should probably just not use a dynamic array. It buys you nothing. You might as well just do. import std.bitmanip; void main() { auto buffer = nativeToBigEndian!uint(104387); // compiler error auto fromBuf = bigEndianToNative!uint(buffer); } though obviously, your actual code may be doing something more complicated that actually makes using the dynamic array reasonable. Regardless, dynamic arrays must be cast to static arrays if you want to use a dynamic array where a static array is required. - Jonathan M Davis
Re: Sorting array of string arrays by an element in the string array
On Wednesday, 22 October 2014 at 01:02:17 UTC, bearophile wrote: neal: Anybody have any suggestions? Something like this, perhaps? data.sort!q{ a[4] > b[4] }; Bye, bearophile Hmmm.. Im getting some interesting results here. So when i put all of the populations in an area and used this code: sort!("a>b")(population); writeln("Top 5 population in 1993: "); for(int i = 0; i < 5;i++) writeln(population[i]); I get: Top 5 population in 1993: 1189550675 916529257 264493898 191658591 156810428 The problem there is that i lose the country that the population is associated with (the top one being China). When I run your code: data.sort!q{ a[4] > b[4] }; for(int i = 0; i < 5; i++) writeln(data[][]); I get this result for the top 5: ["Portugal", "1593140", "-668536", "1993", "9993683", "8"] ["Anguilla", "114", "-536", "1993", "9865", "10"] ["Malawi", "31745", "-25289", "1993", "9862531", "6"] ["Grenada", "2274", "-4890", "1993", "96908", "2"] ["Burkina Faso", "22613", "-38738", "1993", "9688261", "6"] Notice that the top populations arent correct.
Re: bitmanip bigEndianToNative using a buffer slice?
On Wed, 22 Oct 2014 01:30:48 + Lucas Burson via Digitalmars-d-learn wrote: > ketmar, we meet again! Your explanation is great and that solved > my problem. Thank you. Maybe I'll try out templates next... yep, i remember. i was glad to help you. ;-) signature.asc Description: PGP signature
Re: bitmanip bigEndianToNative using a buffer slice?
On Wednesday, 22 October 2014 at 01:08:52 UTC, ketmar via Digitalmars-d-learn wrote: the short answer: uint fromBuf = bigEndianToNative!uint(cast(ubyte[4])buffer[offset..offset+4]); ketmar, we meet again! Your explanation is great and that solved my problem. Thank you. Maybe I'll try out templates next...
How would you dive into a big codebase
Is there any advice/tips for reading medium/big D codebases?
Re: bitmanip bigEndianToNative using a buffer slice?
On Wed, 22 Oct 2014 00:45:17 + Lucas Burson via Digitalmars-d-learn wrote: > I'm trying to create a primitive type given a specific buffer > slice. I can place the uint into a sliced buffer but I'm getting > compiler errors when using a slice to create the uint. Still new > to Dlang and unfamiliar with the template system. > > How do I get this working? the short answer: uint fromBuf = bigEndianToNative!uint(cast(ubyte[4])buffer[offset..offset+4]); the long answer: you are passing dynamic array, and function is expecting static array. slices are always dynamic by nature, so they must be casted to static arrays before passing to bigEndianToNative. 'ubyte[4]' is not a "recomendation", it means "only static arrays will do". dynamic arrays are pointer to hidden "array structure" generated by compiler, which looks like this: `struct { size_t length; void* ptr; }`. but static arrays are just direct pointers to data, there is no need to keep separate length, as it is known in compile time. by casting dynamic array/slice to static array you are telling the compiler "i know the length of the data right now, so you can use direct pointer". sorry if my explanations appears complex and hard to understand -- that's 'cause i'm not very good in teaching. but at least i tried and gave the short answer too. ;-) signature.asc Description: PGP signature
Re: Sorting array of string arrays by an element in the string array
neal: Anybody have any suggestions? Something like this, perhaps? data.sort!q{ a[4] > b[4] }; Bye, bearophile
bitmanip bigEndianToNative using a buffer slice?
I'm trying to create a primitive type given a specific buffer slice. I can place the uint into a sliced buffer but I'm getting compiler errors when using a slice to create the uint. Still new to Dlang and unfamiliar with the template system. How do I get this working? import std.bitmanip; int main() { size_t offset = 3; ubyte[10] buffer; buffer[offset..offset+4] = nativeToBigEndian!uint(cast(uint) 104387); // compiler error uint fromBuf = bigEndianToNative!uint(buffer[offset..offset+4]); return 0; } The compiler error: ./test.d(11): Error: template std.bitmanip.bigEndianToNative does not match any function template declaration. Candidates are: /usr/include/dmd/phobos/std/bitmanip.d(1689): std.bitmanip.bigEndianToNative(T, ulong n)(ubyte[n] val) if (canSwapEndianness!(T) && n == T.sizeof) ./test.d(11): Error: template std.bitmanip.bigEndianToNative(T, ulong n)(ubyte[n] val) if (canSwapEndianness!(T) && n == T.sizeof) cannot deduce template function from argument types !(uint)(ubyte[]) ./test.d(11): Error: template instance bigEndianToNative!(uint) errors instantiating template
Re: Sorting array of string arrays by an element in the string array
On Wednesday, 22 October 2014 at 00:32:56 UTC, neal wrote: Just curious if this is possible. I have some data on different countries that i have stored in a multidimensional array called data[][]. What I want to do is sort data[][] by population which happens to be stored in data[i][4] where i is the index to iterate from country to country. I would like something like this: sort!("a>b")(data[][4]); Anybody have any suggestions? More like this: sort!(a.country > b.country)(data[][4]);
Re: Sorting array of string arrays by an element in the string array
On Wednesday, 22 October 2014 at 00:36:22 UTC, Joel wrote: On Wednesday, 22 October 2014 at 00:32:56 UTC, neal wrote: Just curious if this is possible. I have some data on different countries that i have stored in a multidimensional array called data[][]. What I want to do is sort data[][] by population which happens to be stored in data[i][4] where i is the index to iterate from country to country. I would like something like this: sort!("a>b")(data[][4]); Anybody have any suggestions? More like this: sort!(a.country > b.country)(data[][4]); Oops, forgot the quotes: sort!("a.country > b.country")(data[][4]);
Sorting array of string arrays by an element in the string array
Just curious if this is possible. I have some data on different countries that i have stored in a multidimensional array called data[][]. What I want to do is sort data[][] by population which happens to be stored in data[i][4] where i is the index to iterate from country to country. I would like something like this: sort!("a>b")(data[][4]); Anybody have any suggestions?
Re: Beginner ?. Why does D suggest to learn java
On Wed, 22 Oct 2014 00:57:59 +0300 ketmar via Digitalmars-d-learn wrote: > consitions i don't even know what this word means. honestly. signature.asc Description: PGP signature
Re: Issue with WIKI example Win32_DLLs_in_D
Funnily enough I was just playing with this last night trying to get Excel to talk to dlang DLL. I borrowed a C example elsewhere on web and used a different .def file. Something like this: LIBRARY dprop DESCRIPTION 'My DLL written in D' EXETYPE NT CODEPRELOAD DISCARDABLE DATAWRITE EXPORTS useArray = useArray usemyTest = usemyTest Where the two functions exported from D to excel were as above. Try that and let me know. If that doesn't work I will send you a link to from repository. Tuesday, 21 October 2014 at 19:59:51 UTC, Andre wrote: Hi, by copy and paste the example from http://wiki.dlang.org/Win32_DLLs_in_D exactly as described, the following errors is thrown: J:\Projects\Tests\Example>dmd test mydll.lib -g OPTLINK (R) for Win32 Release 8.00.15 test.obj(test) Error 42: Symbol Undefined _D5mydll12__ModuleInfoZ I created a batch program which executes the commands as described on the wiki page: dmd -c mydll -g dmd mydll.obj mydll.def -g -L/map C:\D\dmd2\windows\bin\implib /noi /system mydll.lib mydll.dll dmd test mydll.lib -g Could you check whats wrong with the example and maybe update the WIKI page? Thanks. Kind regards André
Re: Beginner ?. Why does D suggest to learn java
On Tue, 21 Oct 2014 21:48:14 + Kapps via Digitalmars-d-learn wrote: > I like D's templates, it's one of the things that makes me like D > more than C#. But they can definitely get quite complex. C# > limits templates to generic types, like List, and for a > beginner I think that's an easier way to handle things. nobody forcec any beginner to write complex templates from the start. not even use them: alot of things can be done without phobos, as excercises. yet i think that it's good to start with the language that has powerful templates and metaprogramming, so more and more advanced features can be gently introduced when there is time. we can't (ok, we can, but this is very-very hard) teach people how to use metaprogramming properly if their language of choice aren't supporting metaprogramming at all, for example. start from using templates as generics, then add some sugar, some type consitions, some CTFE and so on. with C# we will stop right after "generics", 'cause there is no other things there. signature.asc Description: PGP signature
Re: Making plugin system with shared libraries. Upcast in shared lib
On Tuesday, 21 October 2014 at 13:57:23 UTC, Kagamin wrote: On Monday, 20 October 2014 at 15:07:43 UTC, MrSmith wrote: On Monday, 20 October 2014 at 14:05:29 UTC, Kagamin wrote: Do it the COM way: publish IModule2 interface and declare GetInterface method, which will return a prepared pointer, which you would reinterpret cast to IModule2. Will it work on linux with simple .so libs? I want it to be as simple as possible. Is it any different from what you already have? Dynamic cast is not guaranteed to work across dll boundary. If it does, you're lucky. If it doesn't, use GetInterface - that will work independently from runtime, environment, language, os, hardware etc. What is GetInterface?
Re: Beginner ?. Why does D suggest to learn java
On Tuesday, 21 October 2014 at 09:14:08 UTC, ketmar via Digitalmars-d-learn wrote: On Tue, 21 Oct 2014 09:01:32 + Kapps via Digitalmars-d-learn wrote: no template magic that's very bad. it's time to stop making people think that templates are inevitably arcane. I like D's templates, it's one of the things that makes me like D more than C#. But they can definitely get quite complex. C# limits templates to generic types, like List, and for a beginner I think that's an easier way to handle things.
Issue with WIKI example Win32_DLLs_in_D
Hi, by copy and paste the example from http://wiki.dlang.org/Win32_DLLs_in_D exactly as described, the following errors is thrown: J:\Projects\Tests\Example>dmd test mydll.lib -g OPTLINK (R) for Win32 Release 8.00.15 test.obj(test) Error 42: Symbol Undefined _D5mydll12__ModuleInfoZ I created a batch program which executes the commands as described on the wiki page: dmd -c mydll -g dmd mydll.obj mydll.def -g -L/map C:\D\dmd2\windows\bin\implib /noi /system mydll.lib mydll.dll dmd test mydll.lib -g Could you check whats wrong with the example and maybe update the WIKI page? Thanks. Kind regards André
Re: Global const variables
On Tuesday, 21 October 2014 at 17:00:49 UTC, Meta wrote: There is no such thing as global scope in D. While that's technically true (and very good for avoiding symbol conflicts), modules at the module level are still typically referred to as global variables.
Re: Global const variables
On Tuesday, 21 October 2014 at 15:51:27 UTC, MachineCode wrote: pure functions are also supposed to don't use global variables at all, according to functional programming paradigm The functional programming paradigm is kind of irrelevant to D's pure, which should really be something more like @global. D's pure makes it so that a function cannot directly access global, mutable state - i.e. no mutable global or static variables which can ever be mutated by anything in the program. So, pure functions can access immutable global and static variables, because their state can never change, and in principle, they could access const variables that were directly initialized, e.g. const int i = 7; However, apparently, the compiler won't do that with arrays right now, as Bearophile has found. Accessing global or static variables that can never change once they're initialized does not violate the guarantees that D's pure makes, because the value is fixed and as such is essentially the same as hard-coding the value in the function directly. It's just those that can change which are a problem (which does potentially include global, const arrays if they were initialized via a static constructor). Now, while D's pure really doesn't directly have anything to do with functional purity (_all_ it does is restrict access to global or static variables which can be mutated - either directly or indirectly), it _is_ a vital building block for functional purity, because if the function parameters are immutable or implicitly convertible to immutable, then the compiler knows that multiple calls to the function with the same arguments will always return the same result, because the function can't access any mutable globals to get at anything different to produce a different result. And even if the parameters aren't immutable or implicitly convertible to immutable, if the parameter types and return type are unrelated, the compiler can also know that the return value was not passed into the function (since it had nowhere else to get it from), so it knows that it's unique and can do stuff like implicitly convert that value to immutable safely. So, with pure, the compiler can recognize actual, functional purity and other useful attributes and take advantage of them, but you're probably better off if you don't think of D's pure as being functionally pure, because there are quite a few things that D's pure functions can do which functionally pure functions can't do (like mutate their arguments if they're mutable). A good article on D's pure: http://klickverbot.at/blog/2012/05/purity-in-d/
Re: Global const variables
On Tuesday, 21 October 2014 at 16:56:06 UTC, Solomon E wrote: On Tuesday, 21 October 2014 at 15:51:27 UTC, MachineCode wrote: ... ... pure functions are also supposed to don't use global variables at all, according to functional programming paradigm Pure functions are immutables (constants but not "const" in the D or C++ senses) and can use other immutables, even if they're global immutables. (I guess it would be better though always to have a named top scope instead of everyone in the world having the same global scope :-) You *do* have a named top scope, the module name (which is accessible with prefixing a symbol with ., i.e., .x refers to the symbol x at module scope). There is no such thing as global scope in D.
Re: Global const variables
On Tuesday, 21 October 2014 at 15:51:27 UTC, MachineCode wrote: ... ... pure functions are also supposed to don't use global variables at all, according to functional programming paradigm Pure functions are immutables (constants but not "const" in the D or C++ senses) and can use other immutables, even if they're global immutables. (I guess it would be better though always to have a named top scope instead of everyone in the world having the same global scope :-)
Re: Global const variables
On Tue, 21 Oct 2014 16:47:04 + Solomon E via Digitalmars-d-learn wrote: > That's unsafe because the implementation might change, and > pointer arithmetic is unsafe in general. sure, ponter casting is implementation-dependend. but .ptr is guaranteed to work as expected. 'a' is just a `struct { size_t length; void* ptr }` now, but this representation is implementation detail, not a convention. signature.asc Description: PGP signature
Re: Global const variables
On Tuesday, October 21, 2014 08:02:50 bearophile via Digitalmars-d-learn wrote: Currently this code gets rejected: const int[] a = [1]; void main() pure { auto y = a[0]; } test2.d(3,14): Error: pure function 'D main' cannot access mutable static data 'a' test2.d(3,14): Error: pure function 'D main' cannot access mutable static data 'a' But is this a good idea? Isn't it better to accept it? In principle, it should be fine, but because it's using const, it won't work. global or static variables which are directly initialized with values that cannot possibly have mutable references elsewhere in the code are the only case where accessing const variables from outside a pure function like this could work - i.e. the cases where immutable and const are essentially identical (the only real difference being that if the variable is a reference type, if it's immutable, it's also shared, whereas if it's const, it's thread-local). So, I don't think that it's at all surprising that the compiler rejects it. It should probably be made smarter so that it doesn't reject it, but because you can just as easily make the variable immutable, you're not losing any real functionality in the interim. - Jonathan M Davis
Re: Global const variables
On Tuesday, 21 October 2014 at 14:25:20 UTC, ketmar via Digitalmars-d-learn wrote: On Tue, 21 Oct 2014 13:43:29 + Solomon E via Digitalmars-d-learn wrote: `b[0] = 8;` or `b[] = 8;` changes a. Printing the values for &a and &b shows they're different pointers, but (a is b) returns true. So I still have more to learn about how it does that. that's 'cause '&b' taking address of hidden "array structure", not the first array element, as in C. try 'a.ptr' and 'b.ptr' to get addresses of array elements. Thanks, that's what I was looking for, in order to understand what's going on. I Googled for it on this site, but without remembering the keyword ptr, I didn't find anything relevant. After I put printouts of .ptr in my test program, I figured out how to get the same result by unsafe pointer arithmetic. Apparently, for an array a, a.ptr == *(cast(ulong*) &a + 1). That's unsafe because the implementation might change, and pointer arithmetic is unsafe in general.
Re: Global const variables
On Tuesday, 21 October 2014 at 08:02:52 UTC, bearophile wrote: Currently this code gets rejected: const int[] a = [1]; void main() pure { auto y = a[0]; } test2.d(3,14): Error: pure function 'D main' cannot access mutable static data 'a' test2.d(3,14): Error: pure function 'D main' cannot access mutable static data 'a' But is this a good idea? Isn't it better to accept it? Bye, bearophile pure functions are also supposed to don't use global variables at all, according to functional programming paradigm
Re: Global const variables
On Tue, 21 Oct 2014 17:25:09 +0300 ketmar via Digitalmars-d-learn wrote: > On Tue, 21 Oct 2014 13:43:29 + > Solomon E via Digitalmars-d-learn > wrote: > > > `b[0] = 8;` or `b[] = 8;` changes a. Printing the values for &a > > and &b shows they're different pointers, but (a is b) returns > > true. So I still have more to learn about how it does that. > that's 'cause '&b' taking address of hidden "array structure", not > the first array element, as in C. try 'a.ptr' and 'b.ptr' to get > addresses of array elements. p.s. '&(a[0])' and '&(b[0])' works too. parens are just for clarifying. signature.asc Description: PGP signature
Re: Global const variables
On Tue, 21 Oct 2014 13:43:29 + Solomon E via Digitalmars-d-learn wrote: > `b[0] = 8;` or `b[] = 8;` changes a. Printing the values for &a > and &b shows they're different pointers, but (a is b) returns > true. So I still have more to learn about how it does that. that's 'cause '&b' taking address of hidden "array structure", not the first array element, as in C. try 'a.ptr' and 'b.ptr' to get addresses of array elements. signature.asc Description: PGP signature
Re: Making plugin system with shared libraries. Upcast in shared lib
On Monday, 20 October 2014 at 15:07:43 UTC, MrSmith wrote: On Monday, 20 October 2014 at 14:05:29 UTC, Kagamin wrote: Do it the COM way: publish IModule2 interface and declare GetInterface method, which will return a prepared pointer, which you would reinterpret cast to IModule2. Will it work on linux with simple .so libs? I want it to be as simple as possible. Is it any different from what you already have? Dynamic cast is not guaranteed to work across dll boundary. If it does, you're lucky. If it doesn't, use GetInterface - that will work independently from runtime, environment, language, os, hardware etc.
Re: Global const variables
On Tuesday, 21 October 2014 at 12:30:30 UTC, anonymous wrote: On Tuesday, 21 October 2014 at 12:08:35 UTC, Solomon E wrote: On Tuesday, 21 October 2014 at 08:48:09 UTC, safety0ff wrote: const int[] a; int[] b; static this() { b = [1]; a = b; } `a` isn't a reference to `b`. `a` is assigned by value and has its own storage. `a` is indeed a copy of `b`. But `b` is a pointer+length, and only those are copied. The array data is not copied. `a` and `b` refer to the same data afterwards. [...] const int[] a; int[] b; static this() { [...] a = b; } [...] void main() { [...] b = [8,7]; Here, making `b` point somewhere else (to [8, 7]). If instead you change b's elements, you'll see that `a` and `b` refer to the same data: b[] = 8; /* Will also change `a`'s data. */ You're right. Thank you, anonymous stranger. Sorry about that, safety0ff. It looks like you were right and I was wrong. `b[0] = 8;` or `b[] = 8;` changes a. Printing the values for &a and &b shows they're different pointers, but (a is b) returns true. So I still have more to learn about how it does that.
Why std.variant.Varian.get is mutable
This doesn't compiles http://dpaste.dzfl.pl/bbcc31fbe016
Re: Global const variables
On Tuesday, 21 October 2014 at 12:08:35 UTC, Solomon E wrote: On Tuesday, 21 October 2014 at 08:48:09 UTC, safety0ff wrote: const int[] a; int[] b; static this() { b = [1]; a = b; } `a` isn't a reference to `b`. `a` is assigned by value and has its own storage. `a` is indeed a copy of `b`. But `b` is a pointer+length, and only those are copied. The array data is not copied. `a` and `b` refer to the same data afterwards. [...] const int[] a; int[] b; static this() { [...] a = b; } [...] void main() { [...] b = [8,7]; Here, making `b` point somewhere else (to [8, 7]). If instead you change b's elements, you'll see that `a` and `b` refer to the same data: b[] = 8; /* Will also change `a`'s data. */
Re: Beginner ?. Why does D suggest to learn java
On Tue, 21 Oct 2014 11:51:16 + via Digitalmars-d-learn wrote: > The problem with template-programming in XSLT/C++/D is that if > you want to learn functional programming you are better off using > a good functional language. templates arent about FP only. yet i agree that Scheme is a very good starting point, SICP rocks. signature.asc Description: PGP signature
Re: Global const variables
On Tuesday, 21 October 2014 at 08:48:09 UTC, safety0ff wrote: On Tuesday, 21 October 2014 at 08:25:07 UTC, bearophile wrote: Minas Mina: Aren't pure functions supposed to return the same result every time? If yes, it is correct to not accept it. But how can main() not be pure? Or, how can't the 'a' array be immutable? Bye, bearophile There can exist a mutable reference to a's underlying memory: const int[] a; int[] b; static this() { b = [1]; a = b; } `a` isn't a reference to `b`. `a` is assigned by value and has its own storage. You could change its type to const int[]* a = &b; then it would be a reference to mutable storage. I made an example program to figure these things out, or else I wouldn't know what I'm talking about. import std.stdio; import std.conv; const int[] a; int[] b; static this() { string entry; while(entry == "") { try { write("enter an int: "); entry = readln(); b = [to!int(entry[0..entry.length-1])]; } catch(ConvException e) { writeln("error, try again"); entry = ""; } } a = b; } int[] x = [0,1,2,3]; class Holder { const(int[]) y; this() { y = x; } } void main() { auto H = new Holder(); writeln("original const a ", a); // [the int that was entered] b = [8,7]; writeln("unaltered const a ", a); // [the int that was entered] x = [10,9]; writeln("unaltered const member y ", H.y); // [0, 1, 2, 3] H = new Holder(); writeln("new const member y ", H.y); // [10, 9] writeln("immutable m ", get_m()); // [42] } immutable int[] m = [42]; immutable(int[]) get_m() pure { return m; }
Re: Beginner ?. Why does D suggest to learn java
On Tuesday, 21 October 2014 at 11:43:56 UTC, ketmar via Digitalmars-d-learn wrote: and other people keep pointing at languages without templates and metaprogramming as "good for learing". it's closed circle. The problem with template-programming in XSLT/C++/D is that if you want to learn functional programming you are better off using a good functional language.
Re: Beginner ?. Why does D suggest to learn java
On Tue, 21 Oct 2014 11:20:42 + Paulo Pinto via Digitalmars-d-learn wrote: > People think templates are magic, due to their skillset. and other people keep pointing at languages without templates and metaprogramming as "good for learing". it's closed circle. signature.asc Description: PGP signature
Re: Beginner ?. Why does D suggest to learn java
On Tuesday, 21 October 2014 at 09:14:08 UTC, ketmar via Digitalmars-d-learn wrote: On Tue, 21 Oct 2014 09:01:32 + Kapps via Digitalmars-d-learn wrote: no template magic that's very bad. it's time to stop making people think that templates are inevitably arcane. People think templates are magic, due to their skillset. I see that all the time in the enterprise, when we get people on projects that would already have issues using something like Clipper/VB back in the day. -- paulo
Re: Global const variables
Szymon Gatner: const int[] a; int[] b; static this() { b = [1]; a = b; } Ant this code works? What is the point of const then if you can assign it to mutable slice? It works, and I think it should work. Inside the (module) constructor the const state is handled differently. Thank you for the example, safety0ff. Bye, bearophile
Re: m_condition.mutex cannot be used in shared method ?
On Monday, 20 October 2014 at 17:37:22 UTC, Sean Kelly wrote: With all the recent work on the GC, we really really need to start tracking which thread owns a given non-shared object so it can be finalized properly. This may mean having the process of casting away shared make the executing thread the new owner of the object. But this change of ownership is usually only temporary: with(cast(BaseType) sharedVar) { // ... }
Re: Global const variables
On Tuesday, 21 October 2014 at 08:48:09 UTC, safety0ff wrote: On Tuesday, 21 October 2014 at 08:25:07 UTC, bearophile wrote: Minas Mina: Aren't pure functions supposed to return the same result every time? If yes, it is correct to not accept it. But how can main() not be pure? Or, how can't the 'a' array be immutable? Bye, bearophile There can exist a mutable reference to a's underlying memory: const int[] a; int[] b; static this() { b = [1]; a = b; } Ant this code works? What is the point of const then if you can assign it to mutable slice?
Re: Beginner ?. Why does D suggest to learn java
On Tue, 21 Oct 2014 09:01:32 + Kapps via Digitalmars-d-learn wrote: > no template magic that's very bad. it's time to stop making people think that templates are inevitably arcane. signature.asc Description: PGP signature
Re: Beginner ?. Why does D suggest to learn java
On Thursday, 16 October 2014 at 22:26:51 UTC, RBfromME wrote: I'm a newbie to programming and have been looking into the D lang as a general purposing language to learn, yet the D overview indicates that java would be a better language to learn for your first programming language. Why? Looks like D is easier than Java... Honestly, I'd recommend starting with VB.net or C# (preferably C#) over D, as: 1) They're simpler, no template magic, not as low level, etc. 2) Having an excellent IDE is very nice for starting out, seeing errors right as you type them, seeing all the methods and how to call them, having the documentation right there in your IDE, and not having to worry about whether what you just wrote is something that the IDE's parser can't handle or is actually invalid code. 3) Being significantly more popular is in general a boon, more tutorials are available (though D has some nice resources like Ali's book), and you're more likely to find a solution to a problem through Google as more people have come across it. 4) Having to deal with any compiler bugs would be very frustrating when starting to learn programming. D still has plenty, even if they're much less noticeable now. Trying to figure out why something doesn't work only to realize it's a compiler bug is frustrating. 5) Having a drag-and-drop GUI designer is very nice. D is still difficult to use for GUIs, and when starting it's really nice to see something significant on the screen right away. 6) You probably won't use most of D's features when you're just learning, so much of D's advantages are gone. D is an awesome language, but I would not recommend it for someone completely new to programming. Once you get the hang of programming, D is an excellent language, until then something simpler, more popular, and more supported, would be better.
Re: Global const variables
On Tuesday, 21 October 2014 at 08:25:07 UTC, bearophile wrote: Minas Mina: Aren't pure functions supposed to return the same result every time? If yes, it is correct to not accept it. But how can main() not be pure? Or, how can't the 'a' array be immutable? Bye, bearophile There can exist a mutable reference to a's underlying memory: const int[] a; int[] b; static this() { b = [1]; a = b; }
Re: Global const variables
Minas Mina: Aren't pure functions supposed to return the same result every time? If yes, it is correct to not accept it. But how can main() not be pure? Or, how can't the 'a' array be immutable? Bye, bearophile
Re: Global const variables
On Tuesday, 21 October 2014 at 08:02:52 UTC, bearophile wrote: Currently this code gets rejected: const int[] a = [1]; void main() pure { auto y = a[0]; } test2.d(3,14): Error: pure function 'D main' cannot access mutable static data 'a' test2.d(3,14): Error: pure function 'D main' cannot access mutable static data 'a' But is this a good idea? Isn't it better to accept it? Bye, bearophile Aren't pure functions supposed to return the same result every time? If yes, it is correct to not accept it.
Global const variables
Currently this code gets rejected: const int[] a = [1]; void main() pure { auto y = a[0]; } test2.d(3,14): Error: pure function 'D main' cannot access mutable static data 'a' test2.d(3,14): Error: pure function 'D main' cannot access mutable static data 'a' But is this a good idea? Isn't it better to accept it? Bye, bearophile
Re: Removing whitespace duplicates
Nordlöw: It would be nice to have a lambda-variant of std.string.tr to call like x.tr!isWhite(['_']) If your text is ASCII, then there is std.ascii.whitespace that can be given as argument to std.string.tr. Bye, bearophile
Re: Removing whitespace duplicates
On Tuesday, 21 October 2014 at 07:31:59 UTC, Nordlöw wrote: std.string.tr is my preferred choice here :) It would be nice to have a lambda-variant of std.string.tr to call like x.tr!isWhite(['_'])
Re: Removing whitespace duplicates
On Monday, 20 October 2014 at 23:25:18 UTC, bearophile wrote: But with tr you can also replace the spaces with the underscore. std.string.tr is my preferred choice here :) Thanks!