generics

Narrow inferred type when function has explicit return

前提是你 提交于 2021-02-09 10:53:14
问题 Look at the following code: type Shape = null | string | string[] | { [key: string]: string | string[] } interface ICfg<InShape extends Shape, OutShape extends Shape> { shapes?: readonly [InShape, OutShape] handle?(x: NonNullable<this["shapes"]>[0]): NonNullable<this["shapes"]>[1] } function apply<InShape extends Shape, OutShape extends Shape>(cfg: () => ICfg<InShape, OutShape>) { return cfg() } const shape = { type: "abc", date: "qwe", } var x = apply(() => ({ shapes: [shape, shape], handle:

How to overload S4 slot selector `@` to be a generic function

≯℡__Kan透↙ 提交于 2021-02-09 08:49:06
问题 I am trying to turn the @ operator in R into a generic function for the S3 system. Based on the chapter in Writing R extensions: adding new generic I tried implementing the generic for @ like so: `@` <- function(object, name) UseMethod("@") `@.default` <- function(object, name) base::`@`(object, name) However this doesn't seem to work as it breaks the @ for the S4 methods. I am using Matrix package as an example of S4 instance: Matrix::Matrix(1:4, nrow=2, ncol=2)@Dim Error in @.default

How do I make a structure generic in Rust without higher kinded type (HKT) support?

让人想犯罪 __ 提交于 2021-02-08 17:38:58
问题 I am trying to make the Iteratee structure generic so I can pass in a different parsing function and get an different Iteratee . This is the non-generic version that works: use std::io::{BufRead, BufReader}; use std::str::{from_utf8, Utf8Error}; #[derive(PartialEq, Debug)] struct Cat<'a> { name: &'a str, } fn parse<'a>(slice: &'a [u8]) -> Result<Cat<'a>, Utf8Error> { from_utf8(slice).map(|name| Cat { name: name }) } struct Iteratee<R> where R: BufRead + Sized { read: R, } impl<R> Iteratee<R>

Overloading operator for generics C# [duplicate]

谁都会走 提交于 2021-02-08 16:55:20
问题 This question already has answers here : C# Generic Operators (5 answers) Closed 5 years ago . I would like to create a procedure class that supports connecting to another procedure like so: a|b|c|d (this should result a procedure takes a's input type, and give d's output type) class Procedure<I,O> { public Func<I,O> func; public Procedure(Func<I, O> func) { this.func = func; } public static Procedure<I, O> operator| (Procedure<I, M> proc1, Procedure<M, O> proc2) { return new Procedure<I,O>(

Sorting an array of Integers with generics (Java)

与世无争的帅哥 提交于 2021-02-08 15:36:35
问题 I'm a second year computer science student currently working in Java and we recently started generics. I have an assignment where I've been given a list of sorting algorithms that use generics and am tasked with using them to sort a list of Integers (not primitive ints). As the sort classes use generics that extend Comparable I thought there would be no problems simply handing them the Integer array but the build output keeps coming up with incompatible types. The relevant code is below;

Scala: normal functions vs tupled functions?

淺唱寂寞╮ 提交于 2021-02-08 14:11:38
问题 What's the difference between these? I know that their type signatures are different, and that all functions start off normal and have to be .tupled to get their tupled form. What's the advantage of using un-tupled (but non-curried) functions? Especially because it seems to me that passing multiple arguments to a tupled function automagically unpacks them anyway, so by all appearances they are the same. One difference i see is that it forces you to have types for every number of function

Execute implicit cast at runtime

社会主义新天地 提交于 2021-02-08 13:35:40
问题 So I have a Generic class (it's mostly a container class) with implicit casting, like this: public class Container<T> { public T Value { get; set; } public static implicit operator T(Container<T> t) { return t.Value; } public static implicit operator Container<T>(T t) { return new Container<T>() { Value = t }; } } So in runtime I would like to cast an instance of Container<int> to int using reflection but cannot seem to find a way, I've tried the "Cast" method invoking mentioned in a couple

Explicit type parameters in let bindings in classes

给你一囗甜甜゛ 提交于 2021-02-08 13:14:35
问题 When I try to create some class like type MyType () = let func<'T> () = () The compiler says that there's an error: Explicit type parameters may only be used on module or member bindings But the MSDN says: A let binding at the module level, in a type, or in a computation expression can have explicit type parameters. A let binding in an expression, such as within a function definition, cannot have type parameters. Why documentation and compiler say different things? 回答1: This appears to be a

Explicit type parameters in let bindings in classes

我只是一个虾纸丫 提交于 2021-02-08 13:14:10
问题 When I try to create some class like type MyType () = let func<'T> () = () The compiler says that there's an error: Explicit type parameters may only be used on module or member bindings But the MSDN says: A let binding at the module level, in a type, or in a computation expression can have explicit type parameters. A let binding in an expression, such as within a function definition, cannot have type parameters. Why documentation and compiler say different things? 回答1: This appears to be a

Explicit type parameters in let bindings in classes

半世苍凉 提交于 2021-02-08 13:13:16
问题 When I try to create some class like type MyType () = let func<'T> () = () The compiler says that there's an error: Explicit type parameters may only be used on module or member bindings But the MSDN says: A let binding at the module level, in a type, or in a computation expression can have explicit type parameters. A let binding in an expression, such as within a function definition, cannot have type parameters. Why documentation and compiler say different things? 回答1: This appears to be a