Implicit type conversion: Difference between revisions
Content added Content deleted
MaiconSoft (talk | contribs) (Added Delphi documentation for Implicit type conversion) |
|||
Line 374: | Line 374: | ||
A IntDouble can be assigned with value literal(s): 0 255 3.0 |
A IntDouble can be assigned with value literal(s): 0 255 3.0 |
||
A ComplexDouble can be assigned with value literal(s): true 'x' 0 255 1L 2.0f 3.0 4.0L 10_000_000_000L </pre> |
A ComplexDouble can be assigned with value literal(s): true 'x' 0 255 1L 2.0f 3.0 4.0L 10_000_000_000L </pre> |
||
=={{header|Delphi}}== |
|||
Delphi has in implicit conversions for primitive numerical types. Any value can be implicitly converted to a value of a larger type. |
|||
Upsize conversions: |
|||
<lang Delphi> |
|||
ValueByte : Byte := 2; // (8 bits size) |
|||
ValueWord : Word := ValueByte; // 2 (16 bits size) |
|||
ValueDWord : DWord := ValueWord; // 2 (32 bits size) |
|||
ValueUint64 : Uint64 := ValueWord; // 2 (64 bits size) |
|||
</lang> |
|||
Unsigend-signed conversions |
|||
<lang Delphi> |
|||
ValueDWord := 4294967295; // 4294967295 (Max DWord value (unsigned)) |
|||
ValueInteger : Integer := ValueDWord; // -1 (two complement conversion) (signed) |
|||
ValueDWord := ValueInteger; // 4294967295 (convert back, unsigned) |
|||
</lang> |
|||
Unsigned variables will not convert literal negative values |
|||
<lang Delphi> |
|||
ValueByte := -1; // this not work, and raise a error |
|||
ValueByte := byte(-1); // this work, and assign 255 (max byte value) |
|||
</lang> |
|||
Float points can convert integers values, may lose precision for large integers |
|||
<lang Delphi> |
|||
ValueDWord := 4294967295; // 4294967295 (Max DWord value (unsigned)) |
|||
ValueDouble : Double := ValueDWord; // 4.29496729500000E+0009 |
|||
</lang> |
|||
Integers can not convert float values implicity |
|||
<lang Delphi> |
|||
ValueDouble := 1.6; |
|||
ValueByte := ValueDouble; // this not work, and raise a error |
|||
ValueByte : Trunc(ValueDouble); // this work, and assign 1 |
|||
ValueByte : Round(ValueDouble); // this work, and assign 2 |
|||
</lang> |
|||
Strings can convert chars, but not convert back |
|||
<lang Delphi> |
|||
ValueChar: Char := #10; |
|||
ValuesString: String := ValueChar; |
|||
</lang> |
|||
Boolean can not convert any type implicity, except it self |
|||
<lang Delphi> |
|||
ValueBoolean: Boolean := True; // this work, and assign True |
|||
ValueBoolean: Boolean := 1; // this not work, and raise a error |
|||
ValueByte := ValueBoolean; // this not work, and raise a error |
|||
ValueByte := Byte(ValueBoolean); // this work, and assign 1 |
|||
ValueBoolean := Boolean(10); // this work, and assign true |
|||
ValueBoolean := Boolean(-1); // this work, and assign true |
|||
ValueBoolean := Boolean( 0); // this work, and assign false |
|||
</lang> |
|||
Variant types can implicit convert allmost types, and convert back, if possible: |
|||
<lang Delphi> |
|||
ValueVariant: Variant := -20; |
|||
ValueVariant := 3.1416; |
|||
ValueVariant := 'string'; |
|||
ValueVariant := #10; |
|||
ValueVariant := True; |
|||
... |
|||
</lang> |
|||
Class and Record can implement implicit operator |
|||
<lang Delphi> |
|||
program Implicit_type_conversion; |
|||
{$APPTYPE CONSOLE} |
|||
{$R *.res} |
|||
uses |
|||
System.SysUtils; |
|||
type |
|||
TFloatInt = record |
|||
value: Double; |
|||
class operator Implicit(a: Double): TFloatInt; |
|||
class operator Implicit(a: Integer): TFloatInt; |
|||
class operator Implicit(a: TFloatInt): Double; |
|||
class operator Implicit(a: TFloatInt): Integer; |
|||
class operator Implicit(a: TFloatInt): string; |
|||
class operator Implicit(a: string): TFloatInt; |
|||
end; |
|||
{ TFloatInt } |
|||
class operator TFloatInt.Implicit(a: Double): TFloatInt; |
|||
begin |
|||
Result.value := a; |
|||
end; |
|||
class operator TFloatInt.Implicit(a: Integer): TFloatInt; |
|||
begin |
|||
Result.value := a; |
|||
end; |
|||
class operator TFloatInt.Implicit(a: TFloatInt): Double; |
|||
begin |
|||
Result := a.value; |
|||
end; |
|||
class operator TFloatInt.Implicit(a: TFloatInt): Integer; |
|||
begin |
|||
Result := Round(a.value); |
|||
end; |
|||
class operator TFloatInt.Implicit(a: string): TFloatInt; |
|||
begin |
|||
Result.value := StrToFloatDef(a, 0.0); |
|||
end; |
|||
class operator TFloatInt.Implicit(a: TFloatInt): string; |
|||
begin |
|||
Result := FloatToStr(a.value); |
|||
end; |
|||
procedure Print(s: string); |
|||
begin |
|||
Writeln(s); |
|||
end; |
|||
var |
|||
val:TFloatInt; |
|||
valInt:Integer; |
|||
begin |
|||
// implicit from double |
|||
val := 3.1416; |
|||
// implicit to string |
|||
Print(val); // 3.1416 |
|||
// implicit to integer |
|||
valInt := val; |
|||
Writeln(valInt); // 3 |
|||
// implicit from integer |
|||
val := valInt; |
|||
// implicit to string |
|||
Print(val); // 3 |
|||
readln; |
|||
end.</lang> |
|||
=={{header|Déjà Vu}}== |
=={{header|Déjà Vu}}== |
||