scoped classes

2017-04-26 Thread Alex via Digitalmars-d-learn
Hi all, a short question about an example. having read this: https://dlang.org/library/std/typecons/scoped.html There is a struct B defined in the middle of the example, with a scoped class member. How to define an array of such members (and to put some items to it)? So, I want to have somethi

Re: scoped classes

2017-04-27 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 27 April 2017 at 06:40:49 UTC, Alex wrote: Hi all, a short question about an example. having read this: https://dlang.org/library/std/typecons/scoped.html There is a struct B defined in the middle of the example, with a scoped class member. How to define an array of such members (a

Re: scoped classes

2017-04-27 Thread Alex via Digitalmars-d-learn
On Thursday, 27 April 2017 at 15:06:44 UTC, Stanislav Blinov wrote: The only "possible" way would be like this: typeof(scoped!A())[] a; a = [ scoped!A(1), scoped!A(2), scoped!A(3) ]; But even so, you shouldn't do that. scoped isn't designed for it. And then, the amount of elements is not kn

Re: scoped classes

2017-04-27 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 27 April 2017 at 15:47:38 UTC, Alex wrote: struct S { @disable this(); @disable this(this); this(size_t dummy){} } Given a struct with an explicit constructor and a postblit. How to make an array of it? You mean with a disabled default ctor and postblit?

Re: scoped classes

2017-04-27 Thread Alex via Digitalmars-d-learn
On Thursday, 27 April 2017 at 16:39:38 UTC, Stanislav Blinov wrote: On Thursday, 27 April 2017 at 15:47:38 UTC, Alex wrote: struct S { @disable this(); @disable this(this); this(size_t dummy){} } Given a struct with an explicit constructor and a postblit. How to make a

Re: scoped classes

2017-04-27 Thread Ali Çehreli via Digitalmars-d-learn
I haven't used it yet but it's worth noting that there is EMSI's container library as well: http://code.dlang.org/packages/emsi_containers Ali

Re: scoped classes

2017-04-27 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 27 April 2017 at 17:07:05 UTC, Ali Çehreli wrote: I haven't used it yet but it's worth noting that there is EMSI's container library as well: http://code.dlang.org/packages/emsi_containers A brief glance at the source of dynamicarray there suggests that it won't help with non-

Re: scoped classes

