What does std.traits.hasAliasing do

Steven Schveighoffer schveiguy at gmail.com
Thu Aug 2 20:50:59 UTC 2018


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


More information about the Digitalmars-d-learn mailing list