Can you constrain a type based on properties of what it is being referenced by?

Steven Schveighoffer schveiguy at yahoo.com
Fri Feb 2 14:45:06 UTC 2018


On 2/2/18 9:19 AM, aliak wrote:
> To further explain what I mean:
> 
> struct A if (!is(this == immutable) && !is(this == shared))
> {
> }
> 
> shared a = A() // error
> auto a = A() // ok
> immutable a = A() // error
> const a = A() // ok

In this case, no. A struct is bit-copyable, so you must be able to move 
it bit-by-bit to another copy.

In the case of attribute addition or removal, the compiler allows 
anything as long as there are no reference types contained in the 
struct. Your example has no members, which means no references, so it's 
a value type.

If you put a pointer inside your struct, you will get the behavior you 
are looking for I think. However, you may have to disable the 
constructor for your exact requirements above.

-Steve


More information about the Digitalmars-d-learn mailing list