does rust like ownership type in CSharp possible? #7680
Replies: 4 comments 3 replies
-
I was told that due to the dotnet runtime and garbage collection it is not possible to modify csharp to use borrow checker and ownership like rust. However, Python is also used garbage collection and has a runtime. So what they did is created another language called Mojo that has the same syntax as Python but use borrow checker instead and has no garbage collection and it can run Python code as well. Maybe csharp can do something similar and call it d# lol |
Beta Was this translation helpful? Give feedback.
-
It looks like |
Beta Was this translation helpful? Give feedback.
-
The first thing I would introduce is the concept of ownership, separated from the borrow checker. Ownership is an important concept to drive development on itself. For example, the standard constructor for StreamReader should declare the Stream argument as the ownership of that class will be transfered to the instance of the StreamReader, because after it the Stream will be disposed. The Stream logically should not be used anymore outside of the StreamReader. Another example are the constructors of some of the collections, where the IEnumerable instance is passed, enumerated and added to the collection. In case of arrays in many collections you would be able to take ownership of the array and use it inside the collection, reducing memory allocation and enumerations. This would by itself enhance C# and allows in a second moment to introduce also a Borrow Checker to mark some instances for a release at the end of a method call instead of entering the big family of the GC instances. |
Beta Was this translation helpful? Give feedback.
-
a few days ago, I see a few amzing project on github, one is a rust compiler for CLR
rust is memory safe lanague , and it's ownership base memeory management is amazing , and now c# is designe to work more on unmanaged memory(eg.
Span<T>
,NativeMemroy
.etc), I'm wonder does it possible to add an ownership base native memory type system in dotnet.some ideas
Beta Was this translation helpful? Give feedback.
All reactions