Re: Double Trouble with Phix (bug)

new topic     » goto parent     » topic index » view thread      » older message » newer message
irv said...

Q: what, exactly, happens in a new() call, e.g. GtkWindow win = new(). And what can go inside those parens? I've read the docs, and only got a headache.

aside: new() actually takes up to two parameters, but here the first is deduced[1] from the opening GtkWindow part of that statement [aka context], so we'll concentrate on the second.

The other parameter (optional, can be first/only) is a sequence of values, assigned in numerical order. For example:

class GtkWindow 
 public: 
  integer one 
  string two 
  atom three 
  sequence four 
  function getw() return {one,two,three,four} end function 
end class 
 
GtkWindow win = new({1,"two",3.3,{4}}), 
          win2 = new() 
win2.one = 1 
win2.two = "two" 
win2.three = 3.3 
win2.four = {4} 
?win.getw() 
?win2.getw() 

Obviously win and win2 are initialised identically. Should you move string two above integer one in the definition, you'll get a typecheck error on the (unmodified) inline declaration of the first win.

Note that if you have any functions/procedures in the class definition, they are treated as integers/routine_ids, but for now I'd recommend moving them below anything you want to initialise.
(I can talk a bit more about initialising/replacing/playing with any such integers/routine_ids, when you're ready.)

[1] The hardest part of the docs is that the first parameter is NOT optional, but in most cases the compiler automatically inserts it, so it feels like it is.
Were you to code something like my_create_instance(new({defaults})), then the compiler would demand you insert GtkWindow, making {defaults} the second parameter.

UPDATE: (This was never documented either) If you have a constructor function, that puts further constraints on the min/max number of {defaults} you must provide. Also, if you added

  function GtkWindow(object arg1=0,arg2=0,arg3=0,arg4=0) 
    ?{arg1,arg2,arg3,arg4} 
    return this 
  end function 

to the above, it would in fact effectively discard the inline defaults, since it's leaving your constructor routine to deal with them.
Note that to permit new({1,"two",3.3,{4}}), I needed the constructor to have at least four parameters,
and that to permit new(), they all had to be optional.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu