A little trick with the "extends" keyword

Most of you probably know that you can use the Extends keyword to add new functions and methods to objects, arrays and simple datatypes, such as:

function Ord (extends b as Boolean) as Integer if b then return 1 end if end function

With that, you could then write:

dim ok as Boolean = ... MsgBox "ok: "+Str(ok.Ord)

But did you know you can also combine this with Byref to do this:

sub Inc (extends ByRef i as Integer) i = i + 1 end sub

Now, instead of writing:

 i = i + 1

you can write:


I like that…

function Ord (extends b as Boolean) as Integer
    return IF(b,1,0)
end function

[quote=204516:@Dave S] return IF(b,1,0)[/quote]
Yeah, that works if you’re using a recent Xojo version, which I don’t :wink:

I really hate it that it’s not been named “iff” as it’s been done in other languages. That would have even made it possible to keep it backward compatible. Sigh.

iif looks like somebody stuttered. :slight_smile:

Using IIf would have broken code that already had a function named IIf, which is a nontrivial number of projects. ‘If’ is already a keyword, so using it for inline if expressions didn’t break any code.

I wrote “iff”, though. I F F. Though, while it is indeed used in tech speak (https://en.wikipedia.org/wiki/If_and_only_if), it’s not necessarily the best match for what I was asking here. But I am pretty sure other languages are using iff() the way I suggest.

Oh… I was wrong, Kem was actually spot-on, see: https://en.wikipedia.org/wiki/IIf#IIf_in_other_programming_languages

Same issue whether its named IIF or IFF
IF was “safe” and assured to not collide with anything users wrote

[quote=204511:@Thomas Tempelmann]sub Inc (extends ByRef i as Integer) i = i + 1 end sub

That means you could also do

sub Inc (extends ByRef i as Integer, IncVal as integer = 1) i = i + IncVal end sub

So you could also do

i.inc 4

…But I see no point to doing any of that…

i = i + 1 is faster, easier to read (IMO) and is not much more typing!

  • Karen

Not to mention explicit. i.Inc doesn’t tell me anything about what it does. I like may names to means something at a glance and this fails that reasoning.

A better name would IncrementByOne which is a lot more typing. Thankfully AutoComplete would make this a non-issue. But then I’m thinking it’s too long so I’d go back to the i = i + 1. Short, sweet, and explicit.

I really don’t want to make Xojo more like c unless I absolutely have to.

Oh heck be done with it and name it “PlusPlus” so you can write i.PlusPlus :stuck_out_tongue:

I’ll mention again that I wrote a series of Keyboard Maestro macros that will let me type ‘statement (opt)+(opt)+’ that will substitute ‘statement = statement + 1’, if anyone wants them.

(Why opt-plus? I found I was accidentally using ++ in other areas that were triggering the macro. Same for minus-minus.)

Neither do Abs, Bin, CDbl, Exp, Hex, Rnd, Val, …

INC (and DEC) are used in Pascal and other programming languages designed by N. Wirth, not in C.

I don’t always agree with Eli, but in this case, we’re on the same page :slight_smile:

You don’t? I’m shocked.

I don’t agree with either of you! :stuck_out_tongue_winking_eye:

Maybe I was confusing you two. Germans are all the same to me.

So we are now three who disagree. Who is the good, who the bad and who the ugly one?

Three weeks after we moved into our new flat I met our 94 year old neighbour on the stairs and she said in kind of a surprised voice: “you have SUCH a beautiful wife!”. I grinned at her and answered: “… and she has SUCH a handsome husband …” and you could see honesty and politeness fighting each other on her face before she squealed: … she has a … very nice … husband." … and we just grinned at each other.

We got on swimmingly afterwards :slight_smile:

So sadly I’m the ugly one …

So you all agree to disagree?