天道酬勤,学无止境

type-inference

What is the original type of interpolated string?

问题 MSDN 文档包含有关隐式转换的部分: var s = $"hello, {name}"; System.IFormattable s = $"Hello, {name}"; System.FormattableString s = $"Hello, {name}"; 从第一个字符串开始,插值字符串的原始类型是string 。 好的,我可以理解,但是……我意识到字符串没有实现IFormattable 。 所以它看起来像是来自编译器的一些魔法,类似于它对 lambdas 所做的事情。 现在猜测这段代码的输出: void Main() { PrintMe("Hello World"); PrintMe($"{ "Hello World"}"); } void PrintMe(object message) { Console.WriteLine("I am a " + message.GetType().FullName); } //void PrintMe(string message) //{ // Console.WriteLine("I am a string " + message.GetType().FullName); //} void PrintMe(IFormattable message) { Console.WriteLine("I am a " +

2022-05-14 13:30:04    分类:技术分享    c#   string   type-inference   overloading   string-interpolation

Haskell type inference for lambda functions (in map) [duplicate]

问题 这个问题在这里已经有了答案: 什么是单态限制? (1 个回答) 5年前关闭。 示例 1 以下没有类型声明的定义将引发错误: f :: Eq t => (t,t) -> Bool -- omiting this line will result in an error f = \(x,y) -> x==y (我知道这个函数可以写得更短,但这不是重点。) 示例 2 另一方面,在使用map的函数中使用相同的 lambda 函数确实可以工作而不会产生错误: g l = map (\(x,y) -> x==y) l (仅作为说明: g [(3,4),(5,5),(7,6)]将产生[False,True,False] 示例 3 以下代码也非常好,它似乎与上面的原始f完全相同。 在这里,类型推断似乎有效。 f' (x,y) = x==y 问题 所以我的问题是:为什么我们在第一种情况下需要类型声明,而在第二种和第三种情况下不需要? 回答1 如果您使用: {-# LANGUAGE NoMonomorphismRestriction #-} f = \(x,y) -> x==y 你不会得到错误。 更新 Haskell Wiki page on Monomorphism Restriction (link) 提供了一些关于为什么这些定义被区别对待的细节: f1 x = show x f2 = \x

2022-05-13 22:36:19    分类:技术分享    haskell   lambda   type-inference

Implicit parameters break type inference or inference does not suffice for their resolution?

问题 在我添加隐式排序证据之前,类型推断在此示例中运行良好。 类型推断规则(从左到右和跨参数列表)似乎得到了满足,但是关于隐式的某些东西打破了它。 case class Insert[I, O : Ordering](arg: I) def execute[I,O](req: Insert[I,O]): O = null.asInstanceOf[O] val result: Int = execute(Insert("test")) Error:(5, 39) diverging implicit expansion for type Ordering[O] starting with method Tuple9 in object Ordering lazy val result: Int = execute(Insert("test"));} ^ 这可以编译并正常工作: case class Insert[I, O](arg: I) def execute[I,O](req: Insert[I,O]): O = null.asInstanceOf[O] val result: Int = execute(Insert("test")) 因此,任何一种类型推断都不足以进行隐式解析,或者隐式解析会破坏类型推断。 我猜想O类型是推断出来的,但是当隐式解析发生时,它认为它是

2022-05-09 18:35:09    分类:技术分享    scala   type-inference   typeclass   implicits

Is there a syntax does a reversed type inference?

