# With for BUOY

**URL:** <https://ifnotnil.com/t/with-for-buoy/4741>\
**Category:** Buoy\
**Tags:** proposal\
**Created:** [4 July 2026 15:54 UTC](https://ifnotnil.com/t/with-for-buoy/4741 "2026-07-04T15:54:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [4 July 2026 15:54 UTC](https://ifnotnil.com/t/with-for-buoy/4741/1 "2026-07-04T15:54:47Z")

</div>

A proposal  
In X you often write code like

```vb
   if thing isa OtherThing then
        var x as OtherThing = OtherThing( thing )
       ... more code that deals with x since its now an OtherThing
  end if

```

OR

```vb
 select case true
         case thing isa otherType
                .. cast to other type & work with the variable
         case thing isa yetAnotherType
                .. cast to yetAnotherType & work with the variable
    end select

```

it sure would be nice to get rid of that boiler plate  
not sure what other languages might have

Maybe some thing like

```vb
   with thing as otherType
            .. all the code that treats thing as an otherType
            AND NO CAST REQUIRED HERE !!!!!!
   end with

```

or

```vb
   with thing as otherType
       ... thing is an otherType
         NO CAST REQUIRED HERE !!!!!!
        
   else with thing as yetAnotherType
       ... else thing is yetAnotherType
         NO CAST REQUIRED HERE !!!!!!

   else with

   end with

```

I suppose there’s pros & cons

Thoughts ?

---

<div class="post-metadata">

**Author:** ![bgrommes](https://ifnotnil.com/user_avatar/ifnotnil.com/bgrommes/32/2651_2.png) [@bgrommes](https://ifnotnil.com/u/bgrommes)\
**Post date:** [4 July 2026 22:52 UTC](https://ifnotnil.com/t/with-for-buoy/4741/2 "2026-07-04T22:52:53Z")

</div>

“if thing isa OtherThing” sounds like thing is an Object (or in X terms, IIRC, Variant) boxing something and you have to figure out what it is? And the possibilities aren’t necessarily in the same inheritance tree or at least implementing the same interface?

I don’t recall X having generics and IIRC Buoy will, so … maybe there would, with proper design, be less need for this kind of thing to begin with.

But there are still situations where you can’t escape it. Still, something about having a variable name morph to a new type and back makes me a little bit uneasy in terms of code readability. Also it’s only buying you one less line of code per sub-block, if I’m understanding your scenario correctly.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [5 July 2026 00:38 UTC](https://ifnotnil.com/t/with-for-buoy/4741/3 "2026-07-05T00:38:17Z")

</div>

I suppose the scenario might be something like examining every control on a window  
In that case while they are all subclasses of UI Control or something like that you might want to treat each different based on type

```vb
for each ctrl as Control in Windows.Controls

select case true 

   case ctrl isa PushButton
        dim pb as PushButton = pushbutton(ctrl)
        ... pb specific code ...

   case ctrl isa CheckBox
        dim cb as CheckBox = CheckBox(ctrl)
        ... cb specific code ...

... etc

```

in a long case like this ( assuming you handle may controls) while you save one line per block you could save a lot of lines overall

The other case I run into is Dictionaries in X  
Or other similar code where what’s stored is some hierarchical arrangement of very generic data

I don’t expect it would save a lot of code though

---

<div class="post-metadata">

**Author:** ![bgrommes](https://ifnotnil.com/user_avatar/ifnotnil.com/bgrommes/32/2651_2.png) [@bgrommes](https://ifnotnil.com/u/bgrommes)\
**Post date:** [5 July 2026 01:28 UTC](https://ifnotnil.com/t/with-for-buoy/4741/4 "2026-07-05T01:28:13Z")

</div>

Ah well this sounds like a use case for the GUI you’re designing. In line of business desktop apps I can’t remember the last time I had to iterate through controls for any purpose that would require me to access anything that’s not on the parent Control class API.

My thought in your shoes would likely be to ask myself how many users care about this and if it’s non-trivial to implement, I’d prioritize other features that will be of interest to the most users, particularly the target product audience and their core use cases.

OTOH I have my own pet ideas and wish lists, so I certainly understand how cool it is to get rid of little constant niggling pain points. They do add up!

---

<div class="post-metadata">

**Author:** ![Stretch327](https://ifnotnil.com/user_avatar/ifnotnil.com/stretch327/32/5162_2.png) [@Stretch327](https://ifnotnil.com/u/Stretch327)\
**Post date:** [5 July 2026 01:40 UTC](https://ifnotnil.com/t/with-for-buoy/4741/5 "2026-07-05T01:40:39Z")

</div>

So it turns out that this is not terribly hard to do. The compiler just needs to synthesize a shadowed variable inside the block for the original one. Using Norman’s original example, what happens under the hood is this:

```vbnet
 with thing as otherType
     dim thing as otherType = thing
       ... thing is an otherType
         NO CAST REQUIRED HERE !!!!!!
        
 else with thing as yetAnotherType
      dim thing as yetAnotherType = thing
       ... else thing is yetAnotherType
         NO CAST REQUIRED HERE !!!!!!

 else
     // thing is the original type here

 end with

```

---

<div class="post-metadata">

**Author:** ![BillG](https://ifnotnil.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@BillG](https://ifnotnil.com/u/BillG)\
**Post date:** [6 July 2026 04:20 UTC](https://ifnotnil.com/t/with-for-buoy/4741/6 "2026-07-06T04:20:36Z")

</div>

If you’re just looking for how other languages do things, C# does basically what I think you want like this:

```auto
private async void Header_OnDoubleTapped(object? Sender, TappedEventArgs E)
    {
        if (Sender is TextBlock textBlock) 
        {
            // Now textBlock is a TextBlock
            Console.WriteLine(textBlock.Text);
        }

        // Or if you want to get the tag property of a textblock into a variable         
        if (Sender is TextBlock { Tag: string tag })
        {
            // Now tag is a string, but there is no TextBlock variable defined
            Console.Writeline(tag);
        }
    }

```

Am I understanding?

---

<div class="post-metadata">

**Author:** ![Stretch327](https://ifnotnil.com/user_avatar/ifnotnil.com/stretch327/32/5162_2.png) [@Stretch327](https://ifnotnil.com/u/Stretch327)\
**Post date:** [6 July 2026 11:08 UTC](https://ifnotnil.com/t/with-for-buoy/4741/7 "2026-07-06T11:08:01Z")

</div>

If you look at build 0.20.0, we introduced a new keyword “treat”

```vbnet
treat thing as otherType
     // thing is othertype in this block
       ... thing is an otherType
         NO CAST REQUIRED HERE !!!!!!
        
 else treat thing as yetAnotherType
      // thing is yetAnotherType in here
       ... else thing is yetAnotherType
         NO CAST REQUIRED HERE !!!!!!

 else
     // thing is the original type here

 end treat

```

---

<div class="post-metadata">

**Author:** ![bgrommes](https://ifnotnil.com/user_avatar/ifnotnil.com/bgrommes/32/2651_2.png) [@bgrommes](https://ifnotnil.com/u/bgrommes)\
**Post date:** [6 July 2026 14:51 UTC](https://ifnotnil.com/t/with-for-buoy/4741/8 "2026-07-06T14:51:42Z")

</div>

> [@Stretch327](#):
>
> new keyword “treat”

Ah, you nailed it – that is much clearer semantics than with. (No offense Norm, I couldn’t come up with a better alternative myself, lol)

---

<div class="post-metadata">

**Author:** ![Stretch327](https://ifnotnil.com/user_avatar/ifnotnil.com/stretch327/32/5162_2.png) [@Stretch327](https://ifnotnil.com/u/Stretch327)\
**Post date:** [6 July 2026 20:35 UTC](https://ifnotnil.com/t/with-for-buoy/4741/9 "2026-07-06T20:35:39Z")

</div>

Just so ya know, “treat” was Norman’s idea.
