Re: Casting in Safe D
On Thursday, 27 November 2014 at 00:27:59 UTC, David Held wrote: On 11/23/2014 3:12 PM, anonymous wrote: [...] And even pointer dereferencing is @safe. Invalid ones will fail with a segfault at run time: void foo(int* a) @safe {*a = 13;} Hmm...throwing an exception is a well-defined behavior, but is segfaulting a well-defined behavior of correct D programs? This seems like a peculiar definition of safe to me... Dave I would personally see that this became a compile-time error in @safe code either always or even better when the compile cannot prove that the operation will never cause an exception at run-time. Alternatively we could disallow this only in @safe *nothrow* functions.
[dub] Size of executable
[Maybe this has been asked before.] I usually use dub to create and build projects. I built one of the projects with dub and then by hand with dmd[1] passing all the files etc. Turned out that the executable built with dub was 1.4 MB whereas the one built by hand was only 807 kB. Why is that? Another thing, I've spotted a typo in dub's[2] command line help: clean [package] Removes intermetiate build files and cached build results It should read intermediate (with d), else it sounds like the Goths in Asterix :-) [1] dmd 2.066.0 [2] DUB version 0.9.22, built on Sep 16 2014
[dub] Size of executable
[Maybe this has been asked before.] I usually use dub to create and build projects. I built one of the projects with dub and then by hand with dmd[1] passing all the files etc. Turned out that the executable built with dub was 1.4 MB whereas the one built by hand was only 807 kB. Why is that? Another thing, I've spotted a typo in dub's[2] command line help: clean [package] Removes intermetiate build files and cached build results It should read intermediate (with d), else it sounds like the Goths in Asterix :-) [1] dmd 2.066.0 [2] DUB version 0.9.22, built on Sep 16 2014
Re: windows linker error
On Wednesday, 26 November 2014 at 04:10:08 UTC, Vlad Levenfeld wrote: So I used the dmd visual studio project to build dmd It can be outdated, because dmd release is built by dmc, not vc.
Re: [dub] Size of executable
On Thursday, 27 November 2014 at 09:33:49 UTC, Chris wrote: I usually use dub to create and build projects. I built one of the projects with dub and then by hand with dmd[1] passing all the files etc. Turned out that the executable built with dub was 1.4 MB whereas the one built by hand was only 807 kB. Why is that? dub compiles and links every file in the source folder whether it's used or not. Whereas with dmd or rdmd you only compile and link the files you actually use.
Re: Error: cannot return non-void from void function
Full function look like this: auto parseConfig() { auto config = Ini.Parse(getcwd ~ \\ ~ config.ini); string txtlinks = getcwd ~ \\ ~ config.getKey(input_links); if(!exists(txtlinks)) { writeln(Can't find input file with list of links.); return; } auto lines = File(txtlinks, r).byLine; return lines; }
Error: cannot return non-void from void function
auto parseConfig() { auto lines = File(txtlinks, r).byLine; return lines; } Error: cannot return non-void from void function I can't understand the reasons of the error; Can I return auto from function?
function app.download (string[] links) is not callableusing argument types (string, string)
I try to compile simple example: import std.net.curl; void main() { download(ftp.digitalmars.com/sieve.ds, D:\\Project\\2014\\txt_downloader\\img\\1.foo); } but I am getting error: function app.download (string[] links) is not callableusing argument types (string, string)
Re: Error: cannot return non-void from void function
On Thursday, 27 November 2014 at 13:07:59 UTC, Suliman wrote: Full function look like this: auto parseConfig() { auto config = Ini.Parse(getcwd ~ \\ ~ config.ini); string txtlinks = getcwd ~ \\ ~ config.getKey(input_links); if(!exists(txtlinks)) { writeln(Can't find input file with list of links.); return; } auto lines = File(txtlinks, r).byLine; return lines; } You have two return statements in your function. Each of them returns a result of a different type (the first one returns a void result). That's not allowed. Instead of writing Can't find input file and then returning, consider throwing an exception. Then you only have one return statement, and one return type. Graham
Re: [dub] Size of executable
On Thursday, 27 November 2014 at 12:29:03 UTC, Gary Willoughby wrote: On Thursday, 27 November 2014 at 09:33:49 UTC, Chris wrote: I usually use dub to create and build projects. I built one of the projects with dub and then by hand with dmd[1] passing all the files etc. Turned out that the executable built with dub was 1.4 MB whereas the one built by hand was only 807 kB. Why is that? dub compiles and links every file in the source folder whether it's used or not. Whereas with dmd or rdmd you only compile and link the files you actually use. I compiled the exact same files. I excluded those I didn't need in the dub configuration like so: excludedSourceFiles: [...] But dub's executable is bigger.
Re: [dub] Size of executable
On Thursday, 27 November 2014 at 13:56:19 UTC, Chris wrote: On Thursday, 27 November 2014 at 12:29:03 UTC, Gary Willoughby wrote: On Thursday, 27 November 2014 at 09:33:49 UTC, Chris wrote: I usually use dub to create and build projects. I built one of the projects with dub and then by hand with dmd[1] passing all the files etc. Turned out that the executable built with dub was 1.4 MB whereas the one built by hand was only 807 kB. Why is that? dub compiles and links every file in the source folder whether it's used or not. Whereas with dmd or rdmd you only compile and link the files you actually use. I compiled the exact same files. I excluded those I didn't need in the dub configuration like so: excludedSourceFiles: [...] But dub's executable is bigger. When you build with dub it should print out (if I remember correctly, its been a little while) the command it uses to build your code. Is there any difference between that command and your 'by hand' version?
Re: [dub] Size of executable
On Thursday, 27 November 2014 at 13:59:23 UTC, CraigDillabaugh wrote: On Thursday, 27 November 2014 at 13:56:19 UTC, Chris wrote: On Thursday, 27 November 2014 at 12:29:03 UTC, Gary Willoughby wrote: On Thursday, 27 November 2014 at 09:33:49 UTC, Chris wrote: I usually use dub to create and build projects. I built one of the projects with dub and then by hand with dmd[1] passing all the files etc. Turned out that the executable built with dub was 1.4 MB whereas the one built by hand was only 807 kB. Why is that? dub compiles and links every file in the source folder whether it's used or not. Whereas with dmd or rdmd you only compile and link the files you actually use. I compiled the exact same files. I excluded those I didn't need in the dub configuration like so: excludedSourceFiles: [...] But dub's executable is bigger. When you build with dub it should print out (if I remember correctly, its been a little while) the command it uses to build your code. Is there any difference between that command and your 'by hand' version? dub says: Compiling using dmd... Linking... I have the exact same setting, I think. I don't build for release with either method. dmd file1.d file2.d file3.d ... (dub compiles the same files, no release build)
Re: function app.download (string[] links) is not callableusing argument types (string, string)
Look like it was missed installation or so. I installed new copy of dmd and error gone away.
Re: function app.download (string[] links) is not callableusing argument types (string, string)
The downloading still do not work. Work only: get(dlang.org);
Re: function app.download (string[] links) is not callableusing argument types (string, string)
On Thursday, 27 November 2014 at 13:39:41 UTC, Suliman wrote: I try to compile simple example: import std.net.curl; void main() { download(ftp.digitalmars.com/sieve.ds, D:\\Project\\2014\\txt_downloader\\img\\1.foo); } but I am getting error: function app.download (string[] links) is not callableusing argument types (string, string) You are fucking retard, bro :) This code works, but you're not showing the rest of it :-D
Re: function app.download (string[] links) is not callableusing argument types (string, string)
On Thursday, 27 November 2014 at 14:35:08 UTC, Daniel Kozák via Digitalmars-d-learn wrote: V Thu, 27 Nov 2014 13:39:40 + Suliman via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsáno: I try to compile simple example: import std.net.curl; void main() { download(ftp.digitalmars.com/sieve.ds, D:\\Project\\2014\\txt_downloader\\img\\1.foo); } but I am getting error: function app.download (string[] links) is not callableusing argument types (string, string) please try std.net.curl.download(ftp.digitalmars.com/sieve.ds, D:\\Project\\2014\\txt_downloader\\img\\1.foo); seems you have some name colision He has, I've seen his whole code. And it's pretty obviuse :-D Let him guess what it is xD
attribute length missing in std.array: Appender
Hi, I implement a network protocol and use an Appender!(ubyte[])(). I have following issue. The first three bytes I have to fill, the following bytes are reserved and must be 0. In this example the overall header length must be 8. import std.array: appender; const HEADER_LENGTH = 8; auto app = appender!(ubyte[])(); app.put(cast(ubyte)40); app.put(cast(ubyte)5); app.put(cast(ubyte)234); // ... add 5 times 0 // variable length body will follow In case of dynamic array I can simple set the length to 8. Appender doesn't have a length attribute. Is there some other nice D functionaliy I can use? Maybe some functionality in std.array is missing: app.fill(0, HEADER_LENGTH)? Currently I do a work around with a for loop. Kind regards André
Re: Error: cannot return non-void from void function
On Thu, 27 Nov 2014 13:07:58 + Suliman via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: Full function look like this: auto parseConfig() { auto config = Ini.Parse(getcwd ~ \\ ~ config.ini); string txtlinks = getcwd ~ \\ ~ config.getKey(input_links); if(!exists(txtlinks)) { writeln(Can't find input file with list of links.); return; } auto lines = File(txtlinks, r).byLine; return lines; } ah, that's it! as spec says, D determines function return value from the first 'return' statement it seen. in your case this is `return;`, so function return type is determined to be `void`. if you doing `auto` functions, try to arrange your code so the first `return` returning the actual value. besides, your code is wrong anyway, 'cause you can't have function that returns both nothing and something. your first `return;` should either return something, or must be changed to throwing some exception. signature.asc Description: PGP signature
Re: [dub] Size of executable
On Thursday, 27 November 2014 at 14:14:50 UTC, Chris wrote: On Thursday, 27 November 2014 at 13:59:23 UTC, CraigDillabaugh wrote: On Thursday, 27 November 2014 at 13:56:19 UTC, Chris wrote: On Thursday, 27 November 2014 at 12:29:03 UTC, Gary Willoughby wrote: On Thursday, 27 November 2014 at 09:33:49 UTC, Chris wrote: I usually use dub to create and build projects. I built one of the projects with dub and then by hand with dmd[1] passing all the files etc. Turned out that the executable built with dub was 1.4 MB whereas the one built by hand was only 807 kB. Why is that? dub compiles and links every file in the source folder whether it's used or not. Whereas with dmd or rdmd you only compile and link the files you actually use. I compiled the exact same files. I excluded those I didn't need in the dub configuration like so: excludedSourceFiles: [...] But dub's executable is bigger. When you build with dub it should print out (if I remember correctly, its been a little while) the command it uses to build your code. Is there any difference between that command and your 'by hand' version? dub says: Compiling using dmd... Linking... I have the exact same setting, I think. I don't build for release with either method. dmd file1.d file2.d file3.d ... (dub compiles the same files, no release build) I am sure there is some way to get it to print out exactly what it is doing. I've done it before when trying to figure out some compilation issues ... unfortunately it was a while ago and the machine I am at now doesn't have dub (or D for that matter) installed. Maybe you need to give the the dub -v (--verbose) option. Type 'dub help' to check - again I can't do that right here.
Re: Error: cannot return non-void from void function
ah, that's it! as spec says, D determines function return value from the first 'return' statement it seen. in your case this is `return;`, so function return type is determined to be `void`. if you doing `auto` functions, try to arrange your code so the first `return` returning the actual value. besides, your code is wrong anyway, 'cause you can't have function that returns both nothing and something. your first `return;` should either return something, or must be changed to throwing some exception. How I can terminate program? First return I used to terminate app if config file is exists.
Re: Error: cannot return non-void from void function
On Thu, 27 Nov 2014 17:22:35 + Suliman via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: ah, that's it! as spec says, D determines function return value from the first 'return' statement it seen. in your case this is `return;`, so function return type is determined to be `void`. if you doing `auto` functions, try to arrange your code so the first `return` returning the actual value. besides, your code is wrong anyway, 'cause you can't have function that returns both nothing and something. your first `return;` should either return something, or must be changed to throwing some exception. How I can terminate program? First return I used to terminate app if config file is exists. throw an exception. uncatched exception will terminate your program. signature.asc Description: PGP signature
Re: function app.download (string[] links) is not callableusing argument types (string, string)
Running .\txtdownloader.exe std.stream.OpenException@std\stream.d(50): Cannot open or create file 'D:\Projec t\2014\txt_downloader\img\1.foo' 0x0041BA59 in void std.stream.File.open(immutable(char)[], std.stream.FileMode) 0x00404B75 in void std.net.curl.download!(std.net.curl.AutoProtocol).download(co nst(char)[], immutable(char)[], std.net.curl.AutoProtocol) at C:\DMD\dmd2\window s\bin\..\..\src\phobos\std\net\curl.d(268) 0x00402038 in _Dmain 0x0041250C in void rt.dmain2._d_run_main(int, char**, extern (C) int function(ch ar[][])*).runAll().void __lambda1() 0x004124DF in void rt.dmain2._d_run_main(int, char**, extern (C) int function(ch ar[][])*).runAll() 0x004123F8 in _d_run_main 0x0040D268 in main 0x0045CF9D in mainCRTStartup 0x75C0336A in BaseThreadInitThunk 0x77679F72 in RtlInitializeExceptionChain 0x77679F45 in RtlInitializeExceptionChain Error executing command run: Program exited with code 1
Re: attribute length missing in std.array: Appender
On Thursday, 27 November 2014 at 16:08:13 UTC, Andre wrote: Hi, I implement a network protocol and use an Appender!(ubyte[])(). I have following issue. The first three bytes I have to fill, the following bytes are reserved and must be 0. In this example the overall header length must be 8. import std.array: appender; const HEADER_LENGTH = 8; auto app = appender!(ubyte[])(); app.put(cast(ubyte)40); app.put(cast(ubyte)5); app.put(cast(ubyte)234); // ... add 5 times 0 // variable length body will follow In case of dynamic array I can simple set the length to 8. Appender doesn't have a length attribute. Is there some other nice D functionaliy I can use? Maybe some functionality in std.array is missing: app.fill(0, HEADER_LENGTH)? Currently I do a work around with a for loop. Kind regards André You can initialize the appender with an existing array, or put an entire array into it at once: import std.array: appender; ubyte[] temp; temp.reserve(8); // reserve first, so that only one allocation happens temp[0 .. 3] = [40, 5, 234]; temp.length = 8; auto app = appender!(ubyte[])(temp); // or: app.put(temp); The array will not be copied when the appender is constructed.
Re: function app.download (string[] links) is not callableusing argument types (string, string)
Is there any way to detect where collision was occurred?
Re: function app.download (string[] links) is not callableusing argument types (string, string)
Oh! It's work! I forgot to change path on my home PC!
Re: function app.download (string[] links) is not callableusing argument types (string, string)
On Thursday, 27 November 2014 at 17:39:10 UTC, Suliman wrote: Is there any way to detect where collision was occurred? It's look like collision was with method name.
Best way to add slash to tail of the path
Sometimes it's path string may do not have tail slash of the path Compare: string path = C:\\folder\\name string path = C:\\folder\\name\\ in case if I need to append file name to path to get full path I can get error like: path ~= foo.txt C:\\folder\\namefoo.txt instead of C:\\folder\\name\\foo.txt what is the best way to add tail slash if it's not exists?
Re: Best way to add slash to tail of the path
On Thu, 27 Nov 2014 18:05:37 + Suliman via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: Sometimes it's path string may do not have tail slash of the path Compare: string path = C:\\folder\\name string path = C:\\folder\\name\\ in case if I need to append file name to path to get full path I can get error like: path ~= foo.txt C:\\folder\\namefoo.txt instead of C:\\folder\\name\\foo.txt what is the best way to add tail slash if it's not exists? see std.path, it contains alot of useful things. signature.asc Description: PGP signature
Re: [dub] Size of executable
On 11/27/2014 03:14 PM, Chris wrote: dub says: Compiling using dmd... Linking... I have the exact same setting, I think. I don't build for release with either method. dmd file1.d file2.d file3.d ... (dub compiles the same files, no release build) dub builds with --debug by default. -- Mike Wey
Re: Best way to add slash to tail of the path
see std.path, it contains alot of useful things. I looked there, but found only buildNormalizedPath, but it's not for such situation...
Re: Best way to add slash to tail of the path
On Thu, 27 Nov 2014 20:02:40 + Suliman via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: see std.path, it contains alot of useful things. I looked there, but found only buildNormalizedPath, but it's not for such situation... take a second look then. ;-) you'll find `buildPath()` here too. signature.asc Description: PGP signature
Re: Best way to add slash to tail of the path
take a second look then. ;-) you'll find `buildPath()` here too. Not better: string foo = D:/code/txtDownloader; writeln(foo); foo = foo.buildPath; foo ~= config.txt; writeln(foo); Running .\txtdownloader.exe D:/code/txtDownloader D:/code/txtDownloaderconfig.txt -- need: D:/code/txtDownloader/config.txt
Re: Best way to add slash to tail of the path
On Thu, 27 Nov 2014 20:20:24 + Suliman via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: take a second look then. ;-) you'll find `buildPath()` here too. Not better: string foo = D:/code/txtDownloader; writeln(foo); foo = foo.buildPath; foo ~= config.txt; writeln(foo); Running .\txtdownloader.exe D:/code/txtDownloader D:/code/txtDownloaderconfig.txt -- need: D:/code/txtDownloader/config.txt and now try to read the documentation. it rocks. no, really, it was written for people to read it! signature.asc Description: PGP signature
Re: Best way to add slash to tail of the path
Could you quote for me part of docs where it's written? I really can't understand about what you are taking.
Re: Best way to add slash to tail of the path
Dne Thu, 27 Nov 2014 21:20:24 +0100 Suliman via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsal(a): take a second look then. ;-) you'll find `buildPath()` here too. Not better: string foo = D:/code/txtDownloader; writeln(foo); foo = foo.buildPath; foo ~= config.txt; writeln(foo); Running .\txtdownloader.exe D:/code/txtDownloader D:/code/txtDownloaderconfig.txt -- need: D:/code/txtDownloader/config.txt what about: string foo = D:/code/txtDownloader; writeln(foo); foo = buildPath(foo, config.txt); writeln(foo);
Re: Best way to add slash to tail of the path
try first few sentences and looked at the example ;) Dne Thu, 27 Nov 2014 21:42:31 +0100 Suliman via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsal(a): Could you quote for me part of docs where it's written? I really can't understand about what you are taking. -- Vytvořeno poštovní aplikací Opery: http://www.opera.com/mail/
Re: Best way to add slash to tail of the path
On Thu, 27 Nov 2014 20:42:31 + Suliman via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: Could you quote for me part of docs where it's written? I really can't understand about what you are taking. right here: http://dlang.org/phobos/std_path.html#buildPath do you see examples section there? you don't even have to read the explanations, as examples are self-explanatory. please, try to read *the* *whole* *docs* on the given function next time. it's very boring to answer the questions that are already answered in documentation, and even demonstrated with samples. almost all your questions here are easily answered by careful reading of documentation. signature.asc Description: PGP signature
Re: function app.download (string[] links) is not callableusing argument types (string, string)
Dne Thu, 27 Nov 2014 18:39:09 +0100 Suliman via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsal(a): Is there any way to detect where collision was occurred? Yes, read error description it has been app.download I guess -- Vytvořeno poštovní aplikací Opery: http://www.opera.com/mail/
Re: Best way to add slash to tail of the path
thanks! I understood!
Re: Error: cannot return non-void from void function
On Thursday, 27 November 2014 at 17:22:36 UTC, Suliman wrote: ah, that's it! as spec says, D determines function return value from the first 'return' statement it seen. in your case this is `return;`, so function return type is determined to be `void`. if you doing `auto` functions, try to arrange your code so the first `return` returning the actual value. besides, your code is wrong anyway, 'cause you can't have function that returns both nothing and something. your first `return;` should either return something, or must be changed to throwing some exception. How I can terminate program? First return I used to terminate app if config file is exists. You can use this: auto parseConfig() { string txtlinks = buildPath(getcwd,notexist); if(exists(txtlinks)) { auto lines = File(txtlinks, r).byLine; return lines; } writeln(Can't find input file with list of links.); return typeof(return)(); } it works somehow, but it is not good way how to do it. Other way is use exit if(!exists(txtlinks)) { import core.runtime; import std.c.process; writeln(Can't find input file with list of links.); Runtime.terminate(); exit(1); } But best way is use throw exception if(!exists(txtlinks)) { throw new Exception(Can't find input file with list of links.); } and catch it somewhere from calling side
Re: Error: cannot return non-void from void function
On Thu, 27 Nov 2014 21:14:57 + Daniel Kozak via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: import core.runtime; import std.c.process; writeln(Can't find input file with list of links.); Runtime.terminate(); exit(1); please-please-please don't teach people that! using such features requiring deep understanding of how D runtime works, and how it interacts with C runtime, with GC, with stack objects and so on. it's better to now show people bad samples instead of telling them don't do what i just wrote. ;-) signature.asc Description: PGP signature
Re: Error: cannot return non-void from void function
Dne Thu, 27 Nov 2014 22:21:52 +0100 ketmar via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsal(a): On Thu, 27 Nov 2014 21:14:57 + Daniel Kozak via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: import core.runtime; import std.c.process; writeln(Can't find input file with list of links.); Runtime.terminate(); exit(1); please-please-please don't teach people that! using such features requiring deep understanding of how D runtime works, and how it interacts with C runtime, with GC, with stack objects and so on. it's better to now show people bad samples instead of telling them don't do what i just wrote. ;-) I know, I just can't help myself :).
Re: Error: cannot return non-void from void function
On Thu, 27 Nov 2014 22:25:10 +0100 Daniel Kozak via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: Dne Thu, 27 Nov 2014 22:21:52 +0100 ketmar via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsal(a): On Thu, 27 Nov 2014 21:14:57 + Daniel Kozak via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: import core.runtime; import std.c.process; writeln(Can't find input file with list of links.); Runtime.terminate(); exit(1); please-please-please don't teach people that! using such features requiring deep understanding of how D runtime works, and how it interacts with C runtime, with GC, with stack objects and so on. it's better to now show people bad samples instead of telling them don't do what i just wrote. ;-) I know, I just can't help myself :). me too sometimes. taking into accout that don't try this at home warning is never working makes it even funnier. ;-) signature.asc Description: PGP signature
Re: Best way to add slash to tail of the path
On Thursday, 27 November 2014 at 20:52:26 UTC, ketmar via Digitalmars-d-learn wrote: On Thu, 27 Nov 2014 20:42:31 + Suliman via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: Could you quote for me part of docs where it's written? I really can't understand about what you are taking. right here: http://dlang.org/phobos/std_path.html#buildPath do you see examples section there? you don't even have to read the explanations, as examples are self-explanatory. please, try to read *the* *whole* *docs* on the given function next time. it's very boring to answer the questions that are already answered in documentation, and even demonstrated with samples. almost all your questions here are easily answered by careful reading of documentation. Don't bother, he is useless.. I've known him for a quite few years :-D Just ignore the stupid questions, this way people would be forced to really look for answers. RTFM 4eva! :-D
thrift and dub
hi, i'm trying to get a thrift example working within a dub project. it seems that the thrift.d in the dub repo is not whats actually needed but i should link against libthriftd.a that comes from the official thrift distro. what i tried is add the following to dub.json: libs: [/path/to/thrift/lib/d/libthriftd.a], sourcePaths: [/path/to/thrift/lib/d/src], but that seems not to be the correct thing to do. anyone has a working example (e.g. the calculator example)? thanks y
Re: attribute length missing in std.array: Appender
Thanks a lot for the help. Kind regards André On Thursday, 27 November 2014 at 17:29:50 UTC, Marc Schütz wrote: On Thursday, 27 November 2014 at 16:08:13 UTC, Andre wrote: Hi, I implement a network protocol and use an Appender!(ubyte[])(). I have following issue. The first three bytes I have to fill, the following bytes are reserved and must be 0. In this example the overall header length must be 8. import std.array: appender; const HEADER_LENGTH = 8; auto app = appender!(ubyte[])(); app.put(cast(ubyte)40); app.put(cast(ubyte)5); app.put(cast(ubyte)234); // ... add 5 times 0 // variable length body will follow In case of dynamic array I can simple set the length to 8. Appender doesn't have a length attribute. Is there some other nice D functionaliy I can use? Maybe some functionality in std.array is missing: app.fill(0, HEADER_LENGTH)? Currently I do a work around with a for loop. Kind regards André You can initialize the appender with an existing array, or put an entire array into it at once: import std.array: appender; ubyte[] temp; temp.reserve(8); // reserve first, so that only one allocation happens temp[0 .. 3] = [40, 5, 234]; temp.length = 8; auto app = appender!(ubyte[])(temp); // or: app.put(temp); The array will not be copied when the appender is constructed.
Passing reference data to class and incapsulation
In D we a several data types which are passed by reference: dynamic arrays, associative arrays. And sometimes we need to pass these reference data to class instance to store it inside. One of the principles of object-oriented programming is incapsulation. So all class data should be only modyfiable via class methods and properties. But I we pass reference data to class (for example as parameter in constructor) we still can change these data from initial code and break some internal logic of class for modifying these data. Example: class Foo { this(int[] b) { bar = b; } private int[] bar; //Some methods } void main() { int[] bar = [1,2,3,4,5]; Foo foo = new Foo(bar); //There I could do some logic with class bar[2] = 6; //I modify class without some checks from class //And there I might pass *bar* somewhere outside and break incapsulation } Same situation happens when I assign reference data to properties. I can check or do something with data at the moment of assignment, but I can't control that someone will modify using initial reference from outside. So do you copy reference data in constructors or properties? Should it be? Or call site should be responsible for not escaping reference somewhere outside and not modifying these data badly? There also possible some situations when these data should be shared between different pieces of code (for example different class instance could reference the same memory area). But I think this is not very good and safe approach and should be avoided if possible. But I think storing reference to class inside another class could be good approach because there could be different type of relations between classes. For plain data types we often have types of relations: *ownership* or *aggregation*. But classes can usualy have more types of relations: *usage* or when one class subscribes for events of another. Is there some good links to read for these questions that you could advice. After several hours of googling I haven't found good topics about these problems. And I have not enough time for reading big book.