You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by "Kaspar Fenner (JIRA)" <ji...@apache.org> on 2015/07/18 15:41:04 UTC

[jira] [Comment Edited] (THRIFT-3227) TypeScript: Export Interfaces + Classes for Structs

    [ https://issues.apache.org/jira/browse/THRIFT-3227?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14623927#comment-14623927 ] 

Kaspar Fenner edited comment on THRIFT-3227 at 7/18/15 1:40 PM:
----------------------------------------------------------------

Thanks a lot for the proposal.

I think it might be useful to also have interface definitions besides the classes in certain scenarios. But I also think that for most use cases, the current class declarations should be enough.
I remember that we tried various combinations and we thought, we should keep it simple and just generate classes, because without classes, you couldn't do "var x = new classX();"
Also, optional members are already declared optional in the constructor declarations:
{code}
declare class Work {
  num1: number;
  num2: number;
  op: Operation;
  comment: string;

  constructor(args?: { num1: number; num2: number; op: Operation; comment?: string; });
}
{code}

So you can instantiate your class without optional members: 
{code}
var work = new Work({num1: 1, num2: 2, Operation.ADD });
{code}
And in JS you don't have to set all properties of an object anyway. So work.comment is just undefined which is perfectly fine.

Now, what could be real use cases to have interfaces? One would be if you want to reuse the generated Thrift type declarations in your code and create your own objects. I'm not sure if this is the idea of Thrift, I mean, does C# or Java generate also interfaces?

So what do you think? Could you show a scenario which you'd need interfaces? Or was it really only this optional member case? Because for this, I think it already works as it is.



was (Author: kfenner):
Thanks a lot for the proposal.

I think it might be useful to also have interface definitions besides the classes in certain scenarios. But I also think that for most use cases, the current class declarations should be enough.
I remember that we tried various combinations and we thought, we should keep it simple and just generate classes, because without classes, you couldn't do "var x = new classX();"
Also, optional members are already declared optional in the constructor declarations:
{code}
declare class Work {
  num1: number;
  num2: number;
  op: Operation;
  comment: string;

  constructor(args?: { num1: number; num2: number; op: Operation; comment?: string; });
}
{code}

So you can intantiate your class without optional members: 
{code}
var work = new Work({num1: 1, num2: 2, Operation.ADD });
{code}
And in JS you don't have to set all properties of an object anyway. So work.comment is just undefined which is perfectly fine.

Now, what could be real use cases to have interfaces? One would be if you want to reuse the generated Thrift type declarations in your code and create your own objects. I'm not sure if this is the idea of Thrift, I mean, does C# or Java generate also interfaces?

So what do you think? Could you show a scenario which you'd need interfaces? Or was it really only this optional member case? Because for this, I think it already works as it is.


> TypeScript: Export Interfaces + Classes for Structs
> ---------------------------------------------------
>
>                 Key: THRIFT-3227
>                 URL: https://issues.apache.org/jira/browse/THRIFT-3227
>             Project: Thrift
>          Issue Type: New Feature
>          Components: JavaScript - Compiler
>            Reporter: Felipe Barriga Richards
>              Labels: typescript
>         Attachments: 0001-TypeScript-Use-interface-instead-of-classes-for-stru.patch
>
>
> Currently when compiling a thrift all the structs are converted into classes.
> Classes in TypeScript doesn't allow optional members.
> The proposed solutions:
> - Generate Interfaces + Classes
> - All the Classes will implement the generated Interfaces
> - All the Classes will have the methods read and write (already implemented on the generated JS code)
> - The Interfaces name will be prefixed with *I_*



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)