www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What does std.traits.hasAliasing do

reply Venkat <venkatram.akkineni gmail.com> writes:
struct SomeStruct {
	string p;
	string q;
	string[] pq;
}

ErrorMessage[] pq;
session.set("registerBody", pq);

/home/venkat/.dub/packages/vibe-d-0.8.4/vibe-d/http/vibe/http/session.d(83,3):
Error: static assert:  "Type SomeStruct contains references, which is not
supported for session storage."


vibe.d session won't let me put in a simple struct with an array 
or an associative array. session.put calls std.traits.hasAliasing 
which is returning true when I have either an array or an 
associative array. I looked through the std.traits.hasAliasing 
code. I can't make a whole lot of sense there.

The hasAliasing function documentation says as long as the array 
or associative array are not immutable it should return true. 
Since session.put does !hasAliasing I changed string[] to 
immutable, that throws a whole lot of other compilation error 
messages.

What is hasAliasing doing ?
Jul 28 2018
parent reply Venkat <venkatram.akkineni gmail.com> writes:
On Sunday, 29 July 2018 at 01:05:19 UTC, Venkat wrote:
 struct SomeStruct {
 	string p;
 	string q;
 	string[] pq;
 }
Session session = req.session; session.get!SomeStruct("registerBody");
 /home/venkat/.dub/packages/vibe-d-0.8.4/vibe-d/http/vibe/http/session.d(83,3):
Error: static assert:  "Type SomeStruct contains references, which is not
supported for session storage."


 vibe.d session won't let me put in a simple struct with an 
 array or an associative array. session.put calls 
 std.traits.hasAliasing which is returning true when I have 
 either an array or an associative array. I looked through the 
 std.traits.hasAliasing code. I can't make a whole lot of sense 
 there.

 The hasAliasing function documentation says as long as the 
 array or associative array are not immutable it should return 
 true. Since session.put does !hasAliasing I changed string[] to 
 immutable, that throws a whole lot of other compilation error 
 messages.

 What is hasAliasing doing ?
Posted the wrong code. Fixed it above. Reposting below for clarity. For the record both session.get and session.put call hasAliasing. struct SomeStruct { string p; string q; string[] pq; } Session session = req.session; session.get!SomeStruct("registerBody");
Jul 28 2018
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 7/28/18 9:21 PM, Venkat wrote:
 On Sunday, 29 July 2018 at 01:05:19 UTC, Venkat wrote:
 struct SomeStruct {
     string p;
     string q;
     string[] pq;
 }
Session session = req.session; session.get!SomeStruct("registerBody");
 /home/venkat/.dub/packages/vibe-d-0.8.4/vibe-d/http/vibe/http/session.d(83,3): 
 Error: static assert:  "Type SomeStruct contains references, which is 
 not supported for session storage."
What is happening is the session storage is complaining it can't store the array. I think because the array data could change without the struct itself changing. Even though string has a pointer, the data is immutable, so it is allowed.
 vibe.d session won't let me put in a simple struct with an array or an 
 associative array. session.put calls std.traits.hasAliasing which is 
 returning true when I have either an array or an associative array. I 
 looked through the std.traits.hasAliasing code. I can't make a whole 
 lot of sense there.

 The hasAliasing function documentation says as long as the array or 
 associative array are not immutable it should return true. Since 
 session.put does !hasAliasing I changed string[] to immutable, that 
 throws a whole lot of other compilation error messages.

 What is hasAliasing doing ?
hasAliasing means there are references to mutable data somewhere in your struct. In this case, the array pq is a reference to mutable data (yes, the strings inside are immutable, but they can be changed). The qualifications are listed here: https://dlang.org/library/std/traits/has_aliasing.html "an array U[] and U is not immutable" Try immutable(string)[] pq. -Steve
Aug 02 2018
parent Venkat <venkatram.akkineni gmail.com> writes:
Thankyou.
Aug 05 2018