Implicit type conversion: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added XPL0 example.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(One intermediate revision by one other user not shown)
Line 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,718 ⟶ 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)
9,476

edits