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

Global using at declaration site. #35

Closed
back2dos opened this Issue Oct 23, 2017 · 12 comments

Comments

Projects
None yet
7 participants
@back2dos
Copy link
Member

back2dos commented Oct 23, 2017

The idea would is to allow using clauses on enum, interface and perhaps also class (although I don't see the use case there).

enum Option<T> using OptionTools {
  Some(v:T);
  None;
}

class OptionTools {
  static public function map<In, Out>(o:Option<T>, f:In->Out):Option<Out> { 
    /* and so on */
  }
  /* and so forth */
}

This would globally apply the static extension, but only to the type being declared. It would solve #10 - albeit in a slightly awkward way.

On interfaces it could also do some good:

interface Iterable<T> using Lambda {
  function iterator():Iterator<T>;
}
@nadako

This comment has been minimized.

Copy link
Member

nadako commented Oct 23, 2017

That's an interesting idea!

@Simn

This comment has been minimized.

Copy link
Member

Simn commented Oct 23, 2017

I had this same idea before. I wonder if this should be implemented as a first step to solve #10. The second step would then be to automatically move function declarations in enums to such static extension classes, and add the using OptionTools implicitly.

On its own, I wonder if its worth it given that we have import.hx and can just add using OptionTools there. I agree this looks nicer, but I'm not sure it's enough to warrant a syntax change.

@RealyUniqueName

This comment has been minimized.

Copy link
Member

RealyUniqueName commented Oct 23, 2017

Import.hx cannot be distributed with haxelib.

@back2dos

This comment has been minimized.

Copy link
Member

back2dos commented Oct 23, 2017

Also, on second thought, I would like this for abstracts too as it allows dispatching on type parameters. Here's a poor example that hopefully still gets the point across:

//Array.hx
@:coreType class Array<T> using ArrayTools { /* all methods except sort */ }
//ArrayTools.hx
class ArrayTools {
  static public function sort<T>(a:Array<T>, compare:T->T->Int):Void;
}
class NumericArrayTools {
  static public function sort<T:Float>(a:Array<T>):Void;
  static public function sum<T:Float>(a:Array<T>):T;
}
class DateArrayTools {
  static public function sort(a:Array<Date>):Void;
}
class ComparableArrayTools {
  static public function sort<T:{ function compare(that:T):Int; }>(a:Array<T>):Void;
}
@bendmorris

This comment has been minimized.

Copy link

bendmorris commented Mar 2, 2018

and perhaps also class (although I don't see the use case there)

It gives a nice method for adding "mixins" to your classes. Multiple classes can declare using with the same extension class. AFAIK you can only do this with tink/macros currently.

@RealyUniqueName

This comment has been minimized.

Copy link
Member

RealyUniqueName commented Jul 27, 2018

@back2dos are you going to convert this idea to a proposal?

@back2dos

This comment has been minimized.

Copy link
Member

back2dos commented Jul 30, 2018

Well, if @ncannasse could confirm that he'll at least genuinely consider it, I can do that ^^

@Simn

This comment has been minimized.

Copy link
Member

Simn commented Sep 3, 2018

This needs a proper proposal. I can't speak for Nicolas, but I for one would genuinely consider it.

@Simn Simn closed this Sep 3, 2018

@kaikoga

This comment has been minimized.

Copy link

kaikoga commented Jan 6, 2019

Whoa, I thought this had been already implemented, but was just closed and forgotten...

Is this going to become a proposal?

@kaikoga

This comment has been minimized.

Copy link

kaikoga commented Jan 6, 2019

oh sorry, I've overlooked HaxeFoundation/haxe#7462

@FuriouZz

This comment has been minimized.

Copy link

FuriouZz commented Jan 6, 2019

Does not seem to works with abstract =/. I have Vec4 has no field add.

class Vec4Tool {

  public static inline function add(out: Vec4, v0: Vec4, v1: Vec4) : Vec4 {
    out[0] = v0[0] + v1[0];
    out[1] = v0[1] + v1[1];
    out[2] = v0[2] + v1[2];
    out[3] = v0[3] + v1[3];
    return out;
  }

}

@:using(Vec4.Vec4Tool)
@:forward abstract Vec4(Float32Array) from Float32Array to Float32Array {
  public function new() {
    this = new Float32Array([ 0.0, 0.0, 0.0, 1.0 ]);
  }

  @:arrayAccess
  public inline function getValue(index: Int) {
    return this[index];
  }

  @:arrayAccess
  public inline function setValue(index: Int, value: Float) {
    return this[index] = value;
  }

}
@back2dos

This comment has been minimized.

Copy link
Member

back2dos commented Jan 6, 2019

You should file a bug report, specifying which Haxe version you're using.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment