"ocaml_beginners"::[] Having trouble with the type inference system

View: New views
4 Messages — Rating Filter:   Alert me  

"ocaml_beginners"::[] Having trouble with the type inference system

by Sharif Oerton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Greetings;

I am extremely new to OCaml and quite new to programming in general, and I'm having quite a lot of trouble writing even simple programs; hopefully the amount of difficulty I'm having with functional programming will decrease with practice. However, I'm having a problem that no amount of RTFM-ing seems to solve:

I'm writing a Pong clone, and fifty lines in I get the following error message upon compilation:

Line 33:
This expression has type game_object but is used here with type
<bmp : Sdlvideo.surface; h : int; w : int; x : int; x_speed : int; y : int; y_speed : int; .. >

This is my code (in its entirety because in my unfamiliarity with the language I'd probably trim out something important):

open Sdlevent
open Sdlkey

let menu_filename = "menu.png"
let ball_filename = "ball.png"
let paddle_filename = "paddle.png"
let screen_width = 300
let screen_height = 300

type game_object = { x : int; y : int; w : int; h : int; x_speed : int; y_speed : int; bmp : Sdlvideo.surface };;

let move o =
  let new_x = o#x + o#x_speed in
  let new_y = o#y + o#y_speed in
  { x=new_x; y=new_y; w=o#w; h=o#h; x_speed=o#x_speed; y_speed=o#y_speed; bmp=o#bmp }

let draw_object screen o =
  let pos = Sdlvideo.rect o#x o#y o#w o#h in
  Sdlvideo.blit_surface ~dst_rect:pos ~src:o#bmp ~dst:screen ()


let rec game_loop screen b p1 p2 =
  match wait_event () with
  | KEYDOWN {keysym=KEY_ESCAPE} ->
      0
  | event ->
      let b = (move b) in
      let p1 = (move p1) in
      let p2 = (move p2) in
      (*check_collisions();*)
      draw_object screen b;
      draw_object screen p1;
      draw_object screen p2;
      sdl.flip screen;
      game_loop screen b p1 p2

let main () =
  Sdl.init [`VIDEO];
  at_exit Sdl.quit;  
  let screen = Sdlvideo.set_video_mode screen_width screen_height [`DOUBLEBUF] in
  let b = {x=(screen_width / 2); y=(screen_height/2); w=ball_width; h=ball_height; x_speed=startingxspeed; y_speed=startingyspeed; bmp=(Sdlloader.load_image ball_filename)} in
  let p1 = {x=(screen_width / 2); y=(screen_height - paddle_width); w=paddle_width; h=paddle_height; x_speed=0; y_speed=0; bmp=(Sdlloader.load_image paddle_filename)} in
  let p2 = {x=(screen_width / 2); y=0; w=paddle_width; h=paddle_height; x_speed=0; y_speed=0; bmp=(Sdlloader.load_image paddle_filename)} in
  game_loop screen b p1 p2

let _ = main ()

Many thanks for any pointers anyone can provide.

Sharif



     


Parent Message unknown Re: "ocaml_beginners"::[] Having trouble with the type inference system

by Hugo Ferreira :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Sharif,

I did not compile the code but I see that you have a record type:

 >
 > type game_object = { x : int; y : int; w : int; h : int; x_speed :
int; y_speed : int; bmp : Sdlvideo.surface };;
 >

and are accessing its fields as an object. Note that I don't know
Ocaml's object system but you may want to code your move function so:

let move o =
   let new_x = o.x + o.x_speed in
   let new_y = o.y + o.y_speed in
   { x=new_x; y=new_y; w=o.w; h=o.h; x_speed=o.x_speed;
y_speed=o.y_speed; bmp=o.bmp }

or more simply:

let move o = { o with x = o.x + o.x_speed ; new_y = o.y + o.y_speed };

Look at the manual's section on records.

HTHs,
Hugo F.

P.S: did not compile the code above.


Sharif Oerton wrote:
> Greetings;
>
> I am extremely new to OCaml and quite new to programming in general, and I'm
having quite a lot of trouble writing even simple programs; hopefully the
amount of difficulty I'm having with functional programming will decrease with
practice. However, I'm having a problem that no amount of RTFM-ing seems to
solve:
>
> I'm writing a Pong clone, and fifty lines in I get the following error message
upon compilation:
>
> Line 33:
> This expression has type game_object but is used here with type
> <bmp : Sdlvideo.surface; h : int; w : int; x : int; x_speed : int; y : int;
y_speed : int; .. >
>
> This is my code (in its entirety because in my unfamiliarity with the language
I'd probably trim out something important):

>
> open Sdlevent
> open Sdlkey
>
> let menu_filename = "menu.png"
> let ball_filename = "ball.png"
> let paddle_filename = "paddle.png"
> let screen_width = 300
> let screen_height = 300
>
> type game_object = { x : int; y : int; w : int; h : int; x_speed : int;
y_speed : int; bmp : Sdlvideo.surface };;
>
> let move o =
>   let new_x = o#x + o#x_speed in
>   let new_y = o#y + o#y_speed in
>   { x=new_x; y=new_y; w=o#w; h=o#h; x_speed=o#x_speed; y_speed=o#y_speed;
bmp=o#bmp }

>
> let draw_object screen o =
>   let pos = Sdlvideo.rect o#x o#y o#w o#h in
>   Sdlvideo.blit_surface ~dst_rect:pos ~src:o#bmp ~dst:screen ()
>
>
> let rec game_loop screen b p1 p2 =
>   match wait_event () with
>   | KEYDOWN {keysym=KEY_ESCAPE} ->
>       0
>   | event ->
>       let b = (move b) in
>       let p1 = (move p1) in
>       let p2 = (move p2) in
>       (*check_collisions();*)
>       draw_object screen b;
>       draw_object screen p1;
>       draw_object screen p2;
>       sdl.flip screen;
>       game_loop screen b p1 p2
>
> let main () =
>   Sdl.init [`VIDEO];
>   at_exit Sdl.quit;
>   let screen = Sdlvideo.set_video_mode screen_width screen_height [`DOUBLEBUF]
in
>   let b = {x=(screen_width / 2); y=(screen_height/2); w=ball_width;
h=ball_height; x_speed=startingxspeed; y_speed=startingyspeed;
bmp=(Sdlloader.load_image ball_filename)} in
>   let p1 = {x=(screen_width / 2); y=(screen_height - paddle_width);
w=paddle_width; h=paddle_height; x_speed=0; y_speed=0;
bmp=(Sdlloader.load_image paddle_filename)} in
>   let p2 = {x=(screen_width / 2); y=0; w=paddle_width; h=paddle_height;
x_speed=0; y_speed=0; bmp=(Sdlloader.load_image paddle_filename)} in

>   game_loop screen b p1 p2
>
> let _ = main ()
>
> Many thanks for any pointers anyone can provide.
>
> Sharif
>
>
>
>
>
>
> ------------------------------------
>
> Archives up to December 31, 2007 are also downloadable at
http://www.connettivo.net/cntprojects/ocaml_beginners/
> The archives of the very official ocaml list (the seniors' one) can be found
at http://caml.inria.fr
> Attachments are banned and you're asked to be polite, avoid flames etc.Yahoo!
Groups Links
>
>
>
>




Re: "ocaml_beginners"::[] Having trouble with the type inference system

by Sharif Oerton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you very much! I foolishly assumed the syntax would be the same, and didn't think to check in the manual.

Thanks again!
Sharif


     


Re: "ocaml_beginners"::[] Having trouble with the type inference system

by Richard Jones-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Aug 22, 2008 at 02:26:14AM -0700, Sharif Oerton wrote:
> type game_object = { x : int; y : int; w : int; h : int; x_speed : int; y_speed : int; bmp : Sdlvideo.surface };;
>
> let move o =
>   let new_x = o#x + o#x_speed in
>   let new_y = o#y + o#y_speed in
>   { x=new_x; y=new_y; w=o#w; h=o#h; x_speed=o#x_speed; y_speed=o#y_speed; bmp=o#bmp }

As another reply pointed out, the problem is you've defined a record,
and so you need to use record access (o.x, o.x_speed, etc)

Avoid the object system if you're a newbie, and stick to using plain
records.

Rich.

--
Richard Jones
Red Hat
LightInTheBox - Buy quality products at wholesale price!