digitalmars.dip.ideas - Infered Function Attributes
- Jin (54/54) Jan 06 Currenly we have:
Currenly we have: - Flags with different rules what can be called by what: pure, nothrow, static... - Different syntax: pure, nogc. Idea - generalize attributes: - Prefer only one syntax. - Add syntax to define rules of attributes infering. - Define this rules for current attributes in std library. - Support any infered type, not only flags. - Support by language server for showing inferred attributes. Some use cases beyond the existing: 1. ** max_stack** - required stack size for calling function. ```d void xxx(); // defined max_stack( X ) by compiler void yyy() max_stack( 128 ); // manually defined bar() { // inferred max_stack( B + max( X, 128 ) ) xxx(); yyy(); } void foo() { // inferred max_stack( F + B + max( X, 128 ) ) bar(); } ``` 2. ** iterations, allocations** - asymptotics of function. ```d iterations( arr.length * log( arr.length ) ) // manual defined allocations( 1 ) // manual defined void sort( Item )( Item[] arr ); // inferred iterations( arr.length * ( arr[0].length * log( arr[0].length ) ) ) // inferred allocations( 1 ) void sort( Item )( Item[][] arr ) { foreach( row; arr ) row.sort; } ``` 3. ** throws** - Exceptions that can be thrown by function. ```d throws( NotFound )// manually defined string read(); throws( NoSpace ) // manually defined string write( string data ); // inferred throws( NotFound, NoSpace ) string update( path ) { write( read() ); } // inferred throws() aka nothrow string tryUpdate( path ) { try { write( read() ); } finally {} } ``` I suggest we fantasize about this topic and come up with other useful applications for inferring function attributes.
Jan 06