assemblyscript icon indicating copy to clipboard operation
assemblyscript copied to clipboard

feat: make result of StaticArray#slice and StaticArray#concat as instance generics. Deprecate static methods

Open MaxGraey opened this issue 1 year ago • 5 comments

StaticArray.slice / StaticArray.concat are quite not standard. Also, many users more expect StaticArray as returning type instead Array for slice method. With this change, we preserve old behavior, so it's non breakable change.

Open question: What if change default return type from Array to StaticArray and remove StaticArray.slice at all? This will be breaking change than.

  • [x] I've read the contributing guidelines
  • [x] I've added my name and email to the NOTICE file

MaxGraey avatar Aug 04 '22 11:08 MaxGraey

Iirc the idea here is that StaticArray#slice returns an Array while StaticArray.slice works with StaticArray, like other instance/static methods, i.e. the static ones exist to provide what would otherwise be missing.

dcodeIO avatar Aug 04 '22 11:08 dcodeIO

The main problem with StaticArray.slice that it's non-standard nature and different signature compare to Array/SliceArray#slice (What makes refactoring difficult) and force people to spend a lot of time in docs. Also sometimes users thinking it's a bug. With new approach user will show signature suggestion. Also with inferring by returned type you will even not need specify generic type like in this case let copy: StaticArray<i32> = origStaticArr.slice()

MaxGraey avatar Aug 04 '22 11:08 MaxGraey

I guess the same would apply to other static methods as well then, and without inference of return types seems tricky. Hmm. Sure it wouldn't be better to defer until we can update all of this according to your suggestion?

dcodeIO avatar Aug 04 '22 11:08 dcodeIO

I think we can move towards this iteratively. These changes don't break anything. And then we can remove all static methods that duplicate instance methods but only with a different return type. I just don't know how soon we will have inferring returned generic types

MaxGraey avatar Aug 04 '22 11:08 MaxGraey

Btw I tried do the same for StringArray.concat but due to some issue with inferring it seems we should delay this change:

ERROR TS2322: Type '~lib/array/Array<~lib/string/String>' is not assignable to type '~lib/array/Array<usize>'.
       :
   118 │ result = source.concat(["foo"]);
       │          ~~~~~~~~~~~~~~~~~~~~~~
       └─ in std/staticarray.ts(118,12)

For new signature:

concat<U extends ArrayLike<T> = Array<T>>(other: U): U {
  ....
}

UPD: Fill an issue for that.

MaxGraey avatar Aug 04 '22 12:08 MaxGraey