Thread overview
importing std.array: empty in a struct messes things up
Mar 04, 2018
Dennis
Mar 04, 2018
ag0aep6g
Mar 04, 2018
ag0aep6g
Mar 04, 2018
visitor
Mar 04, 2018
arturg
Mar 04, 2018
arturg
March 04, 2018
I was making a stack interface for an array:
```
struct Stack(T) {
    import std.array: empty;
    T[] stack;
    alias stack this;
}

void main()
{
    Stack!int stack;
    bool x = stack.empty;
}
```
My expectation is that you can now call `empty` on a stack instance since I imported it in the struct, but it gives this error:
```
Error: cannot resolve type for stack.empty(T)(auto ref scope const(T) a) if (is(typeof(a.length) : size_t) || isNarrowString!T)
```

When adding this method to the struct:
```
bool empty() {return stack.empty;}
```

I get this confusing error:
```
Error: expression stack.empty is void and has no value
```

I can solve this by importing std.array: empty in the method instead of the struct, but for my understanding of import statements in structs I'd appreciate if someone explained what these errors mean and why exactly they occur.

Thanks.
March 04, 2018
On 03/04/2018 08:17 PM, Dennis wrote:
> I was making a stack interface for an array:
> ```
> struct Stack(T) {
>      import std.array: empty;
>      T[] stack;
>      alias stack this;
> }
> 
> void main()
> {
>      Stack!int stack;
>      bool x = stack.empty;
> }
> ```
> My expectation is that you can now call `empty` on a stack instance since I imported it in the struct, but it gives this error:
> ```
> Error: cannot resolve type for stack.empty(T)(auto ref scope const(T) a) if (is(typeof(a.length) : size_t) || isNarrowString!T)
> ```

`stack.empty` is an alias of `std.array.empty`. It's not a method of the struct. It's not a UFCS call to `std.array.empty`.

You can call it this way: `stack.empty(stack)`. You can also call it this way: `Stack!int.empty(stack)`. Maybe that makes it more obvious what's going on.

> When adding this method to the struct:
> ```
> bool empty() {return stack.empty;}
> ```
> 
> I get this confusing error:
> ```
> Error: expression stack.empty is void and has no value
> ```

I don't know what's going on here. The error message doesn't make sense to me. Might be a bug in the compiler.
March 04, 2018
On 03/04/2018 08:45 PM, ag0aep6g wrote:
> I don't know what's going on here. The error message doesn't make sense to me. Might be a bug in the compiler.

This one works:

----
struct Stack(T) {
    T[] stack;
    alias stack this;
    bool empty() {return empty(stack);} /* not using UFCS */
    import std.array: empty; /* has to come after the method */
}
----

Looks very much like a compiler bug now. As far as I know, the order of declarations shouldn't have an effect like that in structs. And UFCS should also be ok there, as far as I can tell.
March 04, 2018
I had the same issue, happens with any conflicting selective import.

It seems to work if you dont use selective imports, but importing it inside the function if possible is a better option.
March 04, 2018
On Sunday, 4 March 2018 at 19:53:59 UTC, ag0aep6g wrote:
> On 03/04/2018 08:45 PM, ag0aep6g wrote:
>> I don't know what's going on here. The error message doesn't make sense to me. Might be a bug in the compiler.
>
> This one works:
>
> ----
> struct Stack(T) {
>     T[] stack;
>     alias stack this;
>     bool empty() {return empty(stack);} /* not using UFCS */
>     import std.array: empty; /* has to come after the method */
> }
> ----
>
> Looks very much like a compiler bug now. As far as I know, the order of declarations shouldn't have an effect like that in structs. And UFCS should also be ok there, as far as I can tell.

?????
https://run.dlang.io/is/Ws0qEx
https://run.dlang.io/is/Bancgx
March 04, 2018
On Sunday, 4 March 2018 at 20:07:06 UTC, visitor wrote:
> On Sunday, 4 March 2018 at 19:53:59 UTC, ag0aep6g wrote:
>> On 03/04/2018 08:45 PM, ag0aep6g wrote:
>>> I don't know what's going on here. The error message doesn't make sense to me. Might be a bug in the compiler.
>>
>> This one works:
>>
>> ----
>> struct Stack(T) {
>>     T[] stack;
>>     alias stack this;
>>     bool empty() {return empty(stack);} /* not using UFCS */
>>     import std.array: empty; /* has to come after the method */
>> }
>> ----
>>
>> Looks very much like a compiler bug now. As far as I know, the order of declarations shouldn't have an effect like that in structs. And UFCS should also be ok there, as far as I can tell.
>
> ?????
> https://run.dlang.io/is/Ws0qEx
> https://run.dlang.io/is/Bancgx

struct Stack(T) {
    import std.array: empty;
    T[] stack;
    alias stack this;
    bool empty() { return empty(stack); }
}

void main()
{
    Stack!int stack;
    //bool x = stack.empty; // fails
    bool x = stack.empty(); // works
}