Skip to content

Instantly share code, notes, and snippets.

View fal-works's full-sized avatar

FAL fal-works

View GitHub Profile
@fal-works
fal-works / Filepath.hx
Created October 22, 2021 14:38
type Filepath for Haxe
import haxe.io.Path;
/**
ファイルパスです。
`haxe.io.Path` と以下の点で異なります。
- イミュータブルです。
- 作成時点で正規化されます。
- ディレクトリーや拡張子が未指定のときは空文字になります。
**/
@fal-works
fal-works / PrintFileTree.hx
Created May 23, 2021 15:13
Print file tree
import haxe.io.Path;
import sys.FileSystem;
function main()
printFileTree(getFileTree("cwd", "relPath")); // change arguments
typedef FileOrDir = {name:String, children:Array<FileOrDir>};
function getFileTree(cwd:String, relPath:String):FileOrDir {
final path = Path.join([cwd, relPath]);
@fal-works
fal-works / watch.js
Created January 23, 2021 19:59
Usage example of chokidar.
import * as chokidar from "chokidar";
/**
* Creates a watcher.
*
* @param {object} options
* @param {string | readonly string[]} options.paths Paths or Glob patterns to watch.
* @param {(path: string) => void} options.onChange
* @param {() => void} [options.onExit]
*/
@fal-works
fal-works / Main.hx
Created August 19, 2020 16:46
HL/JIt error with too many enum constructors
class Main {
static function main() {
final instance: MyEnum = EC0;
trace(switch instance {
case EC0: "EC0";
default: "other";
});
}
}
@fal-works
fal-works / Main.hx
Created July 16, 2020 12:41
Haxe: Multiple Return Values
class Main {
static extern inline function swap(a, b)
return new Int2(b, a);
static function main() {
final input_a = 1974;
final input_b = 777;
final output = swap(input_a, input_b);
trace(output.a);
trace(output.b);
@fal-works
fal-works / lastIndexOfFromNull.js
Created April 26, 2020 00:11
str.lastIndexOf(searchString, null) returns -1
// Node v13.12.0
const str = "abcde";
const searchString = "c";
const indices = [
str.indexOf(searchString), // 2
str.indexOf(searchString, null), // 2
str.lastIndexOf(searchString), // 2
str.lastIndexOf(searchString, null) // -1
@fal-works
fal-works / Main.hx
Created April 21, 2020 13:53
Haxe: std.UInt with HashLink
class Main {
static function main() {
var a: UInt = -1;
Sys.println(a); // -1
trace(a); // 4294967295
}
}
@fal-works
fal-works / Main.hx
Created April 20, 2020 19:59
Haxe: Cannot force Iterator to be inlined
abstract MyInt(Int) from Int to Int {
@:op(A...B) static extern inline function iter(a: MyInt, b: MyInt): MyIntIterator
return new MyIntIterator(a, b);
public extern inline function toInt()
return this;
}
// @:access(IntIterator)
// @:forward(min, max, hasNext)
@fal-works
fal-works / Main.hx
Created April 14, 2020 17:19
Haxe: abstract with @:generic type (doesn't compile)
@:generic // Without this it compiles fine
class GenericObject<T> {
final value: T;
public function new(v: T)
this.value = v;
}
abstract GenericAbstract<T>(GenericObject<T>) from GenericObject<T> {
public inline function new(data: GenericObject<T>)
@fal-works
fal-works / Macro.hx
Created March 19, 2020 17:11
Haxe: Build macro doesn't find namespaced metadata
#if macro
import haxe.macro.Context;
import haxe.macro.Expr.Field;
using StringTools;
class Macro {
static macro function checkMeta():Null<Array<Field>> {
warnIfNotFound(":myMeta");
return null;