Implicit typecasts? How?

raorn

Member
Looks like mafia does implicit typecats. For example, in

Code:
int n = 5;
print("n = " + n);

n in assignment will be autoconverted to_int().

Can anyone describe these rules? When mafia does implicit typecasts and when it bails with "can's assign XXX to YYY type"?

I may admit, that mafia can assign (add, multiple) variable of type XXX to type YYY if it's possible to convert XXX to YYY in one operation, e.g. YYY to_YYY(XXX) function exsist. If it's true, will it work with complex types (arrays, maps, records) and does user-defined functions supported?
 

macman104

Member
[quote author=raorn link=topic=1646.msg7707#msg7707 date=1206509703]n in assignment will be autoconverted to_int().[/quote]?? The only thing I see is that mafia is able to call a to_string() on various non-string types. Why do you say in assignment it will be autoconverted to_int()?
If it's true, will it work with complex types (arrays, maps, records) and does user-defined functions supported?
I don't quite understand what you mean here, can you rephrase?
 

raorn

Member
[quote author=macman104 link=topic=1646.msg7708#msg7708 date=1206518082]
?? The only thing I see is that mafia is able to call a to_string() on various non-string types. Why do you say in assignment it will be autoconverted to_int()?[/quote]
Typo. I mean print("n = " + n.to_string());. Looks like

I don't quite understand what you mean here, can you rephrase?

Can I shorten constructions like "int sk = $skill[CLEESH].to_int()" to "int sk = $skill[CLEESH]" in general? For non-builtin types this would look like:

Code:
record range {
  int max;
  int min;
};

string to_string(range r)
{
  return "(" + r.min + "-" + r.max + ")";
}

range a;
a.min =1;
a.max = 2;

print(a);
print(a.to_string());

First example gives me "Cannot store skill in sk of type int", first print in second example gives me "record range" text. So it casts variables to strings only and real conversion to string is done only for simple built-in types. This feature could be handy, however...
 
Top