Skip to content

Function overload pattern #20

Description

@jingcheng-chen

I notice that we have another way to write overload function in the project:

 public DistanceTo(
    other: Point3d | Line | Plane,
    limitToFiniteSegment: boolean = false
  ): number {
    if (other instanceof Line)
      return Line.LineLineDistance(this, other, limitToFiniteSegment);
    if (other instanceof Plane)
      return Line.LinePlaneDistance(this, other, limitToFiniteSegment);
    return Line.LinePointDistance(this, other, limitToFiniteSegment);
  }

Whereas in the old code I explicitly seperate overload functions into multiple functions, like:

  /**
   * Sums up a point and a vector, and returns a new point.
   * @param vector A vector.
   * @returns A new point that results from the addition of point and vector.
   */
  public Add(vecotr: Vector3d): Point3d {
    return Point3d.Add(this, vecotr);
  }

  /**
   * Sums up a point and a point, and returns a new point.
   * @param point A point.
   * @returns A new point that results from the addition of point and point.
   */
  public AddPoint(point: Point3d): Point3d {
    return Point3d.AddPoint(this, point);
  }

Now I think it's an interesting discussion which pattern we are going to keep.
At first sight the first method is cleaner for the user. Question is can it handle more complex cases?

What I can think of:

  1. What if it returns different types depending on the input? (like point-point->vector, point-vector->point)
  2. What if we have different lengths of input arguments?
  3. What if some arguments can be undefined?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions