-
Notifications
You must be signed in to change notification settings - Fork 213
Introduce additional primitive members #2219
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
Comments
For The |
Thx! I adjusted the table. I did not change the static receiver type for operations on iterables, because the restrictions on the actual receiver already restricts the operations to objects of type |
There has not been much discussion here about making more constructors |
@rakudrama, perhaps the request for constant constructors of I do recognize that it is not possible to have an With that in place, we can say that this issue is exclusively about extending the set of constant expressions (by adding |
Would this allow tighter tree shaking somehow on maps ? If I recall correctly one of the test I tried a while ago const maps weren't tree shaken (on web) but I could be wrong. IE if only a subset of elements are accessed with constants values. |
You could say that there is a potential benefit there, because we could conceptually consider However, this stops working as soon as the program drops a bit of information: If we're passing So there isn't much hope that we could remove any elements from a list, or any key/value pairs from a map, based on a static analysis. Tree shaking works a lot better when we have a declaration which is known to be unused because no identifier in the program resolves to that declaration. I created a proposal which is somewhat relevant here: #371, about 'link time sets and maps'. The main idea for those is that they are populated by things that are reachable already. So if you have a class This basically means that we're tree-shaking some elements out of the set and similarly for a map (e.g., a constructor tear-off like |
Consider adding |
Ultimately what's the limit here? Does it make the compiler more complex ? Is it human time ? Is it performances ? would this work: const MyClass(List<int> list) : assert(checkBlackListed(list));
static double checkBlackListed(List<int> value) => !(const [1,2].contains(value)); Personally, it would have a positive impact if some exceptions could be caught before runtime (especially in tests where the values are often hardcoded), and I'd assume this type of feature is on the low budget side of implementation time compared to static metaprogramming, so I hope it can make it soon. |
Here is another candidate enhancement: If Cf. dart-lang/sdk#51895. |
Cf. #1296 (comment), #1296 (comment). Issue #1296 contains several other comments that are relevant to this discussion, but the topic is separate from that of #1296, so here is an issue where the addition of primitive members is the main topic.
This topic covers a number of requests for specific enhancements of the expressive power of constant expressions. In particular, the ability to perform sanity checks (in an
assert
) on constructor arguments in a constant constructor has been requested many times.The language specification currently specifies what it means to have a primitive operator
==
. Essentially, this means that the implementation is guaranteed to be provided by the system, which again makes it possible to guarantee that the corresponding behavior can also be provided during constant expression evaluation at compile time.We could introduce the corresponding notion of being primitive for any member whose implementation is guaranteed to be system provided, thus extending the set of constant expressions with things like
myList.length
,mySet.isNotEmpty
, etc. Note that even a member likeSet.contains
could be handled, in spite of the fact that it is not a getter, because the semantics of an invocation is known and can be performed during constant expression evaluation.Here is a table showing the rather large number of cases where we could do this (based on a table in this comment by @lrhn):
length
,isEmpty
,isNotEmpty
Iterable
,Map
contains
,first
,last
,single
Iterable
operator []
List
containsKey
,containsValue
,operator []
Map
isNaN
,isFinite
,sign
,ceil
,floor
,round
,ceilToDouble
,floorToDouble
,roundToDouble
num
double
,int
substring
,charCodeAt
,contains
,startsWith
,endsWith
String
String
We would need a number of extra constraints, including:
Pattern
arguments toString
operations must be strings.contains
on iterables andcontainsValue
on maps, the argument must have primitive equality.Of course, we could do this to a tiny extent (e.g., just add
length
for lists), or we could do all of it, and we could do it incrementally over time.The text was updated successfully, but these errors were encountered: