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

Proposal: new syntax for defining abstract methods #6833

Closed
fphh opened this issue Feb 2, 2016 · 3 comments
Closed

Proposal: new syntax for defining abstract methods #6833

fphh opened this issue Feb 2, 2016 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@fphh
Copy link

fphh commented Feb 2, 2016

Currently, we write


abstract class A {
  abstract f() : void;
} 

Wouldn't it be nicer to write:


abstract class A {
  abstract f : () => void;
}

That would syntactically go smoothly with type aliases:


type t = () => void;
abstract class A {
  abstract f : t;
}

which currently (1.7.5) is disallowed. (See #6832).

@sandersn sandersn added the Duplicate An existing issue was already created label Feb 2, 2016
@sandersn
Copy link
Member

sandersn commented Feb 2, 2016

That looks like an abstract property to me. Take a look at #4669 -- is that what you're looking for?

@mhegazy mhegazy closed this as completed Feb 2, 2016
@fphh
Copy link
Author

fphh commented Feb 3, 2016

Sorry, no, I was talking about declaring the type of an abstract method. If we consider


type t = (x : number) => void;
abstract class A {
   abstract f(x : number) : void;
}

then I would argue that f has type t. So why can't we just write:


type t = (x : number) => void;
abstract class A {
  abstract f : (x : number) => void;
}

Pro:

  1. This unifies the syntax for abstract methods with the syntax for abstract properties.
  2. If type aliases were fully implemented (Type alias is not possible when defining abstract method. #6832), we could write for short;

type t = (x : number) => void;
abstract class A {
  abstract f : t;
}

Note that this only goes for abstract methods, because we don't need the parameter names in the function body. Anyhow, If I think a little bit further, I would like to write for non-abstract methods:


type t = (number) => void;
class A {
  f(x) : t { ... }
}

That would be charming, because:

  1. Type aliases are about types only (note that the parameter name is gone).
  2. Types can be writen seperatly from the objects they talk about. This looks much nicer.

Mentally, types are also at a distinct level of programming, so separating them from code would reasonable.

Note that point 1 goes well with the paradigm that parameters are mandatory and types are optional. The vanished parameter name in the type is meaningless anyway as we declare a type and not a function.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 3, 2016

The syntax you are proposing here is already used for property declarations in typescript. properties are not the same as methods in a class, methods go on the prototype, properties go on the instance, i do not think there is much we can change here.

today:

class C {
    a: T;   // this is a property whose name is "a" and type is T, and not a method
}

Consider using an interface instead:

type t = (x: number) => void;

interface A {
    f: t;
}

class Base implements A{
    f(x: number) {
    }
}

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants