Understand typeof trick

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Dec 25 07:12:34 PST 2015


Well, what I'd really want to document here's isn't necessarily 
the nitty-gritty of the idiom and why it is used (that's a thing 
for api authors, but these docs are targeted at api consumers), 
but more just what it actually means at a glance.

That line of code simply means "pred must be a function that 
takes two arguments of the same type as the front of needle and 
haystack"

The binaryFun helper means you can pass your own function, 
callable object, or string! The string is one of those "a == b" 
style things you sometimes see in Phobos.

I could write on that line of code for probably a full page from 
several perspectives - there's quite a lot going on there.

But the user of findSkip, when reading that doc, wants to simply 
know that constraint is about the predicate function, that it 
must take those arguments. Once you get used to the pattern, 
you'll know what it means, but first-time readers would surely 
appreciate a translation right there... and examples of what kind 
of compiler errors will result if you make a mistake.

(And I think it also must return bool, though the constraint 
doesn't say that... nor does the prose... nor do any of the 
examples get into it. Gah, so either I'm wrong or that's an 
undocumented requirement. is(typeof()) could check the return 
value though.)


More information about the Digitalmars-d-learn mailing list