Live data from Hacker News

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

github.com

51–60 of 71 posts

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

#51
post #47

Earlier quoted context omitted.

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.

[dead]

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

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

[dead]

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

#53
post #47

Earlier quoted context omitted.

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.

  > AOT requires a lot of fiddling around, and might break the application unexpectedly, with very weird errors.
It hasn't been my experience. Native AOT does come with some limitations [1][2], but nothing awful. Mostly it's that you can't generate code at runtime and you have to tame the code trimmer. Just don't ignore code analysis warnings and you should be good.

  > It is mostly targeted to Blazor (WASM) and for serverless functions.
Making your CLIs start fast is also a huge use case.

[1]: https://learn.microsoft.com/en-us/dotnet/core/deploying/nati...

[2]: https://learn.microsoft.com/en-us/dotnet/core/deploying/trim...

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

#54
post #2

This seems like the kind of feature that should be built into MSBuild.

The use cases are different. While MSBuild tasks run during build (and partially when loading a project), typically the IDE is oblivious what happens there. The source generator runs directly inside the compiler infrastructure and thus you didn't get error highlights for code that would otherwise be only generated during build but not as you type. This makes it much more friendly than pure build-time generation of code.

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

#55
post #43
post #41

Earlier quoted context omitted.

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.

I see your point and I don't necessarily disagree but better API for embedded resources is just a couple of simple extension methods away. I wouldn't even bother adding an external package for it.

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

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

One man's constant is another man's variable.

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

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

There's also bflat [0]. Not an official Microsoft product, more of a passion project of a specific employee.

"C# as you know it but with Go-inspired tooling that produces small, selfcontained, and native executables out of the box." Really impressive. Self contained and small build system.

[0] https://github.com/bflattened/bflat

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

#58
post #14

Earlier quoted context omitted.

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 altog…

You add PolySharp to your source generator project to get back some of the modern C# features. https://github.com/Sergio0694/PolySharp

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

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

Adding to the sibling comment, there are also Frozen{Dictionary,Set}: https://learn.microsoft.com/en-us/dotnet/api/system.collecti...

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

#60
post #8

I think Zig really shines here: https://ziglang.org/documentation/master/#comptime

I’m annoyed they called it comptime when it isn’t the same as Zig’s more powerful comptime.

You can think of zig’s comptime as partial evaluation. Zig doesnt have a runtime type reflection system, but with comptime it makes it feel like you do.

Post reply on HN