UFCS for struct opCall?

kenji hara k.hara.pg at gmail.com
Mon Apr 8 20:49:14 PDT 2013


UFCS should just work only for 'functions'.
'foo' is not a function, rather it is a functor. So this is proper behavior.

Kenji Hara

2013/4/9 bearophile <bearophileHUGS at lycos.com>

> On request by Maxim Fomin I ask an opinion here. This is a very small
> enhancement request, that for me is borderline bug report (so originally I
> didn't plan in showing it in the main D newsgroup):
>
> http://d.puremagic.com/issues/**show_bug.cgi?id=9857<http://d.puremagic.com/issues/show_bug.cgi?id=9857>
>
>
>
> Maybe this should be valid:
>
>
> struct Foo {
>     int opCall(bool b) {
>         return 0;
>     }
> }
> void main() {
>    Foo foo;
>    auto b1 = foo(true); // OK
>    auto b2 = true.foo;  // Error
> }
>
>
> dmd 2.063alpha gives:
>
> temp.d(9): Error: no property 'foo' for type 'bool'
>
>
> Explanation:
>
> 1) I am using UFCS often in D, for functions, higher order functions, etc.
> A struct with an opCall method is usable like a function with state. So for
> uniformity with the other functions I'd like to use it with UFCS. When you
> have a long UFCS chain you don't want to break it, if it's possible.
>
>
> 2) Both struct constructors, struct implicit constructors and struct
> static opCall support UFCS, so I don't see a good reason for just the
> normal struct opCall to not support it:
>
>
> struct Foo {
>     static int opCall(int x) {
>         return x * 2;
>     }
> }
> struct Bar {
>     int x;
>     this(int y) {
>         x = y * 2;
>     }
> }
> struct Spam {
>     int x;
> }
> void main() {
>     assert(10.Foo == 20);
>     assert(10.Bar.x == 20);
>     assert(10.Spam.x == 10);
> }
>
>
> Jonathan Davis doesn't like this. For more information I suggest to take a
> look at the thread in Bugzilla.
>
> - - - - - - - - - - -
>
> Regarding UFCS, currently this doesn't work, I don't know if this should
> be considered a bug or not (I think the answer is positive):
>
>
> struct Node {}
> void foo(Node* p) {}
> void main() {
>     auto p = new Node();
>     foo(p); // OK
>     p.foo;  // Error: no property 'foo' for type 'Node'
> }
>
>
> Bye,
> bearophile
>
'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130409/f409391a/attachment.html>


More information about the Digitalmars-d mailing list