Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Generics] EachIn type conversion #653

Open
thareh opened this issue Mar 8, 2024 · 1 comment
Open

[Generics] EachIn type conversion #653

thareh opened this issue Mar 8, 2024 · 1 comment

Comments

@thareh
Copy link
Contributor

thareh commented Mar 8, 2024

Good day,

Would it be possible for EachIn to be able to convert objects from generics types when possible such as the code below?

Framework BRL.Blitz
Import BRL.Collections
Import BRL.StandardIO
Import BRL.LinkedList

Type TFoo
	
	Field name:String = "foo"
	
EndType

Type TBar Extends TFoo
	
	Method New()
		name = "bar"
	EndMethod
	
EndType

Local gl:TLinkedList<TFoo> = New TLinkedList<TFoo>()
gl.AddLast(New TFoo())
gl.AddLast(New TBar())

'WORKS
For Local f:TFoo = EachIn gl
	Print f.name
Next

'DOES NOT WORK
'For Local f:TBar = EachIn list
'	Print f.name
'Next

Local l:TList = CreateList()
l.AddLast(New TFoo())
l.AddLast(New TBar())

'WORKS
For Local f:TFoo = EachIn l
	Print f.name
Next

'WORKS
For Local f:TBar = EachIn l
	Print f.name
Next

Thanks!

@HurryStarfish
Copy link
Member

HurryStarfish commented Mar 10, 2024

I think I would prefer if this wasn't allowed.
The "old" EachIn using ObjectEnumerator does this by basically casting each element to the type of the variable and then skipping over Nulls, had to do it this way because it couldn't know the type of the objects in the collection, but that also causes issues. IIterable is an opportunity to improve things there.
One problem is that you can't properly use EachIn on a collection that might actually have Nulls in it Some types like String get special treatment, but that makes it even more confusing. Another problem is that it makes it easier to accidentally break your code: Imagine you have a TLinkedList<TBar> and then at some point you change it to a TLinkedList<TFoo> - if you you forget to change one of the For Local f:TBar = EachIn list anywhere in your code, then that loop will now suddenly start skipping some elements, which is a pretty subtle, hard-to-find bug waiting to happen (it has happened to me before). I think it isn't not worth the risk, especially since it's not much harder to do something like

For Local f:TFoo = EachIn list
	Local b:TBar = TBar(f)
	If Not b Then Continue
	Print b.name
Next

to replicate the ObjectEnumerator behaviour explicitly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants