Skip to content

Commit

Permalink
fix: update import path for Collection type definition and refactor…
Browse files Browse the repository at this point in the history
… to use generics

Ref: bde4671
  • Loading branch information
kgryte committed Sep 20, 2023
1 parent b544a52 commit 50bafc3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

/// <reference types="@stdlib/types"/>

import { Collection } from '@stdlib/types/object';
import { Collection } from '@stdlib/types/array';

/**
* Add-on function.
Expand All @@ -35,7 +35,7 @@ import { Collection } from '@stdlib/types/object';
* // Call into native add-on...
* }
*/
type AddonFcn = ( N: number, dtypeX: number, x: Collection, strideX: number ) => any; // tslint:disable-line:max-line-length
type AddonFcn<T, U> = ( N: number, dtypeX: number, x: Collection<T>, strideX: number ) => U;

/**
* Fallback function.
Expand All @@ -50,7 +50,7 @@ type AddonFcn = ( N: number, dtypeX: number, x: Collection, strideX: number ) =>
* // Fallback JavaScript implementation...
* }
*/
type FallbackFcn = ( N: number, dtypeX: any, x: Collection, strideX: number ) => any; // tslint:disable-line:max-line-length
type FallbackFcn<T, U> = ( N: number, dtypeX: any, x: Collection<T>, strideX: number ) => U;

/**
* Fallback function supporting alternative indexing semantics.
Expand All @@ -66,7 +66,7 @@ type FallbackFcn = ( N: number, dtypeX: any, x: Collection, strideX: number ) =>
* // Fallback JavaScript implementation...
* }
*/
type FallbackFcnWithOffsets = ( N: number, dtypeX: any, x: Collection, strideX: number, offsetX: number ) => any; // tslint:disable-line:max-line-length
type FallbackFcnWithOffsets<T, U> = ( N: number, dtypeX: any, x: Collection<T>, strideX: number, offsetX: number ) => U;

/**
* Dispatches to a native add-on.
Expand All @@ -77,7 +77,7 @@ type FallbackFcnWithOffsets = ( N: number, dtypeX: any, x: Collection, strideX:
* @param strideX - `x` stride length
* @returns `x`
*/
type Dispatcher = ( N: number, dtypeX: any, x: Collection, strideX: number ) => Collection; // tslint:disable-line:max-line-length
type Dispatcher<T> = ( N: number, dtypeX: any, x: Collection<T>, strideX: number ) => Collection<T>;

/**
* Dispatches to a native add-on.
Expand All @@ -89,7 +89,7 @@ type Dispatcher = ( N: number, dtypeX: any, x: Collection, strideX: number ) =>
* @param offsetX - starting `x` index
* @returns `x`
*/
type DispatcherWithOffsets = ( N: number, dtypeX: any, x: Collection, strideX: number, offsetX: number ) => Collection; // tslint:disable-line:max-line-length
type DispatcherWithOffsets<T> = ( N: number, dtypeX: any, x: Collection<T>, strideX: number, offsetX: number ) => Collection<T>;

/**
* Interface for creating a native add-on dispatcher.
Expand Down Expand Up @@ -119,7 +119,7 @@ interface Dispatch {
* // Invoke the dispatch function with strided array arguments:
* f( 2, 'generic', [ 1, 2 ], 1 );
*/
( addon: AddonFcn, fallback: FallbackFcn ): Dispatcher;
<T = unknown, U = unknown>( addon: AddonFcn<T, U>, fallback: FallbackFcn<T, U> ): Dispatcher<T>;

/**
* Returns a function which dispatches to a native add-on applying a nullary function using alternative indexing semantics.
Expand All @@ -145,7 +145,7 @@ interface Dispatch {
* // Invoke the dispatch function with strided array arguments:
* f( 2, 'generic', [ 1, 2 ], 1, 0 );
*/
ndarray( addon: AddonFcn, fallback: FallbackFcnWithOffsets ): DispatcherWithOffsets; // tslint:disable-line:max-line-length
ndarray<T = unknown, U = unknown>( addon: AddonFcn<T, U>, fallback: FallbackFcnWithOffsets<T, U> ): DispatcherWithOffsets<T>;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

