generics

Generics with interfaces in F#

那年仲夏 提交于 2020-01-12 15:14:12
问题 In C# one can state that a generic parameter must implement a certain interface like so: public class Something<T> where T : IComparable { ... } How does one specify this in F#? 回答1: Generic constraints use "when" in F#: type Foo<'a when 'a :> IComparable> = member x.Bla = 0 来源: https://stackoverflow.com/questions/770143/generics-with-interfaces-in-f

Swift Generics: Cannot convert value of type to expected argument type

浪子不回头ぞ 提交于 2020-01-12 14:34:13
问题 Here is my code: protocol SomeProtocol { } class A: SomeProtocol { } func f1<T: SomeProtocol>(ofType: T.Type, listener: (T?) -> Void) { } func f2<T: SomeProtocol>(ofType: T.Type, listener: ([T]?) -> Void) { } func g() { let l1: (SomeProtocol?) -> Void = ... let l2: ([SomeProtocol]?) -> Void = ... f1(ofType: A.self, listener: l1) // NO ERROR f2(ofType: A.self, listener: l2) // COMPILE ERROR: Cannot convert value of type '([SomeProtocol]?) -> Void' to expected argument type '([_]?) -> Void' }

Swift Generics: Cannot convert value of type to expected argument type

99封情书 提交于 2020-01-12 14:32:06
问题 Here is my code: protocol SomeProtocol { } class A: SomeProtocol { } func f1<T: SomeProtocol>(ofType: T.Type, listener: (T?) -> Void) { } func f2<T: SomeProtocol>(ofType: T.Type, listener: ([T]?) -> Void) { } func g() { let l1: (SomeProtocol?) -> Void = ... let l2: ([SomeProtocol]?) -> Void = ... f1(ofType: A.self, listener: l1) // NO ERROR f2(ofType: A.self, listener: l2) // COMPILE ERROR: Cannot convert value of type '([SomeProtocol]?) -> Void' to expected argument type '([_]?) -> Void' }

Swift Generics: Cannot convert value of type to expected argument type

萝らか妹 提交于 2020-01-12 14:32:05
问题 Here is my code: protocol SomeProtocol { } class A: SomeProtocol { } func f1<T: SomeProtocol>(ofType: T.Type, listener: (T?) -> Void) { } func f2<T: SomeProtocol>(ofType: T.Type, listener: ([T]?) -> Void) { } func g() { let l1: (SomeProtocol?) -> Void = ... let l2: ([SomeProtocol]?) -> Void = ... f1(ofType: A.self, listener: l1) // NO ERROR f2(ofType: A.self, listener: l2) // COMPILE ERROR: Cannot convert value of type '([SomeProtocol]?) -> Void' to expected argument type '([_]?) -> Void' }

Mysterious line in stack trace

喜夏-厌秋 提交于 2020-01-12 14:31:11
问题 While investigating a stack trace discrepancy when composing another answer, I came across a behavior I do not understand. Consider the following test program (this is as far down as I could narrow it): interface TestInterface <U> { void test (U u); } static class Test <T extends Test<T>> implements TestInterface<T> { // line 11 @Override public void test (T t) { throw new RuntimeException("My exception"); // line 13 } } static class TestA extends Test<TestA> { } static class TestB extends

Get spring bean via context using generic

☆樱花仙子☆ 提交于 2020-01-12 13:57:59
问题 I have a bunch of repository beans that implement type Repository<T ? extends Node> . Now I can get a list of random nodes from the user and I want to get the appropriate repository for each node. Since Spring 4.0RC1 we can autowire repositories like this: @Autowired Repository<SomeNode> someNodeRepository; As documented here. This works fine, but my question is how I can do this dynamically based on the generic type. What I want to do is something like: public <T extends Node> T saveNode(T

Get spring bean via context using generic

那年仲夏 提交于 2020-01-12 13:57:47
问题 I have a bunch of repository beans that implement type Repository<T ? extends Node> . Now I can get a list of random nodes from the user and I want to get the appropriate repository for each node. Since Spring 4.0RC1 we can autowire repositories like this: @Autowired Repository<SomeNode> someNodeRepository; As documented here. This works fine, but my question is how I can do this dynamically based on the generic type. What I want to do is something like: public <T extends Node> T saveNode(T

C# generics: cast generic type to value type

泄露秘密 提交于 2020-01-12 13:49:06
问题 I have a generic class which saves value for the specified type T. The value can be an int, uint, double or float. Now I want to get the bytes of the value to encode it into an specific protocol. Therefore I want to use the method BitConverter.GetBytes() but unfortunately Bitconverter does not support generic types or undefined objects. That is why I want to cast the value and call the specific overload of GetBytes(). My Question: How can I cast a generic value to int, double or float? This

How do I create a C# Array of Buttons?

烈酒焚心 提交于 2020-01-12 07:48:06
问题 How do I build an array of buttons in a Winforms application? What I am trying to do is this: I have a lot of buttons in a sort of calendar arrangement, that are indicating time slots. IE: Monday0700Button, Monday0730Button, Monday0800Button, and so on in 30min intervals. I have a xml database, where one of the fields for appointments is <Duration> When the duration = 0.5hrs, and the <Time> field equals "07:00am", to color the 'Monday0700Button'. When the Duration is 1.0hrs, I want it to

Default value check using generic types [duplicate]

对着背影说爱祢 提交于 2020-01-12 07:12:10
问题 This question already has answers here : Null or default comparison of generic argument in C# (12 answers) Closed 6 years ago . I want to be able to check whether a value is the default for its value type. Ideally, I'd like to say: DoSomething<TValue>(TValue value) { if (value == default(TValue)) { ... } } However, the compiler complains that it can't do a == comparison on TValue and TValue. This is the best workaround that I've come up with so far: DoSomething<TValue>(TValue value) { if