Live data from Hacker News

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

github.com

31–40 of 71 posts

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

#31
post #26

• Supported return types: ◦ Collections: ..., List , ... ◦ Note: Arrays are not allowed as return types because they are mutable. Use IReadOnlyList instead. I don't understand. Why is List allowed then if it's mutable?

Array also implements IReadOnlyList if I'm not mistaken.

I think C# doesn't really have immutable collections, they just can be typecasted to IReadonly* to hide the mutable operations. But they can always be typecasted back to their mutable base implementation.

The only real immutable collections I know of, are F#s linked lists.

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

#32

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

I used to think so too, but I do have to complain about Microsoft doing the Microsoft thing - they have a brilliant idea (and execution), but they bury it in so much boilerplate that 99% of people who would use it are put off by it.

The amount of stuff you have to wade through here compared to something like comptime in Zig (Roslyn API, setting up the project, having VS recognize it and inject it in the compiler, debugging etc) makes usage of these an absolute pain.

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

#33
post #31
post #26

• Supported return types: ◦ Collections: ..., List , ... ◦ Note: Arrays are not allowed as return types because they are mutable. Use IReadOnlyList instead. I don't understand. Why is List allowed then if it's mutable?

Array also implements IReadOnlyList if I'm not mistaken. I think C# doesn't really have immutable collections, they just can be typecasted to IReadonly* to hide the mutable operations. But they can always be typecasted back to their mutable base implementation. The only real immutable collections I know of, are F#s linked lists.

Immutable collections exit, they were just added later. See System.Collections.Immutable:

https://learn.microsoft.com/en-us/dotnet/api/system.collecti...

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

#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.

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

#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 it executable. It will still compile to a temporary file (slow on first run), but it doesn't require a build step anymore for smaller scripts.

[1]: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals...

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

#36
post #31

Earlier quoted context omitted.

Array also implements IReadOnlyList if I'm not mistaken. I think C# doesn't really have immutable collections, they just can be typecasted to IReadonly* to hide the mutable operations. But they can always be typecasted back to their mutable base implementation. The only real immutable collections I know of, are F#s linked lists.

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

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

#38

Makes me think of Boo language; Boo was so good at metaprogramming and multi-phasr programming! A very fine .NET language that was so far ahead of the curve, with having the tools of that language be usable at runtime. Alas many of the docs are offline now. But it had great quasiquotes, which let you write code that gets turned into AST that you can then process. Good macros. A programmable compiler pipeline. So much…

The syntax looks a bit like F#. But F# only has a few features that generous people might consider meta programming.

There was a lot of fuss about meta programming around 10-15 years ago, but it never got a lot of traction. Maybe for a good reason? I think a lot of the problems it solved, were also solved by functional programming features that slowly appeared in C# over the years.

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

#39
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.

[deleted]

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

#40

How is Comptime different from default AOT compilation? I assume it can work with third-party libraries that AOT don’t work with yet?

It's a code generator that runs during compile time. It's a source generator that adds some generated code files to the project. So it runs way before AOT or JIT. Once AOT/JIT run, Comptime is already invisible to them, they only see the generated code from Comptime.
Post reply on HN