B4J Question What is the B4 equivalent of a Xojo Interface or a Swift Protocol?

Markus Winter

Member
Licensed User
What I am missing is a high-level introduction for people coming from other languages.

Even something like a simple table showing what is equivalent or can be used instead of would be helpful, eg

Swift - Xojo - B4
Var - Dim / Var - Dim
Let - Const - Const
Protocol - Interface - ???

Does this exist somewhere, and I'm just too blind to find it?

And what is the B4 equivalent to a Protocol / Interface? I was looking through the booklets but could not find whether B4 has something like Protocols or Interfaces.
 

aeric

Expert
Licensed User
Longtime User
I have no idea what do you mean by Protocol. From what I can guess, Java has concept of Interface which is similar or different implementation of Class. In some programming languages like .NET or PHP, there is Namespace. I think it is similar concept. B4X supports Class which we can reuse the code module and initialize an object from the Class. B4X also has Type, which is similar to struct.
 
Last edited:
Upvote 0

Markus Winter

Member
Licensed User
Does such a table exist in Java, for example? With equivalences to C++, C#, Python?
Oi! Make your own thread for other languages ?

?

I‘m asking because quite a few former Xojo users have gone to B4 or Java. I‘m evaluating B4 myself, but there doesn’t seem much support for people transitioning from other languages. Xojo used to have help for people coming from VisualBasic (even a code converter though I would argue it was better to do a complete rewrite). And looking at the documentation I am missing a kind of overview of the concepts in B4.

The best example I can give are the first few chapters of Matt Neuberg‘s book “REALbasic - The Definitive Guide” (especially chapters 3 and 4). The book is as outdated as a book on Windows 3.1 - but those chapters are still essential reading even today. Because they teach you about the design philosophy and object-oriented programming in a way that makes it easy to understand where all the bits go and how they interact.

Think of it as the picture on a puzzle box - without it you have a MUCH harder time to assemble the puzzle.

A comparison table would be a simple but useful crutch. decent manual on the concepts and capabilities of B4 would make it MUCH easier to get into.

… and I still don’t know if B4 supports the concept of interfaces or even better protocols …
 
Upvote 0

Markus Winter

Member
Licensed User
I have no idea what do you mean by Protocol. From what I can guess, Java has concept of Interface which is similar or different implementation of Class. In some programming languages like .NET or PHP, there is Namespace. I think it is similar concept. B4X supports Class which we can reuse the code module and initialize an object from the Class. B4X also has Type, which is similar to struct.

An interface in Xojo is a collection of method signatures, and any class can implement an interface.

The beauty of interfaces is that they are a type in their own right, and can cut across class hierarchies.

So if you have an interface CanDrawItself then you can do

B4X:
if someInstance isA CanDrawItself then

where someInstance is any class that has implemented the interface.

In real life an Interface could be described as a Role, eg CanPlayGuitar. Anyone could fill that role, they don’t have to be an instance of Musician or one of its subclasses.

Protocols in Swift are souped up interfaces that not only have method definitions but can have properties, constants, enums as well.
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
What I am missing is a high-level introduction for people coming from other languages.

And what is the B4 equivalent to a Protocol / Interface?
An open question about a container concept such as protocol and interface cannot be answered. A network administrator then thinks of things like TCP/IP, UDP and so on. With an interface he things like a component such as Bluetooth, Ethernet, USB, while a security officer thinks of a lot else with the word protocol for the same container concept.

B4J stands for Basic Four Java, so if you already know one of the many Basic programming dialects, B4J can be learned very quickly. Erel has made a lot of video examples on all kinds of specific aspects of B4J where you can see how your programming interface works. There is a lot to find on this forum about specific solutions and problems. The same goes for using Java.

As with any programming language and environment, to acquire the necessary knowledge is to start by reading, watching instructional videos and downloading and analyzing examples.

And to help you with the latter, if you indicate here what you plan to use B4J for, we can help you very quickly with some examples that you can then adapt to your own needs. Should you get stuck with an error, or how you could do something in your specific situation, there are plenty solutions to found on this forum or people are always willing to help you with a solution.

So what do you want to realize with B4J?
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
protocol or interface is a model that defines the set of properties and methods of an object / class

Unlike the obsolete languages you mentioned, in b4x you don't need to define an interface or protocol by specifying methods and properties before implementing a class. All methods and properties declared public will be part of the class interface.

An exception is if you define Events that must instead be declared in advance.
If your class generates Events on the caller, declare:
B4X:
#Event: NameEvent (parameters Ad Type, ...)

If you wanted to understand how to create a class, see here

