inferred-type

Use function interface to ensure parameters but infer more specific return type

可紊 提交于 2021-01-29 06:13:13
问题 I have a function signature that I need a bunch of functions to follow which is something like this: type ActionCallback<R = any> = (param1: SpecificType, param2: OtherType) => Promise<R> Basically the types for the parameters are well defined and it must return a promise but what that promise resolves to is up to the function. Instead of having to specify the type of both arguments in every callback I'd like to just specify the variable conforms to ActionCallback so the parameters types are

Conditional Implicit Definitions Scala [duplicate]

你说的曾经没有我的故事 提交于 2020-05-17 05:51:10
问题 This question already has answers here : Understanding implicit in Scala (7 answers) Closed last month . I have to solve this quinz but i can't find the correct answer. trait Physics { implicit def air: Gaz, implicit def condense(implicit gaz: Gaz): Liquid, implicit def freeze(implicit liquid: Liquid): Solid implicitly[Solid] } Can you rewrite the last line with the inferred parameter explicitly written? Hint: It should look like implicitly[Solid](... Thank you so much! 回答1: Here is a hint:

Inferring types and using type annotations when parsing a string to a number

三世轮回 提交于 2020-01-04 06:23:08
问题 I'm trying to write a function that will parse float from given string. It should return error in case of wrong or negative value. fn read_value(strvalue: &str) -> Result<f32, Error> { match FromStr::from_str(strvalue) { None => Err(Error::InvalidValue), Some(value) => if value >= 0.0 {Ok(value)} else {Err(Error::InvalidValue)} } } This code gives: src/main.rs:50:27: 50:32 error: the type of this value must be known in this context src/main.rs:50 Some(value) => if value >= 0.0 {Ok(value)}

Why does the Streams API need a hint for generic type in this case?

大兔子大兔子 提交于 2019-12-29 08:45:12
问题 The following fails to compile: @NotNull String defaultFormatter(@Nullable Object value) { if (value instanceof Collection) { return ((Collection) value).stream() .map(MyClass::defaultFormatter) .collect(Collectors.joining(eol)); } return String.valueOf(value); } In particular, when compiled with javac, the error would be: Error:(809, 94) java: incompatible types: java.lang.Object cannot be converted to @org.jetbrains.annotations.NotNull java.lang.String But the following compiles just fine:

Java inferred generic types

柔情痞子 提交于 2019-12-24 00:15:39
问题 I'm looking for a similar concept of inferring captured generic types, similar to the following method snippet, however instead for a class that captures generic types: public <X, Y, Z> static void someMethod(ObjectInterface<X, Y, Z> object) { // code that uses inferred generic type parameters X, Y and Z... } The code in this snippet will capture types and assign them to the generic parameter types X , Y and Z . This allows the use of the generic type variables inside the body of the code and

Unboxing to unknown type

不想你离开。 提交于 2019-12-10 13:09:40
问题 I'm trying to figure out syntax that supports unboxing an integral type (short/int/long) to its intrinsic type, when the type itself is unknown. Here is a completely contrived example that demonstrates the concept: // Just a simple container that returns values as objects struct DataStruct { public short ShortVale; public int IntValue; public long LongValue; public object GetBoxedShortValue() { return ShortVale; } public object GetBoxedIntValue() { return IntValue; } public object

Inferred Generic Type on React Component

a 夏天 提交于 2019-12-10 11:09:53
问题 Typescript is pretty good about inferred generic types. For example, if I write the following code: class AwesomeClass<T> { constructor(public val: T) { // ... } public getVal(): T { return this.val; } } const inst = new AwesomeClass("Hello World"); const result = inst.getVal(); result is automatically typed to string . Nifty! I'd like to take that a step further with React. If I make the follow component interface IProps<T> { value: T; onClick: (result: T) => void; } interface IState { }

Why does the IEnumerable<T>.Select() works in 1 of 2 cases ? Can not be inferred from usage

元气小坏坏 提交于 2019-12-01 18:14:17
I get this error message: The type arguments for method 'System.Linq.Enumerable.Select<TSource,TResult>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,int,TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. The first method has no problems using a IEnumerable<T>.Select() ? Where is the problem with the 2nd method? private void GetPupilsForSchoolclass() { ObservableCollection<PupilViewModel> pupilsOC = new ObservableCollection<PupilViewModel> ( _adminRepo.GetPupilsBySchoolclassId(_selectedSchoolclass.SchoolclassId).Select(p => new

Why does the Streams API need a hint for generic type in this case?

我的梦境 提交于 2019-11-29 14:49:38
The following fails to compile: @NotNull String defaultFormatter(@Nullable Object value) { if (value instanceof Collection) { return ((Collection) value).stream() .map(MyClass::defaultFormatter) .collect(Collectors.joining(eol)); } return String.valueOf(value); } In particular, when compiled with javac, the error would be: Error:(809, 94) java: incompatible types: java.lang.Object cannot be converted to @org.jetbrains.annotations.NotNull java.lang.String But the following compiles just fine: @NotNull String defaultFormatter(@Nullable Object value) { if (value instanceof Collection) { Stream

c++ template parameter type inference

我与影子孤独终老i 提交于 2019-11-27 05:25:55
I have such a template in C++ template<typename T, T* P> struct Ptr {}; so I can use it as such: const int i = 0; Ptr<int, &i> ptr; or Ptr<decltype(i), &i> ptr; But I don't want to specify the type int or identity i twice, I want to use just Ptr<&i> ptr; and let the compiler figure out the int type part by itself. How can I declare my template to do that ? I've read this question but the answer is using macros, that's not nice: template of template c++? can I do this by just template without macros ? I'm using Visual C++ 2013. UPDATE c++17 introduced " P0127R2 Declaring non-type template