C
C#2y ago
LukeJ

✅ Cannot convert from anonymous type

I have a method that takes effectively extends OrderBy, and so one of it's paramaters is a keySelector, just like in OrderBy
Func<TElement, TKey> selector
Func<TElement, TKey> selector
In my use case I separated out the intended key to make it read better, so here is it's signature
private static int SelectByDistanceFromEnemy(this (Option<Unit> unit, int index) units)
private static int SelectByDistanceFromEnemy(this (Option<Unit> unit, int index) units)
I'm then using it in this code (lambda to call SelectByDistanceFromEnemy just to try make the error easier to parse)
nits.Select((unit, index) => new { unit, index })
.RunOrderedDescending(tpl => SelectByDistanceFromEnemy(tpl),
...
nits.Select((unit, index) => new { unit, index })
.RunOrderedDescending(tpl => SelectByDistanceFromEnemy(tpl),
...
which gives me this error cannot convert from '<anonymous type: LanguageExt.Option<Unit> unit, int index>' to '(LanguageExt.Option<Unit> unit, int index) I don't get why it can't though. The only difference is one is an anonymous type. Anyone know why?
20 Replies
Jayy
Jayy2y ago
I mean... That's the difference lol You are trying to pass in a different type as the declared type
LukeJ
LukeJ2y ago
They're the same tuple, no? (Option<Unit>, int)
Jayy
Jayy2y ago
Wait that's an extension method? just switched to my laptop thats an extension lol, call it as an extension an anonymous type is not a tuple no
LukeJ
LukeJ2y ago
Oh, huh.
MODiX
MODiX2y ago
Jayy#6249
REPL Result: Success
(1,2).GetType() == new {foo=1, bar=2}.GetType()
(1,2).GetType() == new {foo=1, bar=2}.GetType()
Result: bool
False
False
Compile: 433.751ms | Execution: 72.370ms | React with ❌ to remove this embed.
Jayy
Jayy2y ago
like how would these ever be the same thing? they are totally different
LukeJ
LukeJ2y ago
Do you have to be like that about it?
Jayy
Jayy2y ago
im not being a dick im just trying to make you think
LukeJ
LukeJ2y ago
If you say so.
Jayy
Jayy2y ago
(1,2) has the type ValueTyple<int, int>
LukeJ
LukeJ2y ago
And the answer of why is because I never real deal with anonymous types and didn't notice this was actually something different.
Jayy
Jayy2y ago
new foo=1, bar=2 is an autogenerated compiler type actually hmm how is that represented
MODiX
MODiX2y ago
Jayy#6249
REPL Result: Success
new {foo=1, bar=2}.GetType()
new {foo=1, bar=2}.GetType()
Result: RuntimeType
<>f__AnonymousType0#1`2[[System.Int32, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Int32, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], ℛ*d0605842-64ac-48ed-bb56-e8a1083db780#2-0, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
<>f__AnonymousType0#1`2[[System.Int32, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Int32, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], ℛ*d0605842-64ac-48ed-bb56-e8a1083db780#2-0, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Compile: 381.479ms | Execution: 62.034ms | React with ❌ to remove this embed.
Jayy
Jayy2y ago
f__AnonymousType0#12<int, int> and tuple is ValueTuple<int, int> ValueTuple is also a struct an anon type is a class
LukeJ
LukeJ2y ago
Ah. So is there an easy conversion here?
Jayy
Jayy2y ago
there is no conversion no this might work tho
nits.Select((unit, index) => (unit, index))
nits.Select((unit, index) => (unit, index))
im not actually sure if you can do extensions on tuples like that tho hmm
LukeJ
LukeJ2y ago
Oh, that totally works. I didn't even realise I was making them as anons and not tuples. Which is weird because I don't seem to be doing that anywhere else in my code... oh well. Learnt something today at least. Thanks.
MODiX
MODiX2y ago
Jayy#6249
REPL Result: Success
public static void Test(this (int, string) foo) => Console.WriteLine(foo.Item2);

var a = (1, "hi");

a.Test();
public static void Test(this (int, string) foo) => Console.WriteLine(foo.Item2);

var a = (1, "hi");

a.Test();
Console Output
hi
hi
Compile: 586.269ms | Execution: 73.390ms | React with ❌ to remove this embed.
Jayy
Jayy2y ago
there we HmmNoted never actively realized you could do that, filing away for later
Accord
Accord2y ago
Closed!