Comments (6)
This is where we disagree.
A function of type bool Function(String)
may be used as a predicate, or a filter, a condition, or a selector. That's different roles, depending on which values it will be called on and how often. Even if the words mean approximately the same thing, and may be the same function in all the cases, there is intent in the naming.
I'd want to put that name into the variable, not the type.
If I have a bool Function(String)
, I get to choose how to use it, and I can use it anywhere that type is allowed.
If it was typed as Predicate<String>
I wouldn't know as easily whether I could use it as a Filter<String>
.
Having type names for each role a function type can occur in, that just doesn't scale. There are too many, and they don't actually enforce anything.
An int Function(String, String)
may be a Comparator<String>
, or it may be a ReverseComparator<String>
, but as type aliases, they're the same type.
And using named types, IMO, hurts readability rather than help it, by hiding the actual function type.
Roles should be variable names.
from sdk.
Java does things this way because they don't have first class functions.
Dart does, and can just declare typedef Consumer<T> = void Function(T);
, with no class or interface needed.
I don't see any need for the platform libraries to expose such interfaces (or named function types for that matter), when a literal function type is fine. Maybe that's just my personal preference, I find it easier to read void Function(T)
than Consumer<T>
because I can see what it means, without having to learn a number of names.
from sdk.
To talk about functional programming, we must always keep in mind the type signature of the functions. This concept is important if we had to write a function in an exotic language like Haskell, we would do it this way.
add_two :: Integer -> Integer
add_two = a + 2
As you can see the type signature is very important. The idea of ββthis library is to have a collection of higher order functions using typedef
of all possible type signatures or the vast majority standardized with key names that allow abstracting concepts that facilitate the implementation of algorithms, this facilitates communication between programmers.
For example, if I ask someone if it is better to implement a Consumer
or a Predicate
in my algorithm, the other members will be able to understand what I am talking about, this helps teams that use agile ways of working.
It also allows us to standardize, avoid repeated code, and helps reduce the code base to maintain because we can always reuse the same functions that the base SDK already provides.
We can have most signature types registered in one place to avoid getting lost in so much code.
from sdk.
Well you decide, this api is quite simple, for that matter it could be written like this:
typedef Predicate<T> = bool Function(T value)
This satisfies all Predicate
of any type possible thanks to the generic type T
typedef Predicate<String> = bool Function(String value);
typedef Predicate<int> = bool Function(int value);
This is where duplicates begin, which I want to avoid by using a generic type.
from sdk.
Labels: area-language
, type-enhancement
Summary: The user proposes creating a dart:function
API to standardize common higher-order function declarations, inspired by Java's java.util.function
package. They suggest two implementation approaches: using functional interfaces or typedefs
.
from sdk.
We have no plans to do this.
from sdk.
Related Issues (20)
- `dart compile js` fails with the newest SDK HOT 17
- Cannot resolve top-level methods with docimports HOT 4
- [Wildcard Variables] Update `DUPLICATE_DEFINTION` reporting
- CFE fails to report error when `super` is used in a redirecting constructor. HOT 2
- DevTools not serving properly on app refresh HOT 1
- [Wildcard Variables] Update `DUPLICATE_FIELD_FORMAL_PARAMETER` reporting
- `@internal` annotation does not supress `public_member_api_docs` lint on functions HOT 1
- Dart analyzer does not infer the correct type for generic record HOT 2
- Having true cancelable future HOT 2
- Simplify branch conditions based on ranges & simplify control flow afterwards if condition turns out to be constant
- Issue in number multiplication with 10, 20, etc HOT 4
- Feature Request: Add `--insert-ignore-comments` flag to `dart fix` command for existing code HOT 1
- [vm/core] `_GrowableList.add` has unnecessary bounds check
- docImports failing to allow for resolution in some flutter animation library code HOT 4
- Is that possible to set breakpoints in the macro code itself? HOT 2
- Implement augmentations HOT 2
- Support macros when analyzer is used as a library HOT 1
- Inability to Convert Dart BigInt to JSBigInt using new js_interop HOT 1
- Dart CLI commands hang on a slow/unstable internet connection due to analytics HOT 2
- Types Generated using the LibraryTypesMacro throws an error when instantiating HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sdk.