Re: Format double in decimal notation without trailing zeros after the decimal point
Thank you. Actually, I'm doing this: format(%.4f, d).stripRight('0').stripRight('.') (not so elegant, but it works.) But I thinking that do not know much about the format string. On Sunday, 29 March 2015 at 03:29:26 UTC, Baz wrote: On Friday, 27 March 2015 at 15:02:19 UTC, akaDemik wrote: The task seemed very simple. But I'm stuck. I want to: 1234567890123.0 to 1234567890123 1.23 to 1.23 1.234567 to 1.2346. With format string %.4f i get 1.2300 for 1.23. With %g i get 1.23456789e+12 for 1234567890123.0. I can not believe that it is not implemented. What did I miss? such a format specifier does not exist. [.number] means the minimal digits to display, so there is always at least `number` digits. In your three examples, there is no common way to format them, you have to write you own helper: struct YourExoticFormater { private float _value; alias _value this; string toString() { // here you test the number and you choose how to diplay it. // for example if frac() returns 0 you return the string repr // esentation of the the integral part, etc... // this will work with to!string(), probably format %s (?), and the // write() functions family. } }
Re: Passing myself, a struct, as a C callback context
On Monday, 30 March 2015 at 02:53:36 UTC, Paul O'Neil wrote: I'm registering a callback with some C code. The simplified story is here, but the actual code is on GitHub [1] at the end if you care. The call looks something like this. void register(void(*fp)(void*), void* context); I have a class that holds state for the callback and registers itself: final class Klass { void method() { register(callback_function, this); } } `this` is already a reference. You're taking the address of that reference. A simple cast should work: `cast(void*) this`.
Re: Format double in decimal notation without trailing zeros after the decimal point
Thanks for the reply. I remember about the accuracy of floating point numbers. It is encouraging that the %g can handle it. format(%.17g, 123456.789123); // == 123456.789123 And we have a flag #. As mentioned in documentation: '#' floating Always insert the decimal point and print trailing zeros. With '#' I get: format(%#.17g, 123456.789123); // == 123456.7891230 So, with the flag '#' %g is almost similar to the %f. But there is no flag repealing '#' if it is enabled by default (in the case of %f). I looked deeper into the function format and realized that my question is more related to the implementation of snprintf. If I understand correctly, snprintf does the job, and std.format provides a safe and very convenient wrapper. On Friday, 27 March 2015 at 17:08:07 UTC, Steven Schveighoffer wrote: On 3/27/15 11:02 AM, akaDemik wrote: The task seemed very simple. But I'm stuck. I want to: 1234567890123.0 to 1234567890123 1.23 to 1.23 1.234567 to 1.2346. With format string %.4f i get 1.2300 for 1.23. With %g i get 1.23456789e+12 for 1234567890123.0. I can not believe that it is not implemented. What did I miss? I think you are asking for trouble to do this. Floating point is not exact, so for example, if I do writefln(%.15f, 123456.789123); I get: 123456.78912295016 How far do you want to go before you determine there will only be zeros? It could be infinity. I'd say your best bet is to format to the max level you want, e.g. %.6f Then trim off any trailing zeros (and decimal point if necessary) after conversion to a string. -Steve
Re: Windows - std.process - Setting env variables from D
On Monday, 30 March 2015 at 12:54:28 UTC, Adam D. Ruppe wrote: On Monday, 30 March 2015 at 12:28:19 UTC, wobbles wrote: Any solutions that people know of? You can't from an exe, it is a limitation of the operating system (same on Linux btw, environment variable inheritance is always from parent to child, never from child to parent). The reason batch files can do it is that they don't run in a separate process, they just run a batch of commands inside the shell itself. https://msdn.microsoft.com/en-us/library/windows/desktop/ms682009%28v=vs.85%29.aspx Altering the environment variables of a child process during process creation is the only way one process can directly change the environment variables of another process. A process can never directly change the environment variables of another process that is not a child of that process. If you're an administrator, you could poke the system-wide variables in the registry and tell the processes to reload them: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682653%28v=vs.85%29.aspx but of course, changing system-wide registry entries affects way more than just your parent shell! If you need to change a parent shell variable, the only way is to do it from a batch file. You could perhaps run a .bat which sets the variable and calls your exe to help it do some work. Thanks Adam, Yeah, I knew it was the case in Linux, I just figured as 'set' worked in a batch file that it must be possible in Windows. I think what I'm going to do is have my D program output the commands as strings that are required to set the ENV variables in the parent and then have a batch file to run the program, get its output and run the commands outputted from the D program. Can also have a bash file to do the same (using the source command). This is for setting up a build system we're using, and is normally run via Jenkins, so running it in a kind of ugly way doesnt really matter. We're currently maintaining two seperate scripts to do this work, I'm trying to consolidate them. Maintaining one large-ish D script to do this work and 2 mini scripts to call them should be easier to maintain than 2 large bash/batch scripts. Thanks!
Re: Windows - std.process - Setting env variables from D
On Monday, 30 March 2015 at 12:28:19 UTC, wobbles wrote: Any solutions that people know of? You can't from an exe, it is a limitation of the operating system (same on Linux btw, environment variable inheritance is always from parent to child, never from child to parent). The reason batch files can do it is that they don't run in a separate process, they just run a batch of commands inside the shell itself. https://msdn.microsoft.com/en-us/library/windows/desktop/ms682009%28v=vs.85%29.aspx Altering the environment variables of a child process during process creation is the only way one process can directly change the environment variables of another process. A process can never directly change the environment variables of another process that is not a child of that process. If you're an administrator, you could poke the system-wide variables in the registry and tell the processes to reload them: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682653%28v=vs.85%29.aspx but of course, changing system-wide registry entries affects way more than just your parent shell! If you need to change a parent shell variable, the only way is to do it from a batch file. You could perhaps run a .bat which sets the variable and calls your exe to help it do some work.
Re: Windows - std.process - Setting env variables from D
On Monday, 30 March 2015 at 12:28:19 UTC, wobbles wrote: I'm trying to set environment variables that will be visible when my D program exits. It is possible in a windows batch file using the set command (like set VAR=VALUE ) However, running this in D using: import std.process; import std.stdio; void main(){ auto pid1 = spawnShell(`set VAR=VALUE`); pid1.wait(); auto pid2 = spawnShell(`set`); pid2.wait(); } however, upon exit, there is no VAR=VALUE in the environment. Using std.process.environment[VAR]= VALUE; doesnt store the variable in the parent either. Any solutions that people know of? Type setx /? in the command shell. (Note the x). http://stackoverflow.com/questions/5898131/set-a-persistent-environment-variable-from-cmd-exe
Re: Specify an entire directory tree for string imports
On Monday, 30 March 2015 at 02:51:56 UTC, Baz wrote: It's a DMD Windows bug. It's just been reported 2 days ago: https://issues.dlang.org/show_bug.cgi?id=14349 so nothing wrong from you side. Ok, glad to see it's a bug and not a (fairly limiting) feature. I might take a stab at fixing it, if it's not too hard.
Re: Windows - std.process - Setting env variables from D
On Monday, 30 March 2015 at 13:29:06 UTC, wobbles wrote: On Monday, 30 March 2015 at 12:54:28 UTC, Adam D. Ruppe wrote: On Monday, 30 March 2015 at 12:28:19 UTC, wobbles wrote: Any solutions that people know of? You can't from an exe, it is a limitation of the operating system (same on Linux btw, environment variable inheritance is always from parent to child, never from child to parent). The reason batch files can do it is that they don't run in a separate process, they just run a batch of commands inside the shell itself. https://msdn.microsoft.com/en-us/library/windows/desktop/ms682009%28v=vs.85%29.aspx Altering the environment variables of a child process during process creation is the only way one process can directly change the environment variables of another process. A process can never directly change the environment variables of another process that is not a child of that process. If you're an administrator, you could poke the system-wide variables in the registry and tell the processes to reload them: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682653%28v=vs.85%29.aspx but of course, changing system-wide registry entries affects way more than just your parent shell! If you need to change a parent shell variable, the only way is to do it from a batch file. You could perhaps run a .bat which sets the variable and calls your exe to help it do some work. Thanks Adam, Yeah, I knew it was the case in Linux, I just figured as 'set' worked in a batch file that it must be possible in Windows. I think what I'm going to do is have my D program output the commands as strings that are required to set the ENV variables in the parent and then have a batch file to run the program, get its output and run the commands outputted from the D program. Can also have a bash file to do the same (using the source command). This is for setting up a build system we're using, and is normally run via Jenkins, so running it in a kind of ugly way doesnt really matter. We're currently maintaining two seperate scripts to do this work, I'm trying to consolidate them. Maintaining one large-ish D script to do this work and 2 mini scripts to call them should be easier to maintain than 2 large bash/batch scripts. Thanks! You tried setx, and it didn't work ? Or you don't want to set permanent environmental variables ?
Windows - std.process - Setting env variables from D
I'm trying to set environment variables that will be visible when my D program exits. It is possible in a windows batch file using the set command (like set VAR=VALUE ) However, running this in D using: import std.process; import std.stdio; void main(){ auto pid1 = spawnShell(`set VAR=VALUE`); pid1.wait(); auto pid2 = spawnShell(`set`); pid2.wait(); } however, upon exit, there is no VAR=VALUE in the environment. Using std.process.environment[VAR]= VALUE; doesnt store the variable in the parent either. Any solutions that people know of?
Re: D1 operator overloading in D2
On 3/30/15 1:42 AM, ketmar wrote: it's still working. moreover, it is used in Phobos! and yet it's not documented anywhere. what i want to know is whether they will be removed for good, or brought back and properly documented? the current situation is awful: compiler has special treatment for some aggregate members, but nothing in documentation tells you that. They technically can be removed for good, because an operator template can now be an alias (this was pretty recent, maybe 1 year ago?). There is still one thing that doesn't work right I think -- covariance. But doing so would break all code that uses it. I think at the very least, Phobos should replace all D1-style operators with D2 style. Dogfooding and all. Originally when the yay, look at these new template-style operators was posted, it was imagined that one could do: mixin(generateD2Operators); in your aggregate, and the links from the new style operators to the old style would give you an upgrade path without having to rewrite all your operators. This really wasn't possible until the alias update. But maybe it's time to add this to std.typecons. I think at the very least we should provide a link to the D1 documentation and say that D1 operators overloads are still supported, but their support is not guaranteed to continue, please use D2 operators wherever possible. Clearly, there is some work that should be done. I agree that if you come across old code, and you are unaware of the old style operators, you will be super-confused as to how the operators are even working. That can be very annoying. I'll put in a doc PR to reference the D1 documentation. -Steve
Re: Passing myself, a struct, as a C callback context
On 3/30/15 5:12 AM, Marc =?UTF-8?B?U2Now7x0eiI=?= schue...@gmx.net wrote: On Monday, 30 March 2015 at 02:53:36 UTC, Paul O'Neil wrote: I'm registering a callback with some C code. The simplified story is here, but the actual code is on GitHub [1] at the end if you care. The call looks something like this. void register(void(*fp)(void*), void* context); I have a class that holds state for the callback and registers itself: final class Klass { void method() { register(callback_function, this); } } `this` is already a reference. You're taking the address of that reference. A simple cast should work: `cast(void*) this`. To build on this further, this for a class is actually taking a local stack reference, this is why it's not allowed. And technically, cast(void*) this is dangerous in the general case because opCast can be overridden. If you absolutely need to get a pointer to a class reference, you would need to do this: auto x = this; auto p = x; For example, for a foolproof implementation of converting a class reference to void *, you would need to do: auto x = this; auto p = *(cast(void **)x); I wonder if those who made this change thought of this problem? -Steve
Re: D1 operator overloading in D2
On Mon, 30 Mar 2015 11:25:01 -0400, Steven Schveighoffer wrote: On 3/30/15 1:42 AM, ketmar wrote: it's still working. moreover, it is used in Phobos! and yet it's not documented anywhere. what i want to know is whether they will be removed for good, or brought back and properly documented? the current situation is awful: compiler has special treatment for some aggregate members, but nothing in documentation tells you that. They technically can be removed for good, because an operator template can now be an alias (this was pretty recent, maybe 1 year ago?). There is still one thing that doesn't work right I think -- covariance. But doing so would break all code that uses it. I think at the very least, Phobos should replace all D1-style operators with D2 style. Dogfooding and all. Originally when the yay, look at these new template-style operators was posted, it was imagined that one could do: mixin(generateD2Operators); in your aggregate, and the links from the new style operators to the old style would give you an upgrade path without having to rewrite all your operators. This really wasn't possible until the alias update. But maybe it's time to add this to std.typecons. I think at the very least we should provide a link to the D1 documentation and say that D1 operators overloads are still supported, but their support is not guaranteed to continue, please use D2 operators wherever possible. Clearly, there is some work that should be done. I agree that if you come across old code, and you are unaware of the old style operators, you will be super-confused as to how the operators are even working. That can be very annoying. I'll put in a doc PR to reference the D1 documentation. -Steve i actually replaced that with D2 opovers in Phobos, and it passes unittests, so i can create ER in bugzilla with patch for someone to turn it into PR. signature.asc Description: PGP signature
rvalue based copy
Hi, Surely I am misunderstanding something. I got something like this : struct S { void opAssign(const ref s) { //... } } S genS() { S s; //... return s; } main() { S s; s = genS(); } DMD says : ...opAssign (ref const(S) point) is not callable using argument types (S). Then how to do what I wanna do ? Why doesn't this works ? (I am gessing ref argument explitly means no rvalue) Thanks in advance for your help ! :)
Re: Windows - std.process - Setting env variables from D
On Monday, 30 March 2015 at 14:14:50 UTC, Laeeth Isharc wrote: On Monday, 30 March 2015 at 13:29:06 UTC, wobbles wrote: On Monday, 30 March 2015 at 12:54:28 UTC, Adam D. Ruppe wrote: On Monday, 30 March 2015 at 12:28:19 UTC, wobbles wrote: Any solutions that people know of? You can't from an exe, it is a limitation of the operating system (same on Linux btw, environment variable inheritance is always from parent to child, never from child to parent). The reason batch files can do it is that they don't run in a separate process, they just run a batch of commands inside the shell itself. https://msdn.microsoft.com/en-us/library/windows/desktop/ms682009%28v=vs.85%29.aspx Altering the environment variables of a child process during process creation is the only way one process can directly change the environment variables of another process. A process can never directly change the environment variables of another process that is not a child of that process. If you're an administrator, you could poke the system-wide variables in the registry and tell the processes to reload them: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682653%28v=vs.85%29.aspx but of course, changing system-wide registry entries affects way more than just your parent shell! If you need to change a parent shell variable, the only way is to do it from a batch file. You could perhaps run a .bat which sets the variable and calls your exe to help it do some work. Thanks Adam, Yeah, I knew it was the case in Linux, I just figured as 'set' worked in a batch file that it must be possible in Windows. I think what I'm going to do is have my D program output the commands as strings that are required to set the ENV variables in the parent and then have a batch file to run the program, get its output and run the commands outputted from the D program. Can also have a bash file to do the same (using the source command). This is for setting up a build system we're using, and is normally run via Jenkins, so running it in a kind of ugly way doesnt really matter. We're currently maintaining two seperate scripts to do this work, I'm trying to consolidate them. Maintaining one large-ish D script to do this work and 2 mini scripts to call them should be easier to maintain than 2 large bash/batch scripts. Thanks! You tried setx, and it didn't work ? Or you don't want to set permanent environmental variables Yep, correct. Don't want them to be permanent. The systems have to be clean for other tests at all times, so they need to be on a shell by shell basis sadly.
Re: string concatenation with %s
On Monday, 30 March 2015 at 17:18:01 UTC, Suliman wrote: same problem. I am preparing string to next SQL request: string sss = format(SELECT * FROM test.imgs WHERE src LIKE CONCAT('%', REPLACE(CAST(CURDATE()as char), -, ), '%') OR CONCAT('%', CAST(CURDATE()as char), '%')); Here's your code without SQL noise: string sss = format(foo-, bar); It should be obvious now that you forgot to escape those double quotes.
Re: rvalue based copy
On Monday, 30 March 2015 at 17:20:30 UTC, matovitch wrote: Yes but you know what they say does it really do a copy of the struct or is the compiler smart enougth most of the time to avoid copy. (I think it's called return value optimization). Copying isn't necessarily a problem, for small structs it is more efficient to copy than passing by ref. But the return value optimization *is* typically done, yes. Why is this only restricted to templates ? It makes two versions of the function, like overloading on the two types of arguments automatically.
Re: rvalue based copy
On Monday, 30 March 2015 at 17:21:53 UTC, Steven Schveighoffer wrote: One solution is to overload void opAssign(ref const S s) {...} void opAssign(const S s) {...} lvalues will go into the ref version, rvalues into the non-ref. There won't be any copying of data, so you still save a postblit and copying on the stack. But you have to repeat the implementation. You can call the ref version from the non-ref version: void opAssign(ref const S s) {...} void opAssign(const S s) {opAssign(s); /* calls the ref version */} Of course, only do this when the ref version doesn't store s.
Re: string concatenation with %s
string sss = format(foo-, bar); It should be obvious now that you forgot to escape those double quotes. Thanks! Is there any way to stay string as is. without need of it's escaping and so on? It's seems I have seen something like it in docs, but I am not sure about it...
Re: string concatenation with %s
On Monday, 30 March 2015 at 17:34:20 UTC, Suliman wrote: string sss = format(foo-, bar); It should be obvious now that you forgot to escape those double quotes. Thanks! Is there any way to stay string as is. without need of it's escaping and so on? It's seems I have seen something like it in docs, but I am not sure about it... There are various other string literal forms [1] where you don't need to escape double quotes: AlternateWysiwygString - backticks as delimiters: `foo -, bar` DelimitedString - can use delimiters of choice, here parentheses: q(foo -, bar) TokenString - for D code, probably not a good choice here, but works: q{foo -, bar} Of course, as they're all delimiter based, there will always something you can't put into the string verbatim. [1] http://dlang.org/lex.html#StringLiteral
Re: rvalue based copy
On Monday, 30 March 2015 at 17:14:27 UTC, Adam D. Ruppe wrote: On Monday, 30 March 2015 at 17:09:14 UTC, matovitch wrote: (I am gessing ref argument explitly means no rvalue) That's right. I'd first say don't use ref, just use const S and it will work and probably do what you need efficiently. Yes but you know what they say does it really do a copy of the struct or is the compiler smart enougth most of the time to avoid copy. (I think it's called return value optimization). If you do want it to be ref though, rvalues aren't allowed unless you make it auto ref which needs to be a template: // this will work, second set of () makes it a template // then auto ref makes it use ref for lvalues and non-ref for rvalues // automatially void opAssign()(const auto ref S s) { //... } Why is this only restricted to templates ?
Re: rvalue based copy
On 3/30/15 1:09 PM, matovitch wrote: Hi, Surely I am misunderstanding something. I got something like this : struct S { void opAssign(const ref s) { //... } } S genS() { S s; //... return s; } main() { S s; s = genS(); } DMD says : ...opAssign (ref const(S) point) is not callable using argument types (S). Then how to do what I wanna do ? Why doesn't this works ? (I am gessing ref argument explitly means no rvalue) Thanks in advance for your help ! :) One solution is to overload void opAssign(ref const S s) {...} void opAssign(const S s) {...} lvalues will go into the ref version, rvalues into the non-ref. There won't be any copying of data, so you still save a postblit and copying on the stack. But you have to repeat the implementation. Another possibility is to use auto ref, but that requires a template. Annoying as this is (and blatantly awkward), it saves you from having to implement twice: void opAssign(T)(auto ref const T s) if(is(T == S)) {...} -Steve
Re: Mapping with partial
(it's not on line 79 obviously, you got me :D)
Mapping with partial
Hi again, I have this simple toy code : import point; import std.random; import std.algorithm; import std.functional; void getRandomPoint(R)(R randVar, ref Point p) { p.x = randVar; p.y = randVar; p.z = randVar; } void main() { Point[500] points; auto randVar = uniform(0.0f, 1.0f); alias test = partial!(getRandomPoint, randVar); points.map!(test); } And get the following error : kmeans_example.d(79): Error: template std.algorithm.iteration.map cannot deduce function from argument types !(partial)(Point[500]), candidates are: /usr/include/dmd/phobos/std/algorithm/iteration.d(434): std.algorithm.iteration.map(fun...) if (fun.length = 1) Btw, I don't understand why : auto test = partial!(getRandomPoint, randVar); dont work...partial doesnt return a delegate it seems...:/
Re: rvalue based copy
On Monday, 30 March 2015 at 17:09:14 UTC, matovitch wrote: (I am gessing ref argument explitly means no rvalue) That's right. I'd first say don't use ref, just use const S and it will work and probably do what you need efficiently. If you do want it to be ref though, rvalues aren't allowed unless you make it auto ref which needs to be a template: // this will work, second set of () makes it a template // then auto ref makes it use ref for lvalues and non-ref for rvalues // automatially void opAssign()(const auto ref S s) { //... }
Re: rvalue based copy
void opAssign(const ref s) should be void opAssign(const ref S s) btw and btw bis, I should probably make it const ref SopAssign(const ref S s) :/ I stop flooding there.
Re: Windows - std.process - Setting env variables from D
You tried setx, and it didn't work ? Or you don't want to set permanent environmental variables Yep, correct. Don't want them to be permanent. The systems have to be clean for other tests at all times, so they need to be on a shell by shell basis sadly. Thanks - was curious to know. Laeeth.
Re: rvalue based copy
The title should be assignement not copy.
Re: string concatenation with %s
same problem. I am preparing string to next SQL request: string sss = format(SELECT * FROM test.imgs WHERE src LIKE CONCAT('%', REPLACE(CAST(CURDATE()as char), -, ), '%') OR CONCAT('%', CAST(CURDATE()as char), '%')); but I am getting next error: source\app.d(178): Error: invalid array operation SELECT * FROM test.imgs WHERE src LIKE CONCAT('%', REPLACE(CAST(CURDATE()as char), - , ), '%') OR CONCAT(' %', CAST(CURDATE()as char), '%') (possible missing []) FAIL .dub\build\application-debug-windows-x86-dmd_2067-112ADE4A65EFD822A10EE5558 208F889\ geodataloader executable
Re: rvalue based copy
On 3/30/15 1:42 PM, matovitch wrote: On Monday, 30 March 2015 at 17:21:53 UTC, Steven Schveighoffer wrote: Annoying as this is (and blatantly awkward), it saves you from having to implement twice: void opAssign(T)(auto ref const T s) if(is(T == S)) {...} Yep, this seems awkward to me too thought according to Adam one can do : void opAssign()(auto ref const S s) {...} Yeah, if Adam says it works, it probably does. I thought it didn't, but I think it's only types that don't allow you to omit the compile-time parameters, not functions. -Steve
Re: rvalue based copy
On Monday, 30 March 2015 at 17:21:53 UTC, Steven Schveighoffer wrote: Annoying as this is (and blatantly awkward), it saves you from having to implement twice: void opAssign(T)(auto ref const T s) if(is(T == S)) {...} Yep, this seems awkward to me too thought according to Adam one can do : void opAssign()(auto ref const S s) {...}
Re: Mapping with partial
On Monday, 30 March 2015 at 18:23:32 UTC, Adam D. Ruppe wrote: On Monday, 30 March 2015 at 18:07:18 UTC, matovitch wrote: kmeans_example.d(79): Error: template std.algorithm.iteration.map That error is easy: use points[].map!(test) instead of points.map. Since points is a static array, it isn't a range. Static arrays can't be popped through. But if you slice it, then it yields a usable range for map. The other problem though is the partial!(). It expects a template argument for the thing so it can make a new function right there at compile time... which doesn't work with a runtime variable. The way I'd do it is just with a little hand written delegate. This will compile, for example: auto test = (ref Point p) = getRandomPoint(randVar, p); points[].map!(test); and should do what you need. Nice ! Thanks for the tip ! I tried importing std.range and points.array works too.
Re: Mapping with partial
Well I have a bit of a similar problem with foreach. If I use classic T[] range, I can do : foreach(int i, auto t, myRange)... But if I use an Array!T (from std.container) I get : cannot infer argument types, expected 1 argument, not 2 Even if I add the brackets []. Any idea ? Thanks for your help ! :)
Re: D1 operator overloading in D2
Wow, it's quite old, 2.041.
Re: Mapping with partial
On Monday, 30 March 2015 at 18:29:32 UTC, matovitch wrote: I tried importing std.range and points.array works too. Aye, that would work too, but the slice I think is more efficient as I'm pretty sure... not completely sure, but I think .array makes a copy of static arrays, whereas the slice doesn't.
Re: Mapping with partial
On Monday, 30 March 2015 at 19:11:10 UTC, matovitch wrote: That settle the point for array as for [] ? I though that was clear. [] doesn't copy. I guess the documentation should have something to say about it too. ;) hopefully
Re: Mapping with partial
On Monday, 30 March 2015 at 19:08:24 UTC, anonymous wrote: On Monday, 30 March 2015 at 18:37:53 UTC, matovitch wrote: On Monday, 30 March 2015 at 18:34:19 UTC, Adam D. Ruppe wrote: [...] Aye, that would work too, but the slice I think is more efficient as I'm pretty sure... not completely sure, but I think .array makes a copy of static arrays, whereas the slice doesn't. I was going to ask you the question does it just add the range shell or does it make a copy ? :/ Maybe someone else know. Let's check the documentation. http://dlang.org/phobos/std_array.html#array says: Allocates an array and initializes it with copies of the elements of range r. Documentation says copy. Let's check the actual behaviour. void main() { int[1] a = [1]; import std.array: array; a.array[0] = 2; import std.stdio: writeln; writeln(a[0]); } (also at http://dpaste.dzfl.pl/1191144a9acf) This program prints 1. That's the output we'd expect when `array` makes a copy. Actual behaviour says copy. So, copy. That settle the point for array as for [] ? I guess the documentation should have something to say about it too. ;)
Re: Mapping with partial
On Monday, 30 March 2015 at 19:15:25 UTC, matovitch wrote: Language ref - Array - Slice An array slice does not copy the data, it is only another reference to it. So the total slice of a static array is a range using the underlying memory of the static array isnt it ? yes
Re: Mapping with partial
On Monday, 30 March 2015 at 19:31:54 UTC, anonymous wrote: On Monday, 30 March 2015 at 19:03:05 UTC, matovitch wrote: Well I have a bit of a similar problem with foreach. If I use classic T[] range, I can do : foreach(int i, auto t, myRange)... But if I use an Array!T (from std.container) I get : cannot infer argument types, expected 1 argument, not 2 Even if I add the brackets []. Any idea ? Thanks for your help ! :) The index is the problem. Generally, foreach doesn't do automatic indices for ranges. You can use std.range.enumerate or count yourself explicitly. foreach(i, t; myRange.enumerate) {...} size_t i = 0; foreach(t; myRange) {... ++i;} Is it a compiler problem or a language restriction ? Thanks for the workaround btw, I will try it !
Re: Mapping with partial
Language ref - Array - Slice An array slice does not copy the data, it is only another reference to it. So the total slice of a static array is a range using the underlying memory of the static array isnt it ?
Re: Mapping with partial
On Monday, 30 March 2015 at 19:03:05 UTC, matovitch wrote: Well I have a bit of a similar problem with foreach. If I use classic T[] range, I can do : foreach(int i, auto t, myRange)... But if I use an Array!T (from std.container) I get : cannot infer argument types, expected 1 argument, not 2 Even if I add the brackets []. Any idea ? Thanks for your help ! :) The index is the problem. Generally, foreach doesn't do automatic indices for ranges. You can use std.range.enumerate or count yourself explicitly. foreach(i, t; myRange.enumerate) {...} size_t i = 0; foreach(t; myRange) {... ++i;}
Re: Mapping with partial
On Monday, 30 March 2015 at 18:34:19 UTC, Adam D. Ruppe wrote: On Monday, 30 March 2015 at 18:29:32 UTC, matovitch wrote: I tried importing std.range and points.array works too. Aye, that would work too, but the slice I think is more efficient as I'm pretty sure... not completely sure, but I think .array makes a copy of static arrays, whereas the slice doesn't. I was going to ask you the question does it just add the range shell or does it make a copy ? :/ Maybe someone else know.
Re: Mapping with partial
Thanks. On a sader note, I found a respons'less thread about my second question : http://forum.dlang.org/thread/mailman.2247.1353945423.5162.digitalmars-d-le...@puremagic.com where std.container.Array is concerned: how come I can't use a foreach(i, x; myArray) formulation? I.e. one where the foreach can infer the index value as well as the contained value ...
Re: Mapping with partial
On Monday, 30 March 2015 at 18:37:53 UTC, matovitch wrote: On Monday, 30 March 2015 at 18:34:19 UTC, Adam D. Ruppe wrote: [...] Aye, that would work too, but the slice I think is more efficient as I'm pretty sure... not completely sure, but I think .array makes a copy of static arrays, whereas the slice doesn't. I was going to ask you the question does it just add the range shell or does it make a copy ? :/ Maybe someone else know. Let's check the documentation. http://dlang.org/phobos/std_array.html#array says: Allocates an array and initializes it with copies of the elements of range r. Documentation says copy. Let's check the actual behaviour. void main() { int[1] a = [1]; import std.array: array; a.array[0] = 2; import std.stdio: writeln; writeln(a[0]); } (also at http://dpaste.dzfl.pl/1191144a9acf) This program prints 1. That's the output we'd expect when `array` makes a copy. Actual behaviour says copy. So, copy.
Re: Mapping with partial
On Monday, 30 March 2015 at 18:07:18 UTC, matovitch wrote: kmeans_example.d(79): Error: template std.algorithm.iteration.map That error is easy: use points[].map!(test) instead of points.map. Since points is a static array, it isn't a range. Static arrays can't be popped through. But if you slice it, then it yields a usable range for map. The other problem though is the partial!(). It expects a template argument for the thing so it can make a new function right there at compile time... which doesn't work with a runtime variable. The way I'd do it is just with a little hand written delegate. This will compile, for example: auto test = (ref Point p) = getRandomPoint(randVar, p); points[].map!(test); and should do what you need.
What ?
Hi again again, ulong u = 1 63; Raise : Error: shift by 63 is outside the range 0..31 This is a bug isn't it, the ulong are supposed to be on 64 bits ? I guess it's time I go to bed. Have a nice night !
Re: What ?
On Monday, 30 March 2015 at 22:34:00 UTC, matovitch wrote: Hi again again, ulong u = 1 63; Raise : Error: shift by 63 is outside the range 0..31 This is a bug isn't it, the ulong are supposed to be on 64 bits ? I guess it's time I go to bed. Have a nice night ! ulong, yes, but 1 is of type int. Correct code: ulong u = 1L 63;
Re: What ?
On Monday, 30 March 2015 at 22:34:55 UTC, Vladimir Panteleev wrote: On Monday, 30 March 2015 at 22:34:00 UTC, matovitch wrote: Hi again again, ulong u = 1 63; Raise : Error: shift by 63 is outside the range 0..31 This is a bug isn't it, the ulong are supposed to be on 64 bits ? I guess it's time I go to bed. Have a nice night ! ulong, yes, but 1 is of type int. Correct code: ulong u = 1L 63; Arf ! Safety. Danke !
Re: What ?
Brian Schott: Do this instead: ulong u = 1L 63; I suggest a more explicit: ulong u = 1UL 63; Alternative: ulong u = 2UL ^^ 63; Bye, bearophile
Re: What ?
On Monday, 30 March 2015 at 22:34:00 UTC, matovitch wrote: Hi again again, ulong u = 1 63; Raise : Error: shift by 63 is outside the range 0..31 This is a bug isn't it, the ulong are supposed to be on 64 bits ? I guess it's time I go to bed. Have a nice night ! The problem is that `1` isn't a ulong. The reason for this is probably C compatibility. Do this instead: ulong u = 1L 63;
Re: Specify an entire directory tree for string imports
On Monday, 30 March 2015 at 14:01:54 UTC, Alex Parrill wrote: On Monday, 30 March 2015 at 02:51:56 UTC, Baz wrote: It's a DMD Windows bug. It's just been reported 2 days ago: https://issues.dlang.org/show_bug.cgi?id=14349 so nothing wrong from you side. Ok, glad to see it's a bug and not a (fairly limiting) feature. I might take a stab at fixing it, if it's not too hard. The limitation is probably intentional, but the reasoning is unreasonably restrictive security limitations (something about path sanitizing being more difficult on Windows than POSIX, which doesn't apply to DMD).
Re: Mapping with partial
On Mon, 30 Mar 2015 19:36:49 +, matovitch wrote: The index is the problem. Generally, foreach doesn't do automatic indices for ranges. You can use std.range.enumerate or count yourself explicitly. foreach(i, t; myRange.enumerate) {...} size_t i = 0; foreach(t; myRange) {... ++i;} Is it a compiler problem or a language restriction ? it is by design. `foreach` is not a fancy for with hidden counter, it's more high-level construct. so if range isn't providing the counter, `foreach` will not guess why, and simply doesn't provide it too. signature.asc Description: PGP signature
Re: Passing myself, a struct, as a C callback context
On 03/30/2015 11:32 AM, Steven Schveighoffer wrote: On 3/30/15 5:12 AM, Marc =?UTF-8?B?U2Now7x0eiI=?= schue...@gmx.net wrote: `this` is already a reference. You're taking the address of that reference. A simple cast should work: `cast(void*) this`. To build on this further, this for a class is actually taking a local stack reference, this is why it's not allowed. And technically, cast(void*) this is dangerous in the general case because opCast can be overridden. If you absolutely need to get a pointer to a class reference, you would need to do this: auto x = this; auto p = x; For example, for a foolproof implementation of converting a class reference to void *, you would need to do: auto x = this; auto p = *(cast(void **)x); I wonder if those who made this change thought of this problem? -Steve Thanks for the explanation. This makes a lot of sense - I forgot that ref actually means special pointer. Luckily, I don't plan on overriding opCast! -- Paul O'Neil Github / IRC: todayman
using exceptions in @nogc
was this ever solved? I did some research and saw static immutable ones suggested a few times, but they can't be chained AFAIK.
Re: using exceptions in @nogc
On Tue, 31 Mar 2015 01:40:52 +, weaselcat wrote: was this ever solved? nope. there were some suggestions, but no decision was made. signature.asc Description: PGP signature
Re: D1 operator overloading in D2
On Mon, 30 Mar 2015 11:25:01 -0400, Steven Schveighoffer wrote: here is ER with patches: https://issues.dlang.org/show_bug.cgi?id=14382 sorry for breaking my promise of not making ERs anymore. ;-) signature.asc Description: PGP signature
Re: using exceptions in @nogc
On Tue, 31 Mar 2015 05:34:11 +, weaselcat wrote: On Tuesday, 31 March 2015 at 03:12:42 UTC, ketmar wrote: On Tue, 31 Mar 2015 01:40:52 +, weaselcat wrote: was this ever solved? nope. there were some suggestions, but no decision was made. sigh do you know if there's an open enhancement request for this? sorry, i don't know. and my bugzilla-fu is very bad, so i can't find it, yet i remember that here was one. maybe it's a false memory, though. signature.asc Description: PGP signature
Re: using exceptions in @nogc
On Tuesday, 31 March 2015 at 03:12:42 UTC, ketmar wrote: On Tue, 31 Mar 2015 01:40:52 +, weaselcat wrote: was this ever solved? nope. there were some suggestions, but no decision was made. sigh do you know if there's an open enhancement request for this?
gdc and ldc command line examples?
Hey all, I am finally working on moving out of dmd territory and playing with gdc and ldc. I was hoping that I could get some links to some example command lines. I'm mainly interested command lines regarding linking to libraries and building static libraries. My guess is that gdc will function essentially the same as gcc for these things, or at least close enough to where I can figure it out, but I have had no experience with ldc and I am having trouble tracking down documentation for it. Thanks!
Re: std.logger sharedLog usage
On Monday, 30 March 2015 at 04:05:12 UTC, lobo wrote: Thank you, lobo. next version will have equal default LogLevel for all Logger. https://github.com/D-Programming-Language/phobos/pull/3124