Re: How to serialize a double.
On Thursday, 1 December 2016 at 00:36:30 UTC, Jake Pittis wrote: How do I convert a double to a ubyte[]? I've tried all sorts of things including converting the double to a ulong and trying to serialize the ulong. For example test bellow fails. unittest { double d = 3.14; ulong l = *cast(ulong*)(&d); double after = *cast(double*)(&l)); assert(after == d); // This fails. } You could do something like below which will allow you to serialize any number. import std.stdio : writeln; import std.traits : isNumeric; ubyte[] bytes(T)(T num) if (isNumeric!T) { auto buf = new ubyte[T.sizeof]; (*cast(T*)(buf.ptr)) = num; return buf; } T value(T)(ubyte[] buf) if (isNumeric!T) { return (*cast(T*)(buf.ptr)); } And example usage: double foo = 3.14; writeln(foo); // Prints 3.14 ubyte[] bar = foo.bytes; writeln(bar); // Prints the bytes equal to 3.14 foo = bar.value!double; writeln(foo); // Prints 3.14
Re: Where should I dump workarounds?
On Wednesday, 30 November 2016 at 21:48:20 UTC, Johan Engelen wrote: Tip: forget about the `master` branch in your own fork on GH. I never use it. Instead, my advice is to locally checkout the master branch from the repo you want to contribute to (dlang/phobos in your case). That works and is fine? Great, no forking should simplify things a bit!
Where should I dump workarounds?
It is my code what caused it after all. How, I do not know yet. But a similar unittest compiled and passed when I tested with the master. Anyway, it seems I have no need to make other branches this time. Thanks for the advice trough. After all, I may need to rebase anyway when doing the pull request.
Re: how to catch D Throwables (or exceptions) from C++?
On Thursday, 1 December 2016 at 01:58:13 UTC, Timothee Cour wrote: eg: ``` dlib.d: extern(C) void dfun(){assert(0, "some_msg");} clib.cpp: extern "C" void dfun(); void fun(){ try{ dfun(); } catch(...){ // works but how do i get "some_msg" thrown from D? } } ``` portably not sure, but if you're using dwarf / libunwind you can probably use the API directly and get a pointer to the exception object and dereference a field of it's (i.e. p+ Throwable.msg.offsetof ).
Re: Use class template as a type
On Tuesday, 29 November 2016 at 15:56:23 UTC, Jerry wrote: To avoid having to use the Object class directly you can make an base class of the class template. Like: ``` abstract class MyClass {} abstract class MyClassImpl(T) { public: @property const(T) value(){return _value;} @property void value(T val){_value = val;} ... private: T _value; ... } MyClassInt and float inherits from MyClassImpl ``` And use it like: ``` void main() { MyClass[] objs; objs ~= new MyClassFloat(); objs ~= new MyClassInt(); } ``` Yes, but anyway you need to downcast if(MyClassBlahBlah subclass = cast(MyClassBlahBlah)obj)... So it's not much sense to have base class or interface MyClass.
how to catch D Throwables (or exceptions) from C++?
eg: ``` dlib.d: extern(C) void dfun(){assert(0, "some_msg");} clib.cpp: extern "C" void dfun(); void fun(){ try{ dfun(); } catch(...){ // works but how do i get "some_msg" thrown from D? } } ```
Re: How to serialize a double.
On Thursday, 1 December 2016 at 00:36:30 UTC, Jake Pittis wrote: How do I convert a double to a ubyte[]? I've tried all sorts of things including converting the double to a ulong and trying to serialize the ulong. For example test bellow fails. unittest { double d = 3.14; ulong l = *cast(ulong*)(&d); double after = *cast(double*)(&l)); assert(after == d); // This fails. } platform, archi, compiler version ?
Re: How to serialize a double.
On Thu, Dec 01, 2016 at 12:36:30AM +, Jake Pittis via Digitalmars-d-learn wrote: > How do I convert a double to a ubyte[]? > > I've tried all sorts of things including converting the double to a > ulong and trying to serialize the ulong. For example test bellow > fails. > > > unittest { > double d = 3.14; > ulong l = *cast(ulong*)(&d); > double after = *cast(double*)(&l)); > assert(after == d); // This fails. > } > union U { ubyte[double.sizeof] bytes; double d; } U u, v; u.d = 3.14159; v.bytes[] = u.bytes[]; assert(v.d == 3.14159); T -- Those who don't understand Unix are condemned to reinvent it, poorly.
Re: How to serialize a double.
On Thursday, 1 December 2016 at 00:36:30 UTC, Jake Pittis wrote: How do I convert a double to a ubyte[]? I've tried all sorts of things including converting the double to a ulong and trying to serialize the ulong. For example test bellow fails. unittest { double d = 3.14; ulong l = *cast(ulong*)(&d); double after = *cast(double*)(&l)); assert(after == d); // This fails. } That test passes for me, are you sure there isn't something else wrong with your code? Check to see if it works for just a ulong that has values in it's upper 32-bits?
How to serialize a double.
How do I convert a double to a ubyte[]? I've tried all sorts of things including converting the double to a ulong and trying to serialize the ulong. For example test bellow fails. unittest { double d = 3.14; ulong l = *cast(ulong*)(&d); double after = *cast(double*)(&l)); assert(after == d); // This fails. }
Re: Where should I dump workarounds?
On Wednesday, November 30, 2016 21:48:20 Johan Engelen via Digitalmars-d- learn wrote: > Tip: use both a GUI and the commandline. Without SourceTree [1], > I would be nowhere near as effective with git. > > With rebasing, you'll end up having to force push. Force pushing > is scary (because the repo state may have changed between you > updating your local state and you pushing), so it's nicer to use > --force-with-lease. [2] > Make an alias for that: > `git config --global alias.pushf "push --force-with-lease"` > then you can do "git pushf" to force push rebased branches > "safely". I can't remember the last time I used `git push > --force`. Really? I use git push -f all the time without problems. But I'm always pushing to a branch that's for a PR on github. So, normally, no one would have been doing anything with it but reviewing it or merging it, and after it's merged, there isn't even a reason to keep the branch around. So, forcing works great when dealing with PRs and github, but I wouldn't use it for much else. Certainly, it isn't at all appropriate for anything that you would normally expect folks to be branching from. - Jonathan M Davis
Re: Where should I dump workarounds?
On Wednesday, 30 November 2016 at 18:50:42 UTC, Dukc wrote: On Wednesday, 30 November 2016 at 18:26:32 UTC, Jonathan M Davis wrote: [snip] - Jonathan M Davis Luckily, I have made a branch for my stuff instead of using master. But thanks for the help, now I know that it does not matter where I create the other branch for that workaround because I can rebase it after I have used it to test my stuff. Tip: forget about the `master` branch in your own fork on GH. I never use it. Instead, my advice is to locally checkout the master branch from the repo you want to contribute to (dlang/phobos in your case). I just hope I don't screw the whole thing up with git commands... Tip: use both a GUI and the commandline. Without SourceTree [1], I would be nowhere near as effective with git. With rebasing, you'll end up having to force push. Force pushing is scary (because the repo state may have changed between you updating your local state and you pushing), so it's nicer to use --force-with-lease. [2] Make an alias for that: `git config --global alias.pushf "push --force-with-lease"` then you can do "git pushf" to force push rebased branches "safely". I can't remember the last time I used `git push --force`. -Johan [1] https://www.sourcetreeapp.com/ [2] https://stackoverflow.com/questions/30542491/push-force-with-lease-by-default
Re: Use class template as a type
On Wednesday, 30 November 2016 at 14:53:21 UTC, ag0aep6g wrote: On 11/30/2016 10:42 AM, Bauss wrote: Usually casts to base classes can be determined if they're valid at compile-time. Yeah, that's what I said. A cast to a base class is an "upcast". Upcasts don't need run-time checks. The other direction (cast to more derived class) is a downcast. Downcasts need run-time checks. Actually I've always called an "upcast" a "downcast" ! This incredible misconception explains why you had to correct me after my yesterday's answer.
Re: Where should I dump workarounds?
On Wednesday, November 30, 2016 18:50:42 Dukc via Digitalmars-d-learn wrote: > On Wednesday, 30 November 2016 at 18:26:32 UTC, Jonathan M Davis > > wrote: > > [snip] > > > > - Jonathan M Davis > > Luckily, I have made a branch for my stuff instead of using > master. But thanks for the help, now I know that it does not > matter where I create the other branch for that workaround > because I can rebase it after I have used it to test my stuff. > > I just hope I don't screw the whole thing up with git commands... Worst case, you copy your changes elsewhere, blow away your branch, create a new one, and then copy your changes back. So, I expect that you'll be fine. git takes some getting used to, but after a bit of practice, it usually isn't a problem unless you're trying to do something particularly complicated or abnormal. It has the right model for how it works. It just isn't as good with the CLI as it should be. But at least we get the "porcelain" layer. If we were stuck with what Linus had done with the CLI originally, git would never have gone anywhere. - Jonathan M Davis
Re: Where should I dump workarounds?
On Wednesday, 30 November 2016 at 18:26:32 UTC, Jonathan M Davis wrote: [snip] - Jonathan M Davis Luckily, I have made a branch for my stuff instead of using master. But thanks for the help, now I know that it does not matter where I create the other branch for that workaround because I can rebase it after I have used it to test my stuff. I just hope I don't screw the whole thing up with git commands...
Re: Where should I dump workarounds?
On Wednesday, November 30, 2016 17:14:37 Dukc via Digitalmars-d-learn wrote: > Well, I was working on std.range.chain (I'm new to contributing), > and when trying to test locally: > > ...\phobos\std\range>rdmd -unittest -main package > C:\D\dmd2\windows\bin\..\..\src\phobos\std\path.d(1319): Error: > pure function 'std.path.buildPath!char.buildPath' cannot call > impure function 'std.path.buildPath!(const(char)[][]).buildPath' > C:\D\dmd2\windows\bin\..\..\src\phobos\std\path.d(1319): Error: > @safe function 'std.path.buildPath!char.buildPath' cannot call > @system function 'std.path.buildPath!(const(char)[][]).buildPath' > C:\D\dmd2\windows\bin\..\..\src\phobos\std\path.d(1319): Error: > function 'std.path.buildPath!(const(char)[][]).buildPath' is not > nothrow > C:\D\dmd2\windows\bin\..\..\src\phobos\std\path.d(1315): Error: > nothrow function 'std.path.buildPath!char.buildPath' may throw > package.d(8718): Error: template instance std.path.buildPath!char > error instantiating > > Error in a totally different place! If my code caused this, I > have no idea how: I haven't touched unittests nor std.path. > > Since I don't think you like if my pr (if I manage to even do it) > contains fixes to other stuff than what I'm trying to do, I > wonder where I should start patching this. Should I make a new > branch off master and when done, merge it into the branch I have > now? Or should I make an entirely new fork? Well, normally, you wouldn't be using master to do pull requests at all. You create a branch for a particular fix, push that up to github, and then you create a PR from that branch. You can then have multiple, independent branches with fixes at once, and your master branch can then always match whatever the official master branch is. I don't think that anyone who contributes more than once or twice is creating pull requests from their master branch. And personally, I think that it's far too valuable to have your master branch match the official master branch to ever do anything on my own master branch. Whether separate PRs or one are better for what you're doing, I can't say, because I don't know what you're doing. But if you have an independent fix - particularly if the main thing that you're doing is an enhancement rather than a fix - then I'd say that you should create a branch for that and submit it. You can then create another branch from that one to work on whatever it is you're doing that needed that fix. Then, if you finish that before the first is merged, you'd either need to wait for the first one to be merged, or you'd need to remove the first fix's commits from the second branch (easy enough with git rebase) and create a PR from that with a comment that it depended on the other PR. If you wait to create the second PR (either because the first gets merged before the second is ready or because you just choose to wait), then you can rebase the second branch so that its changes go on top of the updated master (which should work without removing the first branch's commits from the second branch before rebasing if no other changes were made to the first branch before it was merged, but I'd probably just remove those commits from the second branch to ensure that there were no merge issues), and then you can create a PR from that. But git makes branching very easy (even if the commands are more confusing than they should be), so it makes a lot of sense to create separate branches for each fix, regardless of what gets merged when. In case you're not very familiar with git rebase, here's a relevant SO question (where I got to feel like an idiot for mixing up the git commands): http://stackoverflow.com/questions/7297379 - Jonathan M Davis
Where should I dump workarounds?
Well, I was working on std.range.chain (I'm new to contributing), and when trying to test locally: ...\phobos\std\range>rdmd -unittest -main package C:\D\dmd2\windows\bin\..\..\src\phobos\std\path.d(1319): Error: pure function 'std.path.buildPath!char.buildPath' cannot call impure function 'std.path.buildPath!(const(char)[][]).buildPath' C:\D\dmd2\windows\bin\..\..\src\phobos\std\path.d(1319): Error: @safe function 'std.path.buildPath!char.buildPath' cannot call @system function 'std.path.buildPath!(const(char)[][]).buildPath' C:\D\dmd2\windows\bin\..\..\src\phobos\std\path.d(1319): Error: function 'std.path.buildPath!(const(char)[][]).buildPath' is not nothrow C:\D\dmd2\windows\bin\..\..\src\phobos\std\path.d(1315): Error: nothrow function 'std.path.buildPath!char.buildPath' may throw package.d(8718): Error: template instance std.path.buildPath!char error instantiating Error in a totally different place! If my code caused this, I have no idea how: I haven't touched unittests nor std.path. Since I don't think you like if my pr (if I manage to even do it) contains fixes to other stuff than what I'm trying to do, I wonder where I should start patching this. Should I make a new branch off master and when done, merge it into the branch I have now? Or should I make an entirely new fork?
Re: Use class template as a type
On 11/30/2016 10:42 AM, Bauss wrote: Usually casts to base classes can be determined if they're valid at compile-time. Yeah, that's what I said. A cast to a base class is an "upcast". Upcasts don't need run-time checks. The other direction (cast to more derived class) is a downcast. Downcasts need run-time checks.
Re: Cannot implicitly convert expression (&sbuf) of type char [1024] to IOREQ *
On Wednesday, 30 November 2016 at 13:47:06 UTC, Anders S wrote: On Wednesday, 30 November 2016 at 12:41:24 UTC, Stefan Koch wrote: On Wednesday, 30 November 2016 at 10:20:35 UTC, Anders S wrote: int [1] argv; /* list of arguments */ Is that supposed to be a VLAIS ? That will not port to D. It would be helpful If you could share the code and state the intent. Hi, No to VLAIS (Variable length array in structure). All known size of arrays and structures. testing from terminal with writing to using: Echo "testing pipe and textformat" > .pipes/1234 and reading with cat .pipes/1234 that works just fine ;) /anders
Re: Cannot implicitly convert expression (&sbuf) of type char [1024] to IOREQ *
On Wednesday, 30 November 2016 at 12:41:24 UTC, Stefan Koch wrote: On Wednesday, 30 November 2016 at 10:20:35 UTC, Anders S wrote: int [1] argv; /* list of arguments */ Is that supposed to be a VLAIS ? That will not port to D. It would be helpful If you could share the code and state the intent. Hi, No to VLAIS (Variable length array in structure). All known size of arrays and structures. My intent is to find a way to write a request for data (the IOREQ.fc) to an application. The IOREQ is my message struct and the c-code on the other end of the pipe use same struct to identify request. to keep it backward compatible I need to keep IOREQ. The c-code application will switch out request and return the answer with char[] of data using the (IOREQ.src) dlang's created pipe. i.e. the pipes is oneway "streets" The purpose is to port a c-application to use dlang with a web ui Since I'm new to dlang but know c-code I need to ask stupid level questions ;) Also I'm experimenting on OS X but it is intended for linux x86 in production, for now. Only requirement is the struct IOREQ as sending structure and char[] as receiving structure, using FIFO pipe's. Outgoing pipe is fixed named and returning is based on mypid(). In pseudo code (don't have any working code yet) open pipe and if doesn't exist create it, to receiver open own pipe for reading. create ioreq *io; create spec sized char buffer point io to start of buffer add request to io.fc add return pipe to io.src write in pipe read pipe for answer, into char array close pipe disassemble response into various struct data depending on request. Here I simply try by returning the sent ioreq and echo out the fc and src. /anders
Re: Cannot implicitly convert expression (&sbuf) of type char [1024] to IOREQ *
On Wednesday, 30 November 2016 at 10:20:35 UTC, Anders S wrote: int [1] argv; /* list of arguments */ Is that supposed to be a VLAIS ? That will not port to D. It would be helpful If you could share the code and state the intent.
Re: Cannot implicitly convert expression (&sbuf) of type char [1024] to IOREQ *
On Wednesday, 30 November 2016 at 07:16:38 UTC, Anders S wrote: On Tuesday, 29 November 2016 at 23:33:19 UTC, Ali Çehreli wrote: On 11/29/2016 07:30 AM, Anders S wrote: Ali Thanks you all guys, and the cast (IOREQ *) ... did the trick!! I'll have a look at your other comments aswell on struct a.s.o. /anders Hi again, still have problem. It works when using plain text but not like this: (Hope you can identify my errors ;) ) import std.range, std.stdio, std.conv; import core.stdc.string; import core.stdc.stdio; import core.sys.posix.sys.stat; import core.sys.posix.unistd; import core.sys.posix.fcntl; import core.sys.posix.sys.stat; extern (C) uint read(int, void *, uint); extern (C) uint write(int, void *, ulong); struct IOREQ { short fc; /* function code */ short rs; /* return code */ int size; /* size of this request, including */ short src;/* source */ int [1] argv; /* list of arguments */ }; void main(string[] args) { //f; int fd = -1; IOREQ *io; fd = mkfifo("/Users/anders/pipes/8556",666 ); char [1024]rbuf; char [1024]sbuf; sbuf = "You must use the cast keyword, the pointer to first element of an array is .ptr, and I think you meant sbuf: io = cast(IOREQ *)sbuf.ptr;"; io = cast (IOREQ *) sbuf; io.fc = 501; io.rs = 1234; io.src = 8556; writeln("\nio.fc :", io.fc); writeln("io.rs :", io.rs); writeln("\nio.src :", io.src); writeln ("\nio :",io); writeln("Skrev ", write(fd, cast (void*) sbuf, sbuf.length)); writeln("läste ", read(fd, cast (void*) rbuf, sbuf.length)); writeln("\nrbuf :", rbuf); io = cast(IOREQ *) rbuf; writeln("\nrio.fc :", io.fc); writeln("\nrio.rs :", io.rs); writeln("\nrio.src :", io.src); //unlink("/Users/anders/pipes/8556"); }
Re: Use class template as a type
On Tuesday, 29 November 2016 at 15:56:23 UTC, Jerry wrote: On Monday, 28 November 2016 at 11:26:41 UTC, dm wrote: ``` abstract class MyClass(T) { public: @property const(T) value(){return _value;} @property void value(T val){_value = val;} ... private: T _value; ... } To avoid having to use the Object class directly you can make an base class of the class template. Like: ``` abstract class MyClass {} abstract class MyClassImpl(T) { public: @property const(T) value(){return _value;} @property void value(T val){_value = val;} ... private: T _value; ... } MyClassInt and float inherits from MyClassImpl ``` And use it like: ``` void main() { MyClass[] objs; objs ~= new MyClassFloat(); objs ~= new MyClassInt(); } ``` I would rather go with an interface than a base class.
Re: Use class template as a type
On Tuesday, 29 November 2016 at 09:58:16 UTC, ag0aep6g wrote: On 11/29/2016 02:21 AM, Basile B. wrote: The cast from a class type to a sub class in itself does absolutely nothing. That can't be right. A bad downcast gives you null, so it has to check the dynamic type information. Compare with upcasts which are statically known to be correct, so they don't need to check anything at runtime. Usually casts to base classes can be determined if they're valid at compile-time. Take this for an example: class Foo { } class Bar : Foo { } void main() { auto bar = new Bar; auto foo = cast(Foo)bar; // The compiler should know that bar is of type Bar, which is a subclass of Foo and thus the cast theoretically is redundant. } Even in a situation like this, the compiler should be able to see if the cast could ever be invalid during compile-time determined by calls to fun. void fun(Cast)(Bar bar) { return cast(Cast)bar; // If Cast is Foo then the compiler should know the cast is redundant. ] I don't know if the D compiler actually takes such situation into account, but I'd assume it does some kind of optimization in regards of that.
Re: How can I concatenate a string, a char array and an int
On Tuesday, 29 November 2016 at 15:01:37 UTC, Anders S wrote: Thanks guys for a really quick answer !! OK, a little bit awkward to use but getting there posting a new question about char * to struct ;) Thanks /anders Also: import std.conv : text; string temp = "This is a number"; string greeting5 = text(temp, " ", 314356); Andrea