microsoft / microsoft/TypeScript

Add a mechanism to copy another function/method's overload set

Open
#8,616 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Problem

Outline

When I make a new sub-class from a parent-class who has multiple overloaded constructors (or some methods), I've to re-write the definition (header) of each function and re-write branch-condition again. It's very annoying and tiresome work for me.

With an example

https://github.com/samchon/stl/blob/master/ts/src/std/TreeMap.ts#L72-L177

class TreeMap<Key, T> extends base.UniqueMap<Key, T>
{
    // THOSE DEFINITIONS ARE REPEATED IN EVERY SUB-CLASSES
    constructor();
    constructor(compare: (left: Key, right: Key) => boolean);
    constructor(array: Array<Pair<Key, T>>);
    constructor(array: Array<Pair<Key, T>>, compare: (left: Key, right: Key) => boolean);
    constructor(array: Array<[Key, T]>);
    constructor(array: Array<[Key, T]>, compare: (left: Key, right: Key) => boolean);
    constructor(container: base.MapContainer<Key, T>);
    constructor(container: base.MapContainer<Key, T>, compare: (left: Key, right: Key) => boolean);
    constructor(begin: MapIterator<Key, T>, end: MapIterator<Key, T>);
    constructor(begin: MapIterator<Key, T>, end: MapIterator<Key, T>, compare: (left: Key, right: Key) => boolean);

    constructor(...args: any[])
    {
        // THOSE BRANCH CONDITIONS ARE ALSO REPEATED TOO
        // condition statements identifying type of arguments
        /*IF 
        ELSE IF
        ELSE IF
       ...*/
    }
}

In the link, you can see the TreeMap class inherited from base.UniqueMap. Of course, you also can see that the constructors and branch-condition codes have to be re-written.

Those idiot codes are repeated in every sub-classes in my fucking project.

Solution

Suggest: using statement

Those're very annoying and inefficient job for development. So I want to suggest a notation can reduce such in-efficient works.

In C++, fetching all overloaded methods from parent can be implemented by just a line.
With using statement. using Parent::methodName;

template <class T>
class URLVariables : public std::map<std::string, T>
{
private:
    typedef std::map<std::string, T> super;

public:
    // ALL THE CONSTRUCTORS + CUSTOM ONE
    using super::map; // [using super::super] is also possible

    /* -- THOSE CAN BE OMITTED BY USING STATEMENT --
    URLVariables();
    URLVariables(const super &);
    URLVariables(super &&);
    URLVariables(const std::initializer_list<std::pair<std::string, T>>> &);
    template <InputIterator> URLVariables(InputIterator first, InputIterator last);
    */

    URLVariables(const std::string &);

    // ALL THE COUNT METHODS + CUSTOM ONE
    using super::count;
    bool count(const std::string_view &) const;
    bool count(const ByteArray &) const;
}

If the using statement is adopted in TypeScript, codes will be much precise like below. Isn't it much precise than codes of the top?

class TreeMap<Key, T> extends base.UniqueMap<Key, T>
{
    // THE USING STATEMENT
    using super.constructor;

    // THOSE RE-DEFINITIONS ARE OMITTED BY THE USING STATEMENT
    /*constructor();
    constructor(compare: (left: Key, right: Key) => boolean);
    constructor(array: Array<Pair<Key, T>>);
    constructor(array: Array<Pair<Key, T>>, compare: (left: Key, right: Key) => boolean);
    constructor(array: Array<[Key, T]>);
    constructor(array: Array<[Key, T]>, compare: (left: Key, right: Key) => boolean);
    constructor(container: base.MapContainer<Key, T>);
    constructor(container: base.MapContainer<Key, T>, compare: (left: Key, right: Key) => boolean);
    constructor(begin: MapIterator<Key, T>, end: MapIterator<Key, T>);
    constructor(begin: MapIterator<Key, T>, end: MapIterator<Key, T>, compare: (left: Key, right: Key) => boolean);*/

    constructor(...args: any[])
    {
        // CALLING SUPER CONSTRUCTORS' RE DONE BY
        super(...args);
    }
}

I'm waiting for your opinion and eagerly looking forward to the enhancement.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TreeMap.ts example and compare its repeated constructor overloads and branching with the proposed using syntax. Then review how TypeScript represents inherited constructors and overload sets before defining the feature's semantics and compatibility requirements. Done means the syntax and inherited overload behavior are specified and validated across relevant compiler tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.