dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.21k stars 1.57k forks source link

Functions to move to dart:core from dart:ui #25217

Open Hixie opened 8 years ago

Hixie commented 8 years ago

These are functions and types that we have in dart:ui (Flutter's core library) that really should just be in dart:core:

sethladd commented 8 years ago

cc @floitschG

lrhn commented 8 years ago

The lerpDouble function could be put on double (or num) as

double interpolate(num a, num b) => a + (b - a) * this.toDouble();

(it should return double, not num).

I'm more concerned about the hash functions. The 373 (* 37 + x)* is a classic, so we can at least say that we are no worse than everybody else, but it's not great. It's quick, though. We have other hash implementations spread around the source code that we could centralize and expose.

Hixie commented 8 years ago

Just to be clear, the algorithms used in the Flutter hash functions today are horrible; we'd love to have better ones if you take these functions over. We're planning on changing the implementations of the ones we use anyway (https://github.com/flutter/flutter/issues/859). It's the interface we're interested in moving over to core Dart, not the precise implementations.

If lerpDouble made it over that would be great. We currently have lerp functions on many of our classes (Point, Rect, Color, Border, BoxDecoration, etc) but since there's no function on "num" for it, we have to call this function instead when we need to lerp a double. Calling it "interpolate" would be fine, we'd change our functions to match (though note that "lerp" is a pretty widely used term of art).

kasperpeulen commented 8 years ago

I often see people use those hash functions in Dart libraries:

https://github.com/google/quiver-dart/blob/master/lib/src/core/hash.dart

floitschG commented 8 years ago

We discussed the lerp function, and it seems simpler if this stays a helper function. It's a common operation in drawing/3D, but is otherwise not much used. Maybe we can get it through extension methods (no promises).

Hixie commented 5 years ago

An update from 2018:

matanlurey commented 5 years ago

As of 2018, void Function() is the same as VoidCallback :man_shrugging:

dnfield commented 5 years ago

For the hashing functions,see https://github.com/dart-lang/sdk/issues/11617

dnfield commented 5 years ago

Should we just mark VoidCallback as deprecated and convert all instances of it to void Function()?

Hixie commented 5 years ago

Why would we do that?

dnfield commented 5 years ago

It would achieve our goal of eliminating a dart:ui dependency where we don't want it, assuming we can live with having a void Function() signature instead of VoidCallback.

kevmoo commented 5 years ago

@lrhn had spent some time thinking about hashCode somewhere in dart: – I think

dnfield commented 5 years ago

@lrhn had spent some time thinking about hashCode somewhere in dart: – I think

@kevmoo that's the issue I linked to above, #11617

Hixie commented 5 years ago

@dnfield I think using VoidCallback has more value than not depending on dart:ui. I agree it would be ideal if dart:core declared VoidCallback, but it looks like that's not on the cards.

Hixie commented 1 year ago

Update from 2023:

kevmoo commented 1 year ago

VoidCallback is trivial to define at the use site. void Function() – right?

Hixie commented 1 year ago

Well it would be typedef VoidCallback = void Function(); since types with spaces and parentheses in them are something we try to avoid but the idea is to just define it once and not worry about having to redeclare it everywhere. That way you can be confident that you're really talking about the same type, it cross-links in API docs, etc. As noted earlier in this bug, using VoidCallback has more value than not depending on dart:ui, but it would be great to have both.

natebosch commented 3 months ago

After discussion I don't think there are any signatures/behaviors that make sense to land in the core libraries. For double.clamp and double.lerp the core library maintainers are aligned on waiting to see whether we are able to land static extension methods as the best solution here. There are questions about whether Flutter authors prefer static extensions over the status quo, but from the Dart core library standpoint we're confident with pushing in that direction.

For VoidCallback we're aligned on messaging a preference for void Function() and don't have plans to add the typedef to the core libraries.