Implicit type conversion: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|J}}: fix some archaic bits)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(7 intermediate revisions by 4 users not shown)
Line 1,494:
dict({1: 'one', 'two': (2+3j), ('RC', 3): None}) -> {1: 'one', 'two': (2+3j), ('RC', 3): None}
</pre>
===alternative===
One case of implicit casting and one quasi-case:
<syntaxhighlight lang="python">>>> 12/3 # Implicit cast from int to float, by / operator.
4.0
>>> (2+4j)/2 # But no implicit cast for complex parts.
(1+2j)
>>> (11.5+12j)/0.5 # Quasi-case, complex parts implicit cast from float to int.
(23+24j)
</syntaxhighlight>
 
=={{header|Racket}}==
Line 1,585 ⟶ 1,594:
@() pluralize
%() hashify</pre>
 
=={{header|RPL}}==
To make basic calculations easier, RPL accepts to mix floating-point real numbers and either complex numbers or binary integers when using <code>+ - * /</code> operations.
(2,3) 2 *
#37h 16 +
{{out}}
<pre>
2: (4,6)
1: #47h
</pre>
 
=={{header|REXX}}==
Line 1,687 ⟶ 1,706:
 
Defining a new type requires writing an extension to Tcl in [[C]] (or whatever the host programming language is, so [[Java]] for [[JTcl]]); the interfaces for doing this are not directly exposed to the Tcl script level because they require direct memory access, which Tcl normally does not permit in order to promote overall process stability.
 
=={{header|V (Vlang)}}==
A small primitive type can be automatically promoted if it fits completely into the data range of the type on the other side.
 
These are the allowed possibilities:
<syntaxhighlight lang="Vlang">
i8 → i16 → int → i64
↘ ↘
f32 → f64
↗ ↗
u8 → u16 → u32 → u64 ⬎
↘ ↘ ↘ ptr
i8 → i16 → int → i64 ⬏
</syntaxhighlight>
For example, an int value can be automatically promoted to f64 or i64, but not to u32.
 
Note- u32 would mean loss of the sign for negative values.
 
Promotion from int to f32, however, is currently done automatically (but can lead to precision loss).
<syntaxhighlight lang="Vlang">
u := u16(12)
x := f32(45.6)
a := 75
b := 14.7
c := u + a // c is of type `int` - automatic promotion of `u`'s value
println(c) // 87
d := b + x // d is of type `f64` - automatic promotion of `x`'s value
println(d) // 60.2999984741211
</syntaxhighlight>
 
=={{header|Wren}}==
Line 1,699 ⟶ 1,747:
 
However, you can define implicit conversions for user defined types. For example, BigInts can be generated from integral Nums or Strings and, when doing operations on BigInts, values of these types are automatically converted to BigInts so the operations can succeed.
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt
 
var b1 = BigInt.new(32)
Line 1,706 ⟶ 1,754:
var b3 = b1 + b2 + 2 + "2"
System.print(b3) // 100</syntaxhighlight>
 
=={{header|XPL0}}==
XPL0 doesn't have implicit type conversions. It only has two basic data
types: integer and real. Conversions must be done explicitly. For
example:
<syntaxhighlight lang "XPL0">
int I;
real R;
I:= fix(R);
R:= float(I);
</syntaxhighlight>
 
There is a third declaration type called 'character' (usually abbreviated
'char'). Its variables are the same as integers except when they are
indexed, as for arrays. An indexed character variable accesses bytes,
whereas an indexed integer variable accesses integers.
 
Strings are one-dimensional arrays consisting of bytes containing ASCII
characters.
 
Booleans are represented by integers. Zero is false, and non-zero is
true.
 
Real literals are distinguished from integers by including a decimal point or exponent. For example: 12. or 6e12
 
=={{header|Z80 Assembly}}==
9,476

edits