http://d.puremagic.com/issues/show_bug.cgi?id=8640


Simen Kjaeraas <simen.kja...@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |simen.kja...@gmail.com


--- Comment #3 from Simen Kjaeraas <simen.kja...@gmail.com> 2012-09-11 09:45:47 
PDT ---
(In reply to comment #2)
> However, as pointed out by bearophile, this approach means this:
> --------
> struct C(T) {
>     private T val;
>     @property void front()(T value) {
>         val = value;
>     }
> }
> void main() {
>     C!int ci;
>     auto f = &ci.front; //Should be &ci.front!()
>     assert(ci.val == 0);
>     f(1);
>     assert(ci.val == 1);
> }
> --------
> Ceases to work. So it does have client-side impacts :'(

Indeed. Now, there is another way to do it, which clearly marks the code as
'may or may not work, depending on template parameters', and that involves
static if.

If the method does not work with some types, the correct way to state that is
so that the compiler gives a sensible error message when someone tries to use
your code, not when your code tries to use something else.

In this case:

static if (__traits(compiles, (T value){val = value;})) {
    @property void front()(T value) {
        val = value;
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------

Reply via email to