Skip to content

Commit

Permalink
fix(builders): Return TSelf and cast
Browse files Browse the repository at this point in the history
This fixes an issue where methods were not chainable because the return type was the abstract class instead of TSelf. The cast to TSelf is safe by virtue of being an abstract class, so this will always be a derived class.
  • Loading branch information
VelvetToroyashi committed Mar 17, 2024
1 parent 2aeb012 commit 21a6352
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions Remora.Commands/Builders/AbstractCommandBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,69 +107,69 @@ private AbstractCommandBuilder()
/// </summary>
/// <param name="name">The name of the builder.</param>
/// <returns>The current builder to chain calls with.</returns>
public AbstractCommandBuilder<TSelf> WithName(string name)
public TSelf WithName(string name)
{
Name = name;
return this;
return (TSelf)this;
}

/// <summary>
/// Sets the description of the builder.
/// </summary>
/// <param name="description">The description of the builder.</param>
/// <returns>The current builder to chain calls with.</returns>
public AbstractCommandBuilder<TSelf> WithDescription(string description)
public TSelf WithDescription(string description)
{
Description = description;
return this;
return (TSelf)this;
}

/// <summary>
/// Adds an alias to the builder.
/// </summary>
/// <param name="alias">The alias to add.</param>
/// <returns>The current builder to chain calls with.</returns>
public AbstractCommandBuilder<TSelf> AddAlias(string alias)
public TSelf AddAlias(string alias)
{
Aliases.Add(alias);
return this;
return (TSelf)this;
}

/// <summary>
/// Adds multiple aliases to the builder.
/// </summary>
/// <param name="aliases">The aliases to add.</param>
/// <returns>The current builder to chain calls with.</returns>
public AbstractCommandBuilder<TSelf> AddAliases(IEnumerable<string> aliases)
public TSelf AddAliases(IEnumerable<string> aliases)
{
Aliases.AddRange(aliases);
return this;
return (TSelf)this;
}

/// <summary>
/// Adds an attribute to the builder. Conditions must be added via <see cref="AddCondition"/>.
/// </summary>
/// <param name="attribute">The attribute to add.</param>
/// <returns>The current builder to chain calls with.</returns>
public AbstractCommandBuilder<TSelf> AddAttribute(Attribute attribute)
public TSelf AddAttribute(Attribute attribute)
{
if (attribute is ConditionAttribute)
{
throw new InvalidOperationException("Conditions must be added via AddCondition.");
}

Attributes.Add(attribute);
return this;
return (TSelf)this;
}

/// <summary>
/// Adds a condition to the builder.
/// </summary>
/// <param name="condition">The condition to add.</param>
/// <returns>The current builder to chain calls with.</returns>
public AbstractCommandBuilder<TSelf> AddCondition(ConditionAttribute condition)
public TSelf AddCondition(ConditionAttribute condition)
{
Conditions.Add(condition);
return this;
return (TSelf)this;
}
}

0 comments on commit 21a6352

Please sign in to comment.