An Optional!T and the implementation of the underlying type's opUnary
aliak
something at something.com
Fri Jul 27 12:52:09 UTC 2018
On Thursday, 26 July 2018 at 06:37:41 UTC, Simen Kjærås wrote:
> On Wednesday, 25 July 2018 at 21:59:00 UTC, aliak wrote:
>> It needs to work with const as well and immutable too.
>>
>> immutable a = 3;
>> auto b = -a; // is ok, should be ok with the optional as well.
>>
>> Plus T can be a custom type as well with "some" definition of
>> opUnary. I can't seem to find any implementation guidelines
>> either so I assume opUnary or any of the ops implementation
>> details is implementation defined.
>
> Template this[0] (and CopyTypeQualifiers[1])to the rescue!
>
Ah! Genius!
I had no idea that using TemplateThisParameters would not
necessitate qualifying the function in question either.
>
> As for assigning to Optional!(immutable int), the language
> basically forbids this (cannot modify struct with immutable
> members). It would, as you say, cause problems when you can get
> a pointer to the contents.
So is this undefined behaviour?
import std.stdio;
struct S(T) {
T value;
void opUnary(string op)() inout {
mixin(op ~ "cast(T)value;");
}
}
void main() {
immutable a = S!int(2);
++a;
}
More information about the Digitalmars-d
mailing list