Pending Review

Type inference is too strict on generic Bolt type arguments

Elvisish 8 months ago updated 7 months ago 4

Works if I check the index against the list:

@Reality.Stop() mentioned on the discord:

yes, if the output of the foreach loop is connected to something that has to do type inference, it currently breaks[3:50 PM]which also can't means you can't plug a gameobject into a transform operation without a Get Transform in between, etc. If the type of the Item doesn't match the expected type you feed it into, it gives a very confusing error message. It's like instead of trying to determine what type to transform to going forward, it's trying to transform in reverse (in your example, it's like it thinks the entire For Each should be a list of whatever class "Has Weapon" is on

Bolt Version:
Unity Version:
Scripting Backend:
.NET Version (API Compatibility Level):
Bolt 2
Need More Information

Hi Elvishish,

Thanks for the report.

From a quick glance, it looks like a conflict in the type inference for the For Each unit.

What is the type of "Weapons List" on your component exactly? Is it List<GameObject>?

Yes, it's a gameobject list:

Pending Review

Hi Elvishish,

Thanks for the report. I think I understand what's happening at a glance:

  • Type inference deducts that you need a List<Weapon> as an input because you use Weapon.HasWeapon as a unit form the result
  • But you pass a List<GameObject> as an input instead
  • Bolt is able to convert from GameObject <=> Weapon and vice versa, but not to List<GameObject> <=> List<Weapon> (because there's no clear notion of generics in the Bolt type system)

One thing you can do to fix your first loop more elegantly is to use an Is unit, to check if the Item game object is a Weapon, and then call HasWeapon.

I'll leave this issue open in the mean time to figure out if there's a better way Bolt could be smart about that conversion.

I'm getting the same error with this:

InvalidCastException: Specified cast is not valid.
(wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr)