Coder Social home page Coder Social logo

generalizedtearoffs's People

Contributors

gbracha avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

generalizedtearoffs's Issues

Closurization equivalent to `() { return ...}` function, loses static types.

The definition of closurization of, say, an operator is (a){return u m a;} (where m is the operator).
This loses the static return type, and needs extra specification to put it back in.
If the definition was instead (a)=>u m a then the return type comes automatically because the static return type of a (..)=>expr function is the static type of expr.

The `noSuchMethod` invocation instance isn't fine-grained enough.

When tearing-off a non-existing member of an object, the noSuchMethod of the object is called, but with insufficient information for the method to recognize it as a tear-off.

The spec says:

Otherwise, a new instance im of the predefined class Invocation is created, such that :

  • If m is a setter name, im.isSetter evaluates to true; otherwise im.isMethod evaluates to true.
  • im.memberName evaluates to โ€™mโ€™.
  • im.positionalArguments evaluates to the value of const [].
  • im.namedArguments evaluates to the value of const {}.

This means that tearing off a getter and a method are indistinguishable from calling a method.
This makes it impossible for the object to emulate the tear-off with noSuchMethod.
A method tear-off like o#foo should be emulated by returning a function, but calling the method o.foo() should execute the emulated function instead. The two accesses have the same Invocation instance, and cannot be distinguished.

Changing the invocation to im.isGetter instead won't fix that, because it breaks getter access/tear-off instead.

A vote of "yes please"

Consider this sequence of events:

Day one:

class Foo {
  String get awesome => 'awesome';
}

main() {
  var f = new Foo();
  print(f.awesome);
}

On Day two, I change the getter to a method:

class Foo {
  String awesome() => 'awesome';  // CHANGED
}

main() {
  var f = new Foo();
  print(f.awesome);  // DID NOT CHANGE
}

Oops! Syntactically, this is a valid program. But the intent changed and there's no way for my tools to warn me. This happened to me during a lengthy refactoring, and I was surprised that changing from a getter to a method left my code in a weird/broken state.

So, +1 for a more explicit syntax for closurization.

I hope we can build tools that will minimize the older use of the "implicit closurization" and suggest the new syntax to developers. Maybe even one day we can remove the implicit closurization syntax from the language. :)

Using the tear off operator as a shorthand for (x) => x.foo

I'd like there to be a short-hand for (x) => x.foo.

Assuming a symbol other than # is used for tear offs, i.e. perhaps :, thus removing the conflict with symbols, then (x) => x.foo could then be written as :foo.

This is useful with map. i.e. list.map((x) => x.foo) becomes list.map(:foo).

Another possibility is # could still be used and symbols could be made callable, so that #foo is also equivalent to (x) => x.foo when passed as a function.

Full example:

class Foo {
    Foo(this.bar);
    int bar;
}

List<Foo> foos = [new Foo(1), new Foo(2), new Foo(3)];

// :bar is identical to (x) => x.bar
List<int> bars = foos.map(:bar); // [1, 2, 3]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.