jsigbiz / spec

JavaScript signature notation
131 stars 6 forks source link

Extra ideas #28

Open Raynos opened 9 years ago

Raynos commented 9 years ago

This issue is a set of bullet points for extra ideas in my head

cc @Matt-Esch

junosuarez commented 9 years ago

As we consider each of these features, could you attach a use-case that they solve?

Raynos commented 9 years ago

Without operator

\ allows you to specify a type that is a union with things.

For example the divide : (top: Number, bottom: Number \ 0) => Number function.

This is the inverse of | and is useful when thinking of types as sets.

Sub type operator

Other languages allow you to create generics with constraints (see C# and typescript).

For example an extend function might be xtend : ( T <: Object, S <: Object) => T & S.

By default in generics when we say type Promise<T> : { ... } we actually mean type Promise<T <: Any> : { ... }

Closed and open syntax

This is one is complicated, will need a seperate issue to discuss.

Pattern strings

This allows you to say Object<Pattern<"\d+">, T> i.e. I take an object where the keys are numeric and the value is a T. I'm not sure whether we want this as a first class thing.

We can already define type Pattern<T> : String and then have a smart validator take the Pattern meta data and do smart validation where as the full type checker will just resolve the above object to Object<String, T>

Complex generics

Instead of doing a generic as type MyGeneric<A, B> : { ... } and then doing foo : (MyGeneric<A, B>) we might want to use destructuring instead.

i.e. type MyGeneric<{ a: A, b: B }> : { ... } and then do foo : (MyGeneric<{ a: A, b: B }>)

This is only really useful for building a "DSL" in the custom generics you define in JSig. One use case is the type Range<{ lt: A, gt: B, gte: C, lte: D }> : Number dsl which will resolve to Number checking in the type checker but a smart validator / runtime checker might interpret it as a range restriction on numbers.

Raynos commented 9 years ago

I opened an issue for closed vs open #45