typing

Clean way to map objects to other objects?

大城市里の小女人 提交于 2019-12-01 19:29:56
Is there a good way to map objects to other objects? Library recommendations welcome too. For example, say I have these classes: export class Draft { id: number; name: string; summary: string; } export class Book { id: number; name: string; info: Info; } export class Info { value: string; } Traditionally, to map fields from Draft to Book I'd have to do so manually for each field: export class Book { [...] fromDraft(Draft draft) { this.id = draft.id; this.name = draft.name; this.info = new Info(); this.info.value = draft.summary; } } Is there an easier way to map object fields that are named

Typescript return type depending on parameter

偶尔善良 提交于 2019-12-01 18:31:38
I am trying to write a function which takes a parameter of type boolean and returns one of two types, depending on the value of the input. I have found two approaches: function dependsOnParameter<B extends boolean>(x: B): B extends true ? number : string { if (x) { return 3; } else { return "string"; } } Here, TypeScript says that Type '3'/'"string"' is not assignable to type 'B extends true ? number : string'. My other approach looks like this: function dependsOnParameter(x: true): number; function dependsOnParameter(x: false): string; function dependsOnParameter(x: boolean): number | string

Strange behaviour with Object.intValue()

不想你离开。 提交于 2019-12-01 14:46:07
I am struggling with a problem, which I can't understand why it doesn't work. How do I pass a variable through the double obj and convert to int ? Why does it not work in the top code snippet, but it works in the bottom code snippet below the line? The only difference seems to be adding an extra variable, which is also typed as a double ? //Converting double to int using helper //This doesn't work- gets error message //Cannot invoke intValue() on the primitive type double double doublehelpermethod = 123.65; double doubleObj = new Double( doublehelpermethod); System.out.println("The double

Strange behaviour with Object.intValue()

南笙酒味 提交于 2019-12-01 13:08:57
问题 I am struggling with a problem, which I can't understand why it doesn't work. How do I pass a variable through the double obj and convert to int ? Why does it not work in the top code snippet, but it works in the bottom code snippet below the line? The only difference seems to be adding an extra variable, which is also typed as a double ? //Converting double to int using helper //This doesn't work- gets error message //Cannot invoke intValue() on the primitive type double double

What is the PHP best practice for using functions that return true or false?

怎甘沉沦 提交于 2019-12-01 05:53:45
After playing with PHP, I discovered that true is returned as 1 and false as null. echo (5 == 5) // displays 1 echo (5 == 4) // displays nothing When writing functions that return true or false, what are the best practices for using them? For example, function IsValidInput($input) { if ($input...) { return true; } else { return false; } } Is this the best way to use the function? if (IsValidInput($input)) { ... } How would you write the opposite function? IsBadInput($input) { return ! IsValidInput($input); } When would you use the === operator? After playing with PHP, I discovered that true is

Array of a generic class with unspecified type

南楼画角 提交于 2019-12-01 03:44:58
Is it possible in C# to create an array of unspecified generic types? Something along the lines of this: ShaderParam<>[] params = new ShaderParam<>[5]; params[0] = new ShaderParam<float>(); Or is this simply not possible due to C#'s strong typing? It's not possible. In the case where the generic type is under your control, you can create a non-generic base type, e.g. ShaderParam[] params = new ShaderParam[5]; // Note no generics params[0] = new ShaderParam<float>(); // If ShaderParam<T> extends ShaderParam My guess is that this is an XNA type you have no control over though. You could use a

Converting OCaml to F#: Differences between typing and type inference

断了今生、忘了曾经 提交于 2019-12-01 03:39:43
In researching type inference differences between F# and OCaml I found they tended to focus on nominative vs. structural type system . Then I found Distinctive traits of functional programming languages which list typing and type inference as different traits. Since the trait article says OCaml and F# both use Damas-Milner type inference which I thought was a standard algorithm, i.e. an algorithm that does not allow for variations, how do the two traits relate? Is it that Damas-Milner is the basis upon which both type inference systems are built but that they each modify Damas-Milner based on

Why is GHCi typing this statement oddly?

别说谁变了你拦得住时间么 提交于 2019-12-01 00:17:17
问题 In answering a question on stackoverflow, I noticed that GHCi (interactive) is assigning a too-restrictive type in a let statement. Namely, given the code, import Control.Arrow f = maximum &&& id >>> fst &&& (\(m,l) -> length $ filter (==m) l) (as on my answer to https://stackoverflow.com/questions/6281813/maximum-of-list-and-count-of-repeat-maximum-number/6283594#6283594), if one inserts a "let" before f and enters this in ghci, it gives the following type information Prelude Control.Arrow>

Cython: Should I use np.float_t rather than double for typed memory views

时光总嘲笑我的痴心妄想 提交于 2019-12-01 00:12:23
问题 Concerning memoryviews in cython, is there any advantage of typing a view with NumPy types such as np.float_t instead of simply do double if I'm working with numpy float arrays? And should I type the cdef then the same way, doing e. g. ctypedef np.float64_t np_float_t ... @cython.profile(False) @cython.wraparound(False) @cython.boundscheck(False) cdef np_float_t mean_1d(np_float_t [:] v) nogil: cdef unsigned int n = v.shape[0] cdef np_float_t n_sum = 0. cdef Py_ssize_t i for i in range(n): n

How do you simulate typing using jQuery?

亡梦爱人 提交于 2019-11-30 19:31:25
Like how the click() can be used to trigger a click event on an element, is there any way to simulate the typing of a string? Are you looking for a way to trigger the events associated with typing or do you want to append text to a container so that it looks like someone is typing? If you're looking for the events, then @Nick is absolutely correct in suggesting the methods for triggering the event. If the latter, you'll want to use a timer to replace the text/html of the container with successive substrings of your text. You might be able to get a better effect by appending successive span