digitalmars.D.learn - Is there anything other than nullable to work with optional types?
- aliak (39/39) Dec 25 2016 Hey,
- Stefan Koch (6/11) Dec 25 2016 Well there is one easy way to do this.
- ali ak (5/10) Dec 25 2016 Well sure, certainly solutions. Could also use exception. But I'm
- Seb (5/6) Dec 25 2016 You might be interested in
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.
Dec 25 2016
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.
Dec 25 2016
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.
Dec 25 2016
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
Dec 25 2016
On Sunday, 25 December 2016 at 20:11:50 UTC, Seb wrote:On Sunday, 25 December 2016 at 19:22:10 UTC, aliak wrote:Yes! That! Seems like that PR is on a break though :(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
Dec 25 2016
On Sunday, 25 December 2016 at 22:21:17 UTC, aliak wrote:On Sunday, 25 December 2016 at 20:11:50 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 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) ...On Sunday, 25 December 2016 at 19:22:10 UTC, aliak wrote:Yes! That! Seems like that PR is on a break though :(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
Dec 25 2016
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 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) ...Sure, will take a gander. Might try and revive if I feel up to it after reading through but I don't think I'm comfortable enough with D yet to do that. But lets see.
Dec 26 2016