logo
down
shadow

Pass type to generic function and compare


Pass type to generic function and compare

By : Junhao Dong
Date : November 22 2020, 10:33 AM
wish help you to fix your issue Consider this simple example , You should check if it's T:
code :
if list[i] is T {
    ...
}


Share : facebook icon twitter icon
Should I always use wildcards as generic types for function arguments or only pass instances of one generic type?

Should I always use wildcards as generic types for function arguments or only pass instances of one generic type?


By : user2711870
Date : March 29 2020, 07:55 AM
To fix the issue you can do You can't pass List to a method requiring a List because it may cause some problems. What would happen in this case if you pass in a List?
code :
void foo(List<Base> list) {
    list.add(new Base());
}
Can you write a generic function that will compare any numeric type (e.g. int, float, double) against zero?

Can you write a generic function that will compare any numeric type (e.g. int, float, double) against zero?


By : Kristin
Date : March 29 2020, 07:55 AM
I hope this helps you . My solution adds a second protocol conformance: Numeric provides basic arithmetic operators for scalar types.
Then you are able to calculate the zero value in the generic type
code :
func getChangeColor<T : Comparable & Numeric>(value:T) -> UIColor {

    let zero = value - value

    if value > zero {
        return Theme.Colors.gain
    } else if value < zero {
        return Theme.Colors.loss
    } else {
        return Theme.Colors.noChange
    }
}
func getChangeColor<T : Comparable & Numeric>(value:T) -> UIColor {

    let zero = value - value

    switch value {
    case zero:  return Theme.Colors.noChange
    case zero...: return Theme.Colors.gain
    default: return Theme.Colors.loss
    }
}
func getChangeColor<T : Comparable & Numeric>(value:T) -> UIColor {

    let zero = value - value

    return value > zero
        ? Theme.Colors.gain
        : value < zero
            ? Theme.Colors.loss
            : Theme.Colors.noChange
}
Pass a generic class type as a parameter of a generic function

Pass a generic class type as a parameter of a generic function


By : user1860310
Date : March 29 2020, 07:55 AM
it fixes the issue You can use t.constructor as the Clazz argument. You'll need to tell TypeScript it is of the right type using as:
code :
// From the library
declare type Clazz <T> = {
    new(...args: any[]): T;
};

// From the library
class Converter {
    public fromJson<T>(json: any, clazz: Clazz<T>) { return new clazz(); }
}

class Test {
    public testIt<T>(test: T) {
        const converter = new Converter();
        var o = converter.fromJson("{}", test.constructor as new (...args: any[]) => T);
        return o;
    }
}

class TestObj {
    testProp: string = "hello";
}

var o = new TestObj();
var t = new Test();
var a = t.testIt(o);
console.log(JSON.stringify(a));
Can pass null to generic function when type is a value type

Can pass null to generic function when type is a value type


By : Aidan J Doyle
Date : March 29 2020, 07:55 AM
around this issue Nothing in VB is not the same as null in C#. The better equivalent is default, because it represents the default value for its type. The behavior is the same for reference types (whose default is a null reference) but very different for value types as you note.
Since this is part of the design of the language, there is no way to turn it off.
Can I pass a generic into a non-generic function once I've checked its type?

Can I pass a generic into a non-generic function once I've checked its type?


By : Mentor Neto
Date : March 29 2020, 07:55 AM
With these it helps The fact that you check the dynamic type of the variable doesn't change its static type, and that what it's used to check the call.
If you want to do that, you need a cast. You can use a checked cast to do it in one go:
Related Posts Related Posts :
  • consecutive statement need to have ; error in swift
  • Trying to create a 32 bpc NSBitmapImageRep, getting hit with errors
  • Does locationManager:didUpdateLocations: method called at the same time in extension and its containing app
  • Can't get Data using SwiftyJSON
  • Convert String to UnsafeMutablePointer<char_t> in Swift
  • inout param with empty object since nil won't work
  • How to align a mask onto an image
  • compiler forcing self inside static method of a function while using dispatch_sync
  • private static variable in struct
  • Conditional "in" for arrays in Swift
  • Extension for Double to accept String as initializer in Swift
  • Modifying UIButton Programmatically
  • How to customise the look of the PFSignUpViewController
  • How to take screen shot programmatically (Swift, SpriteKit)
  • Setting NavigationController's NavigationBar translucent property to false causes extra padding
  • Create multiple UIViews when the user taps the screen
  • Filter Array of [AnyObject] in Swift
  • How to add ParseUI to a swift project
  • Can Swift enums have multiple raw values?
  • Using a function() variable in a different Class - Swift
  • Masking an image in Swift using CALayer and UIImage
  • KVC without NSObject (NSPredicate)
  • Any? to Dictionary Cast and retrieving value
  • NSCharacterSet.characterIsMember() with Swift's Character type
  • SCNScene: Calculate projected size of an object
  • How to use contains on a swiftyJSON object
  • Index of element in sorted()
  • How do I initialise a new NSDocument instance in Swift?
  • Multiple functions with the same name
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org