Friends

When you design classes you often make the properties they hold private or protected so outside code cant mess with the innards.

But what if you want to allow that – sometimes ?

This is the notion of a “friend”. Another class that you can literally say “yeah its ok I know them and it’s OK if they mess with my innards as they are my friend”. Xojo doesn’t have such a notion. So how do you make this possible ?

First off you really need to think hard about what it is you want to do and do you really need “friend”liness ? You may not. An interface or some other mechanism may be more suitable.

But if you do some to the conclusion you DO need a “friend” then you can make one using namespaces and private interfaces.

What you CAN do is

  1. take your class (call it AllowFriends) & put it in a module
  2. declare a PRIVATE class interface that class AllowFriends implements since you CAN implement an interface with private methods.
  3. Add classes to this module that you want to make friends of class AllowFriends
  4. on each class that wants to allow “friend” access implement the interface with PRIVATE methods.
  5. now anything in this module, classes or methods, that is a friend of class AllowFriends can cast it to the private interface & access whatever you exposed in that interface – this WILL only be methods but since you can make method pairs act like a getter/setter things should be fine.

And now you have classes in that module that can be “friends” but nothing outside the module can be since they cannot access the private methods on each class OR the private interface. The interface is restricted to only be used by classes & methods that exist in the module.

It’s not exactly “friend” from C++ but its about as close as you can get in Xojo.

Handy tip for Enums

Ever tried to copy an enum from some outside source like Apple, MSDN, etc and wanted to retain the comments they have about each value in the enumeration ?

You can !

Enum values can have comments in them like

Public Enum ConnectionOption
 adConnectUnspecified = -1 //    Default. Opens the connection synchronously (after).
 adAsyncConnect    = 16 // Opens the connection asynchronously (before).
 End Enum

Simply edit each value in the enum and make sure you put one of the valid comment markers in the value line and you can have commented enums.

Enjoy !

Stupid IDE tricks

A question was asked the other day about “how can I list just all the Windows in my application” And that reminded me of something I added to the IDE some time back.

Advanced filters in the find field above the navigator. It has only a very short mention in the IDE user guide.

So if you want to find all the Windows in your application you can type something like this into the filter field

% type:Window

and press enter and you will see all windows and container controls listed (containers are window like objects hence why they are listed). You can use the type filter with just about any type you can imagine in your project; property, constant, enumeration, delegate, method and so on.

You can also put things in the type field that include wild cards. So if you want to see all properties, computed and regular, you can put

% type:%property

This will list any computedproperty or property, regardless of its name, in the navigator.

Combine this with a decent naming convention and you can make the filter work for you in a lot of ways I think a lot of people do not realize exists.

Exceptions vs error codes

This isn’t just a Xojo debate. API 2.0 is reported to use exceptions rather than error codes.

And I’m sure that once preliminary releases of a version with API 2.0 are out that this debate will start in earnest.

Personally I’ve used both. The biggest difference with how I’ve used exceptions in the past in other languages was their built in support for “checked exceptions“. I’d love to see this in Xojo as it makes it so the compiler can at least help you out by giving you a compilation error when you haven’t thrown or caught an exception that you must.

Something like this makes it much easier to track down how an exception propagates since every called method MUST declare it in one way or another. They cant just silently say nothing.

However I doubt Xojo’s compiler guy(s) would add it.

There are some great articles that argue both for and against exceptions and error codes. They are great reading and they are persuasive in both directions.1

But, for my money, if Xojo is going to move to exceptions, checked exceptions would be my preference simply for the clarity it adds.

  1. reading links

Error codes vs exceptions critical code vs typical code

http://javierferrer.me/exceptions-vs-error-codes/

https://blogs.msdn.microsoft.com/oldnewthing/20050114-00/?p=36693

Update on the “sometimes” constructor

In a previous post I showed how you could create a constructor that a layout could use but no other code could.

Seems that in oder to make this a little more generic and cross-platform you need to look for a different stack entry.

Because macOS and Windows do things differently you can get a very different stack trace on Windows that you would on the Mac. So instead of looking for Window.__Init%%o you should look for the substring ._CreateControls0%%o<

So the updated code in the no parameter constructor looks like

Try
  Raise New NilObjectException
  
Catch noe As NilObjectException
  Dim stack() As String = noe.Stack
  
  // IF this is an instance created on a layout we should find a 
  // ._CreateControls0%%o< in the stack above the call to this
  // Constructor
  // cant look for the whole string as parts of it are the specific  
  // name of the window being constructed which will change by  
  // window name

  For i As Integer = 0 To stack.Ubound
  If stack(i).InStr("._CreateControls0%%o<") > 0 Then
    #Pragma BreakOnExceptions default
    
    Return
    End If
  Next
  
  // and if we do not then raise an UnsupportedOperation error
  Raise New UnsupportedOperationException
End Try

Learning HTML is not the same as learning to write software

At least IMHO they are not equivalent. Why not ?
Html is a design markup language. One focused on layout and presentation. A web designer might use Adobe DreamWeaver to create the specific layout and look of a web site. And never touch a line of PHP, C, Javascript, or whatever language is used to implement the functionality of the web site. Wikipedia points this out on the first line of its entry about HTML where it says

Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser.

https://en.wikipedia.org/wiki/HTML

Conversely the developer writing the functionality might never touch a single line of HTML or CSS code to make the layout look any particular way. Layout and design are not necessarily their role.

There are however people who are conversant with both the design language (HTML) and add ons to it (CSS) as well as with certain programming languages that can be used on the Web (Javascript). Usually these are in the “front end” or the Web browser. And it’s not unusual to find someone who understand all of these.

Now there are even people who understand all these technologies that are used in the browser and all the back end technologies used to implement the additional functionality of a web site. Usually they’re known as “full stack developers“.