问题 当我试图回答这个问题时: 是否可以摆脱 Service 类中的 TClient 泛型类型 我发现了一个奇怪的用法,我从来没有设计过这种不可编译的语法,下面是我遇到的一个代表: interface IGeneric<T> { } partial class SomeClass { // won't compile public static void SomeMethod<U>(Action<T> d) where T: IGeneric<U> { } } 即使声明为: class Concrete: IGeneric<object> { } partial class SomeClass { public static void SomeMethod<U>(Action<IGeneric<U>> d) { // compiles } } 不会使以下代码可编译: var d=default(Action<Concrete>); SomeClass.SomeMethod(d); // won't compile 我不知道在不涉及两个类型参数的情况下可以使用的语法。 所以我想知道这种反向类型推断是否有语法? 还是解决方法? 回答1 简单回答是不。 这甚至与类型推断无关——它与类型约束有关。 您只能添加约束在同一声明中引入的类型参数。 所以这: public static void

2022-05-05 09:20:04    分类:技术分享    c#   generics   types   type-inference

Inferred return type when passing function by template

问题 我的问题是关于让编译器根据模板传递的函数的返回类型来推断函数的返回类型。 有什么方法可以称为 foo<bar>(7.3) 代替 foo<double, int, bar>(7.3) 在这个例子中: #include <cstdio> template <class T, class V, V (*func)(T)> V foo(T t) { return func(t); } int bar(double j) { return (int)(j + 1); } int main() { printf("%d\n", foo<double, int, bar>(7.3)); } 回答1 如果你想保持bar作为模板参数,恐怕你只能接近: #include <cstdio> template<typename T> struct traits { }; template<typename R, typename A> struct traits<R(A)> { typedef R ret_type; typedef A arg_type; }; template <typename F, F* func> typename traits<F>::ret_type foo(typename traits<F>::arg_type t) { return func(t); } int

2022-05-05 05:27:07    分类:技术分享    c++   templates   function-pointers   type-inference

Java - Generics vs Casting Objects

问题 我有一个类Data<T> 具有通用属性 private T value; 有没有更好的方法来做以下事情? 即以不同的形式返回泛型类型? public List<String> getValues() { if (value.getClass() != ArrayList.class) throw new Exception("Wrong Enum value '%s'", value); return (ArrayList<String>) value; //ugly } public String getStringValue() { if (value.getClass() != String.class) throw new Exception("Wrong value type '%s'", value); return (String) value; //ugly } public Float getFloatValue() { if (value.getClass() != Double.class) throw new Exception("Wrong value type '%s'", value); return (Float) value; //ugly } public Long getLongValue() { if (value.getClass()

2022-05-03 21:48:23    分类:技术分享    java   generics   casting   gson   type-inference

Behaviour of context bounds and implicit parameter lists with regards to path dependent types

I always thought that context bounds and implicit parameter lists behaved exactly the same, but apparently not. In the example below, I expect summon1[Int] and summon2[Int] to return the same type, but they don't. I expected summon2[Int] to return a path dependent type, but instead it gives me a type projection. Why? Welcome to the Ammonite Repl 2.2.0 (Scala 2.13.3 Java 11.0.2) @ trait Foo[A] { type B def value: B } defined trait Foo @ implicit def fooInt = new Foo[Int] { override type B = String override def value = "Hey!" } defined function fooInt @ implicit def fooString = new Foo[String] {

2022-05-03 06:57:21    分类:问答    scala   type-inference   implicit   path-dependent-type   type-projection

Haskell type inference for lambda functions (in map) [duplicate]

This question already has an answer here: What is the monomorphism restriction? (1 answer) Closed 5 years ago. Example 1 Following definition without the type declaration will throw an error: f :: Eq t => (t,t) -> Bool -- omiting this line will result in an error f = \(x,y) -> x==y (I know this function can be written shorter, but this is not the point here.) Example 2 On the other hand using the same lambda function in a function using map does work without producing an error: g l = map (\(x,y) -> x==y) l (Just as an illustration: g [(3,4),(5,5),(7,6)] will produce [False,True,False] Example

2022-05-03 00:14:44    分类:问答    haskell   lambda   type-inference

How do generic types get inferred in Java?

The Function.identity() returns a function, Function<T, T> that always returns its input argument(i.e. identity function). But being a static method, how does it know which concrete argument to return in place of type parameter T when it doesn't even take any inputs? Illustration of my thought process: Map idToPerson = people.collect( Collectors.toMap( (person -> person.getID() , Function.identity() ) ); Question: So how does the compiler figure out that Function.identity() is supposed to return Function<element of 'people' stream, element of 'people' stream> stream despite having no inputs

2022-05-01 19:18:32    分类:问答    java   generics   java-stream   type-inference

JDK 11.0.2 compilation fails with javac NPE on anonymous parameterized class type inference

Code (spring-web 5.1.2) public static void main(String[] args) { RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.set(HttpHeaders.AUTHORIZATION, "token"); HttpEntity<Object> requestEntity = new HttpEntity<>(headers); ResponseEntity<Object> test = restTemplate.exchange( "https://example.com", HttpMethod.GET, new HttpEntity<>(headers), new ParameterizedTypeReference<>() { // fails here }); } OracleJDK 1.8 (expected output) cannot infer type arguments for org.springframework.core.ParameterizedTypeReference reason: cannot use '<>' with anonymous

2022-05-01 15:24:58    分类:问答    java   javac   type-inference   java-11   compiler-bug