-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
typedef for simple type aliases #2626
Comments
See also bug 84. |
Added this to the Later milestone. |
This comment was originally written by george.moscho...@gmail.com Any ETA wrt type aliasing for all types? Will this make 1.0? |
Removed this from the Later milestone. |
Removed Oldschool-Milestone-Later label. |
Any updates? Will it be ever possible? We have some long classes names that are really awkward to use (to the point where it's impossible to fit the case statement on single line), and aliasing them to an abbreviation would greatly help. |
I could really use typedefs for aliasing my long generic classes like so:
|
Also perhaps for future consideration: if the Dart team/committee ever decides to add union types to the language (#4938) I think arbitrary type aliasing would be very useful for dealing with the unwieldy types this might produce. My current use case concerns vertex attribute types (for WebGL rendering). If Dart were to allow something like this I'd be a very happy Dartisan: typedef VertexAttribute = double | Vector2 | Vector3 | Vector4 | Matrix2 | Matrix3 | Matrix4; |
Thanks for the input! I'm on the Dart team, and I think we'll have union types and maybe the generalized |
Just wondering, would this enhancement allow the following construct? class Foo {} void main() { |
No, being able to access constructors via a value of type class Foo {}
class Foo2 extends Foo {}
typedef Foo FooFactory();
void main() {
FooFactory myFooFactory = () => new Foo2();
Foo foo = myFooFactory();
} |
I suspect the importance of this bug is going to rise with strong_mode (@leafpetersen) since longer types are going to be more common, and folks are going to want to shorten them (as per the ask in #2626 (comment)). |
I'm told that #27527 is viewed as a precursor to this more general issue and is under evaluation currently. |
Is this still being considered? Personally, I view union types and typedefs that are used for more than just functions to be almost imperative to Dart. I find myself in so many situations where |
This is not planned for Dart 2.0. It is still on the list of things that we would like to consider for future releases. |
Just to add, adding type alias is going to make dart compile time even safer, just because two values of the same type doesn't mean they should be compared or assigned to each other. This is one of the primary reasons why many languages like Go, F# allow type alias. A naive example would be a function that does conversion between Celsius and Fahrenheit. Naturally people want to define both of them as If allowing type alias, one can easily do |
Maybe we don't need to use For the example that @stt106 gived, maybe we can use class Celsius = double;
class Fahrenheit = double;
Fahrenheit cToF(Celsius c) => Fahrenheit((c as double) * 9 / 5 + 32);
I thinks that the above syntax is easier to understand in import 'package:test_api/test_api.dart';
class Celsius = Alias<double> with Type;
class Fahrenheit = Alias<double> with Type;
Fahrenheit cToF(Celsius c) => Fahrenheit(c.as(double) * 9 / 5 + 32);
class Alias<T> {
final T _value;
const Alias(T value) : _value = value;
Alias.of(Alias<T> other) : this(other._value);
T /*operator*/ as(Type type) {
if (type == _value.runtimeType) return _value;
throw _CastError(runtimeType, type);
}
@override
String toString() {
return _value.toString();
}
@override
bool operator ==(other) =>
other.runtimeType == this.runtimeType && other._value == _value;
@override
int get hashCode => _value.hashCode;
}
class _CastError extends CastError {
final Object message;
_CastError(Type alias, Type expected)
: message = "alias '$alias' is not type '$expected' in type cast";
@override
String toString() => message;
}
void main() {
test('playground', () async {
var celsius = Celsius(30.0);
print('Celsius: $celsius');
var fahrenheit = cToF(celsius);
print('Fahrenheit: $fahrenheit');
expect(fahrenheit.as(double), 86.0);
});
}
|
I disagree.
But i think that a This especially matters when we take the extension methods proposal in consideration. |
This is now happening. As of 02bb437 (Jan 11, 2019) it is part of the language specification that you can write type aliases like There's an implementation plan. Right now some other things are being pushed harder than this feature, but it's accepted and in the pipeline. Note that you can not use this feature to create new types (similar to Haskell's It is for abbreviation and consistency, not for "branding" of types (that is, creating a "copy" of a type that has the same underlying properties and representation, but which is considered during type checking to be a different type). Type aliases wouldn't fit very well for type branding, anyway, because they have always been specified to create a new way to write an existing type rather than creating a new type. We did have some discussions about type branding (e.g., I wrote #57828 at some point in response to such discussions), but that discussion should be taken separately. So I think we can close this issue now (before it turns 7 ;-). @rakudrama, do you agree? |
Is this functionality in a version of dart in which we can use? I'm using 2.2.0-dev.1.1 |
It is not yet implemented, and it won't make it into Dart 2.2, but it is coming. Also, there is no bleeding-edge version of the tools that have it yet, so you have to find a tiny extra bit of patience somewhere. ;-) I suggested closing this issue because it's about the language—the implementation process would be associated with the implementation plan and its associated issues. |
ok thanks, I'll keep an eye on the changelog in future! (I'm terrible during the buildup to christmas too ;-) The feature looks great! |
@twistedinferno wrote:
A type alias like |
So I believe this issue recently turned 7. Is there any update on the implementation progress? It seemed that this was so close to being implemented about 4 months ago. |
Right, it's still very close. ;-) There is some work on it too, e.g., here's a test: https://github.com/dart-lang/co19/blob/master/Language/Types/Type_Aliases/syntax_t02.dart. Sorry about the delay, but other features (e.g., NNBD) have a very high priority, and this one is in the pipeline now. |
Just found this issue since I also was looking for this feature. I see the last commit from here is over 3 months old. But I still see some issues about NNBDs, so I guess it might take some time still. Is there something new that could be said about this? 😄 Cheers! |
Yes, NNBD and static extension methods are taking up the resources now. So, unfortunately, we're close, but we aren't actively getting any closer at this point. Thanks for pushing the issue, however: The only way to get it into the implementation pipeline is that there is a perceived need. |
I need this. I wonder why it wasn't included in the language from the start... I often use type aliases to simplify complicated variable types while still maintaining 100% safety! It's a practice that carried over from C++ |
Any chances of implementing this feature? |
This is still planned to be implemented, but it's lower on the priority list right now. There's a strong feeling on the team that we need to focus on landing any breaking features (like NNBD) now, since they will just get harder to land as time goes on, and also a desire to prioritize the higher impact features (like extension methods) above smaller features. We still think this is a great feature and look forward to landing it when we can find a good slot for it! |
is this feature still in the development pipeline? I need this really bad. I want to use it to map 'Map<String, dynamic>' to 'JsonObject'. |
Tbh they should just give us an alias like that built-in. In my projects I mainly use Map<String, dynamic> for JSON objects, rarely for other stuff |
Just wondering, is it really a good idea to overload the EDIT: it's probably fine, see notes below. As far as my understanding, type aliases are quite a different thing from type definitions - since a type-definition declares a new type, whereas a type alias merely declares an alias for an existing type, right? So if As opposed to type aliases, which type-check more like interfaces - e.g. in TypeScript, a declaration such as I think both of these features are useful - but if these new Thoughts? |
I would prefer a separate keyword for declaring alias. for ex. So that after this I can use |
A type alias (that is, a declaration starting with |
@eernstg oh, so I have this backwards? The In that case, the proposal is consistent with existing behavior, and my whole argument is moot, so please feel free to mark it as irrelevant to save other readers some time! 😊 |
That's correct: A It does come up, and we do recognize that the ability to define a new type associated with an existing representation is a useful tool (e.g., in order to enforce that a given representation is only used in ways that are suitable for a given interpretation of that representation, e.g., such that we don't add a weight and and height which are both represented as a PS: In case I change my mind about a comment on an issue I usually go back and edit it, with an |
Closing this as we're now tracking this in dart-lang/language#65 |
git log --oneline cf9795f3bb209504c349e20501f0b4b8ae31530c..f0c7771b38155d3829a60d60b5dba2784b100811 f0c7771b Set first version with null safety to 2.12 (#2684) df1140af Warn from get, when mixed mode (#2590) 765778c0 Simplify test detection (#2682) afd66ea2 Inline the single test asset. (#2681) 059e4796 Simplify the logic for unicode and colors in output (#2679) 35ddaec2 Dartify test tool (#2680) 62f26401 Example for User-Agent (#2678) e8b4b114 fixes: #2670 pub global activate commands always exit cmd on windows. (#2671) 93e703b1 Improve stack traces in many tests (#2673) 5b540a39 Fix experiments tests for Dart 2.11 (#2672) b0ac77d8 Bump dependency on pkg:analyzer (#2662) 73f0906e Removed @alwaysThrows (#2642) 88e0a83c Fixed bug in adding dependency to empty dependencies key (#2640) 135d9fa0 Pub add/remove now remove dependencies key if it makes them empty (#2639) f4cc9673 Fix "pubpsec" typo (#2641) 4686d74d Adding an existing package is now a dataError (#2638) 1e93f47c Checks pubspec keys for potential typos (#2616) fa5f51ef Vendor yaml_edit (#2633) ac6d307f Adding a `pub remove` command (#2620) 9d236e00 Adding the `pub add` command (#2618) 04e237f7 Drop upper bound instead of using "any" while resolving in "pub outdated" (#2623) 93954f33 Use InternetAddress.tryParse over try/catch (#2626) 638c81c9 Refine publishing message (#2624) Allow github Embed pub as a library into dartdev Change-Id: Iadc6acb5c3425dfb8848db89820e6c9c8caf16ba Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/167574 Reviewed-by: Jonas Jensen <jonasfj@google.com> Commit-Queue: Sigurd Meldgaard <sigurdm@google.com>
In the dom library there are several types which are sum (union) types.
IDBKey and SerializedScriptValue are two examples - both include numbers, strings and JavaScript arrays (Lists) among other things, and neither includes functions or arbitrary user defined types.
We are forced to use Dynamic for these types.
If typedef could do a simple renaming we could generate more readable interfaces.
e.g.
could become
The text was updated successfully, but these errors were encountered: