generics

In Haxe, can you write a generic interface where a method type parameter is constrained by the class's type parameter?

♀尐吖头ヾ 提交于 2020-05-30 03:53:12
问题 I'm having trouble writing the generic interface below. In my class, I have a function that takes an array of < any type that extends a parent class > and traces its first element. Since I'm only reading elements from the array, I'm using it as if its a covariant compound type, and therefore I'm guaranteed to have the cast statement never fail. Now I want to abstract this out even more and write a interface that defines fn using another generic type T. I want fn to be able to accept any Array

Enforcing type of key in generic function input

瘦欲@ 提交于 2020-05-30 02:52:51
问题 Let's say I have the generic function: function f<T, K extends keyof T>(obj: T, key: K) { ... } I would like to enforce the type of T[K] so I could perform type specific actions. For example, using string: function f<T, K extends keyof T>(obj: T, key: K): string { return obj[key].toLowerCase(); } Is this at all possible without casting things to any ? Edit: To clarify, I'm looking for the ability to disallow certain keys based on the resulting type. Using the example above, something like: f(

Enforcing type of key in generic function input

筅森魡賤 提交于 2020-05-30 02:48:12
问题 Let's say I have the generic function: function f<T, K extends keyof T>(obj: T, key: K) { ... } I would like to enforce the type of T[K] so I could perform type specific actions. For example, using string: function f<T, K extends keyof T>(obj: T, key: K): string { return obj[key].toLowerCase(); } Is this at all possible without casting things to any ? Edit: To clarify, I'm looking for the ability to disallow certain keys based on the resulting type. Using the example above, something like: f(

Generic[T] base class - how to get type of T from within instance?

◇◆丶佛笑我妖孽 提交于 2020-05-29 10:16:05
问题 Assume you have a Python class that inherits from Generic[T]. Is there any way to get a hold of the actual type passed in from within the class/instance? For example, from typing import TypeVar, Type T = TypeVar('T') class Test(Generic[T]): def hello(self): my_type = T # this is wrong! print( "I am {0}".format(my_type) ) Test[int]().hello() # should print "I am int" On here, it is suggested the type arg would be present in in the args field of the type. And indeed, print( str( Test[int]._

Type argument is not within its bounds Expected: Parcelable Found: String

烂漫一生 提交于 2020-05-29 09:45:24
问题 I am creating a generic, abstract class like this: abstract class BaseDialogFragment<T: Parcelable> : DialogFragment() Trying to implement this class as class MyDialogFragment : BaseDialogFragment<String>() gives me Type argument is not within its bounds Expected: Parcelable Found: String for the String in BaseDialogFragment<String>() . So, how can I use String as a value for T ? Is my condition T: Parcelable somehow wrong, if I want T to be a parcelable type? 回答1: So, how can I use String as

Type argument is not within its bounds Expected: Parcelable Found: String

痞子三分冷 提交于 2020-05-29 09:44:49
问题 I am creating a generic, abstract class like this: abstract class BaseDialogFragment<T: Parcelable> : DialogFragment() Trying to implement this class as class MyDialogFragment : BaseDialogFragment<String>() gives me Type argument is not within its bounds Expected: Parcelable Found: String for the String in BaseDialogFragment<String>() . So, how can I use String as a value for T ? Is my condition T: Parcelable somehow wrong, if I want T to be a parcelable type? 回答1: So, how can I use String as

Type argument is not within its bounds Expected: Parcelable Found: String

痞子三分冷 提交于 2020-05-29 09:44:35
问题 I am creating a generic, abstract class like this: abstract class BaseDialogFragment<T: Parcelable> : DialogFragment() Trying to implement this class as class MyDialogFragment : BaseDialogFragment<String>() gives me Type argument is not within its bounds Expected: Parcelable Found: String for the String in BaseDialogFragment<String>() . So, how can I use String as a value for T ? Is my condition T: Parcelable somehow wrong, if I want T to be a parcelable type? 回答1: So, how can I use String as

Type argument is not within its bounds Expected: Parcelable Found: String

最后都变了- 提交于 2020-05-29 09:44:17
问题 I am creating a generic, abstract class like this: abstract class BaseDialogFragment<T: Parcelable> : DialogFragment() Trying to implement this class as class MyDialogFragment : BaseDialogFragment<String>() gives me Type argument is not within its bounds Expected: Parcelable Found: String for the String in BaseDialogFragment<String>() . So, how can I use String as a value for T ? Is my condition T: Parcelable somehow wrong, if I want T to be a parcelable type? 回答1: So, how can I use String as

Conditional types with Generics in TypeScript

萝らか妹 提交于 2020-05-29 07:26:55
问题 What I want to achieve can be best explained in code: Given shoe and dress class: class Shoe { constructor(public size: number){} } class Dress { constructor(public style: string){} } Have a generic box that can only contain Shoe or Dress. Can't contain both: class Box <T extends Shoe | Dress > { } Then have a utility class that takes care of moving shoes: class ShoeMover { constructor(public size: number[]){} } Also, a utility class for moving Dresses: class DressPacker { constructor(public

Conditional types with Generics in TypeScript

不羁的心 提交于 2020-05-29 07:25:24
问题 What I want to achieve can be best explained in code: Given shoe and dress class: class Shoe { constructor(public size: number){} } class Dress { constructor(public style: string){} } Have a generic box that can only contain Shoe or Dress. Can't contain both: class Box <T extends Shoe | Dress > { } Then have a utility class that takes care of moving shoes: class ShoeMover { constructor(public size: number[]){} } Also, a utility class for moving Dresses: class DressPacker { constructor(public