B4X:
'Class Person module
Sub Class_Globals
   Private FirstName, LastName As String
   Private BirthDate As Long
End Sub

Sub Initialize (aFirstName As String, aLastName As String, aBirthDate As Long)
   FirstName = aFirstName
   LastName = aLastName
   BirthDate = aBirthDate
End Sub

Public Sub GetName As String
   Return FirstName & " " & LastName
End Sub

Public Sub GetCurrentAge As Int
   Return GetAgeAt(DateTime.Now)
End Sub

Public Sub GetAgeAt(Date As Long) As Int
   Dim diff As Long
   diff = Date - BirthDate
   Return Floor(diff / DateTime.TicksPerDay / 365)
End Sub

'Main module
...
Dim p As Person
p.Initialize("John", "Doe", DateTime.DateParse("05/12/1970"))
Log(p.GetCurrentAge)
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Does such a table exist in Java, for example? With equivalences to C++, C#, Python?
Python us similar Swift for protocol

Python:
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def myfunc(self):
    print("Hello my name is " + self.name

Swift:
protocol Polygon {
  func getArea(length: Int, breadth: Int)
}
// conform the Polygon protocol
class Rectangle: Polygon {
  // implementation of method
  func getArea(length: Int, breadth: Int) {
    print("Area of the rectangle:", length * breadth)
  }
}

// create an object
var r1 = Rectangle()
r1.getArea(length:5, breadth: 6)
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Python is similar Swift for protocol
What I meant is that the OP would like a table with equivalence between the keywords of 3 languages: Swift - Xojo - B4X, but he will never find documentation of a language in which such a table exists, he will not find documentation on Java in which there is a table with Java keywords and analogous C++ keywords and C# keywords etc.

So...!
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
@LucaMs I think that you misunderstood what OP's question is.

Interface:

Swift protocol:
 
Upvote 0

Markus Winter

Member
Licensed User
Does such a table exist in Java, for example? With equivalences to C++, C#, Python?
That is neither an answer nor advice - what was the point?
What I meant is that the OP would like a table with equivalence between the keywords of 3 languages: Swift - Xojo - B4X, but he will never find documentation of a language in which such a table exists, he will not find documentation on Java in which there is a table with Java keywords and analogous C++ keywords and C# keywords etc.

So...!
I still fail to see your point. Something doesn't exist so it should never be made? It doesn't exist so it would not be useful to make it?

And I said that I come from REALbasic/Xojo which is very similar in many respects to B4, so a lookup table to see what is and isn't available or might be differently named would be VERY useful.
 
Upvote 0

Markus Winter

Member
Licensed User
So what do you want to realize with B4J?
Seems an introduction is in order.

I did a BASIC and C course in the early 90s, then did most things with HyperCard. In 2003 our lab needed to do some exceedingly tedious task that would have taken us 5 days to do by hand, so I decided to write an app that could do it. As HyperCard no longer worked on our new Mac I evaluated SuperCard, LiveCode, and REALbasic on Saturday (with the expectation to go for SuperCard), decided to my surprise on REALbasic, went through a word processor tutorial on Sunday, and wrote an exceedingly simple and ugly app in an hour on Monday morning - but it did the whole work in under 5 seconds for us.

In the years that followed I taught myself to an advanced beginner / intermediate level. When REALbasic introduced the Rapid Release Model in 2007 I predicted that in the long term it would become a permanent beta quality product. Many said I was wrong, but I have a very good track record on long-term predictions. About 7 years ago more Pro developers started to complain about the increasing bugginess of REALbasic, now renamed to Xojo. Then Xojo made some exceedingly unwise decisions like renaming events and keywords (known as API 2) because "beginners don't know what append means, so we rename it to add" (I didn't know the US education system was THAT bad), making 20 years of example code, tutorials, libraries etc obsolete or unviable in the long term; Pros were told they are no longer Xojo's target group, that they are going for the beginners instead. As a result the trickle of people leaving Xojo became a flood, and with the Pros their important and VERY useful open source projects fell behind as well as most users could not maintain them, never mind upgrade them to the new API 2. B4 itself benefitted from that - I guess you all know Alain Bailleul aka AlwaysBusyCorner and his Banano and ABMaterial …

I myself had 20 years of Xojo experience, and written 61 articles and tutorials for the xDev computer magazine. A nice example of what I did can be seen in this short tutorial about using interfaces with the Listbox (a table):

Fig 7 ListBox with OOP.png


Essentially you add very different objects (eg, instances of the classes cHeader, cDepartment, cEmployee) to each rowTag, and each object knows how to paint itself in the row. Very simple and elegant to do with interfaces (in this case because they all implement the CanDrawItself interface).

Protocols (the more powerful Swift equivalent) are EVERYWHERE in Swift. For example you can have a car class cCar, a person class cPerson, a building class cBuilding, an accident class cAccident - and you can display them all on a map by having them implement the MapAnnotationProtocol and setting the features (like title, subtitle, coordinates).

The important point about interfaces / protocols is that they allow you to combine very different objects into one type based on similar behaviour or capabilities - you are not restricted to a hierarchical class structure. As stated here:
Most modern programming languages, in the hopes of enhanced maintainability and reusability of code, offer some constructs that help the developer keep the definition of behavior and its implementation separate. Swift takes the idea of interfaces a step further with protocols. With protocols and protocol extensions, Swift allows developers to enforce elaborate conformity rules without compromising the expressiveness of the language. In this article, Toptal Software Engineer Alexander Gaidukov explores Swift protocols and how protocol-oriented programming can improve the maintainability and reusability of your code.

Anyway, I too am looking for a new programming environment. I really like Swift, but I need cross-platform. Python and Java are possible contenders (and I would think if Python had a decent IDE and compiler it would wipe the floor with pretty much anything else). Alain recommended B4, and I'm aware that B4 only implements a subset of Java's capabilities - but what, and does it implement enough?

That's where a table comparing the languages, or a high level introduction about the design philosophy and capabilities of B4 would be immensely useful.

I have to say it is somewhat sobering that most people here do not know what an interface or protocol is. And it is even more sobering that some seem to feel the need to attack the question rather than contribute useful information.

Bottom line: I still don't know if B4 implements the interface / protocol concept, but I do know that Java does.

I'm starting to think that B4 does not. And that's a pity.
 
Upvote 0

Markus Winter

Member
Licensed User
protocol or interface is a model that defines the set of properties and methods of an object / class

No.

It is an abstract type that defines a set of methods (and properties in the case of protocols) for ANY class that implements it. As I said previously, its strength comes from being able to cut across class hierarchies and combine very different objects / classes based on a shared capability. This increase in abstraction leads not only to less code redundancy but also to simpler and more readable code, eg as interfaces are an object type in their own right (but NOT a class) instead of a long select case statement you can simply say

B4X:
if someInstance isA CanDrawItself then

Unlike the obsolete languages you mentioned

Please don't troll. It is unbecoming.
 
Upvote 0

Markus Winter

Member
Licensed User
And still "This message is awaiting moderator approval, and is invisible to normal visitors." for nearly all my messages?

Others keep telling me they were NEVER moderated, so what is going on???
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Oi! Make your own thread for other languages ?
I still fail to see your point. Something doesn't exist so it should never be made? It doesn't exist so it would not be useful to make it?
Yet it is quite basic.


I, George, know C ++ and want / would like a comparison table between the keywords of this language and those of B4X.

I, Philip, know C # and want / would like a comparison table between the keywords of this language and those of B4X.

I, William, know Xojo and want / would like a comparison table between the keywords of this language and those of B4X.

I, Peter, know RPGILE (!) and I want / would like a comparative table between the keywords of this language and those of B4X.

I, Pasquale, know Java and I want / would like a comparative table between the keywords of this language and those of B4X.


Is it clear enough, now? Or do I have to explicitly write that it is a request that makes little sense?
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Upvote 0

Sandman

Expert
Licensed User
Longtime User
The important point about interfaces / protocols is that they allow you to combine very different objects into one type based on similar behaviour or capabilities - you are not restricted to a hierarchical class structure.
It sounds like you could be talking about what is called traits in php, could that be right? In that case the answer would be no, there's nothing like that in B4X.

As for your question about comparison table: nothing like that exists. And documentation? Almost nothing like what you would expect, coming from other languages. There's some member initiatives that are the closest you'd come. (And they are fine, I especially recommend the booklets.) As for official documentation, there's a set of video tutorials, some webpages and forum tutorials. (All is available from the top menu in the forum, so go explore that.) That might seem anemic, but in all fairness, the language is simple to pick up, so it's worked well for lots and lots of developers.

Also, sorry about the reception. Some forum members can get slightly protective of the language. Don't worry about it, it'll pass once it's clear you're not just here for a drive-by trolling.

And still "This message is awaiting moderator approval, and is invisible to normal visitors." for nearly all my messages?
Standard procedure for all unlicensed users. Most users got here to the forum after buying a B4X product, making them licensed from the start, so most users have never been moderated and thus haven't seen that message. It's just a way to keep spam out and keep the forum a nice place.
 
Upvote 0
Top