Re: non empty slices

2016-06-02 Thread Alex via Digitalmars-d-learn
On Thursday, 2 June 2016 at 23:44:49 UTC, ag0aep6g wrote: On 06/03/2016 01:35 AM, ag0aep6g wrote: The alternative `peek` method is not documented to throw an exception, but it's not @nogc either. No idea why. Maybe Algebraic does GC allocations internally. I wouldn't know for what, though. Or

Re: non empty slices

2016-06-02 Thread Alex via Digitalmars-d-learn
On Thursday, 2 June 2016 at 23:35:53 UTC, ag0aep6g wrote: It's the Algebraic. The `get` method isn't @nogc. The documentation [1] says that it may throw an exception, which is most probably being allocated through the GC. So that's a reason why it can't be @nogc. The alternative `peek` method

Re: non empty slices

2016-06-02 Thread ag0aep6g via Digitalmars-d-learn
On 06/03/2016 01:35 AM, ag0aep6g wrote: The alternative `peek` method is not documented to throw an exception, but it's not @nogc either. No idea why. Maybe Algebraic does GC allocations internally. I wouldn't know for what, though. Or it misses a @nogc somewhere. I've looked at the source to s

Re: non empty slices

2016-06-02 Thread ag0aep6g via Digitalmars-d-learn
On 06/03/2016 01:17 AM, Alex wrote: But still, I can't mark the f-method @nogc, and this is not due to the writeln calls... why GC is invoked, although everything is known and no memory allocation should happen? It's the Algebraic. The `get` method isn't @nogc. The documentation [1] says that

Re: non empty slices

2016-06-02 Thread Alex via Digitalmars-d-learn
On Thursday, 2 June 2016 at 22:17:32 UTC, ag0aep6g wrote: Yeah, can't do it that way. You have only one f_impl call, but want it to go to different overloads based on dynamic information (caseS). That doesn't work. You need three different f_impl calls. You can generate them, so there's onl

Re: non empty slices

2016-06-02 Thread ag0aep6g via Digitalmars-d-learn
On 06/02/2016 11:37 PM, Alex wrote: Just tried this instead of your f-function: void f(int[] arr) { A result; import std.meta; alias TL = AliasSeq!(Empty, int, Many!int); int caseS; switch (arr.length) { case 0: result = Empty.init; caseS = 0; break;

Re: non empty slices

2016-06-02 Thread ag0aep6g via Digitalmars-d-learn
On 06/02/2016 10:11 PM, Alex wrote: The cool thing about the Algebraic is as I expected, that it doesn't change it's type... And the hard thing is, that I'm not used to its Empty, Many, ... things yet. I just made those up on the spot. Note that Many is not actually implemented at all. There i

Re: non empty slices

2016-06-02 Thread Alex via Digitalmars-d-learn
On Thursday, 2 June 2016 at 20:11:21 UTC, Alex wrote: On Thursday, 2 June 2016 at 16:21:03 UTC, ag0aep6g wrote: void f(int[] arr) { A a = arrayToA(arr); foreach (T; A.AllowedTypes) { if (T* p = a.peek!T) f_impl(*p); } } You totally hit the

Re: non empty slices

2016-06-02 Thread Alex via Digitalmars-d-learn
On Thursday, 2 June 2016 at 16:21:03 UTC, ag0aep6g wrote: On 06/02/2016 05:16 PM, Alex wrote: I may be getting what you're up to. Maybe not. So we start with something like this (using arrays instead of arbitrary ranges to keep things simple): import std.stdio: writeln; void main()

Re: non empty slices

2016-06-02 Thread ag0aep6g via Digitalmars-d-learn
On 06/02/2016 05:16 PM, Alex wrote: What I mean is: if there would be a possibility to use algebraic types here (or maybe some kind of template...), then my types would be able (maybe! did not seen anything similar yet...) to choose the proper methods for themselves automatically: As long as my t

Re: non empty slices

2016-06-02 Thread Alex via Digitalmars-d-learn
On Thursday, 2 June 2016 at 14:31:15 UTC, ag0aep6g wrote: A little terminology: "Slice" is not a synonym for "range". iota does not return a slice, it returns a range. "Slice" is being used as a synonym for "dynamic array" (Type[]), and in the context of the slicing operator (expression[] or e

Re: non empty slices

2016-06-02 Thread ag0aep6g via Digitalmars-d-learn
On 06/02/2016 03:37 PM, Alex wrote: The question is, how to define the same thing for ranges. What I started with is: import std.range : iota; alias MrSlice = typeof(iota(M.init)); so far, so good. The MrSlice-thing is the analogy for Mr, as it can be empty, as Mr can. What I also need is the a

non empty slices

2016-06-02 Thread Alex via Digitalmars-d-learn
Ok, a strange question from my side again... Let's begin, with what works: Say, I define two types: import std.typecons : Nullable; alias M = uint; alias Mr = Nullable!M; then, I can write two types of methods. Those which can handle Mr's: void fun1(Mr val) { //do some funny stuff } and t