phantom-types

How do I share a struct containing a phantom pointer among threads?

一曲冷凌霜 提交于 2020-07-08 12:27:25
问题 I have a structure that needs to be generic over a type, yet the type is not actually contained in the structure: it's used in methods of this structure, not in the structure itself. And so, the structure includes a PhantomData member: pub struct Map<T> { filename: String, phantom: PhantomData<*const T>, } The phantom member is defined as a pointer because the structure does not actually own data of type T . This is per advice in the documentation of std::marker::PhantomData: Adding a field

PhantomData type usage in rust

耗尽温柔 提交于 2019-12-24 00:42:12
问题 I was going through some rust source code and I found a data type called PhantomData . I was going through the rust documentation and searched through the internet a lot. However, I couldn't understand the actual use of this data type with rust. If possible, could somebody please explain me this in simple manner? pub struct GPIOD { _marker: PhantomData<*const ()>, } 回答1: The PhantomData struct is meant to signal to the compiler that a type or lifetime is being used in some way that is

Motivation behind Phantom Types?

允我心安 提交于 2019-12-18 10:19:31
问题 Don Stewart's Haskell in the Large's presentation mentioned Phantom Types : data Ratio n = Ratio Double 1.234 :: Ratio D3 data Ask ccy = Ask Double Ask 1.5123 :: Ask GBP I read over his bullet points about them, but I did not understand them. In addition, I read the Haskell Wiki on the topic. Yet I still am missing their point. What's the motivation to use a phantom type? 回答1: To answer the "what's the motivation to use a phantom type". There is two points: to make invalid states

Haskell: Heterogeneous list for data with phantom variable

試著忘記壹切 提交于 2019-12-10 01:59:15
问题 I'm learning about existential quantification, phantom types, and GADTs at the moment. How do I go about creating a heterogeneous list of a data type with a phantom variable? For example: {-# LANGUAGE GADTs #-} {-# LANGUAGE ExistentialQuantification #-} data Toy a where TBool :: Bool -> Toy Bool TInt :: Int -> Toy Int instance Show (Toy a) where show (TBool b) = "TBool " ++ show b show (TInt i) = "TInt " ++ show i bools :: [Toy Bool] bools = [TBool False, TBool True] ints :: [Toy Int] ints =

Haskell: Heterogeneous list for data with phantom variable

╄→гoц情女王★ 提交于 2019-12-05 01:58:45
I'm learning about existential quantification, phantom types, and GADTs at the moment. How do I go about creating a heterogeneous list of a data type with a phantom variable? For example: {-# LANGUAGE GADTs #-} {-# LANGUAGE ExistentialQuantification #-} data Toy a where TBool :: Bool -> Toy Bool TInt :: Int -> Toy Int instance Show (Toy a) where show (TBool b) = "TBool " ++ show b show (TInt i) = "TInt " ++ show i bools :: [Toy Bool] bools = [TBool False, TBool True] ints :: [Toy Int] ints = map TInt [0..9] Having functions like below are OK: isBool :: Toy a -> Bool isBool (TBool _) = True

Motivation behind Phantom Types?

我怕爱的太早我们不能终老 提交于 2019-11-29 20:35:40
Don Stewart's Haskell in the Large 's presentation mentioned Phantom Types : data Ratio n = Ratio Double 1.234 :: Ratio D3 data Ask ccy = Ask Double Ask 1.5123 :: Ask GBP I read over his bullet points about them, but I did not understand them. In addition, I read the Haskell Wiki on the topic. Yet I still am missing their point. What's the motivation to use a phantom type? phadej To answer the "what's the motivation to use a phantom type". There is two points: to make invalid states unrepresentable, which is well explained in Aadit's answer Carry some of the information on the type level For