typechecking

Is it possible to use custom type definitions in an ES6 project?

不打扰是莪最后的温柔 提交于 2020-05-10 04:26:04
问题 My team works on a relatively large NodeJS project, written in ES6, transpiled by babel, and then deployed as AWS lambdas with Serverless. This project is focused around consuming, mapping/transforming, and outputting one specific object type, which we have defined. Our problem is, ECMA/JavaScript is not strongly typed, so if we make a mistake like treating a field as an array somewhere and a string somewhere else, there's nothing to catch that except runtime errors. We have also poorly

Sum types - Why in Haskell is `show (Int | Double)` different than `(show Int) | (show Double)`

末鹿安然 提交于 2020-04-12 09:57:06
问题 Why are these not equivalent? show $ if someCondition then someInt else some double and if someCondition then show someInt else show someDouble I understand that if you isolate the if ... else part in the first example to an expression by itself then you can't represent its type with an anonymous sum type, the kind of Int | Double , like something you could do easily in TypeScript (mentioning TypeScript because it is the langauge I used often and that supports Sum types), and would have to

Is there ar preferable way to create type aliases for compound types with Python's typing module?

限于喜欢 提交于 2020-04-11 04:38:10
问题 I have a function with one parameter, which should take an int or a None as argument. There are several ways to create a type alias for such a compound type: # test.py import typing IntOrNone_1 = typing.TypeVar('IntOrNone_1', int, None) IntOrNone_2 = typing.Union[int, None] def my_func1(xyz: IntOrNone_1): return xyz def my_func2(xyz: IntOrNone_2): return xyz my_func1(12) my_func1(None) my_func1(13.7) my_func1('str') my_func2(12) my_func2(None) my_func2(13.7) my_func2('str') Both methods do

How to use static assert in C to check the types of parameters passed to a macro

不打扰是莪最后的温柔 提交于 2020-04-05 05:28:12
问题 I need to write a C macro that checks to ensure all parameters passed to it are unsigned and of the same integer type. Ex: all input params are uint8_t , or all uint16_t , or all uint32_t , or all uint64_t . Here is how this type of checking can be done in C++: Use static_assert to check types passed to macro Does something similar exist in C, even if only by way of a gcc extension? Note that static asserts are available in gcc via _Static_assert . (See my answer here: Static assert in C).

Check a variable against Union type at runtime in Python 3.6

末鹿安然 提交于 2020-03-13 07:14:22
问题 I'm trying to write a function decorator that uses Python 3.6 type hints to check that a dictionary of arguments respects the type hints and if not raise an error with a clear description of the problem, to be used for HTTP APIs. The problem is that when the function has a parameter using the Union type I can't check a variable against it at runtime. For example, I have this function from typing import Union def bark(myname: str, descr: Union[int, str], mynum: int = 3) -> str: return descr +

Type-checking Pandas DataFrames

感情迁移 提交于 2020-02-27 06:55:17
问题 I want to type-check Pandas DataFrames i.e. I want to specify which column labels a DataFrame must have and what kind of data type ( dtype ) is stored in them. A crude implementation (inspired by this question) would work like this: from collections import namedtuple Col = namedtuple('Col', 'label, type') def dataframe_check(*specification): def check_accepts(f): assert len(specification) <= f.__code__.co_argcount def new_f(*args, **kwds): for (df, specs) in zip(args, specification): spec

Kotlin - Generic Type Parameters Not Being Respected

不问归期 提交于 2020-01-22 14:33:22
问题 Consider the following example: import kotlin.reflect.KProperty1 infix fun <T, R> KProperty1<T, R>.test(value: R) = Unit data class Foo(val bar: Int) fun main() { Foo::bar test "Hello" } Given that test expects a value of type R , why in this context, where the property type is Int , does it allow me to pass a String ? 回答1: First, take a look at the declaration of the interface KProperty1 , which is: interface KProperty1<T, out R> : KProperty<R>, (T) -> R The important part here is the out

Guidance on very shallow embedding VHDL in AGDA

孤者浪人 提交于 2020-01-15 08:57:12
问题 for my project in Programming Languages I am doing a very shallow and simple embedding VHDL digital circuits in agda. The aim is to write the syntax, static semantics, dynamic semantics and then write some proofs to show our understanding of the material. Up till now I have written the following code: data Ckt : Set where var : String → Ckt bool : Bool → Ckt empty : Ckt gate : String → ℕ → ℕ → Ckt -- name in out series : String → Ckt → Ckt → Ckt -- name ckt1 ckt2 parallel : String → Ckt → Ckt

Guidance on very shallow embedding VHDL in AGDA

[亡魂溺海] 提交于 2020-01-15 08:57:02
问题 for my project in Programming Languages I am doing a very shallow and simple embedding VHDL digital circuits in agda. The aim is to write the syntax, static semantics, dynamic semantics and then write some proofs to show our understanding of the material. Up till now I have written the following code: data Ckt : Set where var : String → Ckt bool : Bool → Ckt empty : Ckt gate : String → ℕ → ℕ → Ckt -- name in out series : String → Ckt → Ckt → Ckt -- name ckt1 ckt2 parallel : String → Ckt → Ckt

How to check if a value is an integer in Javascript (special case 1.0 should be float)

好久不见. 提交于 2020-01-11 13:10:08
问题 I am writing some type check functions. I want: isInteger isFloat While writing isInteger, I noticed that isInteger(1.0) returns true. I want this to return false. My function is like this: function isInteger(value) { return typeof value === 'number' && value % 1 === 0; } I also noticed that it return true for 1.0 because the argument 'value' is immediately converted to 1 (which I don't want). I did this: function isInteger(value) { console.log(value) // It prints 1 if value is 1.0 return