News for hackers. Stuff that matters.
Re:I don't need your query language by tester756 on Saturday June 17, 2023 @07:14AM (#36369149)
C#'s LINQ (query syntax, not methods) got it right

var result = from s in stringList where s.Contains("Tutorials") select s;

Re:I don't need your query language by turboponyy on Saturday June 17, 2023 @07:32AM (#36369289)
The work was already done for them, as LINQ is just do-notation from Haskell.
Re:I don't need your query language by DaiPlusPlus on Saturday June 17, 2023 @12:24PM (#36371642)
No, Linq is *not* in any way like Haskell's `do`.

Linq is a (reasonably) compromised, non-referentially-transparent, implementation of a restricted form of relational-algebra with side-effects permitted, so I'm not comfortable describing Linq as "monadic".

Whereas Haskell's `do` is strictly monadic.

Re:I don't need your query language by DaiPlusPlus on Saturday June 17, 2023 @07:22AM (#36369215)
or just `var result = stringList.Where( s => s.Contains("Tutorials") )`

I can't stand the non-extension-method Linq syntax: the _only_ place where it offers a readability improvement over ext-methods is using `join` - but I hardly ever do that in Linq anyway.

Also, in both my code and yours, `result` will be a lazy-evaluated `IEnumerable<String>` which may be undesirable - which means it's probably a good idea to use `.ToList()` to materialize it - which means having to use ext-methods anyway - and mixing both syntaxes in the same expression is aesthetically atrocious.

Re:I don't need your query language by rjbwork on Saturday June 17, 2023 @08:54AM (#36369789)
>I can't stand the non-extension-method Linq syntax: the _only_ place where it offers a readability improvement over ext-methods is using `join`

Hah! I tell the juniors this all the time :D. This seems to be basically the consensus among the C# community these days as far as I can tell as well.

Re:I don't need your query language by recursive on Saturday June 17, 2023 @09:53AM (#36370161)
I guess I'm not in the C# community. `let` is pretty annoying to reproduce with extension methods. Also, when you use multiple `from` clauses, you get access to all scopes, whereas with `SelectMany` you only get the parameters of your current lambda.
Re:I don't need your query language by DaiPlusPlus on Saturday June 17, 2023 @10:01AM (#36370228)
> you get access to all scopes

Read: "you create new heap-allocated closures which wreck your Linq expression's runtime performance"

Or:

"you create Linq queries that cannot be translated into SQL"

Re:I don't need your query language by recursive on Saturday June 17, 2023 @12:31PM (#36371723)
It was all in memory. To the extent that it's a performance tradeoff, it might be one that's worth making depending on the use.
Re:I don't need your query language by DaiPlusPlus on Saturday June 17, 2023 @01:24PM (#36372266)
Aye - but the annoying thing is that it was never necessary: with a few subtle changes to Linq it’s possible to have allocation-free closures by passing state via hidden parameters on the stack - but just like every language out there we’re now hobbled by decisions made 15 years in the past.

——

On a related note, it’s interesting just how unpopular so many new C# language features are (just by looking at the numbers of Thumbs-down reactions on the GitHub Issues/PRs - stuff like top-level Main. It feels like C#’s LDT wants to be like Swift, but without Swift’s willingness to ditch ill-conceived features after a few years… but I think C# would be well-served by taking an axe to some language-features by now - like keyword-Linq and CLS-compliance (honestly, do any ISAs today still lack hardware support for unsigned ints?)

Re:I don't need your query language by recursive on Saturday June 17, 2023 @02:30PM (#36372999)
Interesting.

I think there are some otherwise-seldom-used linq overloads for doing just that. Maybe on Join() or GroupJoin() or something like that. Query expressions use them for compilation to keep closure use down, but I don't know too much about it.

Anyway, if top-level statements are wrong, I don't ever want to be right. I had no idea there was any controversy on that. It's trivial to add the boiler-plate back in if you want it.

The main thing that bugs me is that Expression<> is stuck with language features that existed in C#4, even when there are trivial lowerings. e.g. `is not null` could be `!= null`. (This example ignores operator implementations, but most IQueryables ignore more than that already) Even better, add AST node types for the new language features.

Re:I don't need your query language by Jochim on Saturday June 17, 2023 @03:55PM (#36373941)
I'm not sure the Github issues are the best reflection of the popularity of those features.

Top level statements and the new HostBuilders are fantastic, they're much cleaner and easier to follow than the previous mess. Add Minimal APIs to the mix and C# is finally a viable choice for spinning up something quickly.

Re:I don't need your query language by Volrath89 on Saturday June 17, 2023 @09:39AM (#36370061)
I completely agree with you, in the context of C# code. But also take into account not everybody in HN is a C# developer, and OP syntax works better as an example of his point. All non C# devs will understand his "sql like" example easier.
Re:I don't need your query language by recursive on Saturday June 17, 2023 @09:48AM (#36370122)
> the _only_ place where it offers a readability improvement over ext-methods is using `join`

No love for let?

    from item in items 
    let frob = Expenseive(item.P1) 
    where frob > 3 
    selec new { frob, item }
Re:I don't need your query language by DaiPlusPlus on Saturday June 17, 2023 @09:58AM (#36370198)
That can be done with ValueTuples instead of Anonymous Types: `items.Select( i => ( i, frob: Expensive( i.P1 ) ) ).Where( t => t.frob > 3 );`

...and ValueTuples are superior to Anonymous Types because you can actually return them from a function or use them as parameters - whereas Anonymous Types cannot cross method-call boundaries (excepting using generics for pass-through).

Anonymous Types in C# were a massive mistake. They should be [Obsolete]'d, IMO.

Re:I don't need your query language by recursive on Saturday June 17, 2023 @12:34PM (#36371755)
The tuple might be gone by the time the scope ends. I'm aware it's possible to reproduce the behavior without let. The code just looks worse.
Re:I don't need your query language by tester756 on Saturday June 17, 2023 @01:39PM (#36372434)
chill,

I've been just showing that *approach* of starting SQL query from "from" part is viable, because that's how Microsoft timplemented in one of two LINQ "API"s"

I'm not trying to convince anyone to use LINQ's Query Syntax.

Re:I don't need your query language by Reitet00 on Saturday June 17, 2023 @03:33PM (#36373734)
I wonder if that could be addressed at the spec level allowing reverse order of these keywords. It doesn't seem complex on the surface and the the engines could slowly add support for it.
Re:I don't need your query language by christophilus on Saturday June 17, 2023 @07:22AM (#36369218)
LINQ and proper reflection are what I miss most from C#. (I’m all Typescript at the moment.)
Re:I don't need your query language by rjbwork on Saturday June 17, 2023 @08:55AM (#36369799)
Gods, I tried to do some reflection stuff in a Node TS project I got put on about 18-24 months ago, and it was a total shit show. It was really disappointing to see how lacking the actual runtime capabilities of TS are.
Re:I don't need your query language by hfkwer on Saturday June 17, 2023 @09:15AM (#36369921)
There are no TS runtime capabilities. This isn't a thing.
Re:I don't need your query language by rjbwork on Saturday June 17, 2023 @10:40AM (#36370582)
There are some libraries that allow you to do some stuff. You can attach metadata to some objects about types in some contexts. But obviously it's bolted on because under the hood it's all Javascript.
Re:I don't need your query language by baq on Saturday June 17, 2023 @04:55PM (#36374494)
What runtime capabilities? There shouldn’t be any?
Re:I don't need your query language by DaiPlusPlus on Saturday June 17, 2023 @12:21PM (#36371611)
What does "proper reflection" mean to you?
Re:I don't need your query language by ledgerdev on Saturday June 17, 2023 @08:02AM (#36369485)
I recall Anders saying on some podcast they did that so they could provide auto-complete. Funny thing is that I used it for a couple years, but almost never use the linq syntax any more, and not sure why.
Re:I don't need your query language by cerved on Saturday June 17, 2023 @11:04AM (#36370793)
Probably because if you wanted to write something like SQL, you would write it in SQL, and if you want to write C#, you write C#

Linq query syntax is just dumb

Re:I don't need your query language by jmartrican on Saturday June 17, 2023 @11:30AM (#36371072)
I'm so jealous that Java does not have its own LINQ.
Re:I don't need your query language by Luctct on Saturday June 17, 2023 @02:34PM (#36373052)
Tcl dicts do something very similar, and Tcl is very well integrated with SQLite.