TOPIC: Symbol data type question
#397
Symbol data type question 3 Years, 10 Months ago
What it means in the user's manual when it says (about Symbol data type): "Normally you do not have to worry about symbols - they are used automatically"?
Maybe it means that if I write:

Code:

ob = Object(age: 23)
m = 'age'
ob[m]



this is automatically converted in:

Code:

ob = Object(age: 23)
m = #age
ob[m]



??
 
 
Mauro
 
#399
Re:Symbol data type question 3 Years, 10 Months ago
That is correct. In some languages (like Ruby) "age" would be different from #age (:age in Ruby) but in Suneido they are interchangeable.

ob[#age] will be slightly faster than ob["age"] because the symbol lookup will be done at compile-time rather than at run-time, but this is not worth worrying about most of the time.

When you write Object(age: or #(age: the symbol lookup is also done at compile time.
 
 
andrew
 
#401
Re:Symbol data type question 3 Years, 10 Months ago
Sorry, but now I'm a little more confused than before. You said that ob[#age] will be slightly faster than ob["age"] because the symbol lookup will be done at compile-time rather than at run-time. This means that this optimization is not automatic, but it is the coder that should have to do it, by using the Symbol value rather than the String value, right?
 
 
Mauro
 
#402
Re:Symbol data type question 3 Years, 10 Months ago
For some reason my last post didnt make it..
Try doing a Disasm() on the creation of each ob[#age].Disasm() , ob["age"].Disasm() and checkout the byte code.. You should see the difference
 
 
#404
Re:Symbol data type question 3 Years, 10 Months ago
Thank you zippy, for your suggestion.
From the Disasm() it results that ob['age'] generates different bytecode from ob[#age]... Then, it appears that Suneido compiler does not optimize automatically this situation. So, my question remains: on the user's manual it is written that the symbols are used automatically... When would be that they are used automatically? From this example it appears that this is not the case...
 
 
Mauro
 
#406
Re:Symbol data type question 3 Years, 10 Months ago
You are right, ob['age'] could easily be optimized automatically by the compiler.

However, normally in this case you would use ob.age which does use a symbol.

You would normally use ob[...] with a variable e.g. ob[m] in which case you can not optimize at compile time.
 
 
andrew
 
#410
Re:Symbol data type question 3 Years, 10 Months ago
Ok, now it is more clear. Thanks!
 
 
Mauro