Re: Switch function from runtime to compile time
On Thursday, 14 March 2019 at 11:38:44 UTC, alex1974 wrote: I have several geometric shapes (triangle, trapezoid, gauss, ...) forming the membership functions of a fuzzy set. For example the shape of the triangle is defined by the variables a, b and c. The function calculating membership looks like: real triangle (real a, real b, real c, real value) { if (value <= a || value >= c) return 0.0; else if (value <= b) return (x-a)/(b-a); else return (c-x)/(c-b); } Intuitiv I packed this in a class: class Triangle { real a,b,c; real getValue (real value) { ... // math as above } } My question is if this is the best practice. During the learning process of the fuzzy logic the shape of the triangle will change. But once I found the optimal shape the triangle will be fixed and the program could be recompiled with the optimal shapes. The compiler could then perform optimization of the code at compile-time. Look at the term (b-a) and (c-b) which are then known at compile-time. How can I achieve this without writing duplicate code for runtime and compile-time? Just store them to a file and read the file at compile time if it exists... quite simple. if (exists(file)) import(file); elseif (optimal) write(file); If you need to retrain just remove the file or use something like version.
Re: local class instance (at module-level)
On 15/03/2019 00:45, ag0aep6g via Digitalmars-d-learn wrote: On 14.03.19 20:43, Jacob Carlborg wrote: class C { uint i ; this (uint i) { this.i = i ; } this (uint i) shared { this.i = i ; } this (uint i) immutable { this.i = i ; } } __gshared c0 = new C(0); shared c1 = new shared C(1); immutable c2 = new immutable C(2); You only need one of the constructors depending on if you want a __gshared, shared or immutable variable. If you make it `pure`, you can use the same constructor in all cases: class C { uint i ; this (uint i) pure { this.i = i ; } } __gshared c0 = new C(0); shared c1 = new shared C(1); immutable c2 = new immutable C(2); all right, thank you!
Re: local class instance (at module-level)
On 14.03.19 20:43, Jacob Carlborg wrote: class C { uint i ; this (uint i) { this.i = i ; } this (uint i) shared { this.i = i ; } this (uint i) immutable { this.i = i ; } } __gshared c0 = new C(0); shared c1 = new shared C(1); immutable c2 = new immutable C(2); You only need one of the constructors depending on if you want a __gshared, shared or immutable variable. If you make it `pure`, you can use the same constructor in all cases: class C { uint i ; this (uint i) pure { this.i = i ; } } __gshared c0 = new C(0); shared c1 = new shared C(1); immutable c2 = new immutable C(2);
Re: bug in doc?
On Thursday, 14 March 2019 at 19:46:30 UTC, spir wrote: But the doc (the language ref for the matter) should definitely say what you just explained above, shouldn't they? Well arguably, the spec should detail the language semantics formally and not just be a description of the reference implementation also mentioning bugs. As it's currently written however, and also considering that there's only one implementation, the doc should probably mention it.
Re: Setting the GtkD Include Path in dexed?
On Thursday, 14 March 2019 at 20:06:08 UTC, Ron Tarrant wrote: On Thursday, 14 March 2019 at 16:02:01 UTC, Basile B. wrote: You must register put the static library file, not the object I thing, anyway, i just made you a video showing exactly what to do since finally the linker error is gone (yay !), also launch one of the demo file as a "runnable module". https://www.reddit.com/r/d_language/comments/b120dc/register_gtkd_in_dexed/ It's good to see that the hard work to make this libman useful works... I got this to work on Linux following your video. Thanks! But when I went back to Windows to try it there, dexed doesn't open. I check in Task Manager and dexed runs, but it just doesn't open its window. Also, Task Manager says dexed's power usage is "Very high" whatever that means. I waited about 30 seconds, but nothing changed. I killed dexed and dcd-server, then tried running it again. Same result. Open an issue in dexed bug tracker please. I'll take a look, and fix I hope. I gotta release a point release soon anyway because today a bug causing slow startup and maybe 50% of the docking problems has been fixed. https://github.com/Basile-z/dexed/issues
Re: Setting the GtkD Include Path in dexed?
On Thursday, 14 March 2019 at 16:02:01 UTC, Basile B. wrote: You must register put the static library file, not the object I thing, anyway, i just made you a video showing exactly what to do since finally the linker error is gone (yay !), also launch one of the demo file as a "runnable module". https://www.reddit.com/r/d_language/comments/b120dc/register_gtkd_in_dexed/ It's good to see that the hard work to make this libman useful works... I got this to work on Linux following your video. Thanks! But when I went back to Windows to try it there, dexed doesn't open. I check in Task Manager and dexed runs, but it just doesn't open its window. Also, Task Manager says dexed's power usage is "Very high" whatever that means. I waited about 30 seconds, but nothing changed. I killed dexed and dcd-server, then tried running it again. Same result.
Re: bug in doc?
On 14/03/2019 15:52, H. S. Teoh via Digitalmars-d-learn wrote: On Thu, Mar 14, 2019 at 03:22:52PM +0100, spir via Digitalmars-d-learn wrote: https://dlang.org/spec/hash-map.html#static_initialization: immutable long[string] aa = [ "foo": 5, "bar": 10, "baz": 2000 ]; ==> Error: non-constant expression `["foo":5L, "bar":10L, "baz":2000L]` Also: I don't understand the error message: * What is non-constant in the *expression*? * And why should it be constant at all? (Removing "immutable" does not help...) [...] It's a well-known limitation. The workaround is: immutable long[string] aa; static this() { aa = [ "foo" : 5, ... ]; } All right! No language has to be perfect... (I'm joking). But the doc (the language ref for the matter) should definitely say what you just explained above, shouldn't they? I actually think clearly stating limitations is a +++. (Like an industry company that does not make perfect product but has great client support.) diniz
Re: local class instance (at module-level)
On 2019-03-14 12:05, spir wrote: I desperately try to declare/define/initialise a simple class instance at module-level. This is a special (conceptually static and immutable) instance used as a "marker", that just should exist and be accessible by methods of this class and/or other classes defined in the same module. (Thus I don't care if TLS.) I use it as a remplacement for null, to avoid semantic confusion and unhelpful segfaults in case of bug. I have tried a number of options and never manage to do it, including: * [C/auto/static immutable c0] = new C(0) ; * C c0 ; c0.i = 0 ; * defining a static this() * more... The most confusing error is: Error: variable `_base.c0` is a thread-local class and cannot have a static initializer. Use `static this()` to initialize instead. I also could not find any information --for a while, repeatedly, since I can go on developing in the meantime, using null instead. I'm bluffed and confused, since there is nothing weird in that, is there? (the compiler can just allocate it in static mem and take the address) Reduced test case: === class C { uint i ; this (uint i) { this.i = i ; } } // error auto c0 = new C(0) ; void main () { // ok auto c0 = new C(0) ; } === I would enjoy an explanation (or a pointer to) in addition to a solution. It works if it's not a TLS variable: class C { uint i ; this (uint i) { this.i = i ; } this (uint i) shared { this.i = i ; } this (uint i) immutable { this.i = i ; } } __gshared c0 = new C(0); shared c1 = new shared C(1); immutable c2 = new immutable C(2); You only need one of the constructors depending on if you want a __gshared, shared or immutable variable. PS: I take the opportnity to ask if I can count on the compiler to intern literal strings (which my code may use in several places, including loops), esp. "", or should I declare and use (for instance): static immutable s0 = "" ; String literals are stored in the executable. Each unique string literal is only stored once. -- /Jacob Carlborg
Re: Operator overloading for size_t
On Thursday, 14 March 2019 at 18:25:17 UTC, H. S. Teoh wrote: On Thu, Mar 14, 2019 at 06:07:46PM +, Alec Stewart via Digitalmars-d-learn wrote: [...] bool opEquals(ref const Interval i) const { // probably would be a bit more than just this, but for this issue // let's just stick with this. return d_start.opEquals(other.d_start) && d_end.opEquals(other.d_end); } There's no need to call opEquals explicitly like that. All you need to do is to use <, ==, and > as you normally would: bool opEquals(ref const Interval i) const { return d_start == other.d_start) && d_end == d_end; } T Thanks. I somehow managed to overthink this... For < and >, would one do this? size_t opCmp(ref const Interval other) const { return d_start < other.d_start; } size_t opCmp(ref const Interval other) const { return d_end < other.d_end; } size_t opCmp(ref const Interval other) const { return d_start > other.d_start; } size_t opCmp(ref const Interval other) const { return d_end > other.d_end; } Or would it better to do size_t opCmp(ref const Interval other) const { if (d_start < other.d_start) { return d_start < other.d_start; } else if (d_start > other.d_start) { return d_start > other.d_start; } else if (d_end < other.d_end) { return d_end < other.d_end; } else if (d_end > other.d_end) { return d_end > other.d_end; } else { return false; } }
Re: Operator overloading for size_t
On Thu, Mar 14, 2019 at 06:07:46PM +, Alec Stewart via Digitalmars-d-learn wrote: [...] > bool opEquals(ref const Interval i) const { > // probably would be a bit more than just this, but for this issue > // let's just stick with this. > return d_start.opEquals(other.d_start) && d_end.opEquals(other.d_end); > } There's no need to call opEquals explicitly like that. All you need to do is to use <, ==, and > as you normally would: bool opEquals(ref const Interval i) const { return d_start == other.d_start) && d_end == d_end; } T -- Без труда не выловишь и рыбку из пруда.
Re: Operator overloading for size_t
On Thursday, 14 March 2019 at 18:07:46 UTC, Alec Stewart wrote: // let's just stick with this. return d_start.opEquals(other.d_start) && d_end.opEquals(other.d_end); Why not just use d_start == other.d_start && d_end == other.d_end there? So should I bother with operator overloading here, or just make a member function? You shouldn't often call .opEquals yourself, just write a == b and let the compiler translate it if it needs to.
Operator overloading for size_t
I thought (for shits and giggles) to try and implement the Aho-Corasick algorithm[1]. I thought I'd start with a struct to represent the "interval": struct Interval { size_t d_start; size_t d_end; size_t size; this(size_t start, size_t end) { d_start = start; d_end = end; size = d_end - d_start + 1; } } It'd be useful to check for equality and inequality between instances of `Interval`, so I thought to use `.opEquals` for `d_start` and `d_end`. bool opEquals(ref const Interval i) const { // probably would be a bit more than just this, but for this issue // let's just stick with this. return d_start.opEquals(other.d_start) && d_end.opEquals(other.d_end); } But I do get an error saying `none of the overloads of `opEquals` are callable using argument types `(const(ulong), const(ulong))`, candidates are:` and it doesn't say the candidates. So should I bother with operator overloading here, or just make a member function? [1] https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm
Re: Setting the GtkD Include Path in dexed?
On Thursday, 14 March 2019 at 15:50:55 UTC, Ron Tarrant wrote: Thanks for replying, Basile. It's always nice to get info straight from the original code author. :) On Wednesday, 13 March 2019 at 11:59:11 UTC, Basile B. wrote: 1. "Compile File and Run" It's for the scripts-like program, i.e single module. For this, the dependencies must be registered in the "library manager". An entry defines the import path (the dmd -I option), and the static library file (*.a, *.lib depending on the platform), and a few others fields. I added gtk-d to the library manager and now my code compiles when I select either 'Compile file', 'Compile file and run', or 'Compile file and run...' However, with the last two, the executible doesn't go the next step and actually run. The dexed window becomes deselected (and in the case of 'Compile file and run...', a dialog opens to ask for args) but I see no indicator of the application opening its window. Unless it's opening somewhere off my screen. I have two external monitors connected to a laptop which seems to confuse some applications, especially on Linux. I've been testing on Windows today, however, so I don't know if this is related or not. You must register put the static library file, not the object I thing, anyway, i just made you a video showing exactly what to do since finally the linker error is gone (yay !), also launch one of the demo file as a "runnable module". https://www.reddit.com/r/d_language/comments/b120dc/register_gtkd_in_dexed/ It's good to see that the hard work to make this libman useful works...
Re: Tuple!(string, int))[] field_orders
On Thursday, 14 March 2019 at 15:29:28 UTC, Ozan wrote: Hi In vibe.d / data / mongo / collection I found the function * ensureIndex(Tuple!(string, int))[] field_orders) What could be the right way to use "Tuple!(string, int))[] field_orders"? I tried different ways like [Tuple!("a", 1), Tuple!("b", 2)]", but compiler said "No" Any hint? Thanks & Regards, Ozan field_orders' type: Tuple!(string, int)[] So you have to use [Tuple!(string, int)("b",2), ...] or (shortcut) [tuple("b", 2), ...] Andrea
Re: Setting the GtkD Include Path in dexed?
Thanks for replying, Basile. It's always nice to get info straight from the original code author. :) On Wednesday, 13 March 2019 at 11:59:11 UTC, Basile B. wrote: 1. "Compile File and Run" It's for the scripts-like program, i.e single module. For this, the dependencies must be registered in the "library manager". An entry defines the import path (the dmd -I option), and the static library file (*.a, *.lib depending on the platform), and a few others fields. I added gtk-d to the library manager and now my code compiles when I select either 'Compile file', 'Compile file and run', or 'Compile file and run...' However, with the last two, the executible doesn't go the next step and actually run. The dexed window becomes deselected (and in the case of 'Compile file and run...', a dialog opens to ask for args) but I see no indicator of the application opening its window. Unless it's opening somewhere off my screen. I have two external monitors connected to a laptop which seems to confuse some applications, especially on Linux. I've been testing on Windows today, however, so I don't know if this is related or not.
Re: Setting the GtkD Include Path in dexed?
On Tuesday, 12 March 2019 at 15:48:14 UTC, Ron Tarrant wrote: I managed to get dexed to compile a single-file dub project, but for completeness sake, I'm also trying to configure it to use dmd (non-dub) to compile GtkD projects using Compilation (menu) > Compile File and Run. To that end, I have two questions... Should I be supplying dexed with the include (read: import) path: /usr/include/dmd/gtkd3/ Or the runtime: /usr/lib/x86_64-linux-gnu/libgtkd-3.so And where in dexed does one set the path so it can find a library such as gtkd? I just tried and here is the complete guide to register an entry in the library manager. 1. in the repo root run make to get the static libraries generated 2. in dexed add a new library manager entry (icon with a book and a "+") 3. set the alias to "gtkd" (icon with a book and a pen) 4. set the source path to /generated/gtkd (icon with a folder and a "+") 5. set the static library file to the "libgtkd-3.a" file that should have been generated in the repo root. (icon with a folder and a brick) This has to be done manually because the json DUB project is cannot be registered automatically since it's more a meta project, or however this is called. Then when you compile a runnable with "Compile file and run" the libman entry will be used to solve the import path automatically, and the static library file will be passed automatically as well. For the dexed projects, as explained yesterday, the "libraryAliases" property must contain "gtkd". Note however that here I got linkers error (multiple definition of...), no idea why.
Tuple!(string, int))[] field_orders
Hi In vibe.d / data / mongo / collection I found the function * ensureIndex(Tuple!(string, int))[] field_orders) What could be the right way to use "Tuple!(string, int))[] field_orders"? I tried different ways like [Tuple!("a", 1), Tuple!("b", 2)]", but compiler said "No" Any hint? Thanks & Regards, Ozan
Re: bug in doc?
On Thursday, 14 March 2019 at 14:47:18 UTC, Adam D. Ruppe wrote: On Thursday, 14 March 2019 at 14:22:52 UTC, spir wrote: https://dlang.org/spec/hash-map.html#static_initialization: Well, bug in implementation. That is *supposed* to work, but the compiler never implemented it. The docs really should point out this fact explicitly, though. Especially since it's hard to workaround inside function scope.
Re: bug in doc?
On Thu, Mar 14, 2019 at 03:22:52PM +0100, spir via Digitalmars-d-learn wrote: > https://dlang.org/spec/hash-map.html#static_initialization: > > immutable long[string] aa = [ > "foo": 5, > "bar": 10, > "baz": 2000 > ]; > > ==> Error: non-constant expression `["foo":5L, "bar":10L, "baz":2000L]` > > Also: I don't understand the error message: > * What is non-constant in the *expression*? > * And why should it be constant at all? > (Removing "immutable" does not help...) [...] It's a well-known limitation. The workaround is: immutable long[string] aa; static this() { aa = [ "foo" : 5, ... ]; } T -- Trying to define yourself is like trying to bite your own teeth. -- Alan Watts
Re: bug in doc?
On Thursday, 14 March 2019 at 14:22:52 UTC, spir wrote: https://dlang.org/spec/hash-map.html#static_initialization: Well, bug in implementation. That is *supposed* to work, but the compiler never implemented it. The docs really should point out this fact explicitly, though.
Re: bug in doc?
On Thursday, 14 March 2019 at 14:22:52 UTC, spir wrote: https://dlang.org/spec/hash-map.html#static_initialization: immutable long[string] aa = [ "foo": 5, "bar": 10, "baz": 2000 ]; If I'm right, you can't use this syntax with global array. Insted this works: void main() { immutable long[string] aa = [ "foo": 5, "bar": 10, "baz": 2000 ]; } You should init global AAs using static this() { } as explained in the same doc Andrea
Re: local class instance (at module-level)
On 14/03/2019 12:16, H. S. Teoh via Digitalmars-d-learn wrote: On Thu, Mar 14, 2019 at 12:05:22PM +0100, spir via Digitalmars-d-learn wrote: I desperately try to declare/define/initialise a simple class instance at module-level. This is a special (conceptually static and immutable) instance used as a "marker", that just should exist and be accessible by methods of this class and/or other classes defined in the same module. (Thus I don't care if TLS.) I use it as a remplacement for null, to avoid semantic confusion and unhelpful segfaults in case of bug. Does this work? class C { ... } immutable C c0; static this() { c0 = new C(...); } T Oh yes, that was it ! Thank you very much. diniz
bug in doc?
https://dlang.org/spec/hash-map.html#static_initialization: immutable long[string] aa = [ "foo": 5, "bar": 10, "baz": 2000 ]; ==> Error: non-constant expression `["foo":5L, "bar":10L, "baz":2000L]` Also: I don't understand the error message: * What is non-constant in the *expression*? * And why should it be constant at all? (Removing "immutable" does not help...) diniz
Re: Switch function from runtime to compile time
On Thursday, 14 March 2019 at 11:38:44 UTC, alex1974 wrote: I have several geometric shapes (triangle, trapezoid, gauss, ...) forming the membership functions of a fuzzy set. For example the shape of the triangle is defined by the variables a, b and c. The function calculating membership looks like: real triangle (real a, real b, real c, real value) { if (value <= a || value >= c) return 0.0; else if (value <= b) return (x-a)/(b-a); else return (c-x)/(c-b); } Intuitiv I packed this in a class: class Triangle { real a,b,c; real getValue (real value) { ... // math as above } } My question is if this is the best practice. During the learning process of the fuzzy logic the shape of the triangle will change. But once I found the optimal shape the triangle will be fixed and the program could be recompiled with the optimal shapes. The compiler could then perform optimization of the code at compile-time. Look at the term (b-a) and (c-b) which are then known at compile-time. How can I achieve this without writing duplicate code for runtime and compile-time? Basically, the question is, how to store values, if known and optimize them, if not. Right? In this case, I think, the optimizations done by the compiler are less important, as the learning procedure lasts (by far?) more, compared to a single run without compiler optimizations. However, if you insist, you can use a mixin https://tour.dlang.org/tour/en/gems/string-mixins https://dlang.org/articles/mixin.html 0. Then, you define some default state, which corresponds to the "absence of knowledge". 1. If this state is encountered, you start some learn procedure. As mixins are interpreted at compile time, the state can be checked at compile time. 2. After needed values are learned, overwrite the file with the mixin in such a way, that the learned values are written and exit. 3. After that, you recompile. And as the values differ from the "absence of knowledge" state, you can execute some non-learning run afterward.
Re: local class instance (at module-level)
On Thursday, 14 March 2019 at 11:05:22 UTC, spir wrote: I desperately try to declare/define/initialise a simple class instance at module-level. This is a special (conceptually static and immutable) instance used as a "marker", that just should exist and be accessible by methods of this class and/or other classes defined in the same module. (Thus I don't care if TLS.) I use it as a remplacement for null, to avoid semantic confusion and unhelpful segfaults in case of bug. I have tried a number of options and never manage to do it, including: * [C/auto/static immutable c0] = new C(0) ; * C c0 ; c0.i = 0 ; * defining a static this() * more... The most confusing error is: Error: variable `_base.c0` is a thread-local class and cannot have a static initializer. Use `static this()` to initialize instead. I also could not find any information --for a while, repeatedly, since I can go on developing in the meantime, using null instead. I'm bluffed and confused, since there is nothing weird in that, is there? (the compiler can just allocate it in static mem and take the address) Reduced test case: === class C { uint i ; this (uint i) { this.i = i ; } } // error auto c0 = new C(0) ; void main () { // ok auto c0 = new C(0) ; } === I would enjoy an explanation (or a pointer to) in addition to a solution. Thank you, diniz PS: I take the opportnity to ask if I can count on the compiler to intern literal strings (which my code may use in several places, including loops), esp. "", or should I declare and use (for instance): static immutable s0 = "" ; Basically, this works for me: ´´´ class C { uint i ; this (uint i) { this.i = i ; } } C c0; static this() { c0 = new C(0); } void main () { assert(!(c0 is null)); } ´´´
Switch function from runtime to compile time
I have several geometric shapes (triangle, trapezoid, gauss, ...) forming the membership functions of a fuzzy set. For example the shape of the triangle is defined by the variables a, b and c. The function calculating membership looks like: real triangle (real a, real b, real c, real value) { if (value <= a || value >= c) return 0.0; else if (value <= b) return (x-a)/(b-a); else return (c-x)/(c-b); } Intuitiv I packed this in a class: class Triangle { real a,b,c; real getValue (real value) { ... // math as above } } My question is if this is the best practice. During the learning process of the fuzzy logic the shape of the triangle will change. But once I found the optimal shape the triangle will be fixed and the program could be recompiled with the optimal shapes. The compiler could then perform optimization of the code at compile-time. Look at the term (b-a) and (c-b) which are then known at compile-time. How can I achieve this without writing duplicate code for runtime and compile-time?
Re: local class instance (at module-level)
On Thursday, 14 March 2019 at 11:05:22 UTC, spir wrote: The most confusing error is: Error: variable `_base.c0` is a thread-local class and cannot have a static initializer. Use `static this()` to initialize instead. Error is reffering to: https://dlang.org/spec/module.html#staticorder You can do this: C c0; static this() { c0 = new C(); } Andrea
Re: local class instance (at module-level)
On Thu, Mar 14, 2019 at 12:05:22PM +0100, spir via Digitalmars-d-learn wrote: > I desperately try to declare/define/initialise a simple class instance > at module-level. This is a special (conceptually static and immutable) > instance used as a "marker", that just should exist and be accessible > by methods of this class and/or other classes defined in the same > module. (Thus I don't care if TLS.) I use it as a remplacement for > null, to avoid semantic confusion and unhelpful segfaults in case of > bug. Does this work? class C { ... } immutable C c0; static this() { c0 = new C(...); } T -- To err is human; to forgive is not our policy. -- Samuel Adler
local class instance (at module-level)
I desperately try to declare/define/initialise a simple class instance at module-level. This is a special (conceptually static and immutable) instance used as a "marker", that just should exist and be accessible by methods of this class and/or other classes defined in the same module. (Thus I don't care if TLS.) I use it as a remplacement for null, to avoid semantic confusion and unhelpful segfaults in case of bug. I have tried a number of options and never manage to do it, including: * [C/auto/static immutable c0] = new C(0) ; * C c0 ; c0.i = 0 ; * defining a static this() * more... The most confusing error is: Error: variable `_base.c0` is a thread-local class and cannot have a static initializer. Use `static this()` to initialize instead. I also could not find any information --for a while, repeatedly, since I can go on developing in the meantime, using null instead. I'm bluffed and confused, since there is nothing weird in that, is there? (the compiler can just allocate it in static mem and take the address) Reduced test case: === class C { uint i ; this (uint i) { this.i = i ; } } // error auto c0 = new C(0) ; void main () { // ok auto c0 = new C(0) ; } === I would enjoy an explanation (or a pointer to) in addition to a solution. Thank you, diniz PS: I take the opportnity to ask if I can count on the compiler to intern literal strings (which my code may use in several places, including loops), esp. "", or should I declare and use (for instance): static immutable s0 = "" ;