4

I know that the similarities between boo and Python are only superficial, but still, how can I do an equivalent of the following Python code in boo?

a = 'a'
del a
a = 1

I've tried

a = 'a'
a = null
System.GC.Collect()                                                                                                                                                    
System.GC.WaitForPendingFinalizers()
a = 1

But booish tells me it cannot convert 'int' to 'string'.

The point is not to use the duck type. Is that possible?

Edit: As per comment suggestions, changed del from function to statement.

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
eje211
  • 2,385
  • 3
  • 28
  • 44
  • Why don't you simply use a different name? (I dont' know boo, but it seems it doesn't allow the type of a variable to change inside a single scope, i.e. it seems to be statically typed.) – Sven Marnach Jun 06 '11 at 20:55
  • `del` is a statement not a function in python. – Zach Kelling Jun 06 '11 at 20:58
  • Well, the point of this is to find out how to delete a variable. The type change is really only for testing. I'm trying to understand boo more than anything at this point. I've mostly been an open-source guy; I'm new to .NET . – eje211 Jun 06 '11 at 20:58
  • @zeekay: `del(a)` will work anyway, though you wouldn't need it here at all. – Sven Marnach Jun 06 '11 at 20:59
  • @Sven @zeekay: del(a), or del a, would not be needed here, but I'm looking for the boo equivalent, so wrote it anyway, if that makes sense. – eje211 Jun 06 '11 at 21:01
  • 1
    @eje211: I would strongly suspect that this is not possible. In statically typed languages, variables can go out of scope, but they can't be "deleted" in the sense names can be deleted in Python. Maybe you have to find out how to create a scope in Boo. (Again, I'm just guessing, I don't know that language!) – Sven Marnach Jun 06 '11 at 21:02
  • @Sven Yeah, it works in *this case*. You can also call `print(1)` in Python 2.x but it's similarly improper. Since it's a statement it can only be used syntactically where a statement is valid. – Zach Kelling Jun 06 '11 at 21:05

2 Answers2

3

This is not possible in Boo. Boo uses implicit static-typing. Thus, all variables are strongly typed based on their first assignment within their scope. Once assigned, the type of the variable cannot change.

Garbage collecting will ensure that objects on the heap which are no longer referenced will be removed from the heap, but it has no effect on each language's individual variable scoping. In Boo, a variable does not go out of scope until the end of the block in which it was defined.

Duck typing in Boo is handled by the compiler, which assigns it the static type of "object" and then uses runtime code to effect the duckiness of what the underlying .NET runtime considers a generic object.

Marcus Griep
  • 8,156
  • 1
  • 23
  • 24
0

you can declare a as object

a as object a = "letter_a"

print a "letter a"

a = 1 print a 1

I like more boo than python!

clagccs
  • 2,224
  • 2
  • 21
  • 33