Proposal: Relax rules for 'pure'

Robert Jacques sandford at jhu.edu
Thu Sep 23 15:26:28 PDT 2010


On Thu, 23 Sep 2010 10:05:45 -0400, Steven Schveighoffer  
<schveiguy at yahoo.com> wrote:

> On Wed, 22 Sep 2010 21:48:19 -0400, Robert Jacques <sandford at jhu.edu>  
> wrote:
>
>> On Wed, 22 Sep 2010 13:10:36 -0400, Steven Schveighoffer  
>> <schveiguy at yahoo.com> wrote:
>>
>>> On Wed, 22 Sep 2010 12:00:16 -0400, Robert Jacques <sandford at jhu.edu>  
>>> wrote:
>>>> What about value types?
>>>
>>> Value types are implicitly convertable to immutable, so they can be  
>>> strongly-pure.
>>
>> No their not. Remember, arrays and other structs are value types in the  
>> type system. Logically, they may be reference types, but as far as  
>> their type signature goes, they are value types.
>
> Arrays are not value types.  A value type is one that contains no  
> references, no matter how deep.  It is irrelevant if you have to spell  
> out the references or not.

Arrays are implemented as a struct. And as per the language spec:  
(http://www.digitalmars.com/d/2.0/struct.html) all structs are value types  
*to the compiler*. This doesn't mean that logically, from the programmer's  
point of view, they aren't providing reference semantics.

> another example of something that is not a value type:
>
> alias int * iptr;
>
> foo(iptr p);
>
> iptr is not a value type just because you don't see any * in the  
> signature.

*sigh* I explicitly referred to the type system/compiler. And to the type  
system iptr is a pointer, no matter what you call it.

>>>
>>> But the compiler will be able to tell.  I think adding a  
>>> __traits(isStronglyPure, symbol) will be good for those rare occasions  
>>> where you really want to ensure purity.
>>>
>>> static assert(__traits(isStronglyPure, foo));
>>>
>>> -Steve
>>
>> This would work, but it wouldn't be self-documenting, etc.
>
> Hm... OK, well I disagree, it looks like it's documented to me.  I don't  
> see a difference between tagging something as strongly pure and putting  
> in the static assert (except verbosity of course).  I will note that I  
> think the above would be a rare situation.

To clarify, self-documenting => be able to automatically shows up in ddoc,  
etc.


More information about the Digitalmars-d mailing list