jjjkkkjjj / Matft

Numpy-like library in swift. (Multi-dimensional Array, ndarray, matrix and vector library)
BSD 3-Clause "New" or "Revised" License
134 stars 21 forks source link

Subscript’s getter and setter #4

Closed jjjkkkjjj closed 4 years ago

jjjkkkjjj commented 4 years ago

If I don’t use generics in MfArray, subscript’s getter and setter must be handled as Any. However, using Any type causes unexpected error or performance loss.


MfArray<MfType: MfTypable>{
    Hoge
}

//Initialization 
let a = MfArray<Int>([1,2,3])

//Getter and setter
//Note that scalar will be handled only
subscript(indices: Int...) -> MfType{
    hoge
}

//Note that MfArray will be handled only
subscript(mfslices: MfSlice...) -> MfArray{
    fuga
}
jjjkkkjjj commented 4 years ago
MfArray<MfType: MfTypable>{
    Hoge
}

//Initialization 
let a = MfArray<Int>([1,2,3])

I can't understand how above code can be implemented... Alternative solution is below,


subscript(indices: Int...) -> MfArray{
    hoge
}

extension MfArray{
    public var value?:  Any{
          hoge
    }
}

let a = MfArray([[1,2,3], [4,5,6]])
let b = a[2,1].value as! Int

Or Passed Int of array, return Any.

subscript(indices: Int...) -> Any{
    hoge
}
subscript(mfslices: MfSlice...) -> MfArray{
    fuga
}
let b = a[2,1] as! Int //Scalar or MfArray
let c = a[1] as! MfArray //Scalar or MfArray
let d = a[0~,1] //-> MfArray
jjjkkkjjj commented 4 years ago

Or

subscript(indices: Int...) -> MfArray{
    hoge
}
subscript(mfslices: MfSlice...) -> MfArray{
    fuga
}

public func scalar<T: Numeric>(_ type: T) -> T?{
    return self.size == 1 ? self.first! as? T : nil
}

let b = a[2,1].scalar(Int.self)!
guard let c = a[2,1].scalar(Int.self) else{ return }