1. What's the point of map:map type if load_map() messes it up?

I have

map:map new_game = map:load_map( filename & ".cgm" ) 

but if map:load_map() returns -1 (due to file not existing), I get a type error (type_check failure, new_game is -1). So, the definition of type map is useless! Do I have to resort to this:

map:map new_game 
object maybe_a_map_maybe_not = map:load_map( filename & ".cgm" ) 
 
if map:map( maybe_a_map_maybe_not ) then 
  new_game = maybe_a_map_maybe_not 
else 
  msg( "No saved games. Start a new game." ) 
end if 

Maybe if load_map() fails we can return an empty map (which is not accurate, but better than failing)? or in the type definition we include the possibility that map:map can be -1?

new topic     » topic index » view message » categorize

2. Re: What's the point of map:map type if load_map() messes it up?

euphoric said...

I have

map:map new_game = map:load_map( filename & ".cgm" ) 

but if map:load_map() returns -1 (due to file not existing), I get a type error (type_check failure, new_game is -1). So, the definition of type map is useless! Do I have to resort to this:

Depends. This error only happens when type checking is on, which is supposed to be for development, since it can slow your code down. If you think this is likely, like when you ask a user for a random file that may or may not be legit, then you'll probably be testing it anyways.

Matt

new topic     » goto parent     » topic index » view message » categorize

3. Re: What's the point of map:map type if load_map() messes it up?

mattlewis said...
euphoric said...

I have

map:map new_game = map:load_map( filename & ".cgm" ) 

but if map:load_map() returns -1 (due to file not existing), I get a type error (type_check failure, new_game is -1). So, the definition of type map is useless! Do I have to resort to this:

Depends. This error only happens when type checking is on, which is supposed to be for development, since it can slow your code down. If you think this is likely, like when you ask a user for a random file that may or may not be legit, then you'll probably be testing it anyways.

Matt

It's funny you mention it, because I don't think I ever turn type checking off. :) Do translated programs automatically have type checking off?

Anyway, I've added a check to make sure the file exists.

Thank you!

new topic     » goto parent     » topic index » view message » categorize

4. Re: What's the point of map:map type if load_map() messes it up?

euphoric said...

It's funny you mention it, because I don't think I ever turn type checking off. :) Do translated programs automatically have type checking off?

Sort of. If you have type checking on, then only type check code with side effects will be executed. I think you never get an error due to one returning zero, but if you have it set to log or some other side effect, it will be called whenever the interpreter normally would.

Matt

new topic     » goto parent     » topic index » view message » categorize

5. Re: What's the point of map:map type if load_map() messes it up?

I wonder if in cases like this the map type shouldn't support an error condition.

public type map(object obj_p) 
    if integer(obj_p) then return 1 end if 
    ... 
end if 
 
public function is_error(object obj_p) 
    if integer(obj_p) then return obj_p end if 
    return 0 
end function 

That would allow for nicer code:

map my_map = map:load_map("file.txt") 
if map:is_error(my_map) then 
    printf(1, "Map error: %d\n", { map:error_code(my_map) }) 
    abort(1) 
end if 

Thus, it allows type checking during development and promotes error checking.

Alternatively, you could write a wrapper function

function load_or_empty_map(sequence f) 
    object o = map:load_map(f) 
    if atom(o) then 
        log:warning("map not found") 
        o = map:new() 
    end if 
    return o 
end function 

Not sure that would solve your problem though, because what happens if it is an empty map? Is that a fail condition?

Another option is to have another type:

public type map_or_error(object obj_p) 
    if atom(obj_p) return 1 end if 
    return map:map(obj_p) 
end type 
 
map_or_error m = map:load_map("file.txt") 
if atom(m) then 
    -- handle error 
end if 
 
-- m is a map 

Jeremy

new topic     » goto parent     » topic index » view message » categorize

6. Re: What's the point of map:map type if load_map() messes it up?

euphoric said...

Do I have to resort to this ...

Seems like a good compromise to me, and one that is used in many other circumstances too.

RAMADDR ptr 
object load_result = load_file_to_RAM("some file") 
 
if not RAMADDR( load_result ) then 
  msg( "File failed to load" ) 
else 
  ptr = load_result 
end if 
new topic     » goto parent     » topic index » view message » categorize

7. Re: What's the point of map:map type if load_map() messes it up?

jeremy said...

I wonder if in cases like this the map type shouldn't support an error condition.

I wonder if we should wait until we get a comprehensive solution, such as exceptions built into the language.

new topic     » goto parent     » topic index » view message » categorize

8. Re: What's the point of map:map type if load_map() messes it up?

DerekParnell said...
jeremy said...

I wonder if in cases like this the map type shouldn't support an error condition.

I wonder if we should wait until we get a comprehensive solution, such as exceptions built into the language.

I wonder when that will happen? I don't think it's on the books for 4.1? Thus maybe 4.2?

Jeremy

new topic     » goto parent     » topic index » view message » categorize

9. Re: What's the point of map:map type if load_map() messes it up?

jeremy said...
DerekParnell said...
jeremy said...

I wonder if in cases like this the map type shouldn't support an error condition.

I wonder if we should wait until we get a comprehensive solution, such as exceptions built into the language.

I wonder when that will happen? I don't think it's on the books for 4.1? Thus maybe 4.2?

Jeremy

I thought exceptions were being added in 5.0

Unless we have plans for implementation exceptions immediately, we need some kind of compromise.

I came up with a very generic, if somewhat unwieldy, way to deal with this:

-- generic type test 
public function gtt(object value, integer routine_id_of_type, integer routine_id_of_error_handling_procedure, object default_valid_value) 
	if not call_func(routine_id_of_type, {value}) then 
		if routine_id_of_error_handing_procedure >= 0 then 
			call_proc(routine_id_of_error_handling_procedure, {value}) 
			--return call_func(routine_id_of_error_handling_function, {value}) 
		end if 
		return default_valid_value 
	end if 
end function 
 
--public constant NO_ROUTINE_ID = -2 
map:map t = ggt(map:load_map("file"), routine_id("map:map"), NO_ROUTINE_ID, empty_map) 
new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu