digitalmars.D.learn - Nullable instantiator anyone?
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (7/7) Aug 17 2014 I'm missing an instantiator function for std.typecons:Nullable.
- bearophile (6/13) Aug 17 2014 It could be sufficient, but note that in Phobos there are two
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (16/19) Aug 17 2014 Ok, thanks for the reminder.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (27/46) Aug 17 2014 I don't think it is possible.
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (2/28) Aug 18 2014 Great! Thx.
- Phil Lavoie (20/39) Aug 17 2014 Hi Nordlöw,
I'm missing an instantiator function for std.typecons:Nullable. Is this is intentional? If not, is this Nullable!T nullable(T)(T a) { return typeof(return)(a); } sufficient for our needs?
Aug 17 2014
Nordlöw:I'm missing an instantiator function for std.typecons:Nullable. Is this is intentional? If not, is this Nullable!T nullable(T)(T a) { return typeof(return)(a); } sufficient for our needs?It could be sufficient, but note that in Phobos there are two different versions of Nullable, one of them doesn't require extra memory, it uses one value as the "null" value. Bye, bearophile
Aug 17 2014
On Sunday, 17 August 2014 at 18:51:38 UTC, bearophile wrote:It could be sufficient, but note that in Phobos there are two different versions of Nullable, one of them doesn't require extra memory, it uses one value as the "null" value.Ok, thanks for the reminder. Do you have a suggestion of an instantiator for this variant? I'm not sure how to define this. So far I have Nullable!T nullable(T nullValue, T)(T a, T nullValue) { return Nullable!nullValue(a); } unittest { auto x = nullable!(int.max)(3); } but it fails as mangling.d(50,21): Error: undefined identifier T How do I make the template parameter nullValue infer type T with a single template parameter? Is it even possible?
Aug 17 2014
On 08/17/2014 12:05 PM, "Nordlöw" wrote:On Sunday, 17 August 2014 at 18:51:38 UTC, bearophile wrote:I don't think it is possible. The following solution relies on an alias template parameter and a template constraint. The code covers both Nullable variants: import std.typecons; auto nullable(T)(T value) { return Nullable!T(value); } unittest { auto x = nullable(42.5); assert(is(typeof(x) == Nullable!double)); } auto nullable(alias nullValue, T)(T value) if (is (typeof(nullValue) == T)) { return Nullable!(T, nullValue)(value); } unittest { auto x = nullable!(int.max)(3); assert(is (typeof(x) == Nullable!(int, int.max))); } void main() {} AliIt could be sufficient, but note that in Phobos there are two different versions of Nullable, one of them doesn't require extra memory, it uses one value as the "null" value.Ok, thanks for the reminder. Do you have a suggestion of an instantiator for this variant? I'm not sure how to define this. So far I have Nullable!T nullable(T nullValue, T)(T a, T nullValue) { return Nullable!nullValue(a); } unittest { auto x = nullable!(int.max)(3); } but it fails as mangling.d(50,21): Error: undefined identifier T How do I make the template parameter nullValue infer type T with a single template parameter? Is it even possible?
Aug 17 2014
On Monday, 18 August 2014 at 01:22:00 UTC, Ali Çehreli wrote:The following solution relies on an alias template parameter and a template constraint. The code covers both Nullable variants: import std.typecons; auto nullable(T)(T value) { return Nullable!T(value); } unittest { auto x = nullable(42.5); assert(is(typeof(x) == Nullable!double)); } auto nullable(alias nullValue, T)(T value) if (is (typeof(nullValue) == T)) { return Nullable!(T, nullValue)(value); } unittest { auto x = nullable!(int.max)(3); assert(is (typeof(x) == Nullable!(int, int.max))); } void main() {} AliGreat! Thx.
Aug 18 2014
On Sunday, 17 August 2014 at 19:05:11 UTC, Nordlöw wrote:On Sunday, 17 August 2014 at 18:51:38 UTC, bearophile wrote:Hi Nordlöw, I'm not sure I fully understand your question but I'll try and answer it. First of all, yes, you can have everything inferred as long as your produce a default value for your null value, a.k.a: Nullable!(T, nullValue) nullable(T, T nullValue = T.init)(T val) { return Nullable!(T, nullValue)(val); } unittest{ auto myNullableInt = nullable(5); assert(!myNullableInt.isNull() && myNullableInt == 5); } However, keep in mind that using a sentinel value as the null value can produce surprises: unittest{ auto myNullableInt = nullable(0); assert(myNullableInt.isNull()); //Since T.init == 0 } PhilIt could be sufficient, but note that in Phobos there are two different versions of Nullable, one of them doesn't require extra memory, it uses one value as the "null" value.Ok, thanks for the reminder. Do you have a suggestion of an instantiator for this variant? I'm not sure how to define this. So far I have Nullable!T nullable(T nullValue, T)(T a, T nullValue) { return Nullable!nullValue(a); } unittest { auto x = nullable!(int.max)(3); } but it fails as mangling.d(50,21): Error: undefined identifier T How do I make the template parameter nullValue infer type T with a single template parameter? Is it even possible?
Aug 17 2014