THIS support in WITH blocks

General FreeBASIC programming questions.
Eclipzer
Posts: 432
Joined: Oct 01, 2005 10:50
Location: Maryland
Contact:

THIS support in WITH blocks

Post by Eclipzer »

I've run into this situation a few times while coding and am wondering why this isn't currently supported, how difficult it would be to add and when we can expect to see this in future releases?

Code: Select all

with myVar
  this = value 'this refers to myVar
end with
This is quite useful when your variable is quite complex:

Code: Select all

with myArray(a,b,c)
  this=value 'this refers to myArray(a,b,c)
end with
Thoughts? Comments? Suggestions?
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Post by KristopherWindsor »

I thought about that too; it would be a nice feature, although I don't know if I ever got around to suggesting it. ;)
HD_
Posts: 215
Joined: Jun 10, 2006 12:15
Contact:

Post by HD_ »

I think it would also be useful in Select Case's as well:

Code: Select all

select case something.somethingelse[0].somethingfurther
  case 0,1,2,3,4,5:
    if (This=2)then
       print "stuff specific to 2"
    endif
    print "stuff related to the other cases and 2"
  case Is<0:
    select case This
      case -1:
        print "serious error!"
      case -2:
        print "Abort now!"
    end select
end select
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

And how, would you propose, we solve the ambiguity between a local this and one from a with block? This brings me back to when I requested nested withs and identifiers:

Code: Select all

dim as integer myReallyLongFoo(0 to 9, 0 to 9)
with myReallyLongFoo(5, 2) as bar
    bar = 10
end with
I think my idea's a good solution, and you could pull it off with macros. If not, use pointers.

Code: Select all

Dim As Integer myReallyLongFoo(0 To 9, 0 To 9)
dim as integer ptr bar = myReallyLongFoo(5, 2)
*bar = 10
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

I agree that it would be totally useful, but as anonymous1337 observes, the "this" conflict can't be overlooked.
I've toyed around with a few syntax ideas, but I'm not sure I like any of them. Maybe allow (With) or (Case) in expressions, but I think that looks ugly and unintuitive.
Maybe ".this", but that would mean disallowing "this" as a field name. And there's no analogy for Select Case.

anonymous1337, your syntax is unambiguous, but it doesn't feel very "BASIC": it looks like "Bar" is supposed to be a Type name.

In the absence of a good syntax, all I can suggest is that you can hack a similar feature for With on a per-type basis with something like:

Code: Select all

#define this_myudt *cptr(my_udt ptr, @.myudtsfirstelement)
But of course, there's always the possibility of sticking it in a variable, and just using that.
It may just be that Select Case and With aren't powerful enough, and can't be extended to be powerful enough, for what you want to do with them.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

anonymous1337, your syntax is unambiguous, but it doesn't feel very "BASIC": it looks like "Bar" is supposed to be a Type name.
It looks to me like type forwarding. I consider my suggestion to be derived/extended from this idea.

It's also consistent, IMO, with open xxx for as #x, where you refer to x when operating on files.
Last edited by anonymous1337 on Sep 26, 2008 13:44, edited 1 time in total.
Eclipzer
Posts: 432
Joined: Oct 01, 2005 10:50
Location: Maryland
Contact:

Post by Eclipzer »

I've toyed around with a few syntax ideas, but I'm not sure I like any of them. Maybe allow (With) or (Case) in expressions
So it's really just a syntax issue:

Code: Select all

with myVar
expr=value
end with

select case myVar
case 10: expr=value
case else
end select
why not use myWith/myCase, or withThis/caseThis, or thisWith/thisCase?
Yeah, I like that last one.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

It might just be a syntax issue. I'll be honest here, and say that I probably don't know the situation much better than you do.

It's partly a syntax issue, partly the fact that, of course, the necessary changes would have to be added to the compiler to support it. I wouldn't expect any internal complications, for someone who knew what they were doing, but I'm not sure.

Simply adding a new keyword would further pollute the namespace though, and none of those suggestions really gel with me.

@anonymous1337, viewed as an extension of the Type forwarding syntax, it still looks like Bar should be a Type. I had forgotten about the "As #f" syntax, but still, it's not really the same thing.

The other thing is that the symbol "Bar" would presumably be a reference to the original object. I have no idea whether the groundwork is laid in the compiler for something like that.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

What about incorporating using into syntax, or, assuming that the . is a stand in when no alias is entered, we can use with foo as reference(x, y): print foo: end with. Makes sense to me.

As for bar being a reference to the original object, isn't that already implemented via . symbol and this? I don't see any problem with using a pointer if that's too difficult.
Eclipzer
Posts: 432
Joined: Oct 01, 2005 10:50
Location: Maryland
Contact:

Post by Eclipzer »

Simply adding a new keyword would further pollute the namespace though, and none of those suggestions really gel with me.
thisWith and thisCase are simply extensions of this which is already a keyword. There's is already a precedent for this in the language with keywords such as: file*, screen*, val*, window*, etc. Personally I feel it works as BASIC syntax and it's easy to read and understand:

Code: Select all

with myVar
  thisWith=value
end with

