Callum Slater

Callum Slater

1550033386

Cannot cast to type alias

I created a type alias gp for the PostgreSQL type money, and I can't figure out from the documentation how I'm supposed to use this.

I've set up a test table (and database):

CREATE TYPE gp AS (amt money);
CREATE TABLE test (val gp PRIMARY KEY);

But no matter how I try to insert rows, it fails

INSERT INTO test VALUES (1); -- column "val" is of type gp but expression is of type integer
INSERT INTO test VALUES ((1)); -- column "val" is of type gp but expression is of type integer
INSERT INTO test VALUES (CAST (1 AS gp)); -- cannot cast type integer to gp
INSERT INTO test VALUES (CAST (CAST (1 AS money) AS gp)); -- cannot cast type money to gp
INSERT INTO test VALUES ((amt=1)); -- column "amt" does not exist
INSERT INTO test VALUES ((amt=1)::gp); -- column "amt" does not exist

From what I understand, the gp type should be an alias: gp::money::numeric, so why can't it cast from money to an alias of money?

#sql #postgresql

What is GEEK

Buddha Community

Callum Slater

1550038385

You should use a row constructor:

insert into test values (row(1));

Per the documentation:

The key word ROW is optional when there is more than one expression in the list.
Note, that a composite type with a single component makes a little sense. You can use a domaininstead.

Macey  Legros

Macey Legros

1600075200

Java Type Casting Example | Type Casting In Java Tutorial

Let’s say you want to assign the value of one data type into another variable of another data type, so there might be a case that they are not compatible with each other. To overcome this problem, we have one solution that we are going to learn today, called ‘Type Casting’ In Java, if both data types are compatible with each other, then it automatically has done its conversion, which is known as ‘Automatic type conversion.’

Java Type Casting

Typecasting is used to convert an object or variable of one type into another.

There are two types of typecasting in Java they are:

  1. Implicit Type Casting, known as Widening Type Casting
  2. Explicit Type Casting, also known as Narrowing Type Casting

We will learn about these two types of typecasting.

#java #java type casting #type casting

Arvel  Parker

Arvel Parker

1593156510

Basic Data Types in Python | Python Web Development For Beginners

At the end of 2019, Python is one of the fastest-growing programming languages. More than 10% of developers have opted for Python development.

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

Table of Contents  hide

I Mutable objects

II Immutable objects

III Built-in data types in Python

Mutable objects

The Size and declared value and its sequence of the object can able to be modified called mutable objects.

Mutable Data Types are list, dict, set, byte array

Immutable objects

The Size and declared value and its sequence of the object can able to be modified.

Immutable data types are int, float, complex, String, tuples, bytes, and frozen sets.

id() and type() is used to know the Identity and data type of the object

a**=25+**85j

type**(a)**

output**:<class’complex’>**

b**={1:10,2:“Pinky”****}**

id**(b)**

output**:**238989244168

Built-in data types in Python

a**=str(“Hello python world”)****#str**

b**=int(18)****#int**

c**=float(20482.5)****#float**

d**=complex(5+85j)****#complex**

e**=list((“python”,“fast”,“growing”,“in”,2018))****#list**

f**=tuple((“python”,“easy”,“learning”))****#tuple**

g**=range(10)****#range**

h**=dict(name=“Vidu”,age=36)****#dict**

i**=set((“python”,“fast”,“growing”,“in”,2018))****#set**

j**=frozenset((“python”,“fast”,“growing”,“in”,2018))****#frozenset**

k**=bool(18)****#bool**

l**=bytes(8)****#bytes**

m**=bytearray(8)****#bytearray**

n**=memoryview(bytes(18))****#memoryview**

Numbers (int,Float,Complex)

Numbers are stored in numeric Types. when a number is assigned to a variable, Python creates Number objects.

#signed interger

age**=**18

print**(age)**

Output**:**18

Python supports 3 types of numeric data.

int (signed integers like 20, 2, 225, etc.)

float (float is used to store floating-point numbers like 9.8, 3.1444, 89.52, etc.)

complex (complex numbers like 8.94j, 4.0 + 7.3j, etc.)

A complex number contains an ordered pair, i.e., a + ib where a and b denote the real and imaginary parts respectively).

String

The string can be represented as the sequence of characters in the quotation marks. In python, to define strings we can use single, double, or triple quotes.

# String Handling

‘Hello Python’

#single (') Quoted String

“Hello Python”

# Double (") Quoted String

“”“Hello Python”“”

‘’‘Hello Python’‘’

# triple (‘’') (“”") Quoted String

In python, string handling is a straightforward task, and python provides various built-in functions and operators for representing strings.

