Questions tagged [static-typing]
A programming language is said to use static typing when type checking is performed during compile-time as opposed to run-time.
358
questions
-3
votes
1
answer
46
views
Automatically converting .py file to .pyx using python type hints [closed]
Recently I had to convert a module written in pure python to pyx in in order to compile with cython.
The procedure for converting from py to pyx was very straight forward since the all the variables ...
0
votes
3
answers
35
views
How to define a `Class` object type conforming to a protocol?
Consider the following Objective-C protocol declaration, which requires only class methods:
@protocol TDWMethoding<NSObject>
+ (void)foo;
+ (void)bar;
@end
Assuming I need to return an ...
1
vote
1
answer
47
views
Sorbet does not check runtime type of an arg if it's an array
I have the following code that you can just copy&paste locally and run it as a single file:
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'sorbet-runtime'
end
...
0
votes
1
answer
22
views
What’s the point of mixed-type lists
Some languages like Python allow you to define mixed type lists:
mixed = [1, 'a', 2, 'b']
While other languages would require that all elements of a list be of the same type.
numbers = [1, 2]
letters ...
1
vote
1
answer
25
views
How can I create a TypeScript type that reduces an array of const objects to a const record object?
I want to write a TypeScript type that "reduces" an input array of objects to a record object, with the object keys taken from a property of the input array members.
This will be used for ...
1
vote
0
answers
93
views
How to define the same struct using Sorbet?
all!
I want to define a generic result data type, which is a union of a generic success type and a generic failure type.
The same thing in TS looks like this:
type Success<T> = {
value: T
}
...
1
vote
0
answers
17
views
Remove redundant intersections when flattening a complex record
I have a complex Flattened type that gives me a result that is right for me.
However the resulting type has a lot of duplicate intersections when, in the input record:
A key is isolated (in the ...
4
votes
1
answer
80
views
PHP: Restrict class type in function parameter
I have inherited some code like this
public static function instanciateRepository( $repositoryClass, ... ) {
...
new $repositoryClass( ... );
}
Where $repositoryClass is a class type ...
0
votes
1
answer
52
views
Non-relational database in statically typed languages (rethinkdb, Scala)
I'm still pretty new to Scala and arrived at some kind of typing-roadblock.
Non-SQL databases such as mongo and rethinkdb do not enforce any scheme for their tables and manage data in json format. I'...
0
votes
1
answer
185
views
What is the correct type annotation for a function that returns a Sequence of the same type as one of its inputs?
For the Python code
from typing import TypeVar, Sequence, Iterator
S = TypeVar("S", bound=Sequence)
def chunk(data: S) -> S:
return data[:]
mypy 0.971 reports the error
simple_8....
-3
votes
1
answer
75
views
how should be var type used appropriately in c# ? I'm trying to write my first code using var [closed]
I am new to C# and I'm trying to write my first code using var type .
as you see below ,I make a array of integers (arr1) then I tried to make a new var type and refer my previous array (arr1 --> ...
1
vote
2
answers
114
views
Why is pointer/reference to array allowed to not specify the array size? [duplicate]
This may be a bad question as I'm really new to the concept of statically-typed language and arrays in C++ and may have misunderstood these concepts...
I know that C++ is a statically-typed language, ...
0
votes
1
answer
81
views
Avoid duplication of null type checking in Dart
My current goal is to remove this code duplication:
final int? myNullableInt = 10;
/// Everywhere I need to do this null verification:
if (myNullableInt == null) return null;
return ...
0
votes
1
answer
576
views
error: Incompatible return value type (got "Tuple[Tuple[float, float], ...]", expected "List[Any]")
Context
While trying to implement static typing, I'm experiencing some difficulties in specifying the return type of the following function:
def create_hexagon_coordinates(origin: tuple, unit: float, ...
0
votes
2
answers
317
views
Python typing valid boolean combinations
I'm trying to use Python's type checker to catch incompatible permissions. I have a permission situation where a particular action can be permitted (boolean) and required (boolean). Obviously, I want ...
0
votes
0
answers
168
views
Mypy constraints for relationship between two generic type values?
When a generic class has two generic-type-values(like E and S in BaseRepository in the example below), is it possible to apply a constraint that ensures a certain relationship between the two types?
...
0
votes
0
answers
70
views
Is it possible to have static type checking on custom inner types in child-classes?
Consider the example below; I'm looking for a way to provide proper type-hints for the set method that makes it possible for child-classes of Properties to specify their own set of valid properties ...
0
votes
0
answers
168
views
Using type hints to denote subclass of multiple classes, i.e. `typing.Intersection`?
I am looking for but unable to find a concise way to denote the type hint obj: "subclass of A and B". The type hint obj: typing.Union[A, B] does not cover this case as it will accept an ...
1
vote
2
answers
178
views
How to use __subclasshook__ with Mypy?
How come that under Mypy, __subclasshook__ works for one-trick ponies from collections.abc, but not for user-defined classes?
For instance, this program
from collections.abc import Hashable
class A:
...
0
votes
1
answer
612
views
Why do I need to cast the value of the Rx object on initialization?
Consider this code:
Rx<Widget> rxwidget = const Text("test").obs;
rxwidget.value = Container();
This will throw the following error on runtime
Expected a value of type 'Text', but got ...
4
votes
1
answer
134
views
Homogenize a heterogenous list with a funtion that has a particular kind of context
I am fiddling with the basics of type-level programming in Haskell, and I was trying to write a function that "homogenizes" a heterogeneous list using a function with a context of kind (* -&...
1
vote
1
answer
622
views
How to specify any/unknown/variant type in GDScript?
Is there a way to specify an unknown type in GDScript?
GDScript docs use the type Variant for this (for example in Array.count method).
Say, I'd like to write an identity function. I can do it like so:...
1
vote
1
answer
44
views
Interface with default key value types that can be extended TypeScript
In many of my projects, I have a common pattern which repeats over and over again.
type RequestStatus =
| 'pending'
| 'requesting'
| 'successful'
| 'failed'
type AValue = string | undefined
...
0
votes
0
answers
77
views
ClassVar inside generic class with same name as the parametrizing variable
I have a generic class definition which declares a ClassVar that happens to have the same name as the parametrizing TypeVar. I'd like to keep them the same name. However, mypy complains about this for ...
1
vote
2
answers
223
views
Get rid of type assertion when checking if value is part of union
I have a union type which is partially based on an array - which I also want to use to check values at run-time. However, TypeScript forces me to use a type-assertion here.
Consider the code below:
...
0
votes
0
answers
200
views
mypy not properly parsing a file of constants
I have a Python project that uses a common file for constants, consts.py, that all of the code in the project uses and these same constants should be available to any process that instantiates the ...
1
vote
1
answer
362
views
Typescript: Type alias results in strange type when returning object with same keys but different values [duplicate]
I implemented a function (let's call it valuesOfThings) that takes an object with arbitrary key names and values of a specific object type Thing (effectively a Map). The function should then return an ...
0
votes
1
answer
80
views
What is a good way to get find object types when writing statically typed python?
What is a good way to find the type of a more complex python object when trying to write statically typed python?
For example, I think I've got the type annotation of x correct here:
from itertools ...
0
votes
0
answers
55
views
How can I get the same performance from Cython as I am getting from Numba?
I am trying to speed up my function in python. My original function is:
def SMO(X, y, C, epsilon, tol, max_iter):
#Initialization
n = X.shape[0]
alpha = np.zeros((n))
b ...
1
vote
0
answers
174
views
Mypy type narrowing from return values
I'm looking at the following typed Python example:
from __future__ import annotations
from typing import Optional
class Thing:
def does_intersect(self, other: Thing) -> bool:
return ...
2
votes
1
answer
1k
views
How do you ensure an object conforms to a Typescript interface?
how I could validate that an object has all the properties of an interface.
For example:
interface Vehicle {
name: string
model: string
}
interface User {
Name: string
age: númber
}
interface ...
0
votes
2
answers
58
views
I have an interface with 200+ properties. How can a class implement this interface without declaring all 200+ again?
I have an interface with 200+ properties and a class that uses these same properties.
interface MyProperties {
property1: string
property2: string
...
property232: string
}
class MyClass {
...
6
votes
0
answers
5k
views
MyPy: Can not suppress [no-untyped-call]
I have been using MyPy python type analysis, I am trying to ignore a specific type warning,
Here is my mypy.ini
[mypy]
python_version = 3.9
# Needed because of bug in MyPy
disallow_subclassing_any = ...
0
votes
2
answers
1k
views
What is the type of `async let` declaration in Swift?
Swift 5.5 introduces coroutines, hierarchical task management and function suspension.
One of the new features is a syntax of
async let image = downloadImage(url)
The declaration of image can be ...
0
votes
1
answer
610
views
How can I import an enum through an intermediate TS file?
I file A I have:
export enum MyFluffyEnum { Beauty, Courage, Love }
In file B I have:
import { MyFluffyEnum } from "./A";
export type { MyFluffyEnum };
In file C I have:
import { ...
3
votes
1
answer
314
views
How to efficiently use Python-style integers in Cython?
I am working in Cython, and I need to use Python's int type, not C's int type. How do I declare a Cython variable to be a Python-style integer?
cdef int x will use a C-style integer, not a Python-...
0
votes
0
answers
190
views
Why does Java's Dynamic Proxies need reflection?
Java's Dynamic Proxy Docs describe these constructors as the following:
A dynamic proxy class is a class that implements a list of interfaces specified at runtime such that a method invocation ...
2
votes
4
answers
563
views
Type 'A | B' is not assignable to type 'A & B'
Why does this code not compile?
type A = { n: number }
type B = { s: string }
type Thing = {
a: A
b: B
}
function update(obj: Thing, path: keyof Thing) {
obj[path] = obj[path]
}
I would expect ...
0
votes
1
answer
795
views
Dart list of exactly two types
Is it possible in Dart to have a list of exactly two types like you can in TypeScript? For example, something like this would compile:
List<int|String> foo = [
1,2,3,4,'baz'
];
while this ...
2
votes
1
answer
1k
views
How to test if a class satisfies a concept in C++?
In rust one explicitly types impl Trait for Object which guarantees that Object will have this trait. Now C++20 concepts are of course a bit more general as they are not associated to just one type ...
0
votes
0
answers
51
views
how to take out flowtype from react Js code?
I've integrated flow-bin v0.122.0 and used it in my react+redux project. now i want to run my project thus need react code without flow type. how do i do that ? in the documentation related to react ...
3
votes
1
answer
82
views
Can C++ pattern of making structs to 'correspond' types be emulated in Haskell (template specialization)?
template <typename T>
struct Corresponding;
template <>
struct Corresponding<int> {
using CorrespondingT = boost::multiprecison::cpp_int;
};
template <typename T> using ...
1
vote
0
answers
154
views
In JS, function that requires parameter, when calling that function without that parameter or no parameter, it works, why?
I've been doing some experiments about high-order functions and decided to understand how to make an custom addEventListener function so to understand what's under the hood.
As you can see, it ...
2
votes
2
answers
737
views
Performance comparison between groupBy + join vs window func Spark
These two achieve almost the same (the only diff the order of rows):
import org.apache.spark.sql.expressions._
import org.apache.spark.sql.functions._
def windowSpec = Window.partitionBy("key&...
3
votes
1
answer
368
views
Can I restrict the types of a subclass in Python?
Let's say I want to do define wrapper classes on sets and lists that add some useful methods, like this:
from abc import ABC
class AbstractGizmo(ABC):
def bloviate(self):
print(f"...
2
votes
0
answers
158
views
Overload method and call super
I have a class Animal with a method foo, whose return type depends on the value of inplace.
Likewise, for the subclass Cat.
So far, so good. But, if I call super().foo in Cat, I get an error.
from ...
5
votes
3
answers
1k
views
overload following optional argument
I have a class Animal with a method foo which has different return types according to a boolean parameter inplace which follows an optional parameter bar. I'd like to overload the function so that the ...
0
votes
1
answer
1k
views
No overload variant of "foo" of "Animal" matches argument type "str"
I have a file t.py which has a class Animal and a subclass Cat. Both have the method foo, which has different return types based on the value of the boolean inplace.
Here's the file's compelte code:
# ...
1
vote
1
answer
37
views
Issues with handling union vs intersection Typescript
I'm trying to build out an interface with a couple methods, then calling those methods based on an incoming piece of data being one of the keys of that object.
Here is my type:
type LogicalFunction<...
1
vote
1
answer
428
views
Python type checking numpy arrays include their dtype
I can verify my function receives inputs in the correct type using:
def foo(x: np.ndarray, y: float):
return x * y
Making sure if I try to use this function with x that is not a np.ndarray I will ...