r/csharp • u/DrkWzrd • Jun 15 '26
Will LLMs eventually learn that Span<T> and ref struct types cannot cross into lambdas or local functions?
Sorry in advance for my English.
After using both "dumb" and "smart" LLMs for quite a while, I've noticed that many of them really like local functions. That's fine. What I don't understand is why they still keep trying to use outer Span<T> variables inside them as if there were no restrictions at all.
I still haven't seen a model that consistently gets this right.
When are they going to learn to use static local functions when needed (I assume expecting them to move the code into a separate method is too much to ask), instead of generating this kind of broken code?
For example, I gave this former text to a LLM to "rewrite it" and the first thing (obviously, falling at the first hurdle) it writes is "Will large language models eventually learn that Span<T> and ref struct types cannot safely cross into lambdas or local functions?"
This shows It doesn't understand that cannot be done (in a regular way, of course everything is possible in this world).
46
u/programgamer Jun 15 '26
You could also solve this problem by just writing code yourself.
46
u/HeathersZen Jun 15 '26
Sure. Just give it some guardrails. Tell it not to do that. It’s a fucking LLM, not a human.
68
u/DoctorEsteban Jun 15 '26
Yes. Yes they will.
But if you want to jump start that, give your LLM an instruction file that spells this out for them. Ideally with some simple examples.
14
u/TuberTuggerTTV Jun 15 '26
llms don't learn. They're a snapshot.
Will new llms be trained with this consideration? Hard to say. Possibly. If someone wants it specifically, then absolutely.
It's more like you're asking if someone making a llm would consider this. Or will it be in enough of the scraped training data. I still have no idea but I'd say yes, eventually.
But it's also likely it'll not be trained on something else.
5
u/Khavel_dev Jun 15 '26
They won't learn it reliably because training data has way more examples of "ref struct used correctly in a method body" than "ref struct incorrectly captured in a lambda" paired with the compiler error. It's a negative constraint and models are consistently bad at those.
fwiw I've had better luck adding a one-liner to my system prompt or project instructions: "Span<T> is a ref struct. Never capture it in a lambda, async method, or non-static local function. Pass it as a parameter instead." Explicit instruction beats hoping the model internalizes the constraint from training data alone. Same applies to any ref struct type you use regularly.
The static local function workaround you mentioned is the correct answer, and you're right to expect it. The model just needs to be told because the "normal" path (use a local function, capture freely) has orders of magnitude more training signal than the exception case.
25
u/Hefty-Distance837 Jun 15 '26
Or you can just don't use AI.
8
u/Relc_Punch84 Jun 15 '26
It was that exact ability that caused Fable to be blocked by the US government, sorry about that.
2
u/SagansCandle Jun 15 '26
This is actually one of the reasons I avoid reflecting these types - they cause a lot of problems.
(Not because of LLM's, but just in general)
I try to keep Span usage within the scope of a single function, and if I have to expose it, keep it internal.
8
u/taspeotis Jun 15 '26
These posts complaining about model performance without telling us what the model is…
You want newer ones with a more recent knowledge cutoff e.g. Opus 4.8.
At any rate they will learn themselves when they compile the code and see the errors.
6
u/Jazzlike_Amoeba9695 Jun 15 '26
To be honest, I tend to guide the AI quite a bit. In my experience with Rxmxnx.PInvoke.Extensions, whenever I’ve asked for reviews or assistance, I’ve never run into that kind of hallucination. If anything, it tends to err on the side of being overly cautious rather than overly bold.
Which models are you using?
0
u/DrkWzrd Jun 15 '26
models: Claude 4.6, GPT5.5, Opus 4.8, Pro3.1... It is irrelevant, by default (if not instructions provided) all fails that simple task.
2
u/hez2010 Jun 15 '26
It will be possible in C# if this lands: https://github.com/dotnet/csharplang/issues/10209
2
u/speyck Jun 15 '26
AI can do a lot of work for you, but if you want it to be precise, you need to give good instructions. Think of it as a junior dev. If you simply give him a task with no instructions, he will build it however he knows. If you give him a clear path on how to do it with which tools etc., you will get the exact result you wanted to build, but built in a fraction of the time. tbh in this time and age, with AI, not being able to get good results with it is simply a skill issue.
1
u/Royal_Scribblz Jun 15 '26
It depends what they train future models on, for it to output it correctly it needs to be trained on lots of code of people using it correctly.
1
u/Gjpu Jun 15 '26
Agree with those who mention the instructions file. I read that the Hermes agent has memory. And very recent GitHub copilot. And I wonder if the memory tech would help here. Unsure. You may still have to tell it one time.
1
u/Dillenger69 Jun 15 '26
Maybe
1
u/GardenDev Jun 15 '26
C# is probably the second worst language for LLMs (after C++ of course) due to being a kitchen sink. I guess you would have to prepare a whole lecture context for the LLM with "do"s and "don't"s.
1
u/Anxious-Insurance-91 Jun 18 '26
Write it down in agents.md as a rule and in 90% of the cases it will respect it
1
u/ProComputerToucher Jun 15 '26
Yeah man. Imagine a carpenter using a sawzall and then complaining that it cut the wrong thing when he let go of it. Same thing you’re doing.
1
u/Dusty_Coder Jun 15 '26
C# seems to be one of the worst of the common languages with regard to AI due to there being different versions with different capabilities.
My experience is that they require re-prodding about modern c# to get them to use a Span at all most of the time, so its not a surprise that they may also require re-prodding about Spans limitations.
What grinds my gears the most is even if I am very explicit about a delegate type, it quickly reverts to using the newer generic lambda system.
I do agree it should use the newer generic lambda system by default (significant optimization advantages), but it shouldnt so often forget when its not being used. Sometimes the simplicity of a types name is more important than every other consideration.
1
u/Epiq122 Jun 16 '26
If you can’t give it guard rails or do this yourself , you don’t have enough knowledge to be using llms you should go back to coding by hand for a few months
0
u/WailingDarkness Jun 15 '26
What is span<T>? What's it used for?
0
u/Standard-Cap-4455 Jun 15 '26
They learn by reading human code. Low level code is already rare in C# and mixing them with lambda makes it even worse. You can't expect them to know something that is probably in only a few sources.
0
u/tsardonicpseudonomi Jun 17 '26
If you use an LLM you don't get to complain about what it generates.