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

Use IList or IEnumerable instead of List in method parameters #605

Closed
Swimburger opened this issue Apr 27, 2022 · 3 comments · Fixed by #671
Closed

Use IList or IEnumerable instead of List in method parameters #605

Swimburger opened this issue Apr 27, 2022 · 3 comments · Fixed by #671
Labels
status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap

Comments

@Swimburger
Copy link
Contributor

There may be a reason List<T> is used instead of IList<T> or IEnumerable<T>, but I'd like to be able to pass in other types of collection classes, specifically T[].

For example, here's a snippet that generates TwiML that will gather some input:

var voiceResponse = new VoiceResponse()
    .Gather(
        input: new List<Gather.InputEnum> {Gather.InputEnum.Dtmf}
    );

It works fine, but if the input parameter would accept IList<Gather.InputEnum> or IEnumerable<Gather.InputEnum>, we could pass in an array instead:

var voiceResponse = new VoiceResponse()
    .Gather(
        input: new[]{Gather.InputEnum.Dtmf}
    );

This is super minor, but this would make the APIs a little more flexible and allow developers to write shorter/less verbose code IMO.
This isn't the only location List<T> is specified, so I'd like to see it changed, if possible, wherever :)

@JenniferMah JenniferMah added type: community enhancement feature request not on Twilio's roadmap status: help wanted requesting help from the community labels Apr 28, 2022
@JenniferMah
Copy link
Contributor

This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.

@Swimburger
Copy link
Contributor Author

Here's some more ideas:

var voiceResponse = new VoiceResponse()
    .Gather(
        input: InputEnum.Dtmf + InputEnum.Speech
    );

var voiceResponse = new VoiceResponse()
    .Gather(
        input: InputEnum.Dtmf & InputEnum.Speech
    );

var voiceResponse = new VoiceResponse()
    .Gather(
        input: InputEnum.Dtmf.ToArray()
    );

var voiceResponse = new VoiceResponse()
    .Gather(
        input: InputEnum.Dtmf.ToList()
    );

This could be accomplished by extending the enum classes like this:

public sealed class InputEnum : StringEnum
{
    ...
        
    public static InputEnum[] operator +(InputEnum a, InputEnum b) => new[] {a, b};
    public static InputEnum[] operator &(InputEnum a, InputEnum b) => new[] {a, b};
    public List<InputEnum> ToList() => new List<InputEnum>{this};
    public InputEnum[] ToArray() => new []{this};
    
    ...
}

Personally, I hope someone can come up with better ideas :)

@Swimburger
Copy link
Contributor Author

FYI, for VB .NET, you could do this

Dim gather = new Gather(
    input := {Voice.Gather.InputEnum.Speech}
)

Instead of this

Dim gather = new Gather(
    input := New List(Of Gather.InputEnum) From {Voice.Gather.InputEnum.Speech}
)

If we'd allow IList<Gather.InputEnum> or IEnumerable<Gather.InputEnum> instead of List<Gather.InputEnum>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants