type-safety

Defining a method for a struct only when a field is a certain enum variant?

别等时光非礼了梦想. 提交于 2021-02-02 09:26:52
问题 I have the following struct: #[derive(Debug)] pub struct Entry { pub index: usize, pub name: String, pub filename_offset: u64, pub entry_type: EntryType, } #[derive(Debug)] pub enum EntryType { File { file_offset: u64, length: usize, }, Directory { parent_index: usize, next_index: usize, }, } Entry is an entry in a GameCube ROM file system table which describes a file or directory. I defined various methods for Entry such as Entry::read_filename and Entry::write_to_disk . However, I have some

Defining a method for a struct only when a field is a certain enum variant?

放肆的年华 提交于 2021-02-02 09:21:45
问题 I have the following struct: #[derive(Debug)] pub struct Entry { pub index: usize, pub name: String, pub filename_offset: u64, pub entry_type: EntryType, } #[derive(Debug)] pub enum EntryType { File { file_offset: u64, length: usize, }, Directory { parent_index: usize, next_index: usize, }, } Entry is an entry in a GameCube ROM file system table which describes a file or directory. I defined various methods for Entry such as Entry::read_filename and Entry::write_to_disk . However, I have some

Compiler-enforced semantic types

∥☆過路亽.° 提交于 2020-12-31 06:29:48
问题 Say I have a class representing automata, whose states are numbered ( using state_t = unsigned ) and whose transitons are also numbered ( using transition_t = unsigned ). Of course at some point I end up messing some calls because transition_t and state_t are the same type, so the compiler does not enforce the (semantic) type safety. That's easy to workaround by using a small class templated by a tag ( struct transition_tag {}; struct state_tag {}; ), so now transition_t and state_t are

Pattern matching against type-member with Aux-pattern

心已入冬 提交于 2020-12-31 06:09:25
问题 Consider: sealed trait A case object B extends A case object C extends A case object D extends A sealed trait Test { type AA <: A val aa: AA } object Test { type Aux[AAA <: A] = Test { type AA = AAA } } def compilesOk(t: Test) = t.aa match { case _: B.type => println("B") case _: C.type => println("C") case _: D.type => println("D") } def compileError(t: Test) = t.aa match { case B => println("B") case C => println("C") case D => println("D") } The compileError function fails to compile with

Pattern matching against type-member with Aux-pattern

丶灬走出姿态 提交于 2020-12-31 06:05:27
问题 Consider: sealed trait A case object B extends A case object C extends A case object D extends A sealed trait Test { type AA <: A val aa: AA } object Test { type Aux[AAA <: A] = Test { type AA = AAA } } def compilesOk(t: Test) = t.aa match { case _: B.type => println("B") case _: C.type => println("C") case _: D.type => println("D") } def compileError(t: Test) = t.aa match { case B => println("B") case C => println("C") case D => println("D") } The compileError function fails to compile with

Pattern matching against type-member with Aux-pattern

泄露秘密 提交于 2020-12-31 06:04:59
问题 Consider: sealed trait A case object B extends A case object C extends A case object D extends A sealed trait Test { type AA <: A val aa: AA } object Test { type Aux[AAA <: A] = Test { type AA = AAA } } def compilesOk(t: Test) = t.aa match { case _: B.type => println("B") case _: C.type => println("C") case _: D.type => println("D") } def compileError(t: Test) = t.aa match { case B => println("B") case C => println("C") case D => println("D") } The compileError function fails to compile with

Using the Rust compiler to prevent forgetting to call a method

。_饼干妹妹 提交于 2020-11-30 12:17:10
问题 I have some code like this: foo.move_right_by(10); //do some stuff foo.move_left_by(10); It's really important that I perform both of those operations eventually , but I often forget to do the second one after the first. It causes a lot of bugs and I'm wondering if there is an idiomatic Rust way to avoid this problem. Is there a way to get the rust compiler to let me know when I forget? My idea was to maybe somehow have something like this: // must_use will prevent us from forgetting this if

Is Python type safe?

这一生的挚爱 提交于 2020-06-09 07:54:06
问题 According to Wikipedia Computer scientists consider a language "type-safe" if it does not allow operations or conversions that violate the rules of the type system. Since Python runtime checks ensure that type system rules are satisfied, we should consider Python a type safe language. The same point is made by Jason Orendorff and Jim Blandy in Programming Rust: Note that being type safe is independent of whether a language checks types at compile time or at run time: C checks at compile time,

type-safe Control.Invoke C#

混江龙づ霸主 提交于 2020-03-21 06:36:19
问题 I am programming a software in C# at work that contains 2 Threads a Thread that control a Form (Windows Forms) and interfaces with the user. a Thread that checks online data at the background. I need the second thread to print a massage on the form when the online data is irregular. because only the thread that created the control can change it, I am using delegates. the second thread calls the first thread to execute a delegate by the Control.Invoke method. Example: public partial class

How to make this Strategy-Object pattern type safe

南楼画角 提交于 2020-03-04 19:42:47
问题 This is the long version of a question I asked earlier For the tl;dr version please see here: Link I am sorry for this wall of text, but please bear with me. I put a lot of effort into the question and I believe the problem at hand should interesting to many here. Background I am writing a UI Framework with a classic Scene Graph. I have an abstract Top-Level class called Component and many subclasses, some of which are concrete while others abstract as well. A concrete subclass might be