2017-04-27 Thread ag0aep6g via Digitalmars-d-learn
On 04/27/2017 05:47 PM, Alex wrote: void main() { S[] arr; S s = S(42); arr = [s]; // this doesn't work :( } struct S { @disable this(); @disable this(this); this(size_t dummy){} } 1) Construct the S instance directly in the array literal: arr = [S(42]); But

Re: scoped classes

2017-04-27 Thread Alex via Digitalmars-d-learn
On Thursday, 27 April 2017 at 17:39:42 UTC, ag0aep6g wrote: On 04/27/2017 05:47 PM, Alex wrote: void main() { S[] arr; S s = S(42); arr = [s]; // this doesn't work :( } struct S { @disable this(); @disable this(this); this(size_t dummy){} } 1) Construct the S instance

Re: scoped classes

2017-04-27 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 27 April 2017 at 18:36:08 UTC, Alex wrote: * I'm pretty sure that the code is going to be invalid when you're dealing with const/immutable data. Ok... this is important... I tried this out, and the value of the immutable data even remains the same. But for safety reasons, I would

Re: scoped classes

2017-04-27 Thread Alex via Digitalmars-d-learn
On Thursday, 27 April 2017 at 19:18:14 UTC, Stanislav Blinov wrote: On Thursday, 27 April 2017 at 18:36:08 UTC, Alex wrote: move() is only destructive if the type has custom postblit or destructor, otherwise it's just a bitwise copy. But, as mentioned, there's an issue with it at the moment: i

scoped classes and dependency inversion

2018-11-08 Thread Sjoerd Nijboer via Digitalmars-d-learn
I'm trying to invert the dependency from the classes `Bar -> Foo` to `Foo -> IFoo <- Bar` at compile time. I do want `Foo's` to be embedded into `Bar` So silly me tried something like this: module main; ```import std.stdio; import std.typecons; void main() { auto bar = new Bar!(scoped

Re: scoped classes and dependency inversion

2018-11-08 Thread Alex via Digitalmars-d-learn
On Thursday, 8 November 2018 at 11:04:19 UTC, Sjoerd Nijboer wrote: I'm trying to invert the dependency from the classes `Bar -> Foo` to `Foo -> IFoo <- Bar` at compile time. I do want `Foo's` to be embedded into `Bar` So silly me tried something like this: [...] So how can I delay the constr

Re: scoped classes and dependency inversion

2018-11-08 Thread Sjoerd Nijboer via Digitalmars-d-learn
On Thursday, 8 November 2018 at 12:45:57 UTC, Alex wrote: Hmm... not sure, if I got your idea... Do you think about something like this? **snip** class Bar(TFoo) if(is(TFoo : IFoo)) { typeof(scoped!TFoo()) _foo; this() { _foo = scoped!TFoo();

Re: scoped classes and dependency inversion

2018-11-08 Thread Alex via Digitalmars-d-learn
On Thursday, 8 November 2018 at 15:11:16 UTC, Sjoerd Nijboer wrote: Except if you want to pass a parameter to TFoo it'll become a mess. And I expecially don't want it to become messy. I thought of this case... But passing the argument to TFoo directly while constructing Bar is messier, I thin

Re: scoped classes and dependency inversion

2018-11-08 Thread Neia Neutuladh via Digitalmars-d-learn
On Thu, 08 Nov 2018 12:45:57 +, Alex wrote: > Hmm... not sure, if I got your idea... Do you think about something like > this? The point is dependency inversion. The class shouldn't need to know how to build its dependencies; it should leave that to other code. The fact that you can use the

Re: scoped classes and dependency inversion

2018-11-08 Thread Neia Neutuladh via Digitalmars-d-learn
On Thu, 08 Nov 2018 11:04:19 +, Sjoerd Nijboer wrote: > I'm trying to invert the dependency from the classes `Bar -> Foo` to > `Foo -> IFoo <- Bar` at compile time. > > I do want `Foo's` to be embedded into `Bar` These goals are a *little* at odds with each other; having a scoped!Foo puts si

Re: scoped classes and dependency inversion

2018-11-08 Thread Sjoerd Nijboer via Digitalmars-d-learn
On Thursday, 8 November 2018 at 16:31:26 UTC, Neia Neutuladh wrote: I believe what you need to do is pass a factory function into the constructor. This is a bit awkward. Yep, but I want a "nice and descriptive syntax" for it. Anyway, here's some code to make it work. It's kind of ugly. --- i

Re: scoped classes and dependency inversion

2018-11-09 Thread Sjoerd Nijboer via Digitalmars-d-learn
On Thursday, 8 November 2018 at 21:16:32 UTC, Sjoerd Nijboer wrote: I tried tom make a lazyscoped!T but I'm stuck at creating a constructor and determining the arguments from the Type. Unfortunately I can't find a way in D to get a list of arguments at compile time for a given function. Is thi

Re: scoped classes and dependency inversion

2018-11-09 Thread Alex via Digitalmars-d-learn
On Friday, 9 November 2018 at 09:13:56 UTC, Sjoerd Nijboer wrote: On Thursday, 8 November 2018 at 21:16:32 UTC, Sjoerd Nijboer wrote: I tried tom make a lazyscoped!T but I'm stuck at creating a constructor and determining the arguments from the Type. Unfortunately I can't find a way in D to ge

Re: scoped classes and dependency inversion

2018-11-09 Thread Sjoerd Nijboer via Digitalmars-d-learn
On Friday, 9 November 2018 at 09:17:27 UTC, Alex wrote: Is it this what you are looking for? https://dlang.org/phobos/std_traits.html#Parameters I've been looking over std.traits all day yesterday, how could I've missed that? I'm so glad there are people in this forum that want to help out othe