newtype

How to wrap a borrowed value in a newtype that is also a borrowed value?

。_饼干妹妹 提交于 2020-12-21 02:54:45
问题 I am trying to use the newtype pattern to wrap a pre-existing type. That inner type has a modify method which lets us work with a borrowed mutable value in a callback: struct Val; struct Inner(Val); impl Inner { fn modify<F>(&self, f: F) where F: FnOnce(&mut Val) -> &mut Val { … } } Now I want to provide a very similar method on my newtype Outer , which however should not work on Val s but again a newtype wrapper WrappedVal : struct Outer(Inner); struct WrappedVal(Val); impl Outer { fn modify

Generalized Newtype Deriving

不问归期 提交于 2020-01-03 10:06:27
问题 Haskell can derive the instance for MonadState s in T1 below but not in T2 which is however a very similar type. In which way should I modify the code for T2 so that the instance for MonadState s can be automatically derived? {-# LANGUAGE GeneralizedNewtypeDeriving #-} import Control.Monad.Reader import Control.Monad.State newtype T1 r s a = T1 { runT1 :: ReaderT r (State s) a } deriving (Monad, MonadReader r, MonadState s) newtype T2 r s a = T2 { runT2 :: StateT r (State s) a } deriving

Generalized Newtype Deriving

社会主义新天地 提交于 2020-01-03 10:06:25
问题 Haskell can derive the instance for MonadState s in T1 below but not in T2 which is however a very similar type. In which way should I modify the code for T2 so that the instance for MonadState s can be automatically derived? {-# LANGUAGE GeneralizedNewtypeDeriving #-} import Control.Monad.Reader import Control.Monad.State newtype T1 r s a = T1 { runT1 :: ReaderT r (State s) a } deriving (Monad, MonadReader r, MonadState s) newtype T2 r s a = T2 { runT2 :: StateT r (State s) a } deriving

What is the rule of the order of multiple type variables in haskell?

[亡魂溺海] 提交于 2020-01-03 07:17:13
问题 For example, ParsecT has multiple type variables in its definition. newtype ParsecT s u m a = ParsecT {unParser :: forall b . State s u -> (a -> State s u -> ParseError -> m b) -> (ParseError -> m b) -> (a -> State s u -> ParseError -> m b) -> (ParseError -> m b) -> m b } Can we do it like this ? newtype ParsecT m a s u -- Only the order of s u m a is changed to m a s u. = ParsecT {unParser :: forall b . State s u -> (a -> State s u -> ParseError -> m b) -> (ParseError -> m b) -> (a -> State

C#中as的用法--转发

本秂侑毒 提交于 2020-01-01 01:19:41
在程序中,进行类型转换时常见的事,C#支持基本的强制类型转换方法,例如 Object obj1 = new NewType(); NewType newValue = (NewType)obj1; 这样强制转换的时候,这个过程是不安全的,因此需要用try-catch语句进行保护,这样一来,比较安全的代码方式应如下所示: Object obj1 = new NewType(); NewType newValue = null; try { newValue = (NewType)obj1; } catch (Exception err) { MessageBox.Show(err.Message); } 但是上面的写法在C#中已是过时的写法,也是比较低效的写法,比较高效且时尚的写法是用as操作符,如下: Object obj1 = new NewType(); NewTYpe newValue = obj1 as NewType; 安全性: as操作符不会做过的转换操作,当需要转化对象的类型属于转换目标类型或者转换目标类型的派生类型时,那么此转换操作才能成功,而且并不产生新的对象【当不成功的时候,会返回null】。因此用as进行类型转换是安全的。 效率: 当用as操作符进行类型转换的时候,首先判断当前对象的类型,当类型满足要求后才进行转换,而传统的类型转换方式,是用当前对象直接去转换

Why don't newtypes use the traits from the inner type?

可紊 提交于 2019-12-23 08:54:18
问题 In rust 1.0.0-nightly, this code works fine: fn main() { let x = 10f64; let y = 20f64; let z = x + y; println!("z = {}", z); } But if I try to use a newtype (according to the rust book): struct Metres(f64); fn main() { let x = Metres(10f64); let y = Metres(20f64); let z = x + y; println!("z = {}", z); } I get this compiler error: test.rs:6:13: 6:18 error: binary operation `+` cannot be applied to type `Metres` test.rs:6 let z = x + y; ^~~~~ error: aborting due to previous error Since Metres

Lift Monad Reader local

本秂侑毒 提交于 2019-12-23 05:27:44
问题 import Control.Monad.Reader import Control.Monad.State import Control.Monad.Error data Context data Memory data Functions data InterpreterM a = ExeInterpreter a | PropInterpreter a newtype InterpreterMT m a = InterpreterMT { runInterpreterMT :: m (InterpreterM a) } type Interpreter = InterpreterMT (StateT (Memory, Functions) (ReaderT (Context, Context) (ErrorT String IO))) data Stmt data Stmts = EmptyStmts | Statements Stmt Stmts interpretStmt :: Stmt -> Interpreter Context interpreter ::

How to define MonadUnliftIO instance for a newtype with a phantom type-variable?

十年热恋 提交于 2019-12-22 07:11:21
问题 Related question - Is it safe to derive MonadThrow, MonadCatch, MonadBaseControl, MonadUnliftIO, etc? - where I had enabled, both - DeriveAnyClass and GeneralizedNewtypeDeriving to get the code to compile, but didn't bother looking at the ominous warnings. Now, that I am running my refactored code, it's throwing a runtime error: No instance nor default method for class operation >>= So, I removed DeriveAnyClass and kept ONLY GeneralizedNewtypeDeriving and have the following compile error: {-#

Is there a shorthand for operations like `fromNewtype . f . toNewtype`?

爷,独闯天下 提交于 2019-12-22 04:15:10
问题 A pattern that presents itself the more often the more type safety is being introduced via newtype is to project a value (or several values) to a newtype wrapper, do some operations, and then retract the projection. An ubiquitous example is that of Sum and Product monoids: λ x + y = getSum $ Sum x `mappend` Sum y λ 1 + 2 3 I imagine a collection of functions like withSum , withSum2 , and so on, may be automagically rolled out for each newtype . Or maybe a parametrized Identity may be created,