/// <reference types="@stdlib/types"/>

import { Collection } from '@stdlib/types/object';
import { Collection } from '@stdlib/types/array';
import dispatch = require( './index' );


Expand All @@ -32,7 +32,7 @@ import dispatch = require( './index' );
* @param x - output array
* @param strideX - `x` stride length
*/
function addon( N: number, dtypeX: number, x: Collection, strideX: number ): void { // tslint:disable-line:max-line-length
function addon( N: number, dtypeX: number, x: Collection<number>, strideX: number ): void {
let i;
if ( dtypeX !== dtypeX ) {
throw new Error( 'beep' );
Expand All @@ -50,7 +50,7 @@ function addon( N: number, dtypeX: number, x: Collection, strideX: number ): voi
* @param x - output array
* @param strideX - `x` stride length
*/
function fallback( N: number, dtypeX: any, x: Collection, strideX: number ): void { // tslint:disable-line:max-line-length
function fallback( N: number, dtypeX: any, x: Collection<number>, strideX: number ): void {
let i;
if ( dtypeX !== dtypeX ) {
throw new Error( 'beep' );
Expand All @@ -69,7 +69,7 @@ function fallback( N: number, dtypeX: any, x: Collection, strideX: number ): voi
* @param strideX - `x` stride length
* @param offsetX - starting `x` index
*/
function fallbackWithOffsets( N: number, dtypeX: any, x: Collection, strideX: number, offsetX: number ): void { // tslint:disable-line:max-line-length
function fallbackWithOffsets( N: number, dtypeX: any, x: Collection<number>, strideX: number, offsetX: number ): void {
let i;
if ( dtypeX !== dtypeX ) {
throw new Error( 'beep' );
Expand All @@ -84,7 +84,7 @@ function fallbackWithOffsets( N: number, dtypeX: any, x: Collection, strideX: nu

// The function returns a dispatch function...
{
dispatch( addon, fallback ); // $ExpectType Dispatcher
dispatch( addon, fallback ); // $ExpectType Dispatcher<number>
}

// The compiler throws an error if not provided a first argument which is an add-on function...
Expand Down Expand Up @@ -119,7 +119,7 @@ function fallbackWithOffsets( N: number, dtypeX: any, x: Collection, strideX: nu

const f = dispatch( addon, fallback );

f( x.length, 'float64', x, 1 ); // $ExpectType Collection
f( x.length, 'float64', x, 1 ); // $ExpectType Collection<number>
}

// The compiler throws an error if the returned function is not provided a first argument which is a number...
Expand Down Expand Up @@ -169,7 +169,7 @@ function fallbackWithOffsets( N: number, dtypeX: any, x: Collection, strideX: nu

// Attached to the main export is an `ndarray` method which returns a dispatch function...
{
dispatch.ndarray( addon, fallbackWithOffsets ); // $ExpectType DispatcherWithOffsets
dispatch.ndarray( addon, fallbackWithOffsets ); // $ExpectType DispatcherWithOffsets<number>
}

// The compiler throws an error if the `ndarray` method is not provided a first argument which is an add-on function...
Expand Down Expand Up @@ -204,7 +204,7 @@ function fallbackWithOffsets( N: number, dtypeX: any, x: Collection, strideX: nu

const f = dispatch.ndarray( addon, fallbackWithOffsets );

f( x.length, 'float64', x, 1, 0 ); // $ExpectType Collection
f( x.length, 'float64', x, 1, 0 ); // $ExpectType Collection<number>
}

// The compiler throws an error if the returned function is not provided a first argument which is a number...
Expand Down

0 comments on commit 50bafc3

Please sign in to comment.