Skip to content
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

Static Extension Meta Functions #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions proposals/0000-static-extension-meta-functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Static Extension Meta Functions

* Proposal: [HXP-NNNN](NNNN-static-extension-meta-functions.md)
* Author: [lublak](https://github.com/lublak)

## Introduction

Allow the possibility to use the meta function,
based on the abstraction, also in static extension classes.

## Motivation

Currently, operator overloading, implicit casts
and array access are reserved for abstractions only.
However, it would also be very practical to provide this possibility via static extension classes,
which can then be consulted with using.

Just one example of how the respective functions could look:

```haxe
class UsingWithMetaFunction {
@:to public static inline function to(str:String):Int {
return Std.parseInt(str);
}

@:from public static function from(i:Int):String {
return Std.string(i);
}

@:op(A-B) public static inline function sub(str:String, i:Int) {
return str.substr(0, -i);
}

@:arrayAccess
public static inline function get(str:String, key:Int):Int {
return str.charCodeAt(key);
}
@:arrayAccess
public static inline function set(str:String, k:Int, v:Int):String {
return str.substring(0, k) + String.fromCharCode(v) + str.substring(k+1);
}
}
```

## Detailed design

Basically, the implementation could work via a kind of lookup.
If an operator, an implicister cast to a type or an array acces occurs, it checks if the functions that have been included by using contain the respective meta.
If so, the respective function should be called.

## Impact on existing code

Basically, nothing should break.
But should someone use the meta data in these classes for other information problems could occur here.

## Drawbacks

None.

## Alternatives

Basically, the class can be wrapped with an abstract.
But then you lose the functionalities of a class. (As an example: extends)

## Unresolved questions

None.