A person like this has learned the language of web designers, generally html and css, some programming languages, php and javscript are common, more than likely some web based framework for various controls and layout like jQuery or Angular, and usually has some knowledge about databases like mySQL or postgresql for the back end.

EDIT : 12:47 I’ve received a comment offline where a confused reader seems to think I implied that one is more important than the other. I make no claim one way or the other and, in software, they both play an important but different role. An ugly app is an ugly app. And one that doesn’t do what it says just doesn’t and so both are required whether its a successful app, web app, or web site.

We expect

Very useful words to keep in the communication between any company and its customers. Its not a promise. Just a rough outline of what things you are planning to do.

Some companies use phrases like this to let their customer base know “hey this is what we think we are going to be able to deliver” without having to promise a specific quarter or date. Sometime things don’t work out as you planned (Airpower anyone ??)

But stating something like this is useful for helping customers understand product direction and what things are more important than others. If you don’t plan to ship something for years then don’t say anything about it until it has a reasonable prospect of being in a shipping product within the next year.

Now there’s ALWAYS the implied “oh but things could come along that waylay our plans”- and that’s the beauty of “we expect to”. Its NOT a promise like if you had said “and it will be available in October”. This is very definite (will) and a promise of when (October).

Googling “microsoft says it expects” or “apple says it expects” you will find a lot of articles that have this phrase in them. These huge companies use this kind of talk all the time to set expectations. And for any company setting & managing expectations is a big deal.

*Edited to correct typos

Add item from clipboard

A post on the forums got me thinking that there might be a way to simply add a whole class, module etc to a project just from code copied from the forums.

So I sat and whipped up a little IDE script that takes a clipboard and will convert it to a class or module. Now there are some tricks. One is that because certain aspects of the Xojo text format can be a bit ambiguous there’s a little more markup involved. Nothing really onerous though.

The following is a simple module

Module Module1
  Sub Untitled()
		  
  End Sub

  Property mUntitled1 As Integer

  ComputedProperty untitled1 as integer
    Get
      Return mUntitled1
    End Get
    Set
     mUntitled1 = value
    End Set
  End ComputedProperty
End Module

When copied to your clipboard and the script is run you get a module on your desktop on macOS. Sorry about this but I haven’t tried this on Windows or Linux yet. And I can’t insert it into the project directly with IDE scripting as it work today 🙁
The following gives a class on your desktop

class TOperInfo
  enum TOperType 
    OperNull
    OperMult
    OperDiv
    OperPlus
    OperSous
  end enum

  property Priority as Uint8
  property NbOper as TOperType

  Sub Constructor( priority as Uint8, nbOper as TOperType)
    self.priority = priority
    self.NbOper = nbOper
  end sub
end class

Enjoy !

Making a constructor sometimes illegal to call

Suppose you write a control subclass that you want people to be able to add to a window layout OR be able to create on the fly. But you want to make sure that when an instance is constructed in code it MUST have a parameterized constructor. However, in order to put an instance on a layout by drag and drop it has to have a no parameter constructor. These two design goals seem very much at odds.

So how do you make this happen ?

Such a control might be like :

Class myControl
  Inherits Canvas

  Sub Constructor()
  End Sub

  Sub Constructor(name as string)
     self.mName = name
  End Sub

  Computed Property MyName as string
    Function Getter() as string
       return mName
    End Function
    Sub Getter(value as string)
       mName = value
    End Sub
   
  protected mName as string
end class

We’ll assume that MyName has been exposed as part of the Inspector Behaviour.

But the class definition above would definitely allow you to write code that called either constructor. How to make it so the no parameter constructor can only be used by instances on a layout that, as part of initializing the layout, basically does the following

dim c as new myControl
c.MyName = "whatever value the user set in the inspector"

But there is a trick you can use to know how you have been called. An exception exposes the call stack to you and you can examine that to know if there is a window being initialized that has resulted in your no parameter constructor being called. And so you can make it so a window being initialized can use the no parameter version. In fact to make it so your control can be placed on a layout in the IDE designer it MUST have a no parameter constructor if it has any constructors.

If we make that no parameter constructor read as follows we can make this raise an exception when used improperly outside of a window being constructed. Sorry can’t make the compiler reject it (THAT would be awesome)

Try
  Raise New NilObjectException
  
Catch noe As NilObjectException
  Dim stack() As String = noe.Stack
  
  // IF this is an instance created on a layout we should find a 
  // Window.__Init%%o<Window> in the stack above the call to this Constructor
  For i As Integer = 0 To stack.Ubound
    If stack(i) = "Window.__Init%%o<Window>" Then
      Return
    End If
  Next
  
  // and if we do not then raise an UnsupportedOperation error
  Raise New UnsupportedOperationException
End Try

A window, when being created, calls __Init and so we can tell if we were called by a window being created or not by walking up the call stack. If there is no Window.__Init in the stack then we definitely were not and so can raise an exception to let the coder know that this usage, without a parameter, is only for Layouts and nothing else.

Sampling with a delay

MacOS terminal is a useful tool to know for a few really handy things.

One is getting a sample of a process after some delay so you can start doing whatever it is you want to sample.

Trying to do this in Activity monitor is not possible

But at the command line you can delay the start of the sample so you have a chance to set up the sampling and then switch back to Xojo to do the task you want sampled.

So how ?

Open Terminal

Once there enter

sleep 5; sample Xojo  -file ~/Desktop/Sample2019r1.1macOS12.6.txt

The number after SLEEP is how many seconds to wait before starting to sample

The SAMPLE command takes several parameters. We’ve told it to look at any process containing “XOJO” and to put the output file on our desktop with a specific name.

This handy bit of bash makes it easy to sample something to send to Xojo as part of a bug report.