Re: Is std.variant.visit not @nogc?

2018-04-10 Thread aliak via Digitalmars-d-learn
On Tuesday, 10 April 2018 at 03:48:25 UTC, Paul Backus wrote: Nope! It's just a tagged union, almost exactly the same as what you'd write by hand in C. You can take a look at the source yourself, if you're curious---it's actually pretty simple: https://github.com/pbackus/sumtype/blob/master/sr

Re: Compile-time variables

2018-04-06 Thread aliak via Digitalmars-d-learn
On Friday, 6 April 2018 at 02:20:29 UTC, Kayomn wrote: Wrong example code, here's the correct example: switch (queryString(childJson,"type")) { case (typeof (Sprite).name): child = this.addCh

Re: Optional type - how to correctly reset a wrapped immutable T

2018-03-29 Thread aliak via Digitalmars-d-learn
On Tuesday, 27 March 2018 at 15:37:11 UTC, SimonN wrote: On Tuesday, 27 March 2018 at 15:28:40 UTC, jmh530 wrote: static if (isMutable!T) bag[0] = rhs; else bag = [rhs]; I like this idea. I'd even take it a step futher: When T is a pointer or class refe

Re: Optional type - how to correctly reset a wrapped immutable T

2018-03-27 Thread aliak via Digitalmars-d-learn
On Tuesday, 27 March 2018 at 11:57:28 UTC, jmh530 wrote: On Tuesday, 27 March 2018 at 06:26:57 UTC, aliak wrote: [snip] By the by, how come inout has to be stack based and const/immutable/mutable doesn't? Isn't inout just one of those depending on context? Example? Hmm, now that I'm expl

Re: Optional type - how to correctly reset a wrapped immutable T

2018-03-26 Thread aliak via Digitalmars-d-learn
On Monday, 26 March 2018 at 11:19:31 UTC, Seb wrote: On Monday, 26 March 2018 at 10:13:08 UTC, Simen Kjærås wrote: On Monday, 26 March 2018 at 09:46:57 UTC, Nicholas Wilson wrote: Have a look at Rebindable: https://dlang.org/phobos/std_typecons.html#rebindable Allow me to quote from aliak's p

Re: Optional type - how to correctly reset a wrapped immutable T

2018-03-26 Thread aliak via Digitalmars-d-learn
On Monday, 26 March 2018 at 21:17:10 UTC, jmh530 wrote: On Sunday, 25 March 2018 at 21:26:57 UTC, aliak wrote: Hi, I have this optional type I'm working on and I've run in to a little snag when it comes to wrapping an immutable. Basically what I want is for an Optional!(immutable T) to still b

Re: Optional type - how to correctly reset a wrapped immutable T

2018-03-26 Thread aliak via Digitalmars-d-learn
On Sunday, 25 March 2018 at 23:00:11 UTC, Simen Kjærås wrote: On Sunday, 25 March 2018 at 21:26:57 UTC, aliak wrote: struct Optional(T) { Unqual!T value; opAssign(T t) { value = cast(Unqual!T)(t); } } Consider this case: Optional!(immutable int) a = some(3); immutable int* p = &a.va

Re: Help with specific template function

2018-03-25 Thread aliak via Digitalmars-d-learn
On Sunday, 25 March 2018 at 19:06:14 UTC, Vladimirs Nordholm wrote: On Sunday, 25 March 2018 at 18:24:37 UTC, Vladimirs Nordholm wrote: The underlying problems are: * How do I ensure the two first arguments (used as coordinates) are types of numbers (all kinds: ints, floats, reals, etc.) * At

Optional type - how to correctly reset a wrapped immutable T

2018-03-25 Thread aliak via Digitalmars-d-learn
Hi, I have this optional type I'm working on and I've run in to a little snag when it comes to wrapping an immutable. Basically what I want is for an Optional!(immutable T) to still be settable to "some" value or "no" value because the Optional wrapper itself is mutable. Optional!(immutable i

Re: Logging Function Parameters

2018-03-19 Thread Aliak via Digitalmars-d-learn
On Sunday, 18 March 2018 at 23:17:58 UTC, Dennis wrote: On Sunday, 18 March 2018 at 22:57:15 UTC, aliak wrote: // But you get a: // Error: Using the result of a comma expression is not allowed // writeln(mixin(arguments!f)); You can't mix part of a function call in: "Mixed in tex

Re: Logging Function Parameters

2018-03-18 Thread aliak via Digitalmars-d-learn
On Saturday, 17 March 2018 at 10:34:41 UTC, dom wrote: Hi, I am looking for a method to log the current function name + parameters. Getting the name of the current function is simply possible with __PRETTY_FUNCTION__ Is there some possibility to generically access the parameters of a function

Re: How to simplify nested ifs

2018-03-13 Thread aliak via Digitalmars-d-learn
On Tuesday, 13 March 2018 at 13:13:07 UTC, rumbu wrote: On Tuesday, 13 March 2018 at 12:23:06 UTC, Ozan Süel wrote: Hi I have a construction like the following if (source) { if (source.pool) { if (source.pool.repository) { if (source.pool.repository.directory) { if (source.

Re: UFCS in generic libraries, silent hijacking, and compile errors.

2018-03-13 Thread aliak via Digitalmars-d-learn
On Sunday, 11 March 2018 at 15:24:31 UTC, Jonathan M Davis wrote: On Sunday, March 11, 2018 08:39:54 aliak via Digitalmars-d-learn wrote: On Saturday, 10 March 2018 at 23:00:07 UTC, Jonathan M Davis > issue in practice. That doesn't mean that it's never a > problem, but fro

Re: how to make private class member private

2018-03-13 Thread aliak via Digitalmars-d-learn
On Tuesday, 13 March 2018 at 06:58:08 UTC, psychoticRabbit wrote: On Tuesday, 13 March 2018 at 06:03:11 UTC, Mike Parker wrote: The same applies here. Encapsulation simply isn't broken by this feature. What you're saying, is in D, class encapsulation is really 'module' encapsulation. I ge

Re: How do you call an eponymous template that has a secondary template arg?

2018-03-12 Thread aliak via Digitalmars-d-learn
On Monday, 12 March 2018 at 04:15:23 UTC, Simen Kjærås wrote: Yeah, that's a little hole in the grammar, but there are ways: // Declare an alias: alias aliasOfInt = aliasOf!int; // And use that: assert(!aliasOfInt!string); Or use std.meta.Instantiate: assert(!Instantiate!(aliasOf!int, string

Re: How do you call an eponymous template that has a secondary template arg?

2018-03-11 Thread aliak via Digitalmars-d-learn
On Sunday, 11 March 2018 at 13:44:38 UTC, Basile B. wrote: The first version works here: ``` template aliasOf(T) { enum aliasOf(alias a) = is(typeof(a) == T); } string s; pragma(msg, allSatisfy!(aliasOf!string, s, "string")); ``` I can see that my description was a little confusing, so

How do you call an eponymous template that has a secondary template arg?

2018-03-11 Thread aliak via Digitalmars-d-learn
Eg: template aliasOf(T) { enum aliasOf(alias a) = is(typeof(a) == T); } The use case for this is for std.meta.allSatisfy for variadic args, i.e. template T(values...) if (allSatisfy!(aliasOf!string, values) { ... } But how do you call that template otherwise? I've tries: * aliasOf!in

Re: UFCS in generic libraries, silent hijacking, and compile errors.

2018-03-11 Thread aliak via Digitalmars-d-learn
On Saturday, 10 March 2018 at 23:00:07 UTC, Jonathan M Davis wrote: The idea is that the type can provide its own version of the function that is better optimized for it - e.g. it could potentially provide a member function find that is more efficient for it than std.algorithm.searching.find.

UFCS in generic libraries, silent hijacking, and compile errors.

2018-03-10 Thread aliak via Digitalmars-d-learn
What are the recommended guidelines for using/not using UFCS in writing generic libraries? I ask because if you have an internal generic free function that you use on types in a generic algorithm via ufcs, then everything works fine until the type being operated on has a member function with

Re: isInputRange copied verbatim produces a different result than isInputRange from std.range

2018-03-04 Thread aliak via Digitalmars-d-learn
On Sunday, 4 March 2018 at 13:17:30 UTC, Adam D. Ruppe wrote: On Sunday, 4 March 2018 at 12:57:41 UTC, aliak wrote: @property int front(D d) { return 2; } @property bool empty(D d) { return false; } void popFront(D d) {} Those functions are in scope for your function, but not inside std.range

Re: isInputRange copied verbatim produces a different result than isInputRange from std.range

2018-03-04 Thread aliak via Digitalmars-d-learn
On Sunday, 4 March 2018 at 13:17:30 UTC, Adam D. Ruppe wrote: On Sunday, 4 March 2018 at 12:57:41 UTC, aliak wrote: @property int front(D d) { return 2; } @property bool empty(D d) { return false; } void popFront(D d) {} Those functions are in scope for your function, but not inside std.range

isInputRange copied verbatim produces a different result than isInputRange from std.range

2018-03-04 Thread aliak via Digitalmars-d-learn
Hi, I have a custom type D with front/popFront/empty implemented as free functions, but isInputRange returns false. I copied the implementation of isInputRange, and that custom implementation returns true... anyone know what's going on here? === import std.stdio, std.range, std.traits; // Cod

Re: Trying to forward unwrapped opDispatch names to alias this

2018-02-20 Thread aliak via Digitalmars-d-learn
On Tuesday, 20 February 2018 at 16:12:17 UTC, Adam D. Ruppe wrote: On Monday, 19 February 2018 at 08:28:22 UTC, aliak wrote: T is the wrapped type. So if T has a member (in the example it's the built in field "max") then forward that. Oh, I see what you mean. So the problem is that built in t

Re: Trying to forward unwrapped opDispatch names to alias this

2018-02-20 Thread aliak via Digitalmars-d-learn
On Tuesday, 20 February 2018 at 11:27:23 UTC, Alex wrote: There is a related ticket, https://issues.dlang.org/show_bug.cgi?id=6434 However, not exactly facing this question. Should that ticket be marked as resolved? The issue is for alias this to be considered before opDispatch but there were

Re: Trying to forward unwrapped opDispatch names to alias this

2018-02-19 Thread aliak via Digitalmars-d-learn
On Monday, 19 February 2018 at 01:00:23 UTC, Adam D. Ruppe wrote: On Monday, 19 February 2018 at 00:42:05 UTC, aliak wrote: struct B(T) { T t; A a; alias a this; auto opDispatch(string name)() if (hasMember!(T, name)) { return mixin("t." ~ name); Did you perhaps mean `A

Trying to forward unwrapped opDispatch names to alias this

2018-02-18 Thread aliak via Digitalmars-d-learn
I have a scenario where I'm wrapping functionality for a type, but only if the contained type has a member. I want those to take precedence. If the member is not there, then I want to delegate to an aliases type via alias this. I get an error here when I call b.p. Even though property p is in

Re: opCast cannot implicitly convert a.opCast of type X to Y

2018-02-14 Thread aliak via Digitalmars-d-learn
On Thursday, 15 February 2018 at 00:34:33 UTC, Meta wrote: On Thursday, 15 February 2018 at 00:27:40 UTC, Meta wrote: On Wednesday, 14 February 2018 at 23:46:30 UTC, aliak wrote: On Wednesday, 14 February 2018 at 15:14:24 UTC, Meta wrote: Ooh yes, of course! Thank you :) Even better: import

Re: opCast cannot implicitly convert a.opCast of type X to Y

2018-02-14 Thread aliak via Digitalmars-d-learn
On Wednesday, 14 February 2018 at 15:14:24 UTC, Meta wrote: I think the best way to do this is to implement `map` for your optional type. Optional!U map(U, alias f)() { return empty? no!U : some!U(f(t)); } Optional!int a = 3; auto b = a.map!(v => cast(float)v); assert(is(typeof(b) == Opt

Re: opCast cannot implicitly convert a.opCast of type X to Y

2018-02-13 Thread aliak via Digitalmars-d-learn
On Tuesday, 13 February 2018 at 12:12:30 UTC, Nathan S. wrote: On Monday, 12 February 2018 at 02:05:16 UTC, aliak wrote: struct B(T) { T t; } struct A(T) { T t; auto opCast(U)() { return B!U(cast(U)t); } } void main() { auto a = A!int(3); auto b = cast(float)a;

Re: opUnary with ++ and -- on a struct that has a dynamic array

2018-02-12 Thread aliak via Digitalmars-d-learn
On Monday, 12 February 2018 at 06:16:21 UTC, rumbu wrote: writeln(a++) translates to: A copy = a; a.opUnary!"++"; writeln(copy); copy.a[] and a.a[] are the same reference, you increment a.a[0]/copy.a[0] in opUnary to make this work you will need a postblit constructor: struct A {

opUnary with ++ and -- on a struct that has a dynamic array

2018-02-11 Thread aliak via Digitalmars-d-learn
Hi, Is there a way to get post increment and pre increment working properly in this scenario? import std.stdio; struct A { int[] a; this(int a) { this.a = [a]; } auto opUnary(string op)(){ return A(mixin(op ~ "this.a[0]")); } } void main() { auto a = A(0); int

opCast cannot implicitly convert a.opCast of type X to Y

2018-02-11 Thread aliak via Digitalmars-d-learn
From spec: Cast expression: "cast ( Type ) UnaryExpression" converts UnaryExpresssion to Type. And https://dlang.org/spec/operatoroverloading.html#cast makes no mention of the return type of opCast. One could think that the return type of opCast would be the return type. But it seems it must

Re: free func "front" is not picked up as a candidate when doing range.front(...)

2018-02-11 Thread aliak via Digitalmars-d-learn
On Thursday, 8 February 2018 at 22:57:04 UTC, Jonathan M Davis wrote: D tends to be very picky about what it puts in overload sets in order to avoid function hijacking - e.g. it doesn't even include base class functions in an overload set once you've declared one in a derived class unless you e

Re: free func "front" is not picked up as a candidate when doing range.front(...)

2018-02-08 Thread aliak via Digitalmars-d-learn
On Thursday, 8 February 2018 at 19:32:42 UTC, Jonathan M Davis wrote: On Thursday, February 08, 2018 08:18:20 aliak via Digitalmars-d-learn wrote: On Thursday, 8 February 2018 at 07:16:43 UTC, Jonathan M Davis wrote: > It would be a disaster if free functions could override > member fun

Re: free func "front" is not picked up as a candidate when doing range.front(...)

2018-02-08 Thread aliak via Digitalmars-d-learn
On Thursday, 8 February 2018 at 07:16:43 UTC, Jonathan M Davis wrote: It would be a disaster if free functions could override member functions. For starters, it would be impossible to call the member function if that were allowed, whereas you can always call a free function by not using UFCS. A

Re: free func "front" is not picked up as a candidate when doing range.front(...)

2018-02-07 Thread aliak via Digitalmars-d-learn
On Wednesday, 7 February 2018 at 20:08:10 UTC, Paul Backus wrote: You can only call a function with UFCS syntax if the object you're calling it with does not have a member with the same name as the function. Both iota's `Result` type and `FilterResult` have properties named "front", so you ca

free func "front" is not picked up as a candidate when doing range.front(...)

2018-02-07 Thread aliak via Digitalmars-d-learn
Hi, I'm trying to make a range.front free function that can be given a defaultValue. Doesn't seem to be working as is written below, seems like the compiler doesn't see the free function as a viable candidate. Isn't it supposed to do its UFCS wizardry and pick up the free func? import std.std

Re: Lambda returning a lambda?

2018-02-02 Thread aliak via Digitalmars-d-learn
On Friday, 2 February 2018 at 01:31:15 UTC, Seb wrote: Are you aware of partial? https://dlang.org/phobos/std_functional.html#partial Si si :) And now I'm thinking, practically, that might be enough. So thanks for the prod.

Re: Can you constrain a type based on properties of what it is being referenced by?

2018-02-02 Thread aliak via Digitalmars-d-learn
On Friday, 2 February 2018 at 14:19:37 UTC, aliak wrote: ... (see post in general forum by Simon for details [1]) *Simen Gah! Sorry!

Can you constrain a type based on properties of what it is being referenced by?

2018-02-02 Thread aliak via Digitalmars-d-learn
To further explain what I mean: struct A if (!is(this == immutable) && !is(this == shared)) { } shared a = A() // error auto a = A() // ok immutable a = A() // error const a = A() // ok Fake syntax above of course. I was thinking about this because I read a few posts about const, and approach

Re: Lambda returning a lambda?

2018-02-01 Thread aliak via Digitalmars-d-learn
On Thursday, 1 February 2018 at 14:28:34 UTC, Simen Kjærås wrote: -- Simen Ah, thank you both. For solution and explanation. Me wonders... are there any thoughts around having functions return aliases as well? I have no idea if it's even possible but if it is, then does the initial syntax

Lambda returning a lambda?

2018-02-01 Thread aliak via Digitalmars-d-learn
Is there a way to do this: import std.stdio; void main() { alias f = (a) => (b) => a * b; f(2)(3).writeln; } Error now is: Error: template lambda has no type Cheers

Re: questions around mutating range algorithms, const, and return ref

2018-01-30 Thread aliak via Digitalmars-d-learn
On Tuesday, 30 January 2018 at 09:51:18 UTC, Ali Çehreli wrote: > [...] is trying to > [...] It's the same with C++: A type with a const member cannot have a compiler-generated assignment operator. Ok, that made it obvious :) 'const' as a member function attribute is meaningful: It makes t

Re: questions around mutating range algorithms, const, and return ref

2018-01-30 Thread aliak via Digitalmars-d-learn
On Monday, 29 January 2018 at 13:55:04 UTC, Seb wrote: On Monday, 29 January 2018 at 11:36:26 UTC, aliak wrote: On Monday, 29 January 2018 at 06:46:26 UTC, Ali Çehreli wrote: I think the following trivial wrapper around std.algorithm.remove() should do: void removeMatching(R, N)(ref R r, N ne

Re: questions around mutating range algorithms, const, and return ref

2018-01-30 Thread aliak via Digitalmars-d-learn
On Monday, 29 January 2018 at 12:10:16 UTC, Simen Kjærås wrote: Consider this case: immutable(int)[] a = [1,2,3]; immutable(int)* b = &a[1]; You're free to slice the array or pop elements off its front or back, but if you change the order of elements, the value that b points to will change.

Re: questions around mutating range algorithms, const, and return ref

2018-01-29 Thread aliak via Digitalmars-d-learn
On Monday, 29 January 2018 at 06:46:26 UTC, Ali Çehreli wrote: I think the following trivial wrapper around std.algorithm.remove() should do: void removeMatching(R, N)(ref R r, N needles) { import std.algorithm : remove, canFind; r = r.remove!(e => needles.canFind(e)); } Haha awesome

questions around mutating range algorithms, const, and return ref

2018-01-28 Thread aliak via Digitalmars-d-learn
Hello, I'm trying to write a function called "pull" that, given 2 ranges, "pull"s the values from range 2 out of range 1. I'm not sure if I'm doing it correctly though, and I have some questions so any help is appreciated. This is what I have: ref pull(R1, R2)(return ref R1 r1, R2 r2) { im

Re: Rewriting a c++ template to D (replacing iterator with ranges "properly")

2018-01-27 Thread aliak via Digitalmars-d-learn
On Friday, 26 January 2018 at 23:15:41 UTC, Simen Kjærås wrote: The function is called fill, and assigns a value to every element in the range. If a[0] = false compiles, we also want a.fill(false) to compile. It's simply testing that, rather than caring about the exact type of the elements.

Re: Rewriting a c++ template to D (replacing iterator with ranges "properly")

2018-01-26 Thread aliak via Digitalmars-d-learn
On Friday, 26 January 2018 at 14:59:09 UTC, Simen Kjærås wrote: what is N here? You're declaring it to be an int value in the template<> definition, and then use it as a type in the function definition. Oops again :) Should've been typename N (where N is some integral type). Not exactly. ra

Re: Rewriting a c++ template to D (replacing iterator with ranges "properly")

2018-01-26 Thread aliak via Digitalmars-d-learn
On Friday, 26 January 2018 at 14:35:25 UTC, Meta wrote: On Friday, 26 January 2018 at 14:16:04 UTC, aliak wrote: 1) I've seen some phobos code checking for assignability like this: is(typeof(range.front = false)) ... is that an advantage of that over hasAssignableElements? Or is that just

Re: Rewriting a c++ template to D (replacing iterator with ranges "properly")

2018-01-26 Thread aliak via Digitalmars-d-learn
On Friday, 26 January 2018 at 14:16:04 UTC, aliak wrote: range.front = false; while (!range.empty) { range.popFrontN(N); range.front = false; } } Oops, this should be: while (!range.empty) { range.front = false; range.popFrontN(N); }

Rewriting a c++ template to D (replacing iterator with ranges "properly")

2018-01-26 Thread aliak via Digitalmars-d-learn
It basically steps through in a stride and sets the checkpoints to false. C++: template void mark(It begin, It end, N step) { assert(begin != end) *begin = false; while (end - begin > step) { begin = begin + step; *begin = false; } } For D this is what I figured would be the wa

Re: getting member functions of a struct and Error: identifier expected following ., not this

2018-01-24 Thread aliak via Digitalmars-d-learn
On Wednesday, 24 January 2018 at 07:55:01 UTC, thedeemon wrote: On Tuesday, 23 January 2018 at 00:00:38 UTC, aliak wrote: [...] The struct defined inside a scope can mention variables defined in that scope (e.g. use them in its methods), so it needs a pointer to the place where those closed

Re: getting member functions of a struct and Error: identifier expected following ., not this

2018-01-22 Thread aliak via Digitalmars-d-learn
On Tuesday, 23 January 2018 at 00:00:38 UTC, aliak wrote: Hi, I'm trying to get a list of only member functions of a [...] Cheers And a follow up question: should I be using static foreach in there instead of a normal foreach? i.e. foreach (member; __traits(allMembers, T)) {{ // same cod

getting member functions of a struct and Error: identifier expected following ., not this

2018-01-22 Thread aliak via Digitalmars-d-learn
Hi, I'm trying to get a list of only member functions of a struct. I've found that if you do not declare a struct as static inside a scope, then there's a hidden "this" member as part of the struct. Can someone explain the logic there? Also am I going about this correctly? template MemberFunc

Re: Can you introspect predicate arity and if it's an equality predicate?

2018-01-12 Thread aliak via Digitalmars-d-learn
On Friday, 12 January 2018 at 10:55:53 UTC, Seb wrote: On Friday, 12 January 2018 at 00:16:07 UTC, aliak wrote: Hi, so basically is there a way to: void func(alias pred = null, Range)(Range range) { // 1) check if pred(ElementType!Range.init, ElementType!Range.init) is equality // 2) chec

Re: Can you introspect predicate arity and if it's an equality predicate?

2018-01-12 Thread aliak via Digitalmars-d-learn
On Friday, 12 January 2018 at 08:18:02 UTC, Simen Kjærås wrote: On Friday, 12 January 2018 at 00:16:07 UTC, aliak wrote: Hi, so basically is there a way to: void func(alias pred = null, Range)(Range range) { // 1) check if pred(ElementType!Range.init, ElementType!Range.init) is equality /

Can you introspect predicate arity and if it's an equality predicate?

2018-01-11 Thread aliak via Digitalmars-d-learn
Hi, so basically is there a way to: void func(alias pred = null, Range)(Range range) { // 1) check if pred(ElementType!Range.init, ElementType!Range.init) is equality // 2) check if isUnary!pred // 3) check if isBinary!pred } I think maybe the isUnary or isBinary may not work unless it

Re: Voldemort type for mixin template.

2018-01-11 Thread aliak via Digitalmars-d-learn
On Thursday, 11 January 2018 at 08:56:11 UTC, ChangLong wrote: When I try add some sub type for struct with mixin template, seems there is no way to hidden the private type. Is there a way to hidden type from mix template like Voldemort type ? fake code: mix template TypeX () { alias Th

Re: __traits(isSame, TemplateOf ...) errors and some Idiomatic D questions

2018-01-08 Thread aliak via Digitalmars-d-learn
On Monday, 8 January 2018 at 23:03:46 UTC, H. S. Teoh wrote: On Mon, Jan 08, 2018 at 10:59:44PM +, aliak via Digitalmars-d-learn wrote: [...] onlineapp.d(61): Error: template std.traits.TemplateOf does not match any template declaration. And I use it like this: enum r1Sorted = __traits

Re: __traits(isSame, TemplateOf ...) errors and some Idiomatic D questions

2018-01-08 Thread aliak via Digitalmars-d-learn
On Monday, 8 January 2018 at 23:22:04 UTC, Seb wrote: On Monday, 8 January 2018 at 23:14:32 UTC, Seb wrote: Your problem is that `TemplateOf!(int[])` isn't defined. It should probably be changed to return `void`. https://github.com/dlang/phobos/pull/6016 Damn that's some fast turnaround! And

__traits(isSame, TemplateOf ...) errors and some Idiomatic D questions

2018-01-08 Thread aliak via Digitalmars-d-learn
Hi, trying to write some idiomatic generic D code and I'm a bit stuck with using the TemplateOf to check if a range is a SortedRange or not. A bit about the code, I'm basically rewriting https://dlang.org/library/std/algorithm/setops/set_difference.html but I want to do different things based on

Re: static if and early exit from function doesn't seem to work?

2018-01-01 Thread aliak via Digitalmars-d-learn
On Sunday, 31 December 2017 at 13:47:32 UTC, Adam D. Ruppe wrote: On Sunday, 31 December 2017 at 13:32:03 UTC, aliak wrote: So it seems it tries to compile the statements below the check on V.length even though it's guaranteed to be true and there's a return statement inside the if. Yeah, sta

Re: How do you safely deal with range.front?

2018-01-01 Thread aliak via Digitalmars-d-learn
On Monday, 1 January 2018 at 02:18:36 UTC, Jonathan M Davis wrote: Except that the reason for arrays throwing RangeErrors when you try and index them out-of-bounds is to avoid memory safety issues, which is not necessarily the case at all when you're talking about ranges. Having ranges in gener

Re: How do you safely deal with range.front?

2018-01-01 Thread aliak via Digitalmars-d-learn
On Monday, 1 January 2018 at 04:18:29 UTC, Ali Çehreli wrote: If you're fine with specifying the function as a template argument, the following works. (As seen with 's => s.foo()' below, you have to use a lambda for member functions anyway.) Ali Nice! Thanks :) And I think your usage for som

static if and early exit from function doesn't seem to work?

2017-12-31 Thread aliak via Digitalmars-d-learn
Alo! I'm making a recursive concat function that is similar to chain. The following code works: import std.range: isInputRange; auto concat(R, V...)(R range, V values) if (isInputRange!R) { import std.range: chain, ElementType; static if (V.length) { static if (isInputRange!(V[0

Re: How do you safely deal with range.front?

2017-12-31 Thread aliak via Digitalmars-d-learn
On Sunday, 31 December 2017 at 01:03:17 UTC, Tony wrote: For me, front() should throw a pre-defined exception when called on an empty range in order to eliminate undefined behavior. It does take some time to make a check, but D does array bounds checking by default. Ideally the front() check c

Re: How do you safely deal with range.front?

2017-12-30 Thread aliak via Digitalmars-d-learn
On Friday, 29 December 2017 at 20:33:22 UTC, Jonathan M Davis wrote: Well, I don't know what you're really doing in code here, but in general, you'd write your code in a way that it checks empty and simply doesn't try to do anything with front if the range is empty. Yes, ideally, if programme

Re: How do you safely deal with range.front?

2017-12-30 Thread aliak via Digitalmars-d-learn
On Friday, 29 December 2017 at 20:47:44 UTC, Dukc wrote: On Friday, 29 December 2017 at 19:38:44 UTC, aliak wrote: So when I'm dealing with ranges, there're a number of times where I get the front of the returned result of a set of operations, but of course there is no front so you get an runt

Re: How do you safely deal with range.front?

2017-12-30 Thread aliak via Digitalmars-d-learn
On Friday, 29 December 2017 at 20:11:03 UTC, Seb wrote: On Friday, 29 December 2017 at 19:38:44 UTC, aliak wrote: Hi, So when I'm dealing with ranges, there're a number of times where I get the front of the returned result of a set of operations, but of course there is no front so you get an

How do you safely deal with range.front?

2017-12-29 Thread aliak via Digitalmars-d-learn
Hi, So when I'm dealing with ranges, there're a number of times where I get the front of the returned result of a set of operations, but of course there is no front so you get an runtime access error. In some other languages the concept of "front" or "first" returns a safe referece, or opti

Re: Converting array in to aliased tuple type.

2017-12-25 Thread aliak via Digitalmars-d-learn
On Monday, 25 December 2017 at 17:59:54 UTC, visitor wrote: On Monday, 25 December 2017 at 15:03:08 UTC, aliak wrote: On Monday, 25 December 2017 at 14:08:08 UTC, Mengu wrote: I was kind of hoping for some magical D variadic alias template on Tuple or something that will just deconstruct the

Re: Define enum value at compile time via compiler argument?

2017-12-25 Thread aliak via Digitalmars-d-learn
On Monday, 25 December 2017 at 16:38:32 UTC, Thomas Mader wrote: On Monday, 25 December 2017 at 16:22:11 UTC, Mengu wrote: is it a relative path? if so: pragma(msg, __FILE_FULL_PATH__.split("/")[0..$-1].join("/")); https://run.dlang.io/is/gRUAD6 Nice idea but it is an absolute path. :

Re: Converting array in to aliased tuple type.

2017-12-25 Thread aliak via Digitalmars-d-learn
On Monday, 25 December 2017 at 14:08:08 UTC, Mengu wrote: since Point is a Tuple and does not have a constructor that takes a list of integers (int[]), you should have a helper function. Aukay :( I was kind of hoping for some magical D variadic alias template on Tuple or something that wil

Re: Does to!(string)(char[]) do any memory allocation on conversion?

2017-12-25 Thread aliak via Digitalmars-d-learn
On Monday, 25 December 2017 at 14:12:32 UTC, Marc wrote: Does to!(string)(char[]) do any memory allocation on conversion or is this similar to a cast or what else? As said it calls idup, which calls _trustedDup which seems to call _dup which does memory allocation -> https://github.com/dlang/

Converting array in to aliased tuple type.

2017-12-25 Thread aliak via Digitalmars-d-learn
Hi, been looking for a way to convert an array to a tuple, but can't seem to find one. Is there one? Looking for something like: alias Point = Tuple!(int, "x", int, "y"); enum data = "1,2:8,9"; auto points = data .split(':') .map!(a => a .split(',') .map!(to!int) ) .map!Point; /

Re: std way to remove multiple indices from an array at once

2017-12-22 Thread aliak via Digitalmars-d-learn
On Thursday, 21 December 2017 at 15:59:44 UTC, Steven Schveighoffer wrote: Here's a similar solution with an actual range: https://run.dlang.io/is/gR3CjF Note, all done lazily. However, the indices must be sorted/unique. -Steve Noice! :D

Re: std way to remove multiple indices from an array at once

2017-12-21 Thread aliak via Digitalmars-d-learn
On Thursday, 21 December 2017 at 00:52:29 UTC, Nicholas Wilson wrote: On Thursday, 21 December 2017 at 00:23:08 UTC, Steven Schveighoffer wrote: I'm assuming here indices is sorted? Because it appears you expect that in your code. However, I'm going to assume it isn't sorted at first. auto

std way to remove multiple indices from an array at once

2017-12-20 Thread aliak via Digitalmars-d-learn
Hi, is there a way to remove a number of elements from an array by a range of indices in the standard library somewhere? I wrote one (code below), but I'm wondering if there's a better way? Also, can the below be made more efficient? auto without(T, R)(T[] array, R indices) if (isForwardRang

Re: Halp! type system (__expand_field_0 error), compile time/runtime questions (AoC-2017 puzzle spoilers inside)

2017-12-15 Thread aliak via Digitalmars-d-learn
On Friday, 15 December 2017 at 01:43:04 UTC, Steven Schveighoffer wrote: So the CTFE interpreter doesn't like something to do with your chain of ranges. This is not totally unexpected, as the CTFE engine has lots of quirks that make it sometimes puke on valid CTFE-able code. :( At this time,

Re: Halp! type system (__expand_field_0 error), compile time/runtime questions (AoC-2017 puzzle spoilers inside)

2017-12-14 Thread aliak via Digitalmars-d-learn
On Thursday, 14 December 2017 at 16:38:26 UTC, Steven Schveighoffer wrote: So enumerate returns as its element type a Tuple. Specifically, it's going to be a Tuple!(size_t, int), since you are enumerating an array of ints. I'm not sure why you are having the error, compiling your code above w

Re: Halp! type system (__expand_field_0 error), compile time/runtime questions (AoC-2017 puzzle spoilers inside)

2017-12-14 Thread aliak via Digitalmars-d-learn
On Thursday, 14 December 2017 at 15:28:22 UTC, aliak wrote: int[] rotate(int[] list, int[] lengths) { auto range = list.cycle; foreach (skip, length; lengths.enumerate) { // do stuff to range } return list; } Ok srsly, I got things to at least compile by changing the

Halp! type system (__expand_field_0 error), compile time/runtime questions (AoC-2017 puzzle spoilers inside)

2017-12-14 Thread aliak via Digitalmars-d-learn
Warning: following has Advent of Code 2017 (day 14 and day 10) spoilers incase you are doing those challenges. And apologies for the slightly long post, I'm unsure how much information is needed here and am a bit clueless. Any help would be much appreciated. = Ok, so I'm trying to get thr

Variable cannot be read at compile time.

2017-12-07 Thread aliak via Digitalmars-d-learn
Hi, I'm having a bit a trouble doing some compile time parsing. This works: immutable str = "he-.llo-the.re"; immutable separators = "-."; enum a = str.splitter(separators).array; But this does not: enum b = str.splitter!(a => separators.canFind(a)).array; The error is: cannot deduce function

Re: cannot deduce template lambda from argument

2017-12-06 Thread aliak via Digitalmars-d-learn
On Wednesday, 6 December 2017 at 11:02:01 UTC, Jonathan M Davis wrote: If you only want one type, then given n that type; I'm pretty sure that it would be alias lambda = (int n) => n * n; if you wanted an int. But if you want to do anything more complicated with it, it would make more sense t

Re: cannot deduce template lambda from argument

2017-12-06 Thread aliak via Digitalmars-d-learn
On Wednesday, 6 December 2017 at 08:10:26 UTC, Biotronic wrote: On Tuesday, 5 December 2017 at 23:01:43 UTC, aliak wrote: immutable lambda(T) = (T n) => n * n; Generally, you'd want to write alias lambda = n => n * n; instead. That said, I don't see any reason why your syntax shouldn't

cannot deduce template lambda from argument

2017-12-05 Thread aliak via Digitalmars-d-learn
Hi, Having a little trouble understanding lambda type deduction. I have this lambda: immutable lambda(T) = (T n) => n * n; and if I call it with an explicit type it works else it errors with: lambda cannot deduce function from argument types !()(int) auto x = lambda!int(2); // ok auto x =

Re: Is there anything other than nullable to work with optional types?

2016-12-26 Thread aliak via Digitalmars-d-learn
On Monday, 26 December 2016 at 04:55:30 UTC, Seb wrote: Then help to push it forward!! There are many ways: - review the PR and point out anything problematic you see (lack of reviews/interest is a main reason why PRs get stalled) - post reasons and arguments for merging this PR (seems like yo

Re: Is there anything other than nullable to work with optional types?

2016-12-25 Thread aliak via Digitalmars-d-learn
On Sunday, 25 December 2016 at 20:11:50 UTC, Seb wrote: On Sunday, 25 December 2016 at 19:22:10 UTC, aliak wrote: or is there a way for it to not be so cumbersome to work with? You might be interested in https://github.com/dlang/phobos/pull/3915 Yes! That! Seems like that PR is on a break

Is there anything other than nullable to work with optional types?

2016-12-25 Thread aliak via Digitalmars-d-learn
Hey, So, been using the programming language swift for a while now, the optional types[1] they support makes working with maybe-type (ala haskell) values extremely pleasant. Does D have anything other than the Nullable template that can be used to work with optionals, or is there a way for i

Re: Runtime error trying to call opCall on variant array of objects

2016-12-25 Thread aliak via Digitalmars-d-learn
On Saturday, 24 December 2016 at 23:06:25 UTC, Ali Çehreli wrote: auto c = Command!(fun, Args)(args); writefln("Created %s", c); // (3) Workaround: Storing delegates, not Command instances. return () => c(); Ah, yes. Nice work around :) Thankies!

Runtime error trying to call opCall on variant array of objects

2016-12-24 Thread aliak via Digitalmars-d-learn
Hey, so I'm trying to make an variant array of objects (each one has an opCall defined) and then call each of them in succession. It doesn't seem to be working. The error I get is: "Cannot apply `()' to a value of type `Command!(__lambda1, int)". I think it's failing when it checks for "!isFun

<    1   2   3