generics

How do I write an extension method for a generic type with constraints on type parameters?

我的梦境 提交于 2020-01-23 04:28:10
问题 I'm working with a task specific .NET plattform, which is precompiled and not OpenSource. For some tasks I need to extend this class, but not by inheriting from it. I simply want to add a method. At first I want to show you a dummycode existing class: public class Matrix<T> where T : new() { ... public T values[,]; ... } I want to extend this class in the following way: public static class MatrixExtension { public static T getCalcResult<T>(this Matrix<T> mat) { T result = 0; ... return result

Inheriting generic can't up cast

耗尽温柔 提交于 2020-01-23 04:05:39
问题 I have a problem with inheriting a custom generic. The compiler (Visual Studio for Mac), complaints that the inheriting generic can't be implicitly converted to another, inheriting type. A break down of my code is as follows: I have an interface called IAnsweredCommon and another interface that inherits from it called IAnsweredAndroid public interface IAnsweredCommon and public interface IAnsweredAndroid : IAnsweredCommon I have two other classes that use these interfaces. public abstract

Inheriting generic can't up cast

我的未来我决定 提交于 2020-01-23 04:05:26
问题 I have a problem with inheriting a custom generic. The compiler (Visual Studio for Mac), complaints that the inheriting generic can't be implicitly converted to another, inheriting type. A break down of my code is as follows: I have an interface called IAnsweredCommon and another interface that inherits from it called IAnsweredAndroid public interface IAnsweredCommon and public interface IAnsweredAndroid : IAnsweredCommon I have two other classes that use these interfaces. public abstract

Foreach struct weird compile error in C#

限于喜欢 提交于 2020-01-23 01:49:51
问题 namespace MyNamespace { public struct MyStruct { public string MyString; public int MyInt; public bool MyBool; } public class MyClass { private List<MyStruct> MyPrivateVariable; public List<MyStruct> MyVariable { get { if (MyPrivateVariable == null) { MyPrivateVariable = new List<MyStruct>(); MyPrivateVariable.Add(new MyStruct()); MyPrivateVariable.Add(new MyStruct()); } return MyPrivateVariable; } } public void MyLoop() { foreach (MyStruct ms in MyVariable) { // Doesn't compile, but it works

Passing a channel of things as a channel of interfaces in Go

人走茶凉 提交于 2020-01-23 01:43:13
问题 My program has a pipeline structure, and I just implemented a caching filter that sends stuff directly to output if the already processed version of data is in the cache. func Run(in chan downloader.ReadyDownload) chan CCFile { out := make(chan CCFile) processQueue := make(chan downloader.ReadyDownload) go cache.BypassFilter(in, processQueue, out) // writes the cached, already processed version to out if it exists // otherwise redirects the input to processQueue go process(processQueue, out)

Are static members of a generic class different for different types in Java?

耗尽温柔 提交于 2020-01-22 22:52:27
问题 @Spence asked this Previous Question. So, how's that work in Java? Generic types are discarded at runtime in Java, so what happens to static variables of classes instantiated with different generic types? 回答1: Static members in Java can't have generic type arguments from the class that contains them. public class Gen<T> { public static T foo; // compiler error } 回答2: Static variables are shared among all the instances of that type, even of different type parameters. From the generics tutorial

how to say “same class” in a Swift generic

心不动则不痛 提交于 2020-01-22 22:50:11
问题 If a Swift generic type constraint is a protocol name, I can require that two types, constrained to that protocol, be the same type. For example: protocol Flier {} struct Bird : Flier {} struct Insect: Flier {} func flockTwoTogether<T:Flier>(f1:T, f2:T) {} The function flockTwoTogether can be called with a Bird and a Bird or with an Insect and an Insect, but not with a Bird and an Insect. That is the limitation I want. So far, so good. However, if I try the same thing with a class name, it

Mapped types in Scala

一曲冷凌霜 提交于 2020-01-22 20:04:51
问题 Is there a way to derive a type from an existing one in Scala? For example, for case class Person(name: String, age: Int) I'd like to get a Product / Tuple of (Option[String], Option[Int]) , i.e. a type mapped from an existing one. There's a feature in Typescript (mapped types) that allows this relatively easily, which is how I started thinking down this path. But I'm not sure how something like this would be done in Scala. I feel like the solution involves using shapeless in some way but I'm

extracting data from typing types

微笑、不失礼 提交于 2020-01-22 19:10:48
问题 I am having some issues working with the typing types in Python for any more than type hinting: >>> from typing import List >>> string_list = ['nobody', 'expects', 'the', 'spanish', 'inqusition'] >>> string_list_class = List[str] Now I would like to Check if string_list conforms to string_list_class . Check if string_list_class is a list. If so, check the class, that string_list_class is a list of. I find myself unable to achieve any of those: >>> isinstance(string_list, string_list_class)

C#: Call non-generic method from generic method

一世执手 提交于 2020-01-22 17:44:16
问题 class CustomClass<T> where T: bool { public CustomClass(T defaultValue) { init(defaultValue); // why can't the compiler just use void init(bool) here? } public void init(bool defaultValue) { } // public void init(int defaultValue) will be implemented later } Hello. This seems to be a simple question, but I couldn't find an answer on the Internet: Why won't the compiler use the init method? I simply want to provide different methods for different types. Instead it prints the following error