Type Conversion and ToString() in FluxSharp
Type System Overview
FluxSharp is a statically-typed language with strong type checking. However, it provides automatic and explicit conversion mechanisms through the ToString() function and type casting.
Primitive Types
FluxSharp supports the following primitive types:
Type | Size | Range | Example |
|---|---|---|---|
| 64-bit | -2^63 to 2^63-1 | , , |
| 32-bit | ยฑ3.4Eยฑ38 | , |
| 64-bit | ยฑ1.7Eยฑ308 | , |
| Variable | N/A | , |
| 1-bit | true/false | , |
Implicit Type Conversion Rules
Numeric Type Hierarchy
int โ float โ double
The compiler automatically converts "smaller" types to "larger" types when mixing types in operations:
int a = 10;
float b = 3.14f;
double result = a + b; // int converted to double, float to double
// result = 13.14
Rules
int + float โ float
int + double โ double
float + double โ double
Mixing with string โ String concatenation or ToString() required
Explicit Conversion via ToString()
Converting to String
From Integer
int value = 42;
string str = value.ToString();
print(str); // Output: 42
From Float
float pi = 3.14f;
string str = pi.ToString();
print(str); // Output: 3.14
From Double
double e = 2.71828;
string str = e.ToString();
print(str); // Output: 2.71828
From String (Identity)
string text = "Hello";
string result = text.ToString();
// result = "Hello" (unchanged)
ToString() in Print Statements
The most common use of ToString() is for formatted output:
int x = 100;
int y = 200;
print("X = ");
print(x.ToString());
print("Y = ");
print(y.ToString());
print("Sum = ");
int sum = x + y;
print(sum.ToString());
// Output:
// X =
// 100
// Y =
// 200
// Sum =
// 300
Advanced Type Conversion Scenarios
Converting Between Numeric Types
Integer Arithmetic with Float Input
int a = 10;
float b = 3.14f;
// Automatic conversion happens
float result = a + b; // = 13.14
// For integer results, truncate explicitly
int truncated = (int)a; // = 10
Function Argument Type Matching
Math functions require type-consistent arguments:
int a = 10;
int b = 20;
int maxint = max(a, b); // โ
Works: both int
float x = 3.14f;
float y = 2.71f;
float maxfloat = max(x, y); // โ
Works: both float
// โ Type Error: different types
int bad = max(a, x); // ERROR: mixing int and float
toString() with Variables
public class Person {
public int age;
public string GetAge() {
return age.ToString();
}
}
public class Main {
public void main() {
Person p = new Person();
p.age = 25;
string ageStr = p.GetAge(); // "25"
print("Age: ");
print(ageStr);
}
}
Custom toString() Implementation in Classes
Classes can override toString() to provide meaningful string representations:
public class Point {
public int x;
public int y;
public string ToString() {
return "Point"; // Simplified version
// Full implementation would concatenate: "Point(x, y)"
}
}
public class Person {
public string name;
public int age;
public string ToString() {
return "Person"; // Returns type name
}
}
public class Main {
public void main() {
Point p = new Point();
print("Object: ");
print(p.ToString()); // Output: Point
Person person = new Person();
print("Object: ");
print(person.ToString()); // Output: Person
}
}
Compilation-Time Type Checking
FluxSharp performs strict type checking at compile time:
// โ Type Error
int x = 3.14f; // Cannot assign float to int
// โ
Works: float can hold int implicitly
float y = 42; // Implicit conversion
// โ
Works: toString() provides explicit conversion
string z = 42.ToString(); // Explicit to string
toString() Performance Characteristics
Compile-Time Evaluation
When ToString() is called with compile-time constants, the conversion happens during compilation:
// Compile-time (no runtime cost)
string pi = 3.14.ToString();
// Runtime (minimal cost)
double value = readinput();
string str = value.ToString();
Memory Efficiency
toString() creates new string objects
No caching of converted strings (each call creates new string)
Consider reusing converted strings if called frequently:
// Less efficient: converts twice
print(value.ToString());
print(value.ToString());
// More efficient: convert once
string str = value.ToString();
print(str);
print(str);
Type Safety Examples
Correct Usage
int a = 10;
int b = 20;
int sum = a + b; // โ
int + int = int
float x = 3.14f;
float y = 2.71f;
float product = x y; // โ
float float = float
string result = sum.ToString() + product.ToString();
// โ
string + string = string
Error Cases
// โ Cannot mix types without conversion
int bad1 = 3.14f + 10; // ERROR
// โ Cannot assign incompatible types
float bad2 = "3.14"; // ERROR
// โ toString() on unknown type
string bad3 = someunknown.ToString(); // ERROR: undefined
// โ
Explicit conversion
int value = (int)3.14f; // Explicit cast
string str = value.ToString(); // Explicit to string
Working with Strings
String Concatenation Pattern
int x = 42;
string message = "The answer is: " + x.ToString();
print(message); // Output: The answer is: 42
Building Formatted Output
int hours = 14;
int minutes = 30;
string time = hours.ToString() + ":" + minutes.ToString();
print(time); // Output: 14:30
Summary
Operation | Example | Result |
|---|---|---|
int โ string | | |
float โ string | | |
double โ string | | |
string โ string | | |
Mixed numeric | | |
Type error | | Compilation error |
Best Practices
Always use ToString() for string conversion - It's the standard way in FluxSharp
Group conversions with their use - Convert immediately before printing or concatenating
Reuse converted strings - Store result if using multiple times
Use meaningful class toString() - Override ToString() in custom classes for debugging
Prefer type-safe operations - Keep types consistent when possible
Next Features
Planned enhancements to the type conversion system:
Format strings:
value.ToString("F2")
for precision control
Parse functions:
int.Parse("42")
for string to type conversion
Implicit operator overloading in custom classes
Type inference improvements