www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can't make inout work.

reply aliak <something something.com> writes:
This is the set up I have, and I'm not sure how to get the main 
function at the bottom to compile. The error in the code below is 
that it cannot implicitly convert an inout(C) to a C in the 
constructor of S(T).

If you remove the constructor that takes an inout then you get a 
"cannot deduce function from argument types !()(inout(C))" 
because there's no constructor that can take in inout.

If you remove the inout form the function "f", then "a.f" cannot 
be deduced because there's no f that takes an inout(C).

If you remove the inout from the "make" function, then you get: 
"only parameters or stack based variables can be inout."

Anyone knows how to make this work?

struct S(T) {
     T value = T.init;
}

auto ref make(T)(inout auto ref T value) {
     return S!T(value);
}

auto ref f(T)(inout auto ref S!T s) {
     return make(s.value);
}

void main() {
     class C {}
     const a = S!C();
     a.f;
}

Cheers,
- Ali
Mar 15 2019
parent reply Paul Backus <snarwin gmail.com> writes:
On Friday, 15 March 2019 at 23:57:15 UTC, aliak wrote:
 Anyone knows how to make this work?
You need an explicit `inout` on the return value of `make`: auto ref make(T)(inout auto ref T value) { return inout(S!T)(value); }
Mar 15 2019
next sibling parent reply spir <denis.spir gmail.com> writes:
On 16/03/2019 04:49, Paul Backus via Digitalmars-d-learn wrote:
 On Friday, 15 March 2019 at 23:57:15 UTC, aliak wrote:
 Anyone knows how to make this work?
You need an explicit `inout` on the return value of `make`: auto ref make(T)(inout auto ref T value) {     return inout(S!T)(value); }
I think (but may be wrong) that you don't need inout here, since a plain 'ref' will (and does) work. This is accepted by me (I added vars to make the code clearer to myself): struct S(T) { T value = T.init; } auto ref make(T)(ref T value) { return S!T(value); } auto ref f(T)(ref S!T s) { return make(s.value); } void main() { class C {} C c ; auto s1 = S!C(c) ; auto s2 = make!C(c) ; auto s3 = f!C(s2) ; }
Mar 16 2019
parent reply Paul Backus <snarwin gmail.com> writes:
On Saturday, 16 March 2019 at 11:55:56 UTC, spir wrote:
 I think (but may be wrong) that you don't need inout here, 
 since a plain 'ref' will (and does) work. This is accepted by 
 me (I added vars to make the code clearer to myself):

 struct S(T) {
     T value = T.init;
 }

 auto ref make(T)(ref T value) {
     return S!T(value);
 }

 auto ref f(T)(ref S!T s) {
     return make(s.value);
 }

 void main() {
     class C {}
     C c ;
     auto s1 = S!C(c) ;
     auto s2 = make!C(c) ;
     auto s3 = f!C(s2) ;
 }
This code fails to compile if you change `auto s2` to `const s2`--in other words, it has the same problem as the original example.
Mar 16 2019
parent reply Kagamin <spam here.lot> writes:
On Saturday, 16 March 2019 at 14:57:35 UTC, Paul Backus wrote:
 This code fails to compile if you change `auto s2` to `const 
 s2`--in other words, it has the same problem as the original 
 example.
Maybe there's not much need for qualifiers anyway. struct S(T) { T value; } auto make(T)(ref T value) { return S!T(value); } auto f(T)(ref T s) { return make(s.value); } void f() { class C {} C c; auto s1 = S!C(c); const s2 = make!C(c); auto s3 = f(s2); }
Mar 17 2019
parent spir <denis.spir gmail.com> writes:
On 17/03/2019 18:34, Kagamin via Digitalmars-d-learn wrote:
 On Saturday, 16 March 2019 at 14:57:35 UTC, Paul Backus wrote:
 This code fails to compile if you change `auto s2` to `const s2`--in other 
 words, it has the same problem as the original example.
Maybe there's not much need for qualifiers anyway.
This is what I meant as well. diniz
Mar 18 2019
prev sibling next sibling parent spir <denis.spir gmail.com> writes:
PS: the chapter of Ali Çehreli's book on func args is great:
http://ddili.org/ders/d.en/function_parameters.html
diniz
Mar 16 2019
prev sibling parent reply aliak <something something.com> writes:
On Saturday, 16 March 2019 at 03:49:26 UTC, Paul Backus wrote:
 On Friday, 15 March 2019 at 23:57:15 UTC, aliak wrote:
 Anyone knows how to make this work?
You need an explicit `inout` on the return value of `make`: auto ref make(T)(inout auto ref T value) { return inout(S!T)(value); }
Ah! Thanks! So next problem with that: import std.stdio; struct S(T) { T value; } auto make(T)(inout auto ref T val) { return inout(S!T)(val); } void main() { writeln(make("hello") == S!string("hello")); } Error: Error: incompatible types for (make("hello")) == (S("hello")): immutable(S!(char[])) and S!string I think that's just this bug (which is marked as a diagnostic for some reason): https://issues.dlang.org/show_bug.cgi?id=19126 Thoughts on any workarounds?
Mar 17 2019
next sibling parent reply Kagamin <spam here.lot> writes:
struct S(T) {
     T value;
     bool opEquals(U:S!V,V)(in U r) const
     { return value==r.value; }
}
Mar 17 2019
parent aliak <something something.com> writes:
On Sunday, 17 March 2019 at 17:22:13 UTC, Kagamin wrote:
 struct S(T) {
     T value;
     bool opEquals(U:S!V,V)(in U r) const
     { return value==r.value; }
 }
Hmm, that actually works for opEquals. But now you just hit the same problem with some other construct, unfortunately: auto x = [make("hello"), S!string("hi")]; same error.
Mar 17 2019
prev sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Sunday, 17 March 2019 at 10:49:03 UTC, aliak wrote:
 Ah! Thanks! So next problem with that:

 import std.stdio;

 struct S(T) {
     T value;
 }

 auto make(T)(inout auto ref T val) {
     return inout(S!T)(val);
 }

 void main() {
     writeln(make("hello") == S!string("hello"));
 }

 Error: Error: incompatible types for (make("hello")) == 
 (S("hello")): immutable(S!(char[])) and S!string

 I think that's just this bug (which is marked as a diagnostic 
 for some reason): https://issues.dlang.org/show_bug.cgi?id=19126

 Thoughts on any workarounds?
For some reason, when you call `make("hello")`, the template argument T is being inferred as char[] instead of string. (You can see this by putting `pragma(msg, T)` in the body of make.) It works if you instantiate make explicitly with `make!string("hello")`. This seems like a bug to me. If you remove inout from the code, T is correctly deduced as string.
Mar 17 2019
parent aliak <something something.com> writes:
On Sunday, 17 March 2019 at 20:23:44 UTC, Paul Backus wrote:
 On Sunday, 17 March 2019 at 10:49:03 UTC, aliak wrote:
 [...]
For some reason, when you call `make("hello")`, the template argument T is being inferred as char[] instead of string. (You can see this by putting `pragma(msg, T)` in the body of make.) It works if you instantiate make explicitly with `make!string("hello")`. This seems like a bug to me. If you remove inout from the code, T is correctly deduced as string.
https://issues.dlang.org/show_bug.cgi?id=19749
Mar 18 2019