Re: CTFE fmod ?
On Friday, 20 November 2015 at 13:44:11 UTC, rumbu wrote: On Friday, 20 November 2015 at 11:16:13 UTC, userABCabc123 wrote: [...] [...] Not thoroughly tested and only works for doubles, but this must do the trick. [...] Thx, it works, easy to adapt to float.
template this and traits getOverloads issue.
Background: === http://stackoverflow.com/questions/33764540/warning-about-overriden-methods-when-using-a-mixin Why this horrible trick has to be used: === cast this as (T) in a function template that's been mixed in an ancestor is not always usable, unless I miss something: import std.stdio; mixin template Bug() { import std.traits; void bug(T)() { foreach(member; __traits(allMembers, T)) foreach(overload; __traits(getOverloads, T, member)) { auto dg = writeln(member); } } } class A { mixin Bug; this(){bug!A;} void foo(){} } class B: A { void bar(){} void bar(uint a){} this(){bug!B;} } void main(){ new A; new B; } a.d(11,27): Error: this for bar needs to be type B not type a.A a.d(11,27): Error: this for bar needs to be type B not type a.A a.d(11,27): Error: this for this needs to be type B not type a.A everything that can be done to avoid the compilations errors will also prevent "bar" to be written in the output (because a B will never be analyzed). The "only" fix I see is like in the stack overflow answer: statically check if the mixin methods are already there and remix the mixin in each descendant, so that the getOverloads traits works on the right 'this'. What do you think ? is it a bug ?
Re: template this and traits getOverloads issue.
Alternatively, you can use a static method and pass in the instance. Note that `new B` will print A's members twice, because A's constructor is always called and `__traits(allMembers, B)` includes A's members. --- import std.stdio; mixin template Bug() { import std.traits; static void bug(T)(T t) { writeln(">", T.stringof); foreach(member; __traits(allMembers, T)) foreach(overload; __traits(getOverloads, T, member)) { auto dg = writeln(member); } } } class A { mixin Bug; this(){bug!A(this);} void foo(){} } class B: A { this(){bug!B(this);} void bar(){} void bar(uint a){} } void main(){ new A; new B; }
Re: CTFE fmod ?
On Friday, 20 November 2015 at 11:16:13 UTC, userABCabc123 wrote: Does someone have a good CTFE fmod() function ? The problem is that std.math.fmod() is itself not available at CT, neither do floor() or similar functions necessary to get the quotient when the input value is two times over/under the bounds. Any suggestion ? Or maybe this is a limit ? Not thoroughly tested and only works for doubles, but this must do the trick. double ctfe_trunc(double x) @trusted pure nothrow @nogc { ulong bits = *cast(ulong*)(); auto sign = bits & 0x8000; long exponent = (bits >> 52) & 0x7FF; auto mantissa = (bits & 0xF); if (exponent == 0 && mantissa == 0) return 0.0; else if (exponent == 0x7FF && mantissa == 0) return sign ? -double.infinity : double.infinity; else if (exponent == 0x7FF) return double.nan; exponent -= 1023; auto target = 52 - exponent; if (target >= 0 && target <= 51) { auto msb = mantissa & (1UL << target); auto lsb = mantissa & ~((1UL << target) - 1); bits = sign | ((exponent + 1023)) << 52 | lsb; mantissa += msb; return *cast(double*) } else return sign ? -0.0 : 0.0; } double ctfe_fmod(double x, double y) @safe pure nothrow @nogc { return x - ctfe_trunc(x / y) * y; }
Re: template this and traits getOverloads issue.
On Friday, 20 November 2015 at 14:18:00 UTC, Alex Parrill wrote: If the mixin has to be used on class and on struct, I cant use an interface. In this case override will create an error and go back to the solution on SO: statically check if things are already there. Templates are not virtual, which is why you might be running into issues here; bug thinks it's being called on an `A` object. This is the origin of the problem, I totally forgot this limitation.
Re: template this and traits getOverloads issue.
On Friday, 20 November 2015 at 14:18:00 UTC, Alex Parrill wrote: But you don't need a template for this case; mixin templates have access to `this`: Indeed, this is a good answer too. The difference between this and the template thing I did is that yours is virtual so calling it through an interface will work even on subclasses. However, you must mix it into each sub class. Mine is a template that only needs to be in the base class/interface, but also need a `this` of the derived type to see the derived type. (That's why I ran `this.bug();` in the constructor of B btw) If you call it on an variable typed as the interface, it will only show interface members. IF i = new A(); i.bug(); // would only show interface members with mine A a = new A(); a.bug(); // will now show A's members too That's what the template this parameter does: the type of this at the *usage site* is passed as the parameter. With your solution, the type of this at the *mixin site* is available. I think your solution is generally better for stuff like serialization where you are passed an interface but need child members too. The template this param I used is nice for interface functions that need some kind of covariance; returning a type based on how it was used.
Re: template this and traits getOverloads issue.
On Friday, 20 November 2015 at 14:01:13 UTC, BBasile wrote: everything that can be done to avoid the compilations errors will also prevent "bar" to be written in the output (because a B will never be analyzed). The "only" fix I see is like in the stack overflow answer: statically check if the mixin methods are already there and remix the mixin in each descendant, so that the getOverloads traits works on the right 'this'. Did you try using a template this parameter like I said in my comment? import std.stdio; mixin template Bug() { import std.traits; void bug(this T)() { T this_ = cast(T) this; foreach(member; __traits(allMembers, T)) foreach(idx, overload; __traits(getOverloads, T, member)) { auto dg = &(__traits(getOverloads, this_, member)[idx]); writeln(T.stringof, ".", member, " ", typeof(dg).stringof); } } } class A { mixin Bug; this(){bug;} void foo(){} } class B: A { void bar(){} void bar(uint a){} this(){this.bug;} } void main(){ new A; new B; } There's a couple quirks in there, but I think you'll find the output shows what you want to see.
Re: template this and traits getOverloads issue.
On Friday, 20 November 2015 at 14:01:13 UTC, BBasile wrote: Background: === http://stackoverflow.com/questions/33764540/warning-about-overriden-methods-when-using-a-mixin Why this horrible trick has to be used: === cast this as (T) in a function template that's been mixed in an ancestor is not always usable, unless I miss something: import std.stdio; mixin template Bug() { import std.traits; void bug(T)() { foreach(member; __traits(allMembers, T)) foreach(overload; __traits(getOverloads, T, member)) { auto dg = writeln(member); } } } class A { mixin Bug; this(){bug!A;} void foo(){} } class B: A { void bar(){} void bar(uint a){} this(){bug!B;} } void main(){ new A; new B; } a.d(11,27): Error: this for bar needs to be type B not type a.A a.d(11,27): Error: this for bar needs to be type B not type a.A a.d(11,27): Error: this for this needs to be type B not type a.A everything that can be done to avoid the compilations errors will also prevent "bar" to be written in the output (because a B will never be analyzed). The "only" fix I see is like in the stack overflow answer: statically check if the mixin methods are already there and remix the mixin in each descendant, so that the getOverloads traits works on the right 'this'. What do you think ? is it a bug ? Templates are not virtual, which is why you might be running into issues here; bug thinks it's being called on an `A` object. But you don't need a template for this case; mixin templates have access to `this`: --- import std.stdio; mixin template Bug() { import std.traits; override void bug() { foreach(member; __traits(allMembers, typeof(this))) foreach(overload; __traits(getOverloads, typeof(this), member)) { auto dg = writeln(member); } } } // Interface to allow for mixin template to use `override` without worrying if // `bug` is the first implementation or not. interface IF { void bug(); } class A : IF { mixin Bug; this(){bug();} void foo(){} } class B: A { mixin Bug; // need mixin on B now to override `bug` for the new class void bar(){} void bar(uint a){} } void main(){ new A; new B; }
Re: Thread in detached state?
On Thursday, 19 November 2015 at 22:07:19 UTC, ZombineDev wrote: On Friday, 13 November 2015 at 15:35:11 UTC, Ish wrote: [...] If you're more familiar with pthreads, you can just use them from core.sys.posix.pthread [1]. After all this what core.thread uses on Posix [2]. In general you can use any OS functionality by importing the declarations you need from core.sys.[OS].[header] [3]. However you need to make sure to register your thread if you are going to use features of D that need runtime support (such as the GC). [...] Well I was trying to see how many threads could be created by a process. Linux has limit of 380 joinable threads (POSIX) per process. OS runs out of resources at this point. To increase the limit one has to recover the resources right away when a thread terminates. I will look into the links that you posted. Thanks for answering my query.
Re: template this and traits getOverloads issue.
On Friday, 20 November 2015 at 14:39:29 UTC, Alex Parrill wrote: Alternatively, you can use a static method and pass in the instance. Initially this solution looked awesome but when `bug()` is static, `` returns some functions, not some delegates, which is a problem: this implies that I have to find the matching delegate type, set `.funcptr` to the value of the `dg` function, set `.ptr` to the value of 't'...well not so hard I guess. But then I have no way to detect when a function is really a function or something that might be delegate when instantiated: { auto dg = if (member == "bar") writeln(member, " ", typeof(dg).stringof); } --> bar void function() --> bar void function(uint a) Note that `new B` will print A's members twice, because A's constructor is always called and `__traits(allMembers, B)` includes A's members. Not a problem, it was already the case anyway. But thx much for the attempt.
CTFE fmod ?
Does someone have a good CTFE fmod() function ? The problem is that std.math.fmod() is itself not available at CT, neither do floor() or similar functions necessary to get the quotient when the input value is two times over/under the bounds. Currently I have this one... --- auto warp(T)(T x, T min, T max) { if (x > max) { T rng = max - min; while (x > max + rng) x -= rng * 2; if (x > max) x -= rng; } else if (x < min) { T rng = max - min; while (x < min - rng) x += rng * 2; if (x < min) x += rng; } return x; } --- ...but it fails to compile with certain float values. This example will consume a lot of memory ( I guess it's the while() loop in the CTFE VM who's responsible): --- static assert(warp(2357136044, -5f, 5f).approxEqual(-1f)); --- Any suggestion ? Or maybe this is a limit ?
Re: std.experimental.allocator optlink error
On Friday, 20 November 2015 at 12:31:37 UTC, Tofu Ninja wrote: On Tuesday, 10 November 2015 at 11:39:56 UTC, Rikki Cattermole wrote: One already exists. I've confirmed it in the build scripts. It only effects Windows. Any fix for this right now? No, https://issues.dlang.org/show_bug.cgi?id=15281
Re: std.experimental.allocator optlink error
On 21/11/15 1:31 AM, Tofu Ninja wrote: On Tuesday, 10 November 2015 at 11:39:56 UTC, Rikki Cattermole wrote: One already exists. I've confirmed it in the build scripts. It only effects Windows. Any fix for this right now? Dunno if its been fixed in ~master (doubt it), but all you need to do in your code, is copy it in. I've personally marked (well voted anyway) for this bug to be critical. It seems I'm the only one who thinks this is worth its own point release *shrugs*.
Re: std.experimental.allocator optlink error
On Tuesday, 10 November 2015 at 11:39:56 UTC, Rikki Cattermole wrote: One already exists. I've confirmed it in the build scripts. It only effects Windows. Any fix for this right now?
Re: template this and traits getOverloads issue.
On Friday, 20 November 2015 at 14:49:28 UTC, Adam D. Ruppe wrote: On Friday, 20 November 2015 at 14:01:13 UTC, BBasile wrote: everything that can be done to avoid the compilations errors will also prevent "bar" to be written in the output (because a B will never be analyzed). The "only" fix I see is like in the stack overflow answer: statically check if the mixin methods are already there and remix the mixin in each descendant, so that the getOverloads traits works on the right 'this'. Did you try using a template this parameter like I said in my comment? foreach(idx, overload; __traits(getOverloads, T, member)) { auto dg = &(__traits(getOverloads, this_, member)[idx]); Yes, using an index and a second call to getOverloads works, "finally". No need to remix. Thx. I hadn't understood what you meant on SO. One last question: is it possible to nest the calls to functions that take this kind of parameters ? mixin template Bug() { void bug0(this T)(){} void bug1(this T)(){} void allbugs(this T)(){this.bug0(); this.bug1();} } I've tried different parameters and templates and it never works.
Re: std.experimental.allocator optlink error
On Friday, 20 November 2015 at 12:49:02 UTC, Rikki Cattermole wrote: Dunno if its been fixed in ~master (doubt it), but all you need to do in your code, is copy it in. Cool, that worked. I've personally marked (well voted anyway) for this bug to be critical. It seems I'm the only one who thinks this is worth its own point release *shrugs*. Yeah this is a pretty big oversight and seems like it should be fixed asap.
Re: template this and traits getOverloads issue.
On Friday, 20 November 2015 at 15:43:00 UTC, BBasile wrote: One last question: is it possible to nest the calls to functions that take this kind of parameters ? auto this_ = cast(T) this; this_.bug0(); this_.bug1();
Re: scope keyword
On Thursday, 19 November 2015 at 23:16:04 UTC, Spacen Jasset wrote: I thought scope was deprecated, but I see that this is still here: http://dlang.org/attribute.html#scope Is it just the uses on classes and local variables that are discouraged, but the use in a function signature will continue? in == const scope? The usage of scope as a variable storage modifier to allocate classes on the stack is deprecated, and replaced with std.typecons.scoped. The usage of scope as a parameter attribute is not deprecated, and is used to indicate that references to the parameter will not escape through this function call (ex. the function won't store the parameter in a global variable), though the only thing it affects at the moment is delegates.
Re: GTKD TreeView - Delete TreeView SubItem with Button?
On Friday, 20 November 2015 at 17:09:50 UTC, TheDGuy wrote: Hello, is it possible, to delete a selected TreeView SubItem with a Button-Click? My Code currently looks like this: import gtk.MainWindow; import gtk.Box; import gtk.Main; import gtk.Button; import gdk.Event; import gtk.Widget; import List, Languages; void main(string[] args){ Main.init(args); MainWindow win = new MainWindow("TreeView Example"); win.setDefaultSize(500,300); Box box = new Box(Orientation.VERTICAL, 0); auto list = new List(); auto german = list.addCountry("German", "Germany"); auto city = list.addCountryCity(german, "German", "Munich"); auto sweden = list.addCountry("Swedish", "Sweden"); auto cit = list.addCountryCity(sweden, "Swedish", "Stockholm"); auto treeView = new LanguageTree(list); box.packStart(treeView, true, true, 0); auto btn_Delete = new Button("Delete"); box.add(btn_Delete); win.add(box); win.showAll(); Main.run(); } class DeleteButton : Button { this(in string text) { super(text); addOnButtonRelease(); } private bool del(Event event, Widget widget) { return true; } } But i neither know how i get the currently selected item nor how i could delete it within the "del"-Event of the "DeleteButton"? TreeIter iter = treeView.getSelectedIter; then you can use the TreeIter to delete the item. If your List is a ListStore or TreeStore, it has a remove method that you can call. If its a custom store you will have to implement it yourself like this class List:TreeModel { . void remove(TreeIter iter) { //maybe remove item from your actual data structure or other processing //send rowDeleted signal rowDeleted(iter); } . }
GTKD TreeView - Delete TreeView SubItem with Button?
Hello, is it possible, to delete a selected TreeView SubItem with a Button-Click? My Code currently looks like this: import gtk.MainWindow; import gtk.Box; import gtk.Main; import gtk.Button; import gdk.Event; import gtk.Widget; import List, Languages; void main(string[] args){ Main.init(args); MainWindow win = new MainWindow("TreeView Example"); win.setDefaultSize(500,300); Box box = new Box(Orientation.VERTICAL, 0); auto list = new List(); auto german = list.addCountry("German", "Germany"); auto city = list.addCountryCity(german, "German", "Munich"); auto sweden = list.addCountry("Swedish", "Sweden"); auto cit = list.addCountryCity(sweden, "Swedish", "Stockholm"); auto treeView = new LanguageTree(list); box.packStart(treeView, true, true, 0); auto btn_Delete = new Button("Delete"); box.add(btn_Delete); win.add(box); win.showAll(); Main.run(); } class DeleteButton : Button { this(in string text) { super(text); addOnButtonRelease(); } private bool del(Event event, Widget widget) { return true; } } But i neither know how i get the currently selected item nor how i could delete it within the "del"-Event of the "DeleteButton"?
Re: GTKD TreeView - Delete TreeView SubItem with Button?
On Friday, 20 November 2015 at 19:45:26 UTC, TheDGuy wrote: So like this? public void remove(TreeIter iter) { this.remove(iter); } If i do this, i get a stackoverflow error :( Your List inherits from TreeStore correct? just get rid of your custom remove method entirely, or correctly override it public override void remove(TreeIter iter) { super.remove(iter); }
Re: dataframe implementations
On Thursday, 19 November 2015 at 06:33:06 UTC, Jay Norwood wrote: Maybe the nd slices could be applied if you considered each row to be the same structure, and slice by rows rather than operating on columns. Pandas supports a multi-dimension panel. Maybe this would be the application for nd slices by row. I meant in the sense that Pandas is built upon Numpy.
Re: GTKD TreeView - Delete TreeView SubItem with Button?
On Fri, 20 Nov 2015 18:57:10 +, TheDGuy wrote: > Thanks for your reply, > > now it gets more clear for me but how am i able to access the TreeView > within the CustomButton-Class? If i declare TreeView and TreeStore > public i get "static variable list cannot be read at compile time"? > > auto list = new List(); > auto treeView = new LanguageTree(list); You need to declare them at the module scope but initialize them inside your main function, so: List list; LanguageTree treeView; then inside your main function: list = new List(); treeView = new LanguageTree(list);
Re: GTKD TreeView - Delete TreeView SubItem with Button?
Ahh, okay. Thank you very much for your help, now everything works fine :)
Re: template this and traits getOverloads issue.
On Friday, 20 November 2015 at 15:03:06 UTC, Adam D. Ruppe wrote: On Friday, 20 November 2015 at 14:18:00 UTC, Alex Parrill wrote: But you don't need a template for this case; mixin templates have access to `this`: Indeed, this is a good answer too. The difference between this and the template thing I did is that yours is virtual so calling it through an interface will work even on subclasses. However, you must mix it into each sub class. Mine is a template that only needs to be in the base class/interface, but also need a `this` of the derived type to see the derived type. (That's why I ran `this.bug();` in the constructor of B btw) If you call it on an variable typed as the interface, it will only show interface members. IF i = new A(); i.bug(); // would only show interface members with mine A a = new A(); a.bug(); // will now show A's members too That's what the template this parameter does: the type of this at the *usage site* is passed as the parameter. With your solution, the type of this at the *mixin site* is available. I think your solution is generally better for stuff like serialization where you are passed an interface but need child members too. The template this param I used is nice for interface functions that need some kind of covariance; returning a type based on how it was used. I review what I said before. Actually it doesn't work that well. Using the two methods (yours or A.Parrill's one) the protected members in a class that's located in another module are not accessible, the relationship to the current class the traits code is ran into is totally lost and only public members are visible. when bug() delcaration is `static void bug(T)(T t)`, pointer to members are get using __traits(getMember, t, member) and in your solution using _this instead of t. So it's a more for a less.
Re: GTKD TreeView - Delete TreeView SubItem with Button?
Thanks for your reply, now it gets more clear for me but how am i able to access the TreeView within the CustomButton-Class? If i declare TreeView and TreeStore public i get "static variable list cannot be read at compile time"? import gtk.MainWindow; import gtk.Box; import gtk.Main; import gtk.Button; import gdk.Event; import gtk.Widget; import List, Languages; auto list = new List(); auto treeView = new LanguageTree(list); void main(string[] args){ Main.init(args); MainWindow win = new MainWindow("TreeView Example"); win.setDefaultSize(500,300); Box box = new Box(Orientation.VERTICAL, 0); auto german = list.addCountry("German", "Germany"); auto city = list.addCountryCity(german, "German", "Munich"); auto sweden = list.addCountry("Swedish", "Sweden"); auto cit = list.addCountryCity(sweden, "Swedish", "Stockholm"); //auto treeView = new LanguageTree(list); box.packStart(treeView, true, true, 0); auto btn_Delete = new Button("Delete"); box.add(btn_Delete); win.add(box); win.showAll(); Main.run(); } class DeleteButton : Button { this(in string text) { super(text); addOnButtonRelease(); } private bool del(Event event, Widget widget) { list.remove(treeView.getSelectedIter); return true; } }
Re: GTKD TreeView - Delete TreeView SubItem with Button?
Thanks for your reply, is it also possible to do it like this? import gtk.MainWindow; import gtk.Box; import gtk.Main; import gtk.Button; import gdk.Event; import gtk.Widget; import List, Languages; void main(string[] args){ Main.init(args); MainWindow win = new MainWindow("TreeView Example"); win.setDefaultSize(500,300); Box box = new Box(Orientation.VERTICAL, 0); auto list = new List(); auto german = list.addCountry("German", "Germany"); auto city = list.addCountryCity(german, "German", "Munich"); auto sweden = list.addCountry("Swedish", "Sweden"); auto cit = list.addCountryCity(sweden, "Swedish", "Stockholm"); auto treeView = new LanguageTree(list); auto btn_Delete = new DeleteButton("Delete", treeView, list); box.packStart(treeView, true, true, 0); box.add(btn_Delete); win.add(box); win.showAll(); Main.run(); } class DeleteButton : Button { private LanguageTree mLT; private List mTStore; this(in string text, LanguageTree lT, List tStore){ super(text); this.mLT = lT; this.mTStore = tStore; addOnButtonRelease(); } private bool del(Event event, Widget widget){ mTStore.remove(mLT.getSelectedIter); return true; } } and in my TreeStore-Class: public void remove(TreeIter iter) { rowDeleted(iter); } but now i get the error: "rowDeleted is not callable using argument types (TreeIter)"?
Compile time strings auto concatenation!?
Can DMD frontend optimize string concatenation ``` enum Double(S) = S ~ S; assert(condition, "Text " ~ Double!"+" ~ ___FUNCTION__); ``` to ``` assert(condition, "Text ++_function_name_"); ``` ?
Re: GTKD TreeView - Delete TreeView SubItem with Button?
On Fri, 20 Nov 2015 19:34:01 +, TheDGuy wrote: > Thanks for your reply, > > is it also possible to do it like this? > Yeah, that'll work. > but now i get the error: > > "rowDeleted is not callable using argument types (TreeIter)"? rowDeleted is just to emit the signal that a row has been removed, not actually remove it. You probably want to use the ListStore's remove method.
Re: GTKD TreeView - Delete TreeView SubItem with Button?
So like this? public void remove(TreeIter iter) { this.remove(iter); } If i do this, i get a stackoverflow error :(
Re: GTKD TreeView - Delete TreeView SubItem with Button?
On Fri, 20 Nov 2015 19:45:25 +, TheDGuy wrote: > So like this? > > public void remove(TreeIter iter) > { > this.remove(iter); > } > > If i do this, i get a stackoverflow error :( That's creating an infinite recursion as the method simply calls itself. Assuming the class inherits from ListStore, you can just erase the remove method entirely and let the base class handle it. If you want to do something extra, use: override void remove(TreeIter iter) { // Anything custom super.remove(iter); // Anything custom }
Re: Compile time strings auto concatenation!?
On Fri, 20 Nov 2015 20:39:57 +, Ilya wrote: > Can DMD frontend optimize > string concatenation > ``` > enum Double(S) = S ~ S; > > assert(condition, "Text " ~ Double!"+" ~ ___FUNCTION__); > ``` > > to > > ``` > assert(condition, "Text ++_function_name_"); > > ``` > ? Yes this occurs as part of the constant folding I believe. $ cat test.d enum Double(string S) = S ~ S; void main(string[] args){ assert(args.length, "Text " ~ Double!"+" ~ __FUNCTION__); } $ dmd test.d $ strings test | grep Text Text ++test.main
Build utf.d with MS-COFF failed
Hello, I can't build my project with MS-COFF option. I'm using DMD 2.069. I got this error : utf.d(1109) : invalid UTF-8 sequence (at index 1) I used these options with visual D: Compiler : DMD D-Version : D2 Output Type: DLL Subsystem : Not set Compilation: Combined compile and link Thank you for your help.
Re: char[] == null
On Thursday, 19 November 2015 at 15:36:44 UTC, Steven Schveighoffer wrote: On 11/19/15 3:30 AM, Jonathan M Davis via Digitalmars-d-learn wrote: On Wednesday, November 18, 2015 22:15:19 anonymous via Digitalmars-d-learn wrote: On 18.11.2015 22:02, rsw0x wrote: slices aren't arrays http://dlang.org/d-array-article.html The language reference/specification [1] uses the term "dynamic array" for T[] types. Let's not enforce a slang that's different from that. [1] http://dlang.org/arrays.html Exactly. T[] _is_ a dynamic array. Steven's otherwise wonderful article on arrays in D ( http://dlang.org/d-array-article.html ) made the mistake of calling the buffer that T[] points to on the GC heap (assuming that even does point to the GC heap) the dynamic array. And per the language spec, that's not true at all. It was not a mistake :) It's how I still think of it. The spec is confusing, and my terminology, IMO, is a much more consistent (and accurate) way to think of it. -Steve Why formal definition of dynamic array caused confusion and is inconsistent? It is well consistent with static array and other aggregate types notions. Consider this: int *x = new int; // this is 'int type' ans it is 'dynamic' int y; int *a = // and this is not 'int type' The problem is treating chunk of heap memory as some kind of type (dynamic in this case). Type of object determines an interface of interacting with object while storage determines memory location and possibly duration of lifetime. It is possible to have object of any type to be allocated on different storages - for example, slice does not necessarily points to dynamic array (in your terms). C and C++ established a long tradition of such standard notions as object, lifetime, storage and type. From system language perspective calling memory a type (in other words, type of object depends on where it was allocated) does not make much sense.
Re: Manually allocate delegate?
On Sunday, 12 July 2015 at 09:03:25 UTC, Tofu Ninja wrote: On Sunday, 12 July 2015 at 08:47:37 UTC, ketmar wrote: On Sun, 12 Jul 2015 08:38:00 +, Tofu Ninja wrote: Is it even possible? what do you mean? Sorry, thought the title was enough. The context for a delegate(assuming not a method delegate) is allocated by the GC. Is there any way to allocate the context manually. Yes: class Foo { void bar(){writeln(__PRETTY_FUNCTION__);} } auto uncollectedDelegate(T, string name)(ref T t) { import std.experimental.allocator.mallocator; struct Dg{void* ptr, funcptr;} void* funcptr = &__traits(getMember, T, name); void* ptr = cast(void*)t; Dg* dg = cast(Dg*) Mallocator.instance.allocate(Dg.sizeof); dg.ptr = ptr; dg.funcptr = funcptr; return dg; } void main(string[] args) { Foo foo = new Foo; auto dg = uncollectedDelegate!(Foo, "bar")(foo); auto tdg = cast(void delegate()*) dg; (*tdg)(); } with just a type __traits(getMember,...) on a delegate will only return the function address in the process image. so without the context ptr, just like a static or a global function. Later you set the context by hand, using a pointer to an instance.
RAII trouble
I have the following code in attempt to create an RAII object wrapper, but it seems that it might be impossible. The problem here is that FT_Init_FreeType is a static which is set at some previous time to a function entry point in the FreeType library. I could use a scope block, but would rather have a self contained thing to do the work. Perhaps some sort of mixin is the other solution? The ideal would be to have a struct that can be placed inside a function scope, or perhaps as a module global variable. Why does Ft_Init_FreeType have to be read at compile time? text.d(143,15): Error: static variable FT_Init_FreeType cannot be read at compile time text.d(174,43):called from here: initialise() struct FreeType { @disable this(); static FreeType initialise() { return FreeType(0); } this(int) { int error = FT_Init_FreeType(); /// ERROR enforce(!error); } alias library this; ~this() { FT_Done_FreeType(library); } FT_Library library; } FreeType freeType = FreeType.initialise();
Re: RAII trouble
On 11/20/2015 02:56 PM, Spacen Jasset wrote: > FT_Init_FreeType is a static which is set at some previous time to a > function entry point in the FreeType library. > text.d(143,15): Error: static variable FT_Init_FreeType cannot be read > at compile time The compiler seems to think that FT_Init_FreeType is a variable. Is that really a function pointer? Can you show how it's defined. My simple test works: extern (C) int init_func(double){ return 42; } static auto my_func = _func; struct S { static S init() { return S(0); } this(int) { my_func(1.5); } } void main() { auto s = S.init(); } Ali
Re: RAII trouble
On 20.11.2015 23:56, Spacen Jasset wrote: The ideal would be to have a struct that can be placed inside a function scope, or perhaps as a module global variable. Why does Ft_Init_FreeType have to be read at compile time? text.d(143,15): Error: static variable FT_Init_FreeType cannot be read at compile time text.d(174,43):called from here: initialise() struct FreeType { @disable this(); static FreeType initialise() { return FreeType(0); } this(int) { int error = FT_Init_FreeType(); /// ERROR enforce(!error); } alias library this; ~this() { FT_Done_FreeType(library); } FT_Library library; } FreeType freeType = FreeType.initialise(); FT_Init_FreeType must be read at compile time, because freeType is a module level variable, and initializers for module variables must be static values. The initializer is run through CTFE (Compile Time Function Evaluation). Put the initialization/assignment in a function and it should work. That function can be a static constructor or the main function: FreeType freeType = void; /* 'void' prevents default initialization */ static this() {freeType = FreeType.initialise();} /* should work */ void main() {freeType = FreeType.initialise();} /* too */
Re: RAII trouble
On Friday, 20 November 2015 at 23:21:03 UTC, anonymous wrote: [...] FT_Init_FreeType must be read at compile time, because freeType is a module level variable, and initializers for module variables must be static values. The initializer is run through CTFE (Compile Time Function Evaluation). Put the initialization/assignment in a function and it should work. That function can be a static constructor or the main function: [...] Yes, I see. I made a mistake. I need to initialize it elsewhere. It was quite confusing. Thanks.
Re: RAII trouble
On Friday, 20 November 2015 at 23:35:50 UTC, Spacen Jasset wrote: On Friday, 20 November 2015 at 23:21:03 UTC, anonymous wrote: [...] FT_Init_FreeType must be read at compile time, because freeType is a module level variable, and initializers for module variables must be static values. The initializer is run through CTFE (Compile Time Function Evaluation). Put the initialization/assignment in a function and it should work. That function can be a static constructor or the main function: [...] Yes, I see. I made a mistake. I need to initialize it elsewhere. It was quite confusing. Thanks. I Just noticed this trick you posted, that's interesting. Of course I couldn't get it working without the void to discard the initialization; FreeType freeType = void; /* 'void' prevents default initialization */ static this() {freeType = FreeType.initialise();} /* should work */
Re: Build utf.d with MS-COFF failed
On Friday, 20 November 2015 at 21:27:12 UTC, Pierre wrote: Hello, I can't build my project with MS-COFF option. I'm using DMD 2.069. I got this error : utf.d(1109) : invalid UTF-8 sequence (at index 1) I used these options with visual D: Compiler : DMD D-Version : D2 Output Type: DLL Subsystem : Not set Compilation: Combined compile and link Thank you for your help. What string is throwing the error? I've had that error when I was trying to decode windows1252 strings.
why --shebang for rdmd?
Hello. The following code works fine for me: #! /usr/bin/env rdmd import std.stdio; void main() { writeln(2); } So what is the use of the --shebang option of rdmd? http://dlang.org/rdmd.html does not shed much light on this. Thanks. -- Shriramana Sharma, Penguin #395953
Re: D equivalent of Python's try..else
On 11/20/2015 09:45 PM, Shriramana Sharma wrote: Hello. In Python one has the syntax try..except..else.. where code in the else clause will only be executed if an exception does not occur. (Ref: http://stackoverflow.com/a/22579805/1503120) In D, is there such an idiomatic/canonical construct? The D try statement only seems to support finally (apart from catch). I don't know what idiom that enables in Python but it feels to me like putting the statements right after the ones that could throw suffices in D (and Python): try { may_throw(); may_throw2(); code_for_when_they_succeed(); } catch (/* ... */) { // ... } Ali
Re: D equivalent of Python's try..else
On Saturday, 21 November 2015 at 05:45:37 UTC, Shriramana Sharma wrote: Hello. In Python one has the syntax try..except..else.. where code in the else clause will only be executed if an exception does not occur. (Ref: http://stackoverflow.com/a/22579805/1503120) In D, is there such an idiomatic/canonical construct? The D try statement only seems to support finally (apart from catch). scope(failure) can be used to run code when an exception is thrown inside the scope, and scope(success) only triggers if the scope exited successfully http://ddili.org/ders/d.en/scope.html
Re: D equivalent of Python's try..else
rsw0x wrote: > scope(failure) can be used to run code when an exception is > thrown inside the scope, and scope(success) only triggers if the > scope exited successfully > > http://ddili.org/ders/d.en/scope.html Thanks but I know that and it executes only at the point of scope exit. But I want some code to run immediately after the try clause but only if an exception did not occur. The Python else clause is for code which should be run only if an exception never occurred i.e. even if one occurred and it was handled. It will be executed before `finally`. Is there a D equivalent? -- Shriramana Sharma, Penguin #395953
Re: D equivalent of Python's try..else
Shriramana Sharma wrote: > In Python one has the syntax try..except..else.. where code in the > else clause will only be executed if an exception does not occur. (Ref: > http://stackoverflow.com/a/22579805/1503120) Official Python documentation: https://docs.python.org/3/reference/compound_stmts.html#try -- Shriramana Sharma, Penguin #395953
D equivalent of Python's try..else
Hello. In Python one has the syntax try..except..else.. where code in the else clause will only be executed if an exception does not occur. (Ref: http://stackoverflow.com/a/22579805/1503120) In D, is there such an idiomatic/canonical construct? The D try statement only seems to support finally (apart from catch). -- Shriramana Sharma, Penguin #395953
`finally` is redundant?
The page http://dlang.org/exception-safe.html says: "It's try-finally that becomes redundant." IIUC this is because we have scope(exit). Does this mean that `finally` should eventually be removed from the language? -- Shriramana Sharma, Penguin #395953
Re: D equivalent of Python's try..else
On Saturday, 21 November 2015 at 05:55:53 UTC, Shriramana Sharma wrote: rsw0x wrote: scope(failure) can be used to run code when an exception is thrown inside the scope, and scope(success) only triggers if the scope exited successfully http://ddili.org/ders/d.en/scope.html Thanks but I know that and it executes only at the point of scope exit. But I want some code to run immediately after the try clause but only if an exception did not occur. The Python else clause is for code which should be run only if an exception never occurred i.e. even if one occurred and it was handled. It will be executed before `finally`. Is there a D equivalent? Put the scope(success) inside the try block?