Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isInputRangeOf template constraint #3786

Closed
wants to merge 2 commits into from

Conversation

atilaneves
Copy link
Contributor

Idiomatic D uses input ranges where possible. Right now however, transiotining a function from taking T[] to a range is more cumbersome than necessary. This PR introduces a new template contraint so that:

void func(int[] numbers) { /*...*/ }

Can easily be converted to

void func(R)(R range) if(isInputRangeOf!(R, int)) { /*...*/ }

Instead of manually typing out

void func(R)(R range) if(isInputRange!R && is(ElementType!R == int)) { /*...*/ }

@JackStouffer
Copy link
Member

I understand that we want to make it as easy as possible for people to port their code to ranges, but I really don't see the value in this, especially since most of the time what you want is(ElementType!R : int). The only thing this gives you is saving about 20 keystrokes.

@atilaneves
Copy link
Contributor Author

Usability should be a concern. void func(int[]) is easy to type and read. The equivalent range version is not. Fair point about convertibility though.

@JakobOvrum
Copy link
Member

Actually, I think what you usually want is isIntegral!(ElementType!R). Similarly for string algorithms we almost always see isSomeChar!(ElementEncodingType!R).

Is it common for a range algorithm to require an input range of a specific type?

@atilaneves
Copy link
Contributor Author

Maybe int and string are bad examples. This is supposed to aid replacing T[] with a range in function signatures and struct members. Let's consider instead MyStruct[] and taking a range instead.

Range algorithms don't usually care what the element type is. They're very generic.

@andralex
Copy link
Member

andralex commented Nov 2, 2015

This doesn't seem the be necessary. isOutputRange takes two parameters because output ranges accept more than one type (e.g. strings take both char and dchar etc). For input ranges, there's no such thing.

Also, testing the range capability is often orthogonal from testing the element type. Before long we'll have isForwardRangeOf, isBidirectionalRangeOf, etc. etc. etc. That doesn't scale.

Thanks for the work - I'll close this now. Please reopen if good arguments come about.

@andralex andralex closed this Nov 2, 2015
@nordlow
Copy link
Contributor

nordlow commented Nov 2, 2015

Why not extend existing traits with a second E-parameter instead of adding a new one?

See also: http://forum.dlang.org/post/hakctxoihemjnexknzqi@forum.dlang.org

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants