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

A simple try for HKT #40368

Draft
wants to merge 34 commits into
base: master
from
Draft

A simple try for HKT #40368

wants to merge 34 commits into from

Conversation

@ShuiRuTian
Copy link
Contributor

@ShuiRuTian ShuiRuTian commented Sep 3, 2020

Fixes #1213

Far from complemention, just a try for now.


Backlog for myself

These are not definition, but a simple explaination, for myself.

  • proper type: a specific type, like number, Array<number>. Array<T> is not, because T is not specific.
  • type constructor: interface/class accept generic, like Array which accepts 1 and Map which accepts 2. When used in the rest part, often indicate it is a type parameter too.
  • type parameter: in the declaration, the generic. Like K,V in Map<K,V>
  • type arguments: generic when use generic class, like number in Array<number>

Two list, one is added ability, one is added error check.

Ability

  • class/interface accept type constructor as type parameter and map them correctly.
  • function accept type constructor as type parameter and map them correctly.
  • extends specific type constructor
  • type constructor could accept and check constraint
// constraint might be a very complex issue.
interface G1<T extends number>{}
interface G2<T extends string>{}
interface H1<Container<_> extends G1<_>>{}    // Container<string> should report error?
interface H2<Container<_ extends number>>{}
interface H3<Container<_> extends G1<_>|G2<_> >{}
interface H4<Container<_ extends number> extends G1<_ extends boolean>|G2<_ extends boolean> >{}

// Is H4 complex enough? 
// .......No, it just begin. I could not even give a constraint.
// Accorfing to the BFN, this is allowed.
interface H5<K1<K2<K3<_>>>>{}
// Could someone tell me what the hell is this? How could it be used in Scala or Haskell? And how to add constraint fot it?
  • default typeConstructor parameter
  • distinguish type constructor and proper type in quick info.
  • fix services
    • quick info
    • auto complete

// from communicity

  • type guard could change type arguments in type constructor. #1213 (comment)

Error

  • type parameters and type arguments must match for type constructor
  • infer type must match, this is an example in scala.
object HelloWorld {
   def main(args: Array[String]) {
      var q: List[Int] = List(1, 2, 3, 4)
      var w: Array[Boolean] = Array(true)
      var t = getData(q,w)
   }
   def getData[T,U,C[_]](a:C[T],b:C[U]) = 1/2
}

Some details.

typeConstructor non-typeConstructor
typeParameter typeConstructorpolymorphism(1) ? (2) //Generic?
non-typeParameter ? (3) //Generic class/function? proper type (4)

(1)

interface Example<Container<T>>
                  ^----------^

(2)

interface Example<T>
                  ^

(3)

interface Example<T>{}
          ^--------^

const foo : Example<number>
            ^-----^

(4)

number ,Set<number> ...

we could see that Generic has declaration(2) and instance(3).
Similarly, after (1), we need to distinuish declaration and instance, like 1.1 and 1.2

(1.1)
interface Example<Container<T>>
                  ^----------^
(1.2)
interface Example<Container<T>>{
    foo: Container<number>
         ^--------^
}

Generic could already accept constraint, maybe I could add new abstract to use it? As we could see, they are similar in some degrees.

@Kingwl
Copy link
Member

@Kingwl Kingwl commented Sep 14, 2020

Cool!

Could you fix the test cases(or lint or merge master) and we could pack this.

ShuiRuTian added 8 commits Sep 14, 2020
...
@ShuiRuTian
Copy link
Contributor Author

@ShuiRuTian ShuiRuTian commented Sep 15, 2020

Alright, finally......
It is a little harder than estimated, however, it passed........

@Kingwl
Copy link
Member

@Kingwl Kingwl commented Sep 16, 2020

seems not work again....

@ShuiRuTian
Copy link
Contributor Author

@ShuiRuTian ShuiRuTian commented Sep 16, 2020

Oh, I see the cute little green check mark and would not push any more until it is packed this time

@Kingwl
Copy link
Member

@Kingwl Kingwl commented Sep 16, 2020

@typescript-bot pack this.

@typescript-bot
Copy link
Collaborator

@typescript-bot typescript-bot commented Sep 16, 2020

Heya @Kingwl, I've started to run the tarball bundle task on this PR at a00bd0a. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

@typescript-bot typescript-bot commented Sep 16, 2020

Hey @Kingwl, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/85513/artifacts?artifactName=tgz&fileId=565F0D21981DBF17E9EA13CEC644F3F2B81E0D8A8EA95B514BF63688312A08EB02&fileName=/typescript-4.1.0-insiders.20200916.tgz"
    }
}

and then running npm install.


There is also a playground for this build.

@Kingwl
Copy link
Member

@Kingwl Kingwl commented Sep 16, 2020

Soo coooooooooooooooooooooooool!

@xiaoxiangmoe
Copy link

@xiaoxiangmoe xiaoxiangmoe commented Sep 16, 2020

Excited!

@lupuszr
Copy link

@lupuszr lupuszr commented Sep 16, 2020

@ShuiRuTian Really excited about this feature. But if I am not mistaken it seems like it doesn't work for types only interfaces. So we can't create a sum type like:
type Maybe<A> = Just<A> | Nothing;

@ShuiRuTian
Copy link
Contributor Author

@ShuiRuTian ShuiRuTian commented Sep 18, 2020

@lupuszr
Whoops, I did not think about it.
It would be very helpful if there are some provided examples(the code you expect works). Usually issues come when trying to give a complex example.

@lupuszr
Copy link

@lupuszr lupuszr commented Sep 18, 2020

@typescript-bot pack this.

@ShuiRuTian
Copy link
Contributor Author

@ShuiRuTian ShuiRuTian commented Sep 18, 2020

lol, only TS team member could do that.

@lupuszr
Copy link

@lupuszr lupuszr commented Sep 18, 2020

Sorry I hoped I have magic powers like that :D

Anyway what I tested in the last playground was:

In the playground this was my test:

interface Just<A> {value: A, tag: 'Maybe/Just'};
interface Nothing { tag: 'Maybe/Nothing'};
type Maybe<A> = Just<A> | Nothing;

type Functor<F<_>> = {
    map: <A, B>(f: (a: A) => B) => ((fa: F<A>) => F<B>)
}

type Monad<F<_>> = {
    bind: <A, B>(f: (a: A) => F<B>) => ((fa: F<A>) => F<B>)
}

type L = Functor<Maybe>;

and got an error Generic type 'Maybe' requires 1 type argument(s).(2314)

the same happens for:

type Reader<E, A> = (env: E) => A; type R = Functor<Reader>;
as well

Is this handled by your last commit ?

@ShuiRuTian
Copy link
Contributor Author

@ShuiRuTian ShuiRuTian commented Sep 18, 2020

Not sure, would check when having time. My break time is nearly at an end.

@orta orta added the Experiment label Sep 18, 2020
ShuiRuTian added 2 commits Sep 19, 2020
@ShuiRuTian
Copy link
Contributor Author

@ShuiRuTian ShuiRuTian commented Sep 19, 2020

@lupuszr Auh, it should work for the sample code now. But I do not fix error and the code is really ugly.....
If you could not wait to see the result, you could fork and build this branch, then change the Typescript what vscode is using.

src/compiler/types.ts Outdated Show resolved Hide resolved
@orta
Copy link
Member

@orta orta commented Sep 22, 2020

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

@typescript-bot typescript-bot commented Sep 22, 2020

Heya @orta, I've started to run the tarball bundle task on this PR at 9d1850c. You can monitor the build here.

ShuiRuTian added 5 commits Sep 23, 2020
fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

6 participants
You can’t perform that action at this time.