select case myVar
case n: thisCase=value
end select
if you're looking for a single keyword solution, we could always go with thisExpr or thisBlock. Regardless, it seems silly not to add this on the basis of keyword choice, let's decide on a keyword(s) and get it in.
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Re: THIS support in WITH blocks

Post by notthecheatr »

Hmm. Why not, just for fun, make a that keyword?

Code: Select all

with myVar
  that = value 'that refers to myVar
end with

Code: Select all

with myArray(a,b,c)
  that=value 'that refers to myArray(a,b,c)
end with
It shouldn't conflict with anything (hopefully nobody uses that as a variable name), it fits right in with the syntax, and it even sounds right. What do you think?
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

I constantly use that as a variable name.

Code: Select all

sub Foo.bas( byref that as Foo )
  this.x = that.x
end sub
You'd break nearly all of my code.

And notthecheatr, that doesn't solve our problem. I'm not sure what you were getting at. If we're going to change syntax to support with as varname, we might as well support nested withs with varnames as well.
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Re: THIS support in WITH blocks

Post by KristopherWindsor »

notthecheatr wrote:Hmm. Why not, just for fun, make a that keyword?
theOtherThing = UR_MOM

:P

Maybe that's why I never brought the issue up; if the goal is to improve the syntax, and the result is complicated (new keywords) and limited (still doesn't address the IF expression or allow nested WITH statements to be simultaneously accessible), it's not a good idea.

I think in any case using a pointer would be better, all complications considered. It's more versatile.

Code: Select all

'replacing "with mydata"

dim as myudt ptr withVar = @mydata
with *withVar
  *withVar = [something else]
end with
EDIT: Here's the real way to do it!

With for UDTs:
(Note: does not allow nesting like wit(witVar))

Code: Select all

' Replacing "with x" v1.0
' By Kristopher Windsor

#define wit(v) Scope: Dim As TypeOf(v) Ptr witVarPtr = @v: With *witVarPtr
#define endWit End With: End Scope
#define witVar *witVarPtr

Type x_type
  Declare Sub show ()
  As Integer a, b
End Type

Sub x_type.show ()
  Print a, b
End Sub

#define showall() Print: Print "Vars: ": x.show(): y.show(): z.show(): Print

Dim As x_type x = Type(3, 4), y = Type(5, 6), z = Type(7, 8)

showall()

wit(x)
  .show()
  
  witVar = y
  showall()
  
  .show()
  
  Swap witVar, z
  showall()
  
  .show()
  
  wit(y)
    .show()
  endWit
endWit

Sleep
Select Case for scalars was a bit harder:

Code: Select all

' Replacing "Select Case a" v1.0
' By Kristopher Windsor

#macro selectCase(v)
  Scope
  Dim As TypeOf(v) SCVarTemp = v
  Scope
  Dim As TypeOf(v) thisCase = SCVarTemp
  Select Case thisCase
#endmacro

#define endSelect End Select: End Scope: End Scope

Dim As String a = "apple", b = "boom", c = "pizza!!!"

selectCase(a)
  Case "purple"
    '
  Case "apple", "boom"
    Print "apple? " & thisCase
    thisCase = "happy"
    Print "happy? " & thisCase
    
    selectCase(c)
      Case "55"
        '
      Case "pizza!!!"
        Print "pizza!!!? " & thisCase
        
        selectCase(thisCase + " owns")
          Case "?"
            '
          Case Else
            Print "does pizza own? " & thisCase
        endSelect
        
        Print "now the case should be pizza!!! again: " & thisCase
        thisCase = "deleted variable"
    endSelect
    
    Print "now the case should be happy again: " & thisCase
  Case Else
    '
endSelect

Sleep
:-]
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Post by notthecheatr »

anonymous1337 wrote:I constantly use that as a variable name.

Code: Select all

sub Foo.bas( byref that as Foo )
  this.x = that.x
end sub
You'd break nearly all of my code.

And notthecheatr, that doesn't solve our problem. I'm not sure what you were getting at. If we're going to change syntax to support with as varname, we might as well support nested withs with varnames as well.
:( Why can't you just use nice variable names like everybody else?

Hmm, I suppose if you wanted to do it with nested withs you could. I personally don't use With very often anyways, but I guess it could be useful to nest them.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

Ahah! With Nesting, Mateys!

Code: Select all

#define withAs(x, y) scope: dim as typeof(x) ptr y = @x
#define endWithAs() end scope

dim as integer fooreallylonglong(0 to 5, 0 to 9)
dim as integer barreallylonglong(0 to 3, 0 to 13)

fooreallylonglong(3, 5) = 20

barreallylonglong(1, 9) = 10
barreallylonglong(3, 4) = 5

withAs(fooreallylonglong(3, 5), foo)
	withAs(barreallylonglong(1, 9), bar)
	
		print "Foo in bar scope: " & *foo
		print "Bar in bar scope: " & *bar
			
	endWithAs()	
	withAs(barreallylonglong(3, 4), baz)
	
		print "Foo in baz scope: " & *foo
		print "Bar in baz scope: " & *baz
			
	endWithAs()

	print "Foo in foo scope: " & *foo
''	print *bar ' errors as it should
endWithAs()
sleep
Post Reply