The operator “+” is used to concatenate strings and “*” is used to repeat the string.

“Hello”+“python”

output**:****‘Hello python’**

"python "*****2

'Output : Python python ’

#python web development #data types in python #list of all python data types #python data types #python datatypes #python types #python variable type

Ray  Patel

Ray Patel

1623121920

Python typing and validation with mypy and pydantic.

Python is a dynamically typed programming language, which means the types are only checked at runtime and a variable is allowed to change its type over its lifetime, whereas a statically typed language like Java checks the types at compile-time, and a variable is not allowed to change its type over its lifetime. On the other hand, Python is a strongly typed language because the types cannot be automatically converted at runtime. For example, you cannot have an addition calculation on integer 1 and string "2", while in a weakly typed language such as JavaScript such calculation is allowed.

Even though dynamic typing can make it faster to write Python code in the development stage, it is also very easy to introduce bugs and errors which can only be identified at runtime. Besides, with no type definitions, the code can be more difficult to read and maintain. For example, you need to read through a function to get to know what type of data would be returned by it. However, with type hints or type annotations, the return type of a function can be known immediately. Once a program is developed, you would rarely need to rewrite or redesign it. However, it is much more common that you or your colleagues need to read or maintain it after some time. Therefore, making the code easier to read would be very important, especially if you work in a team where people have to review each other’s code.

Typing has become more and more important in Python and the type hint standards introduced in PEP484 make it possible and easy to add type annotations to your Python code. After type hints have been added to a Python file, the mypy library can be used to do static type checking before it is run. Besides, pydantic, a data validation library using Python type annotations, can enforce type hints at runtime and provide user-friendly errors when data is invalid.

#python #mypy #pydantic #typing #type-hints #python typing and validation with mypy and pydantic.

Python Type-Casting and Type-Conversion with examples - Python

In this tutorial of Python Type-Casting and Type-Conversion, you’ll learn to transform variables in python through the using of built-in type-casting functions such as int()str() etc.

Typecasting relates to the conversion of a variable of one primitive type to another primitive type. Most programming languages contain data-types such as integer, float, string, boolean also built-in keywords are available to convert them to other datatypes such as converting string type to integer type.

#python #learn python #python type-casting

Ari  Bogisich

Ari Bogisich

1595762400

Stretching the Reach of Implicitly Typed Variables in C#

Image for post

Type inference is a common feature among mainstream programming languages. The functional ones, like ML and Haskell, are pioneers in exploring this programming paradigm where the declaration of a variable or a function may omit a type annotation. Today, even classic imperative languages such as C++ offer, to a certain extent, a kind of type inference.

C# is not an exception to the ever-increasing popularity of inference-enabled languages: since C# 3.0, developers may declare implicitly typed variables with the var keyword (in C++ 11, a similar feature was introduced — re-targeting the then-existing auto keyword). This functionality, however, falls under a provision: a declaration must be accompanied by an initializer.

Although one would not expect to see ML and Haskell’s “whole-program type inference” in C#, could this initializer requirement be relaxed? Why does it exist, altogether?

  • Is it due to any limitation or just a matter of taste?
  • Could the variable’s type be inferred from arbitrary expressions?
  • If yes, what are trade-offs from a language-design perspective?

In this article, I will describe the foundation of a standard type inference technique and ramble about the questions above.

Non-initializing Declarations

What do you answer when someone asks: why is it necessary, in C#, that a local var-declared variable is accompanied with an _initializer? _The accurate reply to this question is: because the language specification says so; it is not because the type would not be inferable otherwise. Consider function f.

The two statements above (a declaration- and an expression-statement) are not fundamentally different from the single statement var num = 42;. In fact, if permitted, the _Intermediate Language _(IL) code produced for the former would probably be equal to that produced for the latter. Also, one may easily observe that int is a valid replacement for var in f.

To set the stage for our discussion, let us quote — with minor adjustments for pedagogical reasons — the relevant fragment of the C# specification to which var is subject:

“When the local variable type is specified as var… the declaration is an implicitly typed local variable declaration, whose type is inferred from the type of the associated initializer expression.”

Another characteristic of C# is that a variable must be definitely assigned before we attempt to obtain its value. Let us also quote the language specification fragment that states such mandatory assignment.

“For an initially unassigned variable to be considered definitely assigned… , an assignment to the variable must occur in every possible execution path…”

It is not my intent to enter the pedantic realm of language specifications. Yet, based on our (informal) observation that var num; num = 42; is equivalent to var num = 42;; and the fact that a variable must be assigned before being used, could we slightly stretch the functionality provided by var?

Image for post

#type-systems #hindley-milner #type-theory #type-inference #csharp #programming-c