Skip to content

Instantly share code, notes, and snippets.

@y-yu
Last active August 29, 2015 14:09
Show Gist options
  • Save y-yu/18139b05415ccb179056 to your computer and use it in GitHub Desktop.
Save y-yu/18139b05415ccb179056 to your computer and use it in GitHub Desktop.
CharArray
abstract CharArray(String) {
inline function new(x) {
this = x;
}
public function get() { return this; }
@:from public static inline function fromString(x : String) : CharArray {
return new CharArray(x);
}
@:to public inline function toString() : String {
return this;
}
@:arrayAccess public inline function arrayGet(i:Int) : String {
return this.charAt(i);
}
@:arrayAccess public inline function arraySet(i:Int, v:String) : Void {
this = this.substr(0, i) + v + this.substr(i + 1);
}
// to access String's method
@:op(!A) public inline function fieldAccess() : String {
return this;
}
}
class Test {
static function main() {
var x : CharArray = "hoge";
trace(x[1]);
trace((!x).substr(1)); // I have no idea except this...
x[1] = "O";
trace(x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment