Odin » Forums » casting allot
adekto
8 posts
#10867 casting allot
6 months, 1 week ago

Hi still trying to understand the language but i updated the game.odin to work with v0.0.6b.

Most of the compatibility issue is "as" to "cast()", not sure why we even casting this much
im mostly casting to a type that is the same if you dig down into the code a little

Its probably just the library's but converting types the hole time doesn't sound very efficient

gingerBill
Ginger Bill
211 posts
2 projects

I am ginger thus have no soul.

#10869 casting allot
6 months, 1 week ago Edited by Ginger Bill on Feb. 12, 2017, 11:42 a.m.

This is due to the type system. When you make a type definition, you don't make an alias of the type (like C/C++) but create a completely different type.

1
2
3
4
5
6
My_Int :: int;
// My_Int != int
a: My_Int;
b: int;
c := a + b; // Not valid as `a` and `b` are different types.
c := a + cast(My_Int)b;


I've thought about aliases (not just types) but they are not very clear and don't really give that much benefit.

The exception to this rule are procedures types which have a slightly different rules for sanity sake.
adekto
8 posts
#10873 casting allot
6 months, 1 week ago

Oke, so im going to sound stupid, but why is useful?
i do not see why we need all these types that are technically the same type and we end up converting them about everywhere
not even mentioning the increase library incompatibility and complexity

also don't you mean this on line 6?
1
c := cast(int)a + b;


im not saying to get rid of it, just why the we relying on them so much in all the current code

gingerBill
Ginger Bill
211 posts
2 projects

I am ginger thus have no soul.

#10876 casting allot
6 months, 1 week ago

Thank you, that was a typo in my "code".

Having distinct types is quite useful. It allows for better error messages, type safety, and showing intent. In practice you will not need to do this amount of casting because, you should only make a new type of something simple (like an integer, float, etc) if you really need to.

A short example would be something like time
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Duration :: i64;
Nanosecond:  Duration : 1;
Microsecond: Duration : 1000 * Nanosecond;
Millisecond: Duration : 1000 * Microsecond;
Second:      Duration : 1000 * Millisecond;
Minute:      Duration : 60 * Second;
Hour:        Duration : 60 * Minute;

d := 12 * Second;
fmt.println(d/time.Millisecond) // 12000


This is really personal opinion at the end of the day and I prefer distinct types compared to type aliases.
adekto
8 posts
#10877 casting allot
6 months, 1 week ago

Ok i think i finally got it. sorry for the inconvenience