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

Suggestion: Allow type and value parameters for modules #2625

Closed
zpdDG4gta8XKpMCd opened this issue Apr 6, 2015 · 9 comments
Closed

Suggestion: Allow type and value parameters for modules #2625

zpdDG4gta8XKpMCd opened this issue Apr 6, 2015 · 9 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@zpdDG4gta8XKpMCd
Copy link

Consider a situation when each class and function in the module depends on the same type/value parameters:

// current
module foo {
   export class C<a> {
      value: a;
      constructor(private areEqual: (one: a, another:a) => boolean) { /*...*/ } 
   }
   export function f1<a>(areEqual: (one: a, another: a) => boolean, ones: a[], anothers: a[]) : boolean {
       // ...
   }
   export function f2<a>(areEqual: (one: a, another: a) => boolean, values: a[], sample: a): number[] {
       // ...
   }
}

It would be awesome if such parameters went to the module level and were resolved at importing

// suggested
module foo<a>(areEqual: (one: a, another: a) => boolean) {

   export class C {
      value: a;
   }

   export function f1(ones: a, anothers: a): boolean {
      //...
   }
   export function f2(values: a[], sample: a): number[] {
      //...
   }
}



import foo = require('foo')((one: Date, another:Date) => one.getTime() === another.getTime());
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Needs More Info The issue still hasn't been fully clarified labels Apr 6, 2015
@RyanCavanaugh
Copy link
Member

It's kind of confusing -- what is the type of the value expression foo.C ?

@zpdDG4gta8XKpMCd
Copy link
Author

interface CStatic {
   new() : CIntsnace;
}
interface CInstance{
   value: Date;
}

@RyanCavanaugh
Copy link
Member

Sorry, I meant in the case where you hadn't written an import yet. It would just be disallowed to reference foo<a> except through an import ?

@zpdDG4gta8XKpMCd
Copy link
Author

so module foo<a>(areEqual: (one: a, another: a) => boolean) is essentially a generic function that returns a resolved module when called with all required type and value arguments

answering your questions, can you currently refer a generic function in TS? yes, but it doesn't make much sense in most cases (unless for calling it once resolved)

i hope the analogy with generic functions can be used in this case

@zpdDG4gta8XKpMCd
Copy link
Author

@danquirk
Copy link
Member

danquirk commented Apr 6, 2015

I think if we fully supported local types and class expressions you could probably get what you want by making foo a generic class containing a variety of other generic types.

@zpdDG4gta8XKpMCd
Copy link
Author

@danquirk I am afraid I am not following how it could work.

class X<a> {
   class Y {
      value: a;
   }
}

var x = new X<Date>();
var y = new x.Y(); // <-- is this going to be allowed? so that y.value is of Date

@danquirk
Copy link
Member

danquirk commented Apr 7, 2015

@Aleksey-Bykov I certainly imagine considering making something like that work. Obviously I don't want to promise anything at this point though :) It doesn't seem all that different from this kind of thing that we allow today:

function aConstructor<T>() {
   return function anotherConstructor() {
       var data: T;
       return { value: data }
   }
}

var a = aConstructor<Date>(); // we don't allow 'new' here since this isn't void returning
var b = a();
var r = b.value; // Date

Those are essentially the static sides of your 2 classes. Each function could return an interface type describing the instance side, but of course the problem today is you can't define an interface inside aConstructor that would allow you to correctly type the instance side members like value. It probably still doesn't do everything you want in terms of first class modules like OCaml but I can imagine it gets you most of what you want with lower complexity/cost (both from an implementation perspective and a concept perspective).

@mhegazy mhegazy added Declined The issue was declined as something which matches the TypeScript vision Out of Scope This idea sits outside of the TypeScript language design constraints and removed Needs More Info The issue still hasn't been fully clarified labels Feb 20, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Feb 20, 2016

With ES6 modules in place, we have shied away from any changes to the module system. it is already complicated enough, and there is not an implementation yet.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Declined The issue was declined as something which matches the TypeScript vision Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants