Classes programming

Hello everyone,

I’ve been using SmallBASIC for a while now, and I’m facing a problem I can’t seem to solve.
I create a lot of “classes”, which I find convenient even though it’s limited compared to other languages (I come from Pascal).

I’ve made a very simple example to explain what I mean.

In this code, we have class1 (c1) and class2 (c2).
If line 13 is commented out, it works and we get the expected result.

external hello from class1
external hello from class2

Press any key to exit.

However, if you uncomment it, it seems to work since the out method of class c2 is indeed called from c1, but we still get an error!

external hello from class1
internal hello from class2

  • RTE-ERROR AT main.bas:4 *
    Description:
    Invalid SUB pointer variable

Stack:
SUB: 22

I’m running on Linux (Kubuntu 24.04) and using the console version compiled from source.
But I get the same result no matter which version of SmallBASIC I use.

Thanks — and sorry for the wall of text!!
JF.

Hi Jfd, SmallBASIC is strict with return values. If you use a function, the return value has to be used. For example by assigning it to a variable. In line 2 you are defining a function. That line 22 works, is somehow an unexpected behaviour. To get you program working, you can either use a subroutine or assign the return value.

With functions:

func class2()
  func m_out(msg)
    ?msg+" from class 2"
  end
  local vt
  vt.out=@m_out
  return vt
end

func class1()
  func m_out(msg)
    ?msg+" from class 1"
    'a = self.c2.out("internal hello")
    a = call(self.c2.out, "internal hello")
  end
  local vt
  vt.out=@m_out
  vt.c2=class2()
  return vt
end

c1=class1()
a = c1.out("external hello")
a = c1.c2.out("external hello")

With subroutine

func class2()
  sub m_out(msg)
    ?msg+" from class 2"
  end
  local vt
  vt.out=@m_out
  return vt
end

func class1()
  sub m_out(msg)
    ?msg+" from class 1"
    'self.c2.out("internal hello")
    call self.c2.out, "internal hello"
  end
  local vt
  vt.out=@m_out
  vt.c2=class2()
  return vt
end

c1=class1()
c1.out("external hello")
c1.c2.out("external hello")

I hope this solves your problem. Best regards, J7M

Hi Joerg, thanks for the clarification.
I really did overlook that.
Bad Pascal habits…

It clearly says: “The definition of a function starts with the keyword FUNC and ends with the keyword END. It has a name name, can have parameters par1 to parN and must return a value value.”

But I skimmed right over the “must”. RTFM