Hacker News new | ask | show | jobs
by noblethrasher 4153 days ago
But, structs pay a very small runtime penalty, if at all, right?

The verbosity will be mostly ameliorated with primary constructor. I propose going further by allowing programmers to annotate constructors so that they can be used as user-defined conversion operators.

Thus, the aforementioned code could become something like:

	 public struct GreaterThanFive implicit (int n)
	 {
	 	readonly int n = n;

	 	if(n <= 5)
	 	    throw new ArgumentException("n must be greater than 5");	 	

	 }
1 comments

Traditionally, structs paid a large codegen penalty, as a lot of optimizations and stuff were turned off. It also seemed like the codegen wasn't super smart about passing them around. Maybe that's all changed.

I meant the verbosity of having to create a custom type for each kind of restriction, versus some inline "int n [n > 5]" notation.

In addition to the benefit of brevity, something like your proposal also has the virtue of being compatible with the `checked/unchecked` keywords.