www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - importing std.array: empty in a struct messes things up

reply Dennis <dkorpel gmail.com> writes:
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.
Mar 04 2018
parent reply ag0aep6g <anonymous example.com> writes:
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.
Mar 04 2018
next sibling parent reply ag0aep6g <anonymous example.com> writes:
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.
Mar 04 2018
parent reply visitor <visitor gmail.com> writes:
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
Mar 04 2018
parent arturg <var.spool.mail700 gmail.com> writes:
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 }
Mar 04 2018
prev sibling parent arturg <var.spool.mail700 gmail.com> writes:
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.
Mar 04 2018