__dil__
Explore posts from servers❔ Rider's "cleanup on save" adds named parameters seemingly at random
I have a function with this signature:
Then, when I call it like so:
Rider will automatically add a named parameter like
serialNum: ref serialNum,
. It only adds it to this particular parameter. I don't understand why?
I there's a particular reason I'm happy to keep it as is, but it just seems random. Anybody knows what's happening?3 replies
❔ How to make Rider format and cleanup code by default?
Everytime I create a new solution, I have to go into the options and enable "actions on save" -> "format and cleanup". How can I change it so it's there by default when I create a new solution?
18 replies
❔ Prevent Rider from removing "redundant" named arguments.
As per the title, I have constructors which take a lot of arguments. I use the constructors to manually instantiate a bunch of stuff in a list, like this:
A lot of these arguments have the same type and it's possible that I might add/remove some arguments as the project evolves.
So, for the sake of readability and not breaking code when I change the constructors, I'd like all the arguments to be named. Unfortunately, Rider removes the annotations that it thinks are redundant. That's usually fine, but in this specific instance I want full clarity and consistency.
Any way I can achieve this without globally disabling code cleanup?
2 replies
❔ [Avalonia] Basic question regarding subclassing
Let's say I have some types like so:
I'd like to display a
List<Foo>
where FooA
s and FooB
s would be displayed completely differently.
Just as a random example, FooA
would simply display "This is a FooA" with a red background while FooB
would display "This is a FooB with name '...' and age '...' " in a blue background. You get the idea.
The way I envision this is that FooA
and FooB
would themselves define how they should be displayed.
I would be really grateful if someone could break this down to me. Bonus points if there's a working example.60 replies
❔ Design question
Hi! I'm a beginner and I'm having trouble finding a decent design for this toy application I'm making.
So, at a high level, I have a list of items, and I'd like to let the user filter the items and only display the relevant ones. So far so good, but the complexity comes from the fact that the items in my case come in many variants (let's call them A, B, C, etc. for the sake of the discussion). Item variants are definitely related in terms of the domain, but they are fairly unrelated when it comes to the data itself. So, A might have a little bit in common with C, B might have a little in common with A, etc. There's very little that is common to all the variants.
So, all the items should live in the same list to be filtered, but the filter might refer to a field that doesn't even exist on a specific variant (in that case it should be excluded). Furthermore, once the items have been filtered, it should be possible to select a particular item and see all the data specific to that variant. So I need some sort of downcasting mechanism.
I'm sorry if this sounds confusing, well I am confused :/
14 replies
❔ Dependency Injection
Disclaimer: I come from a low-level language, never did Java and very new in C#.
I started to read the Avalonia UI MVVM tutorial, and it mentioned that usually you'd use DI to avoid coupling the model and the viewmodel.
This got me curious about what exactly is DI. My understanding is that you have a service that uses a dependency. Instead of using the dependency directly, you make the service use an interface for that service, and provide an instance of that interface somehow.
So, the "replace concrete type with interface" I get.
My main confusion is about how to provide the instance. From what I read, it's either
1) passing a concrete type instance to a constructor that expects the interface type.
Or
2) Something involving "frameworks and "automatic registration".
Option 1) is pretty intuitive, not much to be confused about here, but option 2) is complete gibberish to me.
57 replies
✅ I don't understand how to test my project.
I have an existing command line project. Since this is a pretty complex project with multiple complex parts, I want to ensure everything is working correctly.
How do I go about adding xUnit to this project?
58 replies
✅ Introduce variable in `while` condition
Is it possible to achieve something like this? (pseudo-C#)
The idea is that the loop should go until the result of some computation does not match some predicate, and to also keep the result of the computation around in the loop.
If not, how would you achieve a similar pattern?
9 replies
✅ How to pattern-match a `Rune`?
is this the best I can do?
That's very noisy visually and it's unclear to me if the compiler is smart enough to perform the
(Rune)
cast on char
literals at compile-time.
Is there not a way to specify a constant/literal Rune value?41 replies
❔ Generics and nullable types
This snippet below results in an error
Cannot convert null to type parameter 'T' because it could be a value type. Consider using 'default(T)' instead.I don't really understand the error. I thought the type of
_foo
was T?
, not T
.58 replies
❔ Disable specific formatting rule only in one class in Rider
So, I have a class that is a bit pathological in that it has a bunch of one-liner declarations. Rider puts an extra newline between them which takes a ton of space and actually makes it harder to read.
I found that there's a setting for this "blank lines in declarations", but I only want this in this specific class, not anywhere else. Is it possible?
5 replies
❔ Enum with data
I'm working on a lexer, and I'm encountering this problem where I need to associate different data with different token types. My first attempt was this:
then you can use it like so:
I kind of dislike this implementation though. For one it seems kind of messy with the null-forgiving stuff, but it also makes the token type very bulky since it requires a separate field for every data variant. Since there will be a ton of tokens, it's important to minimize their size.
It's also error-prone in the sense that someone can try to read data without first checking the token kind.
What I'm trying to do is conceptually this:
It doesn't have to look like this, but this is the kind of behavior I'm looking for (Rust enums, or algebraic sum types for those who know).
What would be the clean way to make this in C#? Bear in mind There is actually a lot of token kinds, and not all token kinds have data associated with them (in fact the majority do not).
76 replies
❔ Reading from stdin
So, I'm trying to work with basic IO stuff. I have this snippet:
I get a warning about assigning a nullable value to a non-nullable variable. I understand what the warning is saying, but I'm not sure how I'm supposed to do any better than this considering I am already checking for null values. Any recommendations?
8 replies
❔ How to structure project and import items?
I'm just starting with C#, and I'm having trouble understanding how project structure and importing works.
I come from Rust where you can import a single item from a module with something like
You can also import multiple items using this notation:
I tried something similar in C#:
But that gives an error
Namespace name expected ...
. Is it not possible to only import some items from a namespace?
I'm also having trouble understanding the relationship between namespaces and files. It seems like you can add stuff to a namespace from anywhere inside your project. So it seems like namespaces are not really self-contained modules, but more like heaps where you can throw in anything semi-related to some concept.
Finally it seems to me like you can't have items private to a module?
This all seems rather unstructured to me, so surely I must be missing something. I can summarize my questions as such:
1. Can you import only specific items?
2. What's the idiomatic way to structure complex libraries? namespace per file?
3. What are the tools to restrict visibility such that I can have parts of my library that are only available internally?
Please bear in mind I'm not familiar with "old-school" OOP stuff so I might be oblivious to the obvious 🙂 I would highly appreciate it if you could explain how it works.62 replies
❔ Entry API for dictionaries?
Rust has this nifty API called
Entry
which allows one to write code such as:
So, you can modify a value in a hashmap (dictionary) if the key exists, or insert a new entry if the key does not exist.
Is there something similar in C#?26 replies
❔ Is there a built-in way to map (transform) a tuple?
For example, I'd like to do this (hypothetical code):
instead of that
The reasoning is that it's cumbersome to have to introduce variable names for intermediate computations involving tuples. Being able to transform them directly makes it possible to avoid that.
9 replies
✅ Switch pattern matching with integers
Is this a valid/idiomatic way to use pattern matching to compare integers?
One thing bugging me in particular is: are the arms checked in the order they were written, or is the order not guaranteed?
If the order is not guaranteed, then obviously the code above does not work.
45 replies