Chose Return Type? is it possible?

BlitzMax Forums/BlitzMax Beginners Area/Chose Return Type? is it possible?

MOBii(Posted 2014) [#1]
I have a Method that look if it's a Int or a String and I have a desire to return it as a Int if it's a Int or String if it's a String

If _name[ .. 1 ] = "%" Return Int( Map.ValueForKey( _name ) )
Compile Error: Unable to convert from 'Object' to 'Int'


col(Posted 2014) [#2]
Hi,

In this scenario you can use a String to store an Int. You can coerce to an Int easily.

If _name[..1] = "%" Return Int(String(Map.ValueForKey(_name)))


MOBii(Posted 2014) [#3]
Method Get:Int( _name:String )
' If _name[ .. 1 ] = "%" Return Int( Map.ValueForKey( _name ) )
' Return String( Map.ValueForKey( _name ) )
Return Int( String ( Map.ValueForKey( _name ) ) )
End Method

Ja Now it's return the Int but all Sting return as 0


Brucey(Posted 2014) [#4]
I think you are complicating things... trying to store different types in a map like that is just going to make your life harder. How will you know what type is actually stored for a given name, other than sticking a "special" character at the end of the name?

Your other option could be to store different Objects in the map instead.
For example, you could store a String, a TInt type, etc.
Your TInt type might look like this :
Type TInt
  Field value:Int
End Type

Which is basically just a wrapper for the primitive type.

When you retrieve a value from the map, you simply cast it to find out what Type it is. An invalid cast will return Null, a successful cast will be your object of that type.

It also allows you to store more complex objects in the map, if required.

Perhaps you need to think more about what you are trying to achieve first?


MOBii(Posted 2014) [#5]
From the beginning I wanted to add script functions to Change this tile to, and Change all tile to

I see many nice thing I can include
Why text? I want the script to handle the NPC text conversations
I also want it to be my more advance .ini read/writer that I use for everything
I become addictive to .ini
If I can try to figure out how to make your dbmysql.mod to work then I think my old programming environment is toast

I think it's impossible to return different types from a Method so the Function that going use the variables need to know what type it going to handle
so no point for me to add % and $ to the script file
I get little damage use php where i can return what ever..

I need to Split to: GetInt and GetString


If I use the Map.ValueForKey( _name ) myself in the code I hopefully know what type it is

the mapfile is binary, but all none mapdata is readable and is show separately in the mapeditor


MOBii(Posted 2014) [#6]
map.Insert( _name, _data ) as I understand only save as String?


Henri(Posted 2014) [#7]
Nope. As was in my example map accepts any object (like string or your own type). Int's are not objects, they are primitive types.

If you want to use Integer then convert it to string like:
'To string
Local s:String = String(1)
Print s

'And back
local i:Int = Int(s)
Print i

...or wrap integer with your own type like in Brucey's example.

Of course, in your own type there can any number of variables and variable types inside (including any number of custom types as well). You can then pass this collection object to map.Insert-method.


-Henri


MOBii(Posted 2014) [#8]
Thank Henri
I didn't understand the fundamental of Type, I probably need to study it allot more.
I manage to read .ini now and save the type (Int/String) automatically

I shall try speed test to see how it compare to other .ini readers