Tag: type in​​ference

在C#中推断常量

在C#中,以下types推断的工作原理是: var s = "abcd"; 但是,为什么当variables是一个常量时不能推断出这个types呢? 下面抛出一个编译时exception: const var s = "abcd"; // <= Compile time error: // Implicitly-typed local variables cannot be constant

Java 8中exceptiontypes推断的一个特殊function

当在这个网站上编写另一个答案的代码时,我遇到了这个特点: static void testSneaky() { final Exception e = new Exception(); sneakyThrow(e); //no problems here nonSneakyThrow(e); //ERRROR: Unhandled exception: java.lang.Exception } @SuppressWarnings("unchecked") static <T extends Throwable> void sneakyThrow(Throwable t) throws T { throw (T) t; } static <T extends Throwable> void nonSneakyThrow(T t) throws T { throw t; } 首先,我很困惑为什么sneakyThrow调用对编译器是可以的。 当没有提及任何未经检查的exceptiontypes时,它推断T是什么types的? 其次,接受这个工作,为什么编译器会抱怨nonSneakyThrow调用呢? 他们看起来非常相似。

在C ++中用auto来声明variables是否有缺点?

似乎auto是一个相当重要的function,在C ++ 11中join,似乎遵循了很多新的语言。 就像Python这样的语言,我还没有看到任何显式的variables声明(我不确定是否有可能使用Python标准)。 使用auto声明variables而不是显式声明variables是否有缺点?

decltype和括号

我不明白FCD第148页示例的最后一行(§7.6.1.2/ 4): const int&& foo(); int i; struct A { double x; }; const A* a = new A(); decltype(foo()) x1 = i; // type is const int&& decltype(i) x2; // type is int decltype(a->x) x3; // type is double decltype((a->x)) x4 = x3; // type is const double& 为什么括号在这里有所作为? 它不应该简单地像上面那样是double吗?

genericstypes推断不适用于方法链接?

这在Java 7中无法编译: class Map<K,V> { static <K,V> Map<K,V> empty() {return null;} Map<K,V> put(K k, V v) {return null;} V get(K k) {return null;} } class A { static void f(Map<Integer,String> m){} public static void main(String[] args) { f(Map.empty()); } } 它不会推断从Map.empty()返回的Map的具体types: $ javac7 A.java A.java:10: error: method f in class A cannot be applied to […]

我如何定义Lisp在Haskell中的应用?

这个定义不应该像Haskell这样一个懒惰的语言被允许,在这个语言中,函数是被curry的吗? apply f [] = f apply f (x:xs) = apply (fx) xs 它基本上是一个函数,它将给定的函数应用于给定的参数列表,在Lisp中很容易完成。 有没有解决办法?

Javatypes推断:在Java 8中引用是不明确的,但不是Java 7

可以说我们有两个class。 一个空的Base类和这个Derived类的一个子类。 public class Base {} public class Derived extends Base {} 那么我们在另一个类中有几个方法: import java.util.Collection public class Consumer { public void test() { set(new Derived(), new Consumer().get()); } public <T extends Base> T get() { return (T) new Derived(); } public void set(Base i, Derived b) { System.out.println("base"); } public void set(Derived d, Collection<? extends […]

C ++ 11 auto关键字多less钱?

我一直在C ++ 11标准中使用新的auto关键字,用于复杂的模板types,这是我相信它的目的。 但我也使用它的东西,如: auto foo = std::make_shared<Foo>(); 更怀疑的是: auto foo = bla(); // where bla() return a shared_ptr<Foo> 我没有看到有关这个话题的很多讨论。 看起来auto可能被过度使用,因为types通常是一种文件和健全性检查。 你在哪里画线使用auto和什么是这个新function的推荐用例? 澄清:我不是要求哲学观点, 我要求标准委员会对这个关键词进行预期的使用,可能会对实际中如何实现这个预期的使用做出评论。 附注:这个问题被转移到SE.Programmers,然后回到堆栈溢出。 关于这个的讨论可以在这个元问题中find。

在C#中使用var关键字

在与同事讨论在C#3中使用“var”关键字之后,我想知道人们对通过var? 例如,我宁愿在可疑的情况下使用var,例如: – foreach(var item in someList) { // … } // Type of 'item' not clear. var something = someObject.SomeProperty; // Type of 'something' not clear. var something = someMethod(); // Type of 'something' not clear. var更合理的用法如下: var l = new List<string>(); // Obvious what l will be. var s = new SomeClass(); // […]