Live data from Hacker News

Comptime – C# meta-programming with compile-time code generation and evaluation

github.com

41–50 of 71 posts

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#41
post #34

Does this finally allow reading string literals from files and include them into the binaries? I've seen a Go project that was heavily using https://pkg.go.dev/embed for loading some template files, and got a bit jealous. Back in the days of .NET Framework it was common to compile file contents into resource files, but it was always quite cumbersome.

Can't you already do that with embedded resources?

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#42
post #34

Does this finally allow reading string literals from files and include them into the binaries? I've seen a Go project that was heavily using https://pkg.go.dev/embed for loading some template files, and got a bit jealous. Back in the days of .NET Framework it was common to compile file contents into resource files, but it was always quite cumbersome.

C# / .NET has always had that abililty.

   
Then you can read that file from the assembly with:

  assembly.GetManifestResourceStream
I'm not sure what's awkward about that, but it's nothing new.

Discoverability for this is in visual studio, if you go to file properties there's a drop-down where you choose between Content, EmbeddedResource, Ignore, etc.

That determines what happens in compile, whether it is copied to output directory, compiled into the binary, etc.

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#43
post #41
post #34

Does this finally allow reading string literals from files and include them into the binaries? I've seen a Go project that was heavily using https://pkg.go.dev/embed for loading some template files, and got a bit jealous. Back in the days of .NET Framework it was common to compile file contents into resource files, but it was always quite cumbersome.

Can't you already do that with embedded resources?

Yes, you can. But it's a bit cumbersome. The API to read them is not really intuitive, they are only accessible as a Stream, so they need to be either read every time into a string (new allocation, slow), or you need a helper that reads them once and keeps them in memory. I think there are also a lot of gotchas around naming and listing them.

In modern code I don't see them that often anymore.

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#44
post #42
post #34

Does this finally allow reading string literals from files and include them into the binaries? I've seen a Go project that was heavily using https://pkg.go.dev/embed for loading some template files, and got a bit jealous. Back in the days of .NET Framework it was common to compile file contents into resource files, but it was always quite cumbersome.

C# / .NET has always had that abililty. Then you can read that file from the assembly with: assembly.GetManifestResourceStream I'm not sure what's awkward about that, but it's nothing new. Discoverability for this is in visual studio, if you go to file properties there's a drop-down where you choose between Content, EmbeddedResource, Ignore, etc. That determines what happens in compile, whether it is copied to output…

The Stream is awkward. It needs to be processed either once at startup or lazy on access, or every time it's used (allocations!). Also discovering multiple files is awkward.

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#45
post #35

I started using C# recently for a hobby project writing a game engine on top of Monogame, and I have been very surprised at how nice of a language C# is to use. It has a clean syntax, decent package management, and basically every language feature I regularly reach for except algebraic data types, which are probably coming eventually. I think the association of .NET to Microsoft tarnished my expectations.

Modern C# and .NET are great. It still suffers from the bad reputation of the Windows-only .NET Framework. It's still a quite heavy platform with a lot of features, but the .NET team invested a lot of time to make it more approachable recently. With top level Programs and file-based apps[1] it can be used as a scripting language now, just add a shebang (#!/usr/local/share/dotnet/dotnet run) to the first line and make…

Also, if you compile Ahead Of Time (AOT) you can cut down on the features and get basically as small a subset as you want of the libraries. IMHO C# and dotnet are really starting to become very impressive.

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#46

I started using C# recently for a hobby project writing a game engine on top of Monogame, and I have been very surprised at how nice of a language C# is to use. It has a clean syntax, decent package management, and basically every language feature I regularly reach for except algebraic data types, which are probably coming eventually. I think the association of .NET to Microsoft tarnished my expectations.

I love C# and .NET and use it every day.

Sometimes I wonder if the .NET department is totally separated from the rest of Microsoft. Microsoft is so bad on all fronts I stopped using everything that has to do with it. Windows, Xbox, the Microsoft account experience, the Microsoft store, for me it has been one big trip of frustration.

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#47
post #35

Earlier quoted context omitted.

Modern C# and .NET are great. It still suffers from the bad reputation of the Windows-only .NET Framework. It's still a quite heavy platform with a lot of features, but the .NET team invested a lot of time to make it more approachable recently. With top level Programs and file-based apps[1] it can be used as a scripting language now, just add a shebang (#!/usr/local/share/dotnet/dotnet run) to the first line and make…

Also, if you compile Ahead Of Time (AOT) you can cut down on the features and get basically as small a subset as you want of the libraries. IMHO C# and dotnet are really starting to become very impressive.

AOT requires a lot of fiddling around, and might break the application unexpectedly, with very weird errors. It is mostly targeted to Blazor (WASM) and for serverless functions.

The default runtime and JIT are fine for most use cases.

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#48
post #46

I started using C# recently for a hobby project writing a game engine on top of Monogame, and I have been very surprised at how nice of a language C# is to use. It has a clean syntax, decent package management, and basically every language feature I regularly reach for except algebraic data types, which are probably coming eventually. I think the association of .NET to Microsoft tarnished my expectations.

I love C# and .NET and use it every day. Sometimes I wonder if the .NET department is totally separated from the rest of Microsoft. Microsoft is so bad on all fronts I stopped using everything that has to do with it. Windows, Xbox, the Microsoft account experience, the Microsoft store, for me it has been one big trip of frustration.

Microsoft is huge, it's many companies inside one company.

.NET seems to be somewhere close to Azure, but now far away from Windows or the business applications (Office/Teams, Dynamics, Power Platform). Things like GitHub, LinkedIn or Xbox seem to be de facto separate companies.

Edit: .NET used to be tied closely to Windows, which gave it the horrible reputation. The dark age of .NET ;)

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#49
post #36

Earlier quoted context omitted.

Immutable collections exit, they were just added later. See System.Collections.Immutable: https://learn.microsoft.com/en-us/dotnet/api/system.collecti...

I do a lot of .NET programming, and I've never seen them getting used. :O

Roslyn, the C#/VB compiler, uses them extensively, but in other code they're indeed quite rare.

Re: Comptime – C# meta-programming with compile-time code generation and evaluation

#50
post #14

C# meta programming game is strong. Source generators are :chefs_kiss:

I love (and heavily use) source generators, but the development experience is godawful. Working with the raw Roslyn types is painful at best and this is compounded by them having to be written against .NET Standard, severely limiting the use of newer .NET functionality. Eventually I want to write a good baseline library to use for my source generators -- simplifying finding definitions with attributes, mapping types…

I agree, .NET Standard limitation unnecessarily complicates development experience. I think it's because some tools (Visual Studio) is still use legacy .NET Framework. I don't understand why they didn't integrate them via out of process architecture into these tools, since source generators didn't exist in the legacy framework anyway.

I sometimes generate code from plain CLI projects (avoiding source generators altogether), as whole debugging and DX is so much better.

Post reply on HN