generics

Is there any equivalent of C#'s “default” keyword for Kotlin?

和自甴很熟 提交于 2020-05-15 10:03:02
问题 Code Example: import java.util.UUID interface InterfaceOne<AType> { var abcOne:AType } interface InterfaceTwo<AType> { var abcTwo:AType } class Example<AType>: InterfaceOne<AType>, InterfaceTwo<AType> { override var abcOne: AType // Looking for default value to not from constructor set(value) { field = value //... } override var abcTwo: AType // Looking for default value to not from constructor set(value) { field = value //... } fun test(uuid: AType) { abcTwo = uuid abcOne = default // I'm

Calling method on generic type Dart

你离开我真会死。 提交于 2020-05-15 08:31:30
问题 I am trying to get a way to call method on a generic type. But can't find it. In swift I can write like: protocol SomeGeneric { static func createOne() -> Self func doSomething() } class Foo: SomeGeneric { required init() { } static func createOne() -> Self { return self.init() } func doSomething() { print("Hey this is fooooo") } } class Bar: SomeGeneric { required init() { } static func createOne() -> Self { return self.init() } func doSomething() { print("Hey this is barrrrrrr") } } func

Why doesn't array conform to Equatable, when its items are Equatable in Swift?

梦想与她 提交于 2020-05-14 17:46:07
问题 UPDATE: As of Xcode 9.3, which includes Swift 4.1 , the array equality works as expected , and the code in the original question compiles without errors. However, please see the accepted answer , because it provides a better, more modern solution. The original question is below: When I try to declare an instance of a generic enum with type [Post] , I get an error saying Type '[Post]' does not conform to protocol 'Equatable' which is nonsense, because Post conforms to Equatable and I can

Generics: cannot convert from <capture#1-of ? extends Object,D> to <S,D>

99封情书 提交于 2020-05-14 16:49:05
问题 I have the following class structure: public interface CopyMapper<S, D> { public D map(S sourceObject); } public interface CopyMapperFactory { public <S, D> CopyMapper<S, D> getMapper(Class<S> sourceClass, Class<D> destinationClass); } public class Mapper { public <S, D> D map(S source, Class<D> destinationClass) { //This is where I get compile time error CopyMapper<S, D> copyMapper = mapperFactory.getMapper(source.getClass(), destinationClass); return copyMapper.map(source); } My Eclipse

C# Using Equals method in a generic list fails

旧街凉风 提交于 2020-05-13 17:57:26
问题 I have a project where I have class State which uses templates. I have a class Cell, and I use it as State, so State holds a Cell as genericState. Now I have a generic function which checks if two instances are equal. Problem is, it never leaves the State Equals method to Cell Equals method. public class State<T> { public T genericState; //in my case T is a cell public State(T cellState) // CTOR { this.genericState = cellState; } public override bool Equals(object obj) { return genericState

C# Using Equals method in a generic list fails

北城以北 提交于 2020-05-13 17:56:22
问题 I have a project where I have class State which uses templates. I have a class Cell, and I use it as State, so State holds a Cell as genericState. Now I have a generic function which checks if two instances are equal. Problem is, it never leaves the State Equals method to Cell Equals method. public class State<T> { public T genericState; //in my case T is a cell public State(T cellState) // CTOR { this.genericState = cellState; } public override bool Equals(object obj) { return genericState

“Generic parameter could not be inferred” in SwiftUI UIViewRepresentable

二次信任 提交于 2020-05-13 14:43:25
问题 I've got the following code, which makes it possible to use the UIKit's UIScrollView in my SwiftUI code. It can be pasted in a new SwiftUI project. struct LegacyScrollView<Content: View>: UIViewRepresentable { enum Action { case idle case offset(x: CGFloat, y: CGFloat, animated: Bool) } @Binding var action: Action let content: Content func makeCoordinator() -> Coordinator { Coordinator(self) } func makeUIView(context: Context) -> UIScrollView { let hosting = UIHostingController(rootView: self

Difference between TypeVar('T', A, B) and TypeVar('T', bound=Union[A, B])

|▌冷眼眸甩不掉的悲伤 提交于 2020-05-13 14:36:27
问题 I'm struggling to get my head around the difference between the following two TypeVar s from typing import TypeVar, Union class A: pass class B: pass T = TypeVar("T", A, B) T = TypeVar("T", bound=Union[A, B]) anyone want to enlighten me? An example of something I don't get ... T = TypeVar("T", bound=Union[A, B]) class AA(A): pass class X(Generic[T]): pass class XA(X[A]): pass class XAA(X[AA]): pass passes type checking, but with T = TypeVar("T", A, B) it fails with generics.py:31: error:

System.AttributeTargets.GenericParameter in C# : how do I use such an attribute?

允我心安 提交于 2020-05-13 05:09:16
问题 In C#, when specifying how an attribute class should be used, there is a GenericParameter value in the System.AttributeTargets enum. How can we apply such an attribute, what is the syntax ? [System.AttributeUsage(System.AttributeTargets public sealed class MyAnnotationAttribute : System.Attribute { public string Param { get; private set; } public MyAnnotationAttribute(string param) { Param = param; } } Same question goes for other exotic attribute targets, like System.AttributeTargets.Module

TypeScript: Is it possible to get the return type of a generic function?

偶尔善良 提交于 2020-05-13 01:05:06
问题 I have exported a function from some module that looks like: export function MyFunc<A>() { return { foo: (in: A) => void } } Now, in some other module, I want to be able to talk about the different return types of MyFunc . Since I didn't export the type, I'll use typeof to get hold of the type I want given the value MyFunc . Ideally I would do the following : import { MyFunc } from "mymodule"; type MyFuncReturned<A> = ReturnType<typeof MyFunc<A>>; function foo(): MyFuncReturned<string> { // .