Thread overview | |||||
---|---|---|---|---|---|
|
February 02, 2018 Can you constrain a type based on properties of what it is being referenced by? | ||||
---|---|---|---|---|
| ||||
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 Fake syntax above of course. I was thinking about this because I read a few posts about const, and approaching it via allowing an object to define a head mutable version of itself (see post in general forum by Simon for details [1]) And there're few constructs like RefCounted which don't make sense with an immutable qualifier. And then there're situations where a mutable version does not make sense (I think this would be a much bigger application), ie: handles, resource identifiers, application configuration details, etc. [1] https://forum.dlang.org/thread/cpxfgdmklgusodqouqdr@forum.dlang.org |
February 02, 2018 Re: Can you constrain a type based on properties of what it is being referenced by? | ||||
---|---|---|---|---|
| ||||
Posted in reply to aliak | On Friday, 2 February 2018 at 14:19:37 UTC, aliak wrote:
> ... (see post in general forum by Simon for details [1])
*Simen
Gah! Sorry!
|
February 02, 2018 Re: Can you constrain a type based on properties of what it is being referenced by? | ||||
---|---|---|---|---|
| ||||
Posted in reply to aliak | 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
|
Copyright © 1999-2021 by the D Language Foundation