foo!(bar) ==> foo{bar}

Lionello Lunesu lionello at lunesu.remove.com
Wed Oct 8 01:13:16 PDT 2008


"Andrei Alexandrescu" <SeeWebsiteForEmail at erdani.org> wrote in message 
news:gchhfa$nsj$2 at digitalmars.com...
> Lionello Lunesu wrote:
>>
>> "Andrei Alexandrescu" <SeeWebsiteForEmail at erdani.org> wrote in message 
>> news:gch67k$1jl$1 at digitalmars.com...
>>> Range overlap(Range r1, Range r2) if (isRandomAccessRange!(Range))
>>> {
>>>     ...
>>> }
>>>
>>> The signature clarifies the requirements on the type much crisper than 
>>> the loose, vale tudo:
>>>
>>> auto overlap(auto r1, auto r2) { ... }
>>>
>>> which will (1) catch every call (blech) and (2) attempt to plow through 
>>> its implementation and fail to compile with an uninformative message, 
>>> file, and line.
>>
>> But this would do the same and, in fact, is more correct:
>>
>> typeof(r1) overlap(auto r1, auto r2)
>>  if (isRandomAccessRange!(typeof(r1)) && 
>> isRandomAccessRange!(typeof(r2)))
>> {
>>    ...
>> }
>>
>> The only thing the original (Range)(Range r1, Range r2) syntax adds, 
>> then,
>> is an implicit "if (typeof(r1) == typeof(r2))" constraint. If this
>> constraint is used often, that syntax can be retained, but would solely
>> affect the declaration of the template.
>
> Hmmmmmmm... there are multiple issues to discuss here.
>
> One is, you want to compare ranges of different types. I agree that that's 
> more general, e.g. a range could iterate const stuff and the other mutable 
> stuff. But then which of the two range types do you return? You'd need 
> some extra type pushups for that. But it's doable.

True, but that has nothing to do with the current topic. Even 'old-style' 
templates would have that problem. I should have sticked to typeof==typeof 
for the analogy.

> The other is more trivial: auto was supposed to save some typing, but the 
> abundant typeof's in the signature considerably reduce that advantage. 
> IMHO auto is useful when types are NOT restricted, and as far as I 
> understand things now, future only has less of those.

Right. And suddenly I realise that I've been wrong: I was discussing the 
template declaration, but !() only applies to its usage. Whether the 
template uses overlap(Range)(Range r1, Range r2) or (auto r1, auto r2) is 
irrelevant.

So what we'd need is for the caller to be able to specify some additional 
constraints. Can this be done by passing the constraints in the normal 
argument list?

auto result = overlap(r1,r2);//let the compiler figure it out
auto result = overlap(Array,r1,r2);//instantiate using Array

...I guess not..

L. 




More information about the Digitalmars-d mailing list