Weird UFC and opCall issues
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jan 3 14:52:33 PST 2015
On 01/03/2015 12:26 PM, Darrell wrote:
> Fails with:
> t.d(34): Error: need 'this' for 'opCall' of type 'int()'
>
> Also opCall seems to be required to create a range.
D has a feature that does not exists e.g. in C++: You can call the type
itself as a function. The 'Test()' syntax is a call to that type's
static opCall().
>
> class Test
> {
> int opCall()
> {
> return 1;
> }
Not being static, that would require an instance of the Test class.
>
> @property int front()
> {
> return 2;
> }
You may want to define that member function 'const' as well.
>
> void popFront()
> {
> }
>
> @property bool empty()
> {
> return false;
> }
That can be 'const' as well.
>
> };
D does not require that semicolon.
>
> void main(){
> ubyte [] p1;
> Test();
> }
I am removing the opCall and assuming that you actually want a ubyte I
am returning a ubyte from front().
Also note the almost-mandatory-with-ranges convenience function 'test()'
below, which hides the invocation of 'new':
class Test
{
@property ubyte front()
{
return 2;
}
void popFront()
{
}
@property bool empty()
{
return false;
}
}
Test test()
{
return new Test();
}
/* Alternatively, you can move test() inside Test as a static opCall:
static Test opCall()
{
return new Test();
}
Then, the syntax in main could be
ubyte [] p1 = Test().take(3).array;
*/
import std.stdio;
import std.range;
void main(){
ubyte [] p1 = test().take(3).array;
assert(p1 == [ 2, 2, 2 ]);
}
Ali
More information about the Digitalmars-d-learn
mailing list