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

2016-12-25 Thread Seb via Digitalmars-d-learn

On Sunday, 25 December 2016 at 22:21:17 UTC, aliak wrote:

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 though :(


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 you 
have a couple of good real world examples - post them!)
- revive the PR (it's quite old and the author probably lost 
interest. There's absolutely nothing wrong with taking the diff 
and resubmitting it - on the contrary that's often the only 
possible way to revive/save old PRs)

...


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 though :(


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

2016-12-25 Thread ali ak via Digitalmars-d-learn

On Sunday, 25 December 2016 at 20:01:21 UTC, Stefan Koch wrote:

Well there is one easy way to do this.
pass a pointer to the data.
Another easy one is the definition of one invalid set of values 
and using that is initial value and for convenience overload 
opCast(T:bool) to check against that instance.


Well sure, certainly solutions. Could also use exception. But I'm 
looking for the enhanced API clarity, reduced pointer related 
errors, and added type safety that optionals usually provide, and 
it seems the closest thing in D was Nullable.


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

2016-12-25 Thread Seb via Digitalmars-d-learn

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

And

https://github.com/dlang/phobos/pull/4989


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

2016-12-25 Thread Stefan Koch via Digitalmars-d-learn

On Sunday, 25 December 2016 at 19:22:10 UTC, aliak wrote:

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.


[...]


Well there is one easy way to do this.
pass a pointer to the data.
Another easy one is the definition of one invalid set of values 
and using that is initial value and for convenience overload 
opCast(T:bool) to check against that instance.


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 it to not be 
so cumbersome to work with?


Eg: Currently I have a function like this:

struct MarkerData {
long start;
long end;
long length;
long times;
}

Nullable!MarkerData pluckMarker(string str) {
auto start = str.indexOf("(");
if (start == -1) {
return typeof(return).init;
}
auto end = str.indexOf(")", start);
if (end == -1) {
return typeof(return).init;
}
auto parts = str[start+1..end].split("x");
auto length = to!long(parts[0]);
auto times = to!long(parts[1]);
return Nullable!MarkerData(MarkerData(start, end, length, 
times));

}

Everywhere I have to return the Nullable! type, I have to either 
use that typeof(return).init to make it a null value, or a quite 
verbose constructor call to return a concrete type.


Can it be made simpler to use without me having to alias 
Nullable!MarkerData (which still wouldn't help with just 
returning null).


[1] will try a super quick explanation of optional types. You 
basically add a '?' to any type declaration and then it's 
nullable. And you can implicitly assign nil and also the concrete 
type.


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!


Re: How to initialize a associative array?

2016-12-25 Thread Jack Applegame via Digitalmars-d-learn

On Saturday, 24 December 2016 at 00:55:01 UTC, Yuxuan Shui wrote:

I tried this:

immutable int[char] xx = ['Q':0, 'B':1, 'N':2, 'R':3, 
'P':4];


And got a "non-constant expression" error (with or without 
'immutable').


What's the correct way?


This works:

void main() {
immutable int[char] xx = ['Q':0, 'B':1, 'N':2, 'R':3, 'P':4];
}

If you want to initialize global variable, then

immutable int[char] xx;
shared static this() {
xx = ['Q':0, 'B':1, 'N':2, 'R':3, 'P':4];
}