overloading evaluation (treating objects as functions)
Is it possible to define a class F so that auto f=new F(); writeln(The value of f at 7 is ,f(7)); compiles and works as expected? So the idea would be to be able to use notation like f(7) instead of f.eval(7) or something along those lines. My guess is no, it is impossible to do this, because i can't find it on the internet or in Alexandrescu's book. But it is also possible that everybody considers it so obvious that they just don't elaborate on it. I'd be delighted if there were the case, at least if somebody would elaborate on it if so. TIA for any info! dan
Re: overloading evaluation (treating objects as functions)
Awesome!! Thanks Gary and namespace (and obviously i gotta improve my google-fu). dan On Sunday, 17 May 2015 at 19:40:10 UTC, Gary Willoughby wrote: On Sunday, 17 May 2015 at 18:58:32 UTC, Namespace wrote: http://dlang.org/operatoroverloading.html#function-call Like this: module main; import std.stdio; class F { int opCall(int value) { return value * 2; } } void main(string[] args) { auto f = new F(); writeln(The value of f at 7 is , f(7)); }
Re: Can't call GetWindowTextW - Error:undefined identifier
thank you John it worked :) do I always need do the same for all windows API?
Re: Can't call GetWindowTextW - Error:undefined identifier
thank you so much John :)
Can't call GetWindowTextW - Error:undefined identifier
hi I'm using those imports: import core.runtime; import core.sys.windows.windows; when I call GetWindowTextW DMD compiler complains! (error:undefined identifier) any solution?
Re: Can't call GetWindowTextW - Error:undefined identifier
GetWindowTextW(hWindow, buffer, sizeof(title)); -- Problem here please Ignore the sizeof(title) parameter, I copied that from c++ equivalent code :D
Re: Can't call GetWindowTextW - Error:undefined identifier
I'm new to Dlang and I have no Idea whats wrong with this code! wchar[260] buffer; HWND hWindow = GetForegroundWindow(); GetWindowTextW(hWindow, buffer, sizeof(title)); -- Problem here
Looking for a language to hang my hat on.
I am a very new c++ programmer, having just learned the language this year. A few months ago I completed a course on Coursera that dealt with the security aspect of c (which I don't know, but it is similar enough): https://class.coursera.org/softwaresec-008 The course highlighted just how dangerous c/c++ can be. My reaction to the course has been an increased use of shared/weak pointers over standard pointers, more judicious use of "assert" and increased use of destructors, where class pointers are destroyed via the destructor so I don't need to worry about memory leaks as much (all of my non-vector arrays are created/destroyed via a class w/ template). Some of this slows programs down, but in reality it does not have much of an impact. But doubt will always linger that I caught every vulnerability. Therefore I am shopping for a language that codes like c++ but is safer. It sounds like D may fit that requirement. My platform of choice is 64-bit Fedora using Code::Blocks (yes, I use an IDE as a crutch). It seems that D supports this combo. I have been lurking on this site over the past few weeks trying to decide when (and if) to make the transition. Can anyone here who has already made that transition tell me how smoothly it went? Any major unexpected problems? Advice? thanks! Dan
Re: Looking for a language to hang my hat on.
Thanks everyone for taking the time to respond! @Lobo, Start using D now. It's not all or nothing so you don't have to give up on C++. I have several projects that contain both C++ and D intermixed. Using both does seem like a good way to transition. I could combine the strengths of D with the strengths of c++. I have never mixed two programming languages in one project, all have contained one language exclusively. This is another bridge to cross. D will make you a better C++ programmer, but especially C++ template programming. D metaprogramming is so easy to read, write and understand compared to C++ and many of the patterns still apply when you're standing knee deep in C++it. I use c++ templates extensively, and if D offers a better solution that is fantastic. @Chris Wright, Your largest problem in the short term is documentation quality. This concerns me since it makes it very difficult for people trying to learn the language. I don't need that additional frustration. Your largest problem in the long run will be libraries. Also concerning, but if I can combine the two languages somehow as lobo suggested, there may be a solution (just need to figure out how and how difficult that is). ...but you'd still have to write bindings. htod doesn't exactly work on Linux. I am not exactly sure what that means, which is probably not a good sign. --- @Russel Windmer But doesn't code::blocks just interface with the compiler? I (naively?) thought I could just install the compiler and point code::blocks to that compiler. --- @Bachmeier What do you plan to do with D? Good point, I did not make that clear. Right now I just want to use it for personal projects, but someday I hope to take it further. Personally, I don't think there is a reason to transition. Instead, you should learn D and then use it when you are ready. That is troubling, but reasons to transition must exist or the language would not exist, right? I find that if I "learn" a language I forget it unless I actually start using it, at least for a short while. January 2016 is when I should have time to experiment with D. I will attempt to install the language in Linux and kick the tires for a while. If I continuously stumble into insurmountable barriers, the experiment will end.
Re: standard alias for a class name inside the class code?
On Sunday, 29 May 2016 at 00:28:13 UTC, Mithun Hunsur wrote: On Sunday, 29 May 2016 at 00:14:17 UTC, dan wrote: Is there a standard alias for a class name inside class code? Something like 'this' referring to a class instance, but referring instead to the class itself? [...] typeof(this) gets you the type of the current class. :) Great!! Thanks Mithun! That certainly works. But i sure don't understand how. Especially in a declaration like static typeof(this) make_instance( ) but also in the 'new typeof(this)'. In both cases, 'this' doesn't even exist. In fact, if you make a reference to this inside the static function make_instance(), you get an error: 'this' is only defined in non-static member functions, not make_instance So the compiler itself states that 'this' is not defined. But nevertheless, your method absolutely does work. So i suppose i should not look a gift horse in the mouth, but i'm still puzzled. Anyhow, thanks a million, because whether or not i understand your idiom, it is exactly what i need. dan
standard alias for a class name inside the class code?
Is there a standard alias for a class name inside class code? Something like 'this' referring to a class instance, but referring instead to the class itself? What i would like to do is have something like class Clas { // alias Clas THIS; <- don't want this boilerplate static THIS make_instance( ) { auto x = new THIS( ); return x; } } This would be great for copy/paste, changing class names, and in general communicating your intention. I'm guessing the answer is no, and that there's some compelling reason why a compiled language wouldn't want to provide this feature. (But the php interpreter, whatever else is good or bad about it, does let you write 'new self(...)' and does the right thing with it.) TIA for any clues. dan
Re: standard alias for a class name inside the class code?
On Sunday, 29 May 2016 at 02:44:33 UTC, jhps wrote: On Sunday, 29 May 2016 at 00:48:20 UTC, dan wrote: Especially in a declaration like static typeof(this) make_instance( ) but also in the 'new typeof(this)'. In both cases, 'this' doesn't even exist. https://dlang.org/spec/declaration.html#Typeof it's another 'this' that has not the same semantic as the reference holder. Just like 'const' can have 3 meanings, 'this' also: - this.member: typical usage, it hold the instance reference - void foo(this T)(): template this parameter, T is typeof(this) where the template is used. - typeof(this): you can use it in static func, this 'this' is not the 'this' instance. OK, thanks JHPS for the detailed explanation of this construction Mithun pointed out, and also for the link. It makes a lot of sense the way you put it.
Re: Is there a way to make a class variable visible but constant to outsiders, but changeable (mutable) to the class itself?
On Monday, 23 May 2016 at 07:03:08 UTC, chmike wrote: On Saturday, 21 May 2016 at 17:32:47 UTC, dan wrote: (This effect could be simulated by making my_var into a function, but i don't want to do that.) May I ask why you don't want to do that ? In D you can call a function without args without (). So if you write private int my_var_ = 4; // where 4 is the default initialization value @property int my_var1() { return my_var_; } final int my_var2() { return my_var_; } int my_var3() { return my_var_; } int x = obj.my_var1; x = obj.my_var2; x = obj.my_var3; my_var3 is virtual so I guess you get the overhead of a virtual method call which is probably not what you want. my_var2 can't be overriden and if it doesn't itself override a method with a same name in a base class the compiler may optimize its call by inlining it. It's like a static method with 'this' passed as argument. I'm not fully sure about my_var1. I'm still a beginner, but I think the compiler will optimize it into inlined instruction if it can as for my_var2. Making the user accessing the member variables directly may look like it's more efficient, but it's bad API design because you can't change the class implementation affecting my_var_ without breaking the API. The D way enforces good programming and API design and optimizes as much as possible. Thanks Ch Mike for your reply and explanation, and the further information about calling functions. Thanks also to the other Mike, to Daniel for the interesting union technique, and to Meta for further elaboration. Daniel's union technique is pretty close to what i was asking for. Now, since you explicitly ask me 'why you don't want to do that', i should answer. But my answer won't be nearly as good as your analysis and explanation, nor as good as any of the other replies. :( Just aesthetically, i'd like to refer to the variable within the class and outside the class with exactly the same symbol. But with all the ideas presented in the prior discussion, i can get pretty close so it would be a ridiculous point for me to complain about. Thanks again for your help! dan
Re: Is there a way to make a class variable visible but constant to outsiders, but changeable (mutable) to the class itself?
Thanks Vit, Meta, and Yuxuan for your speedy help! So 3 pieces to put together, function, const, and @property (and i guess final for protection against subclasses).
Is there a way to make a class variable visible but constant to outsiders, but changeable (mutable) to the class itself?
Is it possible to have a class which has a variable which can be seen from the outside, but which can only be modified from the inside? Something like: class C { int my_var = 3; // semi_const?? void do_something() { my_var = 4; } } And then in another file auto c = new C(); c.my_var = 5; // <<<- should trigger a compile-time error writeln("the value is ", c.my_var); // <<<- should print 3 c.do_something(); writeln("the value is ", c.my_var); // <<<- should print 4 Reading Alexandrescu's book suggests the answer is "no" (the only relevant type qualifiers are private, package, protected, public, and export, and none seem appropriate). (This effect could be simulated by making my_var into a function, but i don't want to do that.) TIA for any info! dan
persistence, serialization, history (run-to-run) in small self-contained program
I'm writing a small program (compiled with gdc on xubuntu 16.04). I would like it to remember a little data (a few kilobytes maybe). It looks like d comes with standard support for both sqlite3 and json --- is there any particular reason to prefer one over the other? Or maybe something else entirely? (In each case, i would store the data in some file, and on second and subsequent runs of the program attempt to recover the data from that file if possible.) My main concern is minimizing program complexity. TIA for any advice. dan
Re: persistence, serialization, history (run-to-run) in small self-contained program
On Thursday, 14 July 2016 at 08:28:56 UTC, Jacob Carlborg wrote: On 2016-07-14 07:18, dan wrote: I'm writing a small program (compiled with gdc on xubuntu 16.04). I would like it to remember a little data (a few kilobytes maybe). . My main concern is minimizing program complexity. There's Orange [1]. It's a serialization library that serializes to XML. [1] https://github.com/jacob-carlborg/orange Thanks Jacob! I was unaware of Orange. @rikki --- Thanks also for your remarks. Although in principle both sql and json are complex, they are familiar, so if they work as in other languages i'm hoping (knock on wood) they won't seem complex. As to what i'm storing, it's a little indefinite now, but will include strings and times.
Re: Is there a d analog of strncmp?
On Monday, 22 August 2016 at 01:45:02 UTC, Jonathan M Davis wrote: On Monday, August 22, 2016 00:14:31 Adam D. Ruppe via Digitalmars-d-learn wrote: int strncmp(string a, string b, int n) { if(a.length > n) a = a[0 .. n]; if(b.length > n) b = b[0 .. n]; import std.algorithm.comparison : cmp; return cmp(a, b); } Aside from the imports, it can be turned into a one-liner if you use take: return cmp(take(a, n), take(b, n)); - Jonathan M Davis Thanks Adam and Jonathan for your solutions. For reference, one of the imports Jonathan is referring to is import std.range; I did not know about take. Well, i also did not know about cmp. So my code is probably not very idiomatic. But i do appreciate all of you d-learn people!
Is there a d analog of strncmp?
In c, there's this very nice function strncmp(s1,s2,count) which compares two c strings, using at most count characters. count can be less than, more than, or equal to either or both of the lengths of the two strings. It can be used to see if two c-strings have the same prefix of some length. Now, strncmp indeed seems to be packaged up in core.stdc.string, but i would like to use some something like it on 2 d strings (which, as i understand it, need not be zero-terminated). I suppose it would be possible to do some conversion with toStringz() or something and then invoke the strncmp(), but that seems very wordy and also it's not clear that it would handle all pairs of d strings (e.g., what if there were some 0's in the first count characters?). So i would like to call a d function which works on d strings, but don't want to write my own if one already exists. (At the very least, i'd have to get a much sharper understanding of d strings, whether internal 0's can occur, etc. And i would not want to do egregious string allocation.) TIA for any info!
dependency analysis for makefile construction
Are there any FOSS tools for doing dependency analysis of (e.g.) all the d files in a directory, to let you know when a .o file needs to be regenerated? This presumably would depend mostly on the import statements (including import of any file to be used in string construction, as in 'auto my_string = import("my_file");'). My guess is there must be, because one of the big deals about d is the more regular syntax it offers to make compiler building more reliable. (I'm using gdc, and building with gnumake.) TIA for any info! dan
Re: dependency analysis for makefile construction
On Monday, 5 September 2016 at 18:49:25 UTC, Basile B. wrote: On Monday, 5 September 2016 at 18:22:08 UTC, ag0aep6g wrote: On 09/04/2016 12:07 AM, dan wrote: Are there any FOSS tools for doing dependency analysis of [...] [...] I'm not aware of a standalone tool that does something like this. If you want to write one, you could do like rdmd and use `dmd -deps`/`dmd -v`, or you could use a standalone D parser like libdparse. http://code.dlang.org/packages/libdparse I have one in dastworx, based on dparse: https://github.com/BBasile/Coedit/blob/master/dastworx/src/imports.d#L64 Thanks Basile and also ag0aep6g for your replies, which give me several things to try. dan
using assignment statement as conditional in a where
In c, you can have code like this: static void wtest( void ) { int f; while ( ( f = some_val( ) ) ) { printf(" our value is now: %d\n", f ); } } gcc compiles this without warning or error (at least if you use the double parentheses to assure the compiler that you realize you are testing an assignment, not a comparison). I would like to do the same thing in d, something like this: private void wtest( ) { int f; while ( ( f = some_val( ) ) ) { writeln(" our value is now: ", f ); } } or even better: private void wtest( ) { while ( ( auto f = some_val( ) ) ) { writeln(" our value is now: ", f ); } } This however does not work, and the gdc compiler says "assignment cannot be used as a condition, perhaps == was meant?" I don't absolutely have to do it this way, as i guess i could do 'while (true) {...' and then break if the assignment returns zero. But i really, really would like to use the idiom of assigning a value from inside a while condition. Is it possible to do this? Perhaps with extra braces or something, like while ( {something here} ) { } ? TIA for any pointers or advice. dan
Re: using assignment statement as conditional in a where
On Saturday, 3 December 2016 at 09:03:25 UTC, rikki cattermole wrote: On 03/12/2016 9:55 PM, dan wrote: [...] If you can use another compiler do so, gdc is on an old frontend/Phobos now. I recommend ldc or you know the reference compiler dmd if performance/platform isn't an issue (not that dmd can't produce decent codegen). This does compile: int func() { return 0; } void main() { int x; while((x = func()) != 0) { } } Thanks Rikki, that works great and is nearly ideal (doesn't seem to allow 'auto' but probably that's some scoping issue). I do prefer gdc because it is gpl'ed, but appreciate any suggestions. Thanks again for your help! dan
Re: How do i find a list of the methods Object implements, or maybe just locate the 'write' method?
On Tuesday, 7 November 2017 at 21:32:26 UTC, Adam D. Ruppe wrote: On Tuesday, 7 November 2017 at 21:25:00 UTC, dan wrote: I looked in my distribution's object.d (debian stretch, gdc, in Did you import std.stdio in the file? If so, it is calling the std.stdio.write on the object (this is called UFCS, uniform function call syntax, the language allows you to call any free function in scope with obj.foo by rewriting it to foo(obj)) I then thought that i should just get an authoritative list of Object's methods, and hope something was documented there, but my searching for a list of for this also failed. http://dpldocs.info/experimental-docs/object.Object.html Awesome, great, thanks Adam!!! I certainly was, and that must have been what was in play. And thanks for the pointer to Object's methods (which are very few, as i thought). dan
How do i find a list of the methods Object implements, or maybe just locate the 'write' method?
I was writing some code and added a line like x.write; expecting to fill it in later. I forgot to actually write a function write, but it compiled anyway, and some testing shows that if you write auto o = new Object; o.write; then this compiles just fine. (The 'write' method, whatever it is, does not return a value, as 'auto y = o.write;' will not compile.) So that presumably means that i'm about to override 'write' (when i finish up my class), but i have no idea what the original 'write' does. I looked in my distribution's object.d (debian stretch, gdc, in /usr/lib/gcc/x86_64-linux-gnu/6/include/d/object.d) but i don't see anything that looks like a write method there. I tried to do a net search, but 'write' is a very common word. I then thought that i should just get an authoritative list of Object's methods, and hope something was documented there, but my searching for a list of for this also failed. So i'd be grateful if somebody could tell me where i can find documentation on 'write' and/or what it means for an Object to write, and/or where i can see just what methods Object implements, and what those methods do. Thanks in advance for any clues, or a pointer to page 1 of the manual if it's there and i'm just being dense. dan
Re: formatting a float or double in a string with all significant digits kept
On Wednesday, 9 October 2019 at 10:54:49 UTC, David Briant wrote: On Tuesday, 8 October 2019 at 20:37:03 UTC, dan wrote: I have a double precision number that i would like to print all significant digits of, but no more than what are actually present in the number. Or more exactly, i want to print the minimum number of digits necessary to recover the original number to within 2 or 3 least significant bits in the stored, in-core, version of its bit pattern. For example, import std.string; import std.stdio; import std.math; void main( ) { auto t = format("%3.30f", PI ); writeln("Value of PI is: ", PI, " or, : ", t); } The default way writeln prints is 5 digits to the right of the decimal point. I can format to print with any number of digits, such as 30 above, but that's too many. For pi, the correct number of digits to print looks to be about 18 (and the extra 12 digits presumably are from the decimal expansion of the least significant bit?). But i would like to be able to do this without knowing the expansion of pi, or writing too much code, especially if there's some d function like writeAllDigits or something similar. Thanks in advance for any pointers! dan Hi Dan, What's your usecase here, e.g. a csv/json reader / writer? You say it's for double precision numbers (64bit format) then provide an example for reals (80bit format). So I'm not certain your goal. If you google "what every developer should know about doubles" you'll hit a number of useful articles that explain the common issues of floating point representation in detail. -- David Thanks David for your reply. Thanks also berni44 for the information about the dig attribute, Jon for the neat packaging into one line using the attribute on the type. Unfortunately, the version of gdc that comes with the version of debian that i am using does not have the dig attribute yet, but perhaps i can upgrade, and eventually i think gdc will have it. And thanks GreatSam4sure for your reply --- i searched the archives first, but very poorly :(. But it's easy to believe that i'm not the first person in the history of the world with this issue. Now, my use case is nothing so useful or general as a csv/json reader/writer. I'm just doing some computations, incorrectly i think, and i want to be able to print out the results and feed them to other software. I'm trying to chase down a problem and rule out as many places for error as i can, and it just seemed strange not to be able to get all the digits out in some easy way. But the dig attribute seems to be a big step forward, and for that i am grateful. dan
Re: formatting a float or double in a string with all significant digits kept
On Thursday, 10 October 2019 at 22:44:05 UTC, H. S. Teoh wrote: On Thu, Oct 10, 2019 at 09:13:05PM +, Jon Degenhardt via Digitalmars-d-learn wrote: On Thursday, 10 October 2019 at 17:12:25 UTC, dan wrote: > Thanks also berni44 for the information about the dig > attribute, Jon > for the neat packaging into one line using the attribute on > the > type. > Unfortunately, the version of gdc that comes with the > version of > debian that i am using does not have the dig attribute yet, > but > perhaps i can upgrade, and eventually i think gdc will have > it. What's the output of dmd --version? I find it extremely odd that .dig isn't supported. AFAIK, it's been there since the early days of D. Certainly, it has been there since I started using D, which was before dmd was ever available in Debian. What's the compiler output of: pragma(msg, float.dig); ? T Thanks HS! I sure thought i got a compile time error when i used .dig, but i tried it again, and it works. I tried the pragma, and it printed out 6, and i tried PI.dig and double.dig and they're all working now. Just for reference, i'm using what i think is the standard gdc on debian 9, not dmd, and the version is 2068L. Thanks again for your help in encouraging me to try harder, and thanks again everybody else for all the help. dan
formatting a float or double in a string with all significant digits kept
I have a double precision number that i would like to print all significant digits of, but no more than what are actually present in the number. Or more exactly, i want to print the minimum number of digits necessary to recover the original number to within 2 or 3 least significant bits in the stored, in-core, version of its bit pattern. For example, import std.string; import std.stdio; import std.math; void main( ) { auto t = format("%3.30f", PI ); writeln("Value of PI is: ", PI, " or, : ", t); } The default way writeln prints is 5 digits to the right of the decimal point. I can format to print with any number of digits, such as 30 above, but that's too many. For pi, the correct number of digits to print looks to be about 18 (and the extra 12 digits presumably are from the decimal expansion of the least significant bit?). But i would like to be able to do this without knowing the expansion of pi, or writing too much code, especially if there's some d function like writeAllDigits or something similar. Thanks in advance for any pointers! dan
a function like writeln that returns a string rather than writes to a file
I'm looking for a function something like writeln or write, but instead of writing to stdout, it writes to a string and returns the string. So i would like something like: import std.stdio; import std.conv; string write_to_string(T...)(T values ) { string s; foreach ( value; values ) s ~= to!string( value ); return s; } But because this is such a standard type of thing to do, i'd like to use whatever the standard function is for doing it, if there is one. So . . . is there one? Like maybe some way to dress a string up as a file and pass it through the usual write/writeln apparatus? My only real requirement is that it be something really easy to do. Thanks in advance for any pointers. dan
Re: a function like writeln that returns a string rather than writes to a file
On Saturday, 2 May 2020 at 02:29:43 UTC, H. S. Teoh wrote: On Sat, May 02, 2020 at 02:22:42AM +, dan via Digitalmars-d-learn wrote: I'm looking for a function something like writeln or write, but instead of writing to stdout, it writes to a string and returns the string. [...] import std.format : format; string str = format("%s %s %s", obj1, obj2, obj3); T Thanks HS! That looks like a good move, if format will do the string conversion for me. But one thing that would be troublesome is that i would have to make sure to count up the %s so that they match the number of arguments. I would like to do without that, just like writeln does. Anyhow, though, thanks for point out format. dan
Re: a function like writeln that returns a string rather than writes to a file
On Saturday, 2 May 2020 at 02:49:04 UTC, Steven Schveighoffer wrote: On 5/1/20 10:40 PM, dan wrote: On Saturday, 2 May 2020 at 02:29:43 UTC, H. S. Teoh wrote: On Sat, May 02, 2020 at 02:22:42AM +, dan via Digitalmars-d-learn wrote: [...] [...] import std.format : format; string str = format("%s %s %s", obj1, obj2, obj3); T Thanks HS! That looks like a good move, if format will do the string conversion for me. But one thing that would be troublesome is that i would have to make sure to count up the %s so that they match the number of arguments. I would like to do without that, just like writeln does. import std.conv: text; string str = text(obj1, " ", obj2, " ", obj3); -Steve Awesome, thanks Steve. That's perfect. So the function i was looking for was text (or, i guess, std.conv.text). dan
Re: a function like writeln that returns a string rather than writes to a file
On Saturday, 2 May 2020 at 10:36:47 UTC, Ali Çehreli wrote: On 5/1/20 7:40 PM, dan wrote:> On Saturday, 2 May 2020 at 02:29:43 UTC, H. S. Teoh wrote: >> On Sat, May 02, 2020 at 02:22:42AM +0000, dan via Digitalmars-d-learn >> wrote: >>> I'm looking for a function something like writeln or write, but >>> instead of writing to stdout, it writes to a string and returns the >>> string. >> [...] >> >> import std.format : format; >> string str = format("%s %s %s", obj1, obj2, obj3); >> >> >> T > > Thanks HS! > > That looks like a good move, if format will do the string conversion for > me. > > But one thing that would be troublesome is that i would have to make > sure to count up the %s so that they match the number of arguments. I > would like to do without that, just like writeln does. If you can live with a mildly awkward way of passing it, format() can take the format string at compile time as well: string str = format!"%s %s %s"(obj1, obj2, obj3); You get a compilation error if format specifications don't match the arguments. (There are bug reports about that check but it mostly works great.) Ali Thanks Ali. That's also a good point, and would remove one of my qualms about all of the %s reps. So if for any reason i cannot use the text function (or if i want to double check on the types of the objects) this would be a good thing to use. dan
which free operating systems have a gtkd package?
Debian 10 has a nice gtkd package, stored in libgtkd-3-dev i believe (when i installed it, i installed several packages at once, basically everything that had 'gtkd' as a substring in the package name). It uses ldmd2 (part of the ldc package). So it's possible to write and build a gtkd application using only debian packages. I've done this with debian 10, and it works well imvho. Although i have not tried it, it looks like ubuntu (20.4 and presumably later) also has a gtkd package. I'm going to install a new os on a machine, and i'm trying to pick one that has a gtkd package available, so that all the d imports are in standard locations and i don't have to mess with anything. So debian 10 and ubuntu 20.4 are candidates, but i'm wondering if there are others. (I tried to find gtkd on linux mint but did not see a package for it, but man i could sure be wrong.) Thanks in advance for any info! dan
Re: running a d compiler on the Mac Mini with an M1 chip
On Friday, 26 March 2021 at 21:54:20 UTC, rikki cattermole wrote: On 27/03/2021 10:51 AM, dan wrote: Are there any d compilers that run natively on the Mac Mini with an M1 chip? If so, does anybody here have any experience with them that can be shared? If not, and your machine is a mac mini, how would you go about programming in d on it? TIA for any info! Looks like latest ldc has an arm build. But both dmd and ldc should already work due to x86 emulation that takes place. https://github.com/ldc-developers/ldc/releases/tag/v1.25.1 Thanks Rikki! If anybody has any particular experience using d on a mac mini with M1 that they want to share, please do post, but this does look promising. dan
running a d compiler on the Mac Mini with an M1 chip
Are there any d compilers that run natively on the Mac Mini with an M1 chip? If so, does anybody here have any experience with them that can be shared? If not, and your machine is a mac mini, how would you go about programming in d on it? TIA for any info!
Re: Forcing my module to be initialized first
On Monday, 16 October 2023 at 10:23:54 UTC, Richard (Rikki) Andrew Cattermole wrote: Okay, after looking at gtkd, I don't think this can be solved with module constructors or swapping out to lazy initialization. One way that might work however is to use a crt_constructor as that runs before the D stuff. However it would now hard code your program to your system. Over all my suspicion is that there is something wrong with your system related to library lookup paths and that should be fixed instead. ```d void main() { import std.stdio; import std.process; writeln(environment["GTK_BASEPATH"]); } pragma(crt_constructor) extern(C) void myEnvironmentVarSetter() { import core.sys.posix.stdlib : putenv; putenv(cast(char*)"GTK_BASEPATH=~/bin/gtk".ptr); } ``` Really awesome, Rikki, your code does the trick. Thank you so much for your pragma. Since you are so right about the code, maybe you are also right about my system. For reference, i'll describe it in case anybody else somehow wanders into the same situation. My system is a Mac Catalina (OSX 10.15). I installed the latest dmd i could find, directly from the Digital Mars website. This was so that i could use any ports system (macports, fink, or brew) and keep the same d compiler. The dmd compiler is in /usr/local/bin/dmd, and the files are in some standard location /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/. I installed the gtk stuff (but not gtkd) from macports, and that all went into /opt/local, which is where macports puts things. I believe that gtkd does not have a 'configure' script, so i directly edited the top level GNUMakefile in one spot, to specify a prefix; my changed line was prefix?=/opt/some-particular-path-distinct-from-local-and-all-others I wanted the installation of gtkd to be in some parallel location so that there could be multiple versions if needed, or it could be removed if needed without any chance of disturbing macports or anything else. It built ok, and the demo worked ok (setting GTK_BASEPATH of course, per gtkd's README.md). Note that i built it on an account for which the PATH has /opt/local/bin and /opt/local/sbin at the start. So when gtkd was under construction, it knew just where to find all the gtk files. Thanks again for pointing out the crt_constructor pragma. (Now, i still think that when module initialization order is not forced, it should be something a programmer or systems integrator can choose, but i don't want to be too greedy.) Thanks again for your help!! dan
Re: Setting GTK_BASEPATH for gtkd
On Monday, 16 October 2023 at 18:57:45 UTC, bachmeier wrote: On Monday, 16 October 2023 at 18:28:52 UTC, dan wrote: (Now, i still think that when module initialization order is not forced, it should be something a programmer or systems integrator can choose, but i don't want to be too greedy.) Thanks again for your help!! dan I changed the subject line, so if case Mike Wey sees this, he knows it's about gtkd. If you haven't already, you make want to post your question at https://forum.gtkd.org/groups/GtkD/ Thanks Bachmeier. It's not exactly a question anymore since Rikki clued me in on the crt_constructor pragma. But i guess i should ask the gtkd forum if there's a better approach, or perhaps a way to set a hook into gtkd for pre-initialization activity (giving a program a chance to scout around for libraries or other resources before actually attempting to load anything). dan
Forcing my module to be initialized first
I have some code that i would like executed before anything else is. The code is to set an environment variable which is used by a library. I'm trying to find some way to avoid setting the environment variable on the command line, or in any shell script or initialization file. I think the place to put such code would be in a 'static this' inside a module. So i need some way to force my module to be initialized first, ahead of all others. I suppose if i could figure out a way to make all other modules depend on my module this would happen, but the module which uses the variable i want to set is in some already-compiled dynamic library that i would prefer not to touch. Alternatively, is there some entry point besides main that i could use, that executes before main and before any module initialization is done? (Is it possible to get in before the d runtime starts?) Although i would prefer to code in d, it would be ok to do it in c. This is on MacOS (Catalina) in case that makes a difference, and i'm using dmd v2.104.0. Thanks in advance for any clues. dan
Re: Forcing my module to be initialized first
On Monday, 16 October 2023 at 03:33:55 UTC, Richard (Rikki) Andrew Cattermole wrote: On 16/10/2023 4:31 PM, dan wrote: I suppose if i could figure out a way to make all other modules depend on my module this would happen, but the module which uses the variable i want to set is in some already-compiled dynamic library that i would prefer not to touch. If its in a shared library, then that shared library gets setup prior to your binary. There is nothing that you can do. You gotta override rather than initialize. Thanks Rikki. I was wrong in my statement of the problem: it is not a dynamic library, but rather a static library, libgtkd-3.a. I apologize for being so careless. libgtkd-3.a throws an exception before main is reached, in the Loader.d file, in a method with signature 'public static void loadLibrary(string library)'. That method is used trying to load a library which really is dynamic, libatk-1.0.0.dylib. That library is in my system, among the Mac ports files, in a standard place, namely /opt/local/lib. I can set the environment variable GTK_BASEPATH to help it out, and that works, but i would like to do all of this inside the executable rather than outside the executable. The Loader.d file depends on std.process. So, given that i was wrong and it is not a dynamic library i'm trying to get in ahead of, but a static one, is there a way to execute a small snippet of code after std.process is initialized, but before any other code (such as Loader.d) uses it? Thanks in advance for any ideas. dan
Re: Forcing my module to be initialized first
On Monday, 16 October 2023 at 04:26:32 UTC, Paul Backus wrote: On Monday, 16 October 2023 at 03:31:13 UTC, dan wrote: I have some code that i would like executed before anything else is. The code is to set an environment variable which is used by a library. I'm trying to find some way to avoid setting the environment variable on the command line, or in any shell script or initialization file. I think the place to put such code would be in a 'static this' inside a module. So i need some way to force my module to be initialized first, ahead of all others. You may find this article enlightening: https://devblogs.microsoft.com/oldnewthing/20050607-00/?p=35413 Hi Paul, Thanks for the link. It was funny, and for sure too many cooks spoil the broth. I don't think that applies here, since i'm just trying to tweak the initialization order, and i'm the only programmer involved. Of course, i guess all sinners try to justify their wrongdoing --- hope i'm not doing that here!! Thanks for your reply. :) dan
Want a function that determines a double or float given its 80-bit IEEE 754 SANE (big endian) representation
Hi, I'm parsing some files, each containing (among other things) 10 bytes said to represent an IEEE 754 extended floating point number, in SANE (Standard Apple Numerical Environment) form, as SANE existed in the early 1990s (so, big endian). Note that the number actually stored will probably be a positive even integer less than 100,000, so a better format would have been to store a two-byte ushort rather than a 10-byte float. However the spec chose to have an encoded float there. I would like to have a function of the form public bool ubytes_to_double( ubytes[10] u, out double d ) { /* stuff */ } which would set d to the value encoded provided that the value is a number and is sane, and otherwise just return false. So my plan is just to do this: examine the first 2 bytes to check the sign and see how big the number is, and if it is reasonable, convert the remaining 8 bytes to a fractional part, perhaps ignoring the last 2 or 3 as not being significant. But --- it seems like this kind of task may be something that d already does, maybe with some constructor of a double or something. Thanks in advance for any suggestions. dan
Re: Want a function that determines a double or float given its 80-bit IEEE 754 SANE (big endian) representation
On Wednesday, 23 August 2023 at 03:24:49 UTC, z wrote: On Tuesday, 22 August 2023 at 22:38:23 UTC, dan wrote: Hi, I'm parsing some files, each containing (among other things) 10 bytes said to represent an IEEE 754 extended floating point number, in SANE (Standard Apple Numerical Environment) form, as SANE existed in the early 1990s (so, big endian). Note that the number actually stored will probably be a positive even integer less than 100,000, so a better format would have been to store a two-byte ushort rather than a 10-byte float. However the spec chose to have an encoded float there. I would like to have a function of the form public bool ubytes_to_double( ubytes[10] u, out double d ) { /* stuff */ } which would set d to the value encoded provided that the value is a number and is sane, and otherwise just return false. So my plan is just to do this: examine the first 2 bytes to check the sign and see how big the number is, and if it is reasonable, convert the remaining 8 bytes to a fractional part, perhaps ignoring the last 2 or 3 as not being significant. But --- it seems like this kind of task may be something that d already does, maybe with some constructor of a double or something. Thanks in advance for any suggestions. dan On 32bit x86 an endianness swap and pointer cast to `real` should be enough.(seems to be the same format but i could be wrong.) Else(afaik `real` on 64 bit x86 is just `double`?) you can always isolate sign mantissa and exponent to three isolated `double` values(cast from integer to `double`) and recalculate(`sign*mantissa*(2^^exponent)` according to wikipedia) the floating point number, since they mostly contain integers precision loss probably won't be a problem. Thank you z. My machine is 64-bit and is little-endian but the method you suggest actually gives the right answer in my case. More exactly, a real on my machine is 16-bytes (128 bits), quadruple precision, and it has a sign bit with 15 bits of exponent. But the 80-bit format also has a sign bit with 15 bits of exponent. So all i have to do is declare a real y, cast it to ubyte*, and copy the 10 ubytes from the file over its first 10 bytes (but backwards). Then the sign bit and exponent exactly match in position. (The remaining 6 ubytes are left in their initial state because they're way out to the least significant part of the number.) Now, for my final code, i'm not actually doing this because the size of real may be different on another machine, or the exponents may get different sizes due to a different layout, or some other problem. So i just do it by hand (although i'm ignoring the last 4 ubytes since for my usage, ultimately it gets boiled down to a 32-bit integer anyway). And "by hand" is pretty close to what you also mention in your mantissa*2^^exponent expression. Thanks again.
Function which returns a sorted array without duplicates
I would like to write a function which takes an array as input, and returns a sorted array without duplicates. In fact, i have a function which does this, but i think it may have some extra unnecessary steps. ```d private S[] _sort_array( S )( S[] x ) { import std.algorithm; auto y = x.dup; y.sort; auto z = y.uniq; // Cannot just return z; this gives: // Error: cannot implicitly convert expression `z` of type // `UniqResult!(binaryFun, uint[])` to `uint[]` // // You also cannot just return cast( S[] ) z; // // Nor can you do: // import std.conv; // return to!( S[] )( z ); typeof( x ) w; foreach ( v ; z ) w ~= v; return w; } ``` My only constraint is that i really want to keep the same signature (i.e., return an array, not sharing structure or storage with the input). Here's the usage: ```d void main( ) { uint[] nums = [1, 3, 2, 5, 1, 4, 2, 8]; auto sorted = _sort_array( nums ); import std.stdio; writeln( "Input: ", nums ); writeln( "Output: ", sorted ); } ``` Thanks in advance for any info! dan
Re: Function which returns a sorted array without duplicates
On Sunday, 22 January 2023 at 07:33:01 UTC, evilrat wrote: On Sunday, 22 January 2023 at 04:42:09 UTC, dan wrote: I would like to write a function which takes an array as input, and returns a sorted array without duplicates. ```d private S[] _sort_array( S )( S[] x ) { import std.algorithm; auto y = x.dup; y.sort; auto z = y.uniq; // Cannot just return z; this gives: // Error: cannot implicitly convert expression `z` of type // `UniqResult!(binaryFun, uint[])` to `uint[]` uniq and other algorithms often returns a lazy range, you can build an array by using `std.array.array()` https://dlang.org/phobos/std_array.html#array try something like this or just `return array(y.uniq);` ```d private S[] _sort_array( S )( S[] x ) { import std.algorithm; import std.array; return x.dup .sort .uniq .array(); } ``` And IIRC you probably don't need `dup` as sort produces a lazy range. Thanks evilrat, this works perfectly, and is just the right style too (imvho). So what i was missing was std.array. Thanks also Ali for your subsequent clarifying remarks. (Probably what i need to do is read a good book on the std library for d.) dan Thanks also Ali for your subsequent remarks