String interpolation
Hi, The only example of string interpolation I've found so far is on Rosetta Code: void main() { import std.stdio, std.string; "Mary had a %s lamb.".format("little").writeln; "Mary had a %2$s %1$s lamb.".format("little", "white").writeln; } Is this a "proper" way of string interpolation in D? This looks a little clumsy. Are there any better approaches? Quick skimming through the docs didn't give anything.
Re: String interpolation
On Tuesday, 10 November 2015 at 10:33:30 UTC, Tobias Pankrath wrote: On Tuesday, 10 November 2015 at 10:21:32 UTC, tired_eyes wrote: Hi, The only example of string interpolation I've found so far is on Rosetta Code: void main() { import std.stdio, std.string; "Mary had a %s lamb.".format("little").writeln; "Mary had a %2$s %1$s lamb.".format("little", "white").writeln; } Is this a "proper" way of string interpolation in D? This looks a little clumsy. Are there any better approaches? Quick skimming through the docs didn't give anything. std.string.format and std.format are the standard options. What are you missing? Ruby: a = 1 b = 4 puts "The number #{a} is less than #{b}" PHP: $a = 1; $b = 4; echo "The number $a is less than $b"; D: ???
Re: String interpolation
On Tuesday, 10 November 2015 at 11:29:32 UTC, TheFlyingFiddle wrote: On Tuesday, 10 November 2015 at 10:41:52 UTC, tired_eyes wrote: On Tuesday, 10 November 2015 at 10:33:30 UTC, Tobias Pankrath wrote: Ruby: a = 1 b = 4 puts "The number #{a} is less than #{b}" PHP: $a = 1; $b = 4; echo "The number $a is less than $b"; D: ??? int a = 1, b = 4; writefln("The number %s is less than %s", a, b); You can't do it the ruby / perl / php way in D. It could be possible if we had AST macros in the language but as it stands now it's not possible to do that. the closest you could get is something like this: string s = aliasFormat!("The number $a is less than $b", a, b); or aliasWrite!("The number $a is less than $b", a, b); Not really recommended though as these would end up creating lots of template bloat. Thank everybody for the answers. I know about writefln, but I was hoping I just overlooked some simpler way.
std.socket replacement
I was a bit surprised to see that std.socket is deprecated as of 2.069. Just curious, what's wrong with it? And what should I use as a replacement? I know there is vibe.socket, but I don't want to include fullstack web framework as a dependency just to make some HTTP reqests. I also don't see any proposed replacements in a review queue. Will std.socket and std.socketstream be just thrown away?
Re: std.socket replacement
On Sunday, 29 November 2015 at 09:05:37 UTC, tcak wrote: On Sunday, 29 November 2015 at 08:56:30 UTC, tired_eyes wrote: I was a bit surprised to see that std.socket is deprecated as of 2.069. Just curious, what's wrong with it? And what should I use as a replacement? I know there is vibe.socket, but I don't want to include fullstack web framework as a dependency just to make some HTTP reqests. I also don't see any proposed replacements in a review queue. Will std.socket and std.socketstream be just thrown away? I would say "WTF" at first, then checked the documentation, but don't see anything about deprecation. My current whole business relies on that. Wow, sorry, I meant std.stream and std.socketstream, not std.socket and std.socketstream
Re: std.socket replacement
On Sunday, 29 November 2015 at 16:10:22 UTC, Alex Parrill wrote: std.stream, and the stream interface in general, is deprecated in favor of ranges, which are more generic and flexible. Could you please give a small example? Consider this minimal app: import std.stdio; import std.socket; import std.socketstream; void main() { auto socket = new TcpSocket(new InternetAddress("dlang.org", 80)); scope(exit) socket.close(); auto ss = new SocketStream(socket); ss.writeString("GET http://dlang.org HTTP/1.1\r\n" "Host: dlang.org\r\n" "Connection: close\r\n" "\r\n"); while (! ss.eof) { writeln(ss.readLine()); } } How should it look with ranges instead of socketstream? I thought I understand ranges in general, but I can't figure out how they can be applied to this case.
std.json questions
Hello, D community! I'm pretty new to D and to compiled languages in general, and have primarily web background (PHP, JS), when JSON workflow is very organic. I was always sure that JSON is a simple thing, but std.json proves me wrong. So may I have a little advice from more experienced D folk? Say, I have a simple JSON file: { "entities" : [ { "x" : 0, "y" : 0, "texture" : "box1" }, { "x" : 100, "y" : 200, "texture" : "box2", "isControllable" : true } ] } First issue: what is the proper ("idiomatic") way to conver JSONValue to the proper types? Second: what is the proper way of handling boolean values in JSON (how to convert JSON_TYPE.TRUE and JSON_TYPE.FALSE to bool)? Righ now I'm doing is something like this: string data = readText("file.json"); JSONValue[string] parsedData = parseJSON(data).object; JSONValue[] etities = stateData["entities"].array; foreach(e; entities) { int x = to!int(e["x"].integer); int y = to!int(e["y"].integer); string texture = stripExtension(e["texture"].str); auto isControllable = "isControllable" in e; if (isControllable !is null) { if (e["isControllable"].type == JSON_TYPE.TRUE) { isControllable = true; } else { isControllable = false; } } } I think this is ugly and clunky approach, what is the beautiful one? A brief look at code.dlang.org gives us 7 (!) additional JSON libraries. Keeping in mind that D community isn't so huge, I think I'm not the only person struggling with std.json. Are there any plans on upgrading it? Thank you in advance!
Re: std.json questions
Thank everybody for you help. For now, yajl-d seems to be an optimal for my task, however will keep an eye for stdx.data.json too.
std.random question
Feels pretty silly, but I can't compile this: import std.random; auto i = uniform(0, 10); DMD spits this: /usr/include/dmd/phobos/std/random.d(1188): Error: static variable initialized cannot be read at compile time /usr/include/dmd/phobos/std/random.d(1231):called from here: rndGen() /usr/include/dmd/phobos/std/random.d(1231):called from here: uniform(a, b, rndGen()) Perhaps I'm missing something obvious? dmd 2.067.1, openSUSE 13.2 x64
Re: std.random question
On Sunday, 3 May 2015 at 08:48:52 UTC, Dennis Ritchie wrote: On Sunday, 3 May 2015 at 08:42:57 UTC, tired_eyes wrote: Feels pretty silly, but I can't compile this: import std.random; auto i = uniform(0, 10); DMD spits this: /usr/include/dmd/phobos/std/random.d(1188): Error: static variable initialized cannot be read at compile time /usr/include/dmd/phobos/std/random.d(1231):called from here: rndGen() /usr/include/dmd/phobos/std/random.d(1231):called from here: uniform(a, b, rndGen()) Perhaps I'm missing something obvious? dmd 2.067.1, openSUSE 13.2 x64 void main() { import std.random; auto i = uniform(0, 10); } Not so simple, unfortunately. Actual code: import std.random; struct Mystruct { auto id = uniform(0, 10); } void main() { // wahtever } ..and no luck.
Re: std.random question
Hmmm. hap.random from http://code.dlang.org/packages/hap behaves exactly the same.
Re: std.random question
import std.random; struct Mystruct { int id; static opCall() { Mystruct s; s.id = uniform(0, 10); return s; } } void main() { auto s = Mystruct(); // whatever } --- This make sense, thant you for the explanation.
Array of objects and their inheritance
Hi. I'm having a hard time understanding D's inheritance. Consider the following code: class Parent { public int x = 10; } class Child : Parent { public int y = 20; } void main() { import std.stdio; Parent[] array; auto obj1 = new Parent(); auto obj2 = new Child(); array ~= obj1; array ~= obj2; writeln(array[0]); // prints "Parent", as expected writeln(array[1]); // prints "Child", so I assume that if it's a Child, we can access Child's fields writeln(array[0].x); // 10 writeln(array[1].y); // Error: no property 'y' for type 'Parent' } First, I don't understand why we see array[2] as 'Child'. While it is a 'Child', shouldn't it be shown as a 'Parent' due to we explicitly create an array of 'Parents'? Well, if it's still a 'Child', why we can't access it's fields? And what is the proper way of storing a collection of inherited objects without losing access to their fields and methods? Please point me in the right direction. I'm (still) relatively new to D, and will appreciate any help.
Re: Array of objects and their inheritance
Thank you for the explanation
Re: How to simulate a new type
On Friday, 15 May 2015 at 06:11:41 UTC, Charles Hixson wrote: On 05/14/2015 06:38 PM, Adam D. Ruppe via Digitalmars-d-learn wrote: On Friday, 15 May 2015 at 01:03:32 UTC, Charles Hixson wrote: Yes, that looks as if it would do the job, but what are its advantages over a simple struct? None really, except perhaps automatic forwarding of operators which is easy enough to do on a struct too (and with a struct, you can do only the ones that actually make sense for you). Typedef has a few major disadvantages too - surprising behavior if you forget the second parameter, for example. The library typedef should really be removed. Thank you. I was hoping that struct was a good approach, I've just never seen anyone recommending it, so I wanted to check. FWIW, I've seen a lot of user-defined types made with structs in the code of C-ported libs, and was sure that is a common approach.
Re: How to avoid multiple spelling `import`
On Tuesday, 16 June 2015 at 15:42:02 UTC, Dennis Ritchie wrote: On Tuesday, 16 June 2015 at 12:41:14 UTC, Daniel Kozák wrote: On Tue, 16 Jun 2015 11:45:22 + Dennis Ritchie via Digitalmars-d-learn wrote: I just want to import individual features of these modules. mixin template include(w...) { mixin _include!(w.length - 1, w); } mixin template _include(long N, i...) { mixin("import " ~ i[N] ~ ";"); mixin _include!(N - 1, i); } mixin template _include(long N : 0, i...) { mixin("import " ~ i[N] ~ ";"); } mixin include!( "std.stdio : writeln, write", "std.conv : to" ); void main() { writeln(to!string(7)); } Thanks. Maybe I'll use this code in your own programs. I still believe that this design deserves existence in D: https://issues.dlang.org/show_bug.cgi?id=14704 I also think this might be useful, and, more important, consistent. If we have shorthand syntax for full imports, why there is no option for shorthand partial imports? This is expected behavior.
Re: How to avoid multiple spelling `import`
On Wednesday, 17 June 2015 at 01:56:45 UTC, flamencofantasy wrote: On Tuesday, 16 June 2015 at 16:40:39 UTC, tired_eyes wrote: On Tuesday, 16 June 2015 at 15:42:02 UTC, Dennis Ritchie wrote: On Tuesday, 16 June 2015 at 12:41:14 UTC, Daniel Kozák wrote: [...] Thanks. Maybe I'll use this code in your own programs. I still believe that this design deserves existence in D: https://issues.dlang.org/show_bug.cgi?id=14704 I also think this might be useful, and, more important, consistent. If we have shorthand syntax for full imports, why there is no option for shorthand partial imports? This is expected behavior. No Can I have some more serious argumentation than just plain "no"?
std.net.curl
Hi, I'm trying to compile this trivial example of std.net.curl: // app.d import std.stdio; import std.net.curl; void main() { auto content = get("dlang.org"); } Hovewer, "dmd app.d" spits a whole bunch of strange error messages: /usr/lib64/libphobos2.a(curl.o): In function `_D3std3net4curl4HTTP21_sharedStaticCtor1520FZv': std/net/curl.d:(.text._D3std3net4curl4HTTP21_sharedStaticCtor1520FZv+0xf): undefined reference to `curl_version_info' /usr/lib64/libphobos2.a(curl.o): In function `_D3std3net4curl4Curl21_sharedStaticCtor1522FZv': std/net/curl.d:(.text._D3std3net4curl4Curl21_sharedStaticCtor1522FZv+0xa): undefined reference to `curl_global_init' /usr/lib64/libphobos2.a(curl.o): In function `_D3std3net4curl4Curl21_sharedStaticDtor1523FZv': std/net/curl.d:(.text._D3std3net4curl4Curl21_sharedStaticDtor1523FZv+0x5): undefined reference to `curl_global_cleanup' /usr/lib64/libphobos2.a(curl_1ca7_192.o): In function `_D3std3net4curl4HTTP4Impl6__dtorMFZv': std/net/curl.d:(.text._D3std3net4curl4HTTP4Impl6__dtorMFZv+0x19): undefined reference to `curl_slist_free_all' /usr/lib64/libphobos2.a(curl_1cca_ea.o): In function `_D3std3net4curl3FTP4Impl6__dtorMFZv': std/net/curl.d:(.text._D3std3net4curl3FTP4Impl6__dtorMFZv+0x18): undefined reference to `curl_slist_free_all' /usr/lib64/libphobos2.a(curl_1cca_ea.o): In function `_D3std3net4curl3FTP3dupMFZS3std3net4curl3FTP': std/net/curl.d:(.text._D3std3net4curl3FTP3dupMFZS3std3net4curl3FTP+0xab): undefined reference to `curl_slist_append' /usr/lib64/libphobos2.a(curl_1cca_ea.o): In function `_D3std3net4curl3FTP13clearCommandsMFZv': std/net/curl.d:(.text._D3std3net4curl3FTP13clearCommandsMFZv+0x20): undefined reference to `curl_slist_free_all' /usr/lib64/libphobos2.a(curl_1cca_ea.o): In function `_D3std3net4curl3FTP10addCommandMFAxaZv': std/net/curl.d:(.text._D3std3net4curl3FTP10addCommandMFAxaZv+0x67): undefined reference to `curl_slist_append' /usr/lib64/libphobos2.a(curl_1ccf_432.o): In function `_D3std3net4curl4Curl10initializeMFZv': std/net/curl.d:(.text._D3std3net4curl4Curl10initializeMFZv+0x3d): undefined reference to `curl_easy_init' /usr/lib64/libphobos2.a(curl_1cd0_149.o): In function `_D3std3net4curl4Curl3dupMFZS3std3net4curl4Curl': std/net/curl.d:(.text._D3std3net4curl4Curl3dupMFZS3std3net4curl4Curl+0x25): undefined reference to `curl_easy_duphandle' /usr/lib64/libphobos2.a(curl_1cd4_37c.o): In function `_D3std3net4curl4Curl8shutdownMFZv': std/net/curl.d:(.text._D3std3net4curl4Curl8shutdownMFZv+0x1a): undefined reference to `curl_easy_cleanup' /usr/lib64/libphobos2.a(curl_1cd6_14c.o): In function `_D3std3net4curl4Curl3setMFE3etc1c4curl10CurlOptionAxaZv': std/net/curl.d:(.text._D3std3net4curl4Curl3setMFE3etc1c4curl10CurlOptionAxaZv+0x5d): undefined reference to `curl_easy_setopt' /usr/lib64/libphobos2.a(curl_1cd7_14c.o): In function `_D3std3net4curl4Curl3setMFE3etc1c4curl10CurlOptionlZv': std/net/curl.d:(.text._D3std3net4curl4Curl3setMFE3etc1c4curl10CurlOptionlZv+0x2e): undefined reference to `curl_easy_setopt' /usr/lib64/libphobos2.a(curl_1cd8_14c.o): In function `_D3std3net4curl4Curl3setMFE3etc1c4curl10CurlOptionPvZv': std/net/curl.d:(.text._D3std3net4curl4Curl3setMFE3etc1c4curl10CurlOptionPvZv+0x2e): undefined reference to `curl_easy_setopt' /usr/lib64/libphobos2.a(curl_1cd9_207.o): In function `_D3std3net4curl4Curl5clearMFE3etc1c4curl10CurlOptionZv': std/net/curl.d:(.text._D3std3net4curl4Curl5clearMFE3etc1c4curl10CurlOptionZv+0x28): undefined reference to `curl_easy_setopt' /usr/lib64/libphobos2.a(curl_1cda_67c.o): In function `_D3std3net4curl4Curl16clearIfSupportedMFE3etc1c4curl10CurlOptionZv': std/net/curl.d:(.text._D3std3net4curl4Curl16clearIfSupportedMFE3etc1c4curl10CurlOptionZv+0x28): undefined reference to `curl_easy_setopt' /usr/lib64/libphobos2.a(curl_1cdb_2fb.o): In function `_D3std3net4curl4Curl7performMFE3std8typecons41__T4FlagVAyaa12_7468726f774f6e4572726f72Z4FlagZi': std/net/curl.d:(.text._D3std3net4curl4Curl7performMFE3std8typecons41__T4FlagVAyaa12_7468726f774f6e4572726f72Z4FlagZi+0x22): undefined reference to `curl_easy_perform' /usr/lib64/libphobos2.a(curl_1cb0_65b.o): In function `_D3std3net4curl4HTTP16addRequestHeaderMFAxaAxaZv': std/net/curl.d:(.text._D3std3net4curl4HTTP16addRequestHeaderMFAxaAxaZv+0x11b): undefined reference to `curl_slist_append' /usr/lib64/libphobos2.a(curl_1cd2_4a1.o): In function `_D3std3net4curl4Curl11errorStringMFiZAya': std/net/curl.d:(.text._D3std3net4curl4Curl11errorStringMFiZAya+0x11): undefined reference to `curl_easy_strerror' collect2: error: ld returned 1 exit status --- errorlevel 1 DMD 2.068.0, openSUSE 13.2 x64 I'm absolutely sure that I have licurl installed. "zypper info libcurl4" shows me that it's version is 7.43.0-2.1
Re: std.net.curl
On Monday, 17 August 2015 at 12:58:54 UTC, Adam D. Ruppe wrote: On Monday, 17 August 2015 at 12:52:37 UTC, tired_eyes wrote: Hovewer, "dmd app.d" spits a whole bunch of strange error messages: try "dmd -lcurl app.d" and see if that helps. Error: unrecognized switch '-lcurl'
Re: std.net.curl
On Monday, 17 August 2015 at 13:26:29 UTC, Adam D. Ruppe wrote: On Monday, 17 August 2015 at 13:04:31 UTC, tired_eyes wrote: Error: unrecognized switch '-lcurl' ooh I'm sorry, should have been `dmd -L-lcurl yourprogram.d` Yes, it did the trick.
Re: Dub package with C code
I meant if there is already a place where I can upload my post to. Something like blog.dlang.org OT: Once again, I'm absolutely sure tha D should have an official blog! Forums can't replace blogs, forums are for discussions, not for content presentation.
Compilation time profiling
Hi, are there any tools for compilation time profiling? I'm trying to find what part of the code increases compilation time and don't want to stumble around.
Re: Compilation time profiling
On Saturday, 24 October 2015 at 22:16:35 UTC, Vladimir Panteleev wrote: On Saturday, 24 October 2015 at 21:56:05 UTC, tired_eyes wrote: Hi, are there any tools for compilation time profiling? I'm trying to find what part of the code increases compilation time and don't want to stumble around. There's this: https://github.com/CyberShadow/DBuildStat Example output: https://github.com/rejectedsoftware/vibe.d/issues/208#issuecomment-15875240 Wow, looks promising, thank you! This definitely should be mentioned in the wiki. Mind if I do?