About Monkey 2 › Forums › Monkey 2 Programming Help › Nullable Int, Double, Float ?
This topic contains 2 replies, has 2 voices, and was last updated by 
 Diffrenzy
 2 years, 1 month ago.
- 
		AuthorPosts
 - 
		
			
				
February 28, 2017 at 9:05 am #7323
How do I do nullable types in MX2? Basically I’d like to have the equivalent of c#’s int?, float? etc.
Is there a way in MX2, without wrapping in a class, and ending up with a nullabletype.Value = xxx like syntax?
(I’m deserializing/serializing a generic type to xml, and need to only read/write values when they are present.)
PS: Speaking of nulls, I’d like to be able to do:
Monkey12If mystruct 'instead ofIf mystruct<>nulllike I can with types. Is that a builtin given for structs, or could it be added to MX2?
February 28, 2017 at 10:33 pm #7335There are no nullable types in mx2. There are variants though – see reflection in monkey2 language docs.
could it be added to MX2?
quite possibly – can you post an issue on github?
March 1, 2017 at 9:28 am #7344Thanks Mark, I’ve put the Null issue on github (and also a request for the Elvis operator, which I really enjoy in C# )
Thanks for pointing me towards Variants. They a not a perfect for for my intended use, so I’ve ended up copying std.geom Vec2 and making a Vec1 struct, by removing the y vals and a few methods. If it’s not too weird, maybe MX2 could come with a Vec1 struct , to have a standard for float/int boxing ?
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159Namespace difference.geom#rem monkeydoc Convenience type alias for Vec1\<Int\>.#endAlias Vec1i:Vec1<Int>#rem monkeydoc Convenience type alias for Vec1\<Float\>.#endAlias Vec1f:Vec1<Float>#rem monkeydoc The Vec1 type provides support for 2 component vectors.#endStruct Vec1<T>#rem monkeydoc Vector x coordinate.#endField x:T#rem monkeydoc Creates a new vector.#endMethod New()EndMethod New( t:T )x=tEndMethod New( x:T,y:T )Self.x=xEnd#rem monkeydoc Converts the vector to a vector of a different type.#endOperator To<C>:Vec1<C>()Return New Vec1<C>( x )End#rem monkeydoc Converts the vector to a printable string.#endOperator To:String()Return "Vec1("+x+")"End#rem monkeydoc The X coordinate of the vector.#endProperty X:T()Return xSetter( x:T )Self.x=xEnd#rem monkeydoc Negates the vector components and returns the result.#endOperator-:Vec1()Return New Vec1( -x )End#rem monkeydoc Multiplies the vector by another vector and returns the result.#endOperator*:Vec1( v:Vec1 )Return New Vec1( x*v.x )End#rem monkeydoc Divides the vector by another vector and returns the result.#endOperator/:Vec1( v:Vec1 )Return New Vec1( x/v.x )End#rem monkeydoc Adds another vector to the vector and returns the result.#endOperator+:Vec1( v:Vec1 )Return New Vec1( x+v.x )End#rem monkeydoc Subtracts another vector from the vector and returns the result.#endOperator-:Vec1( v:Vec1 )Return New Vec1( x-v.x )End#rem monkeydoc Scales the vector by a value and returns the result.#endOperator*:Vec1( s:Double )Return New Vec1( x*s )End#rem monkeydoc Inverse scales the vector by a value and returns the result.#endOperator/:Vec1( s:Double )Return New Vec1( x/s )End#rem monkeydoc Adds a value to the vector components and returns the result.#endOperator+:Vec1( s:T )Return New Vec1( x+s )End#rem monkeydoc Subtracts a value from the vector components and returns the result.#endOperator-:Vec1( s:T )Return New Vec1( x-s )End#rem monkeydoc The length of the vector.#endProperty Length:Double()Return xEnd#rem monkeydoc The normal to the vector.#end' #rem monkeydoc Computes the dot product of the vector with another vector.' #end' Method Dot:Double( v:Vec1 )' Return x*v.x+y*v.y' End#rem monkeydoc Computes the distance from this vector to another.#endMethod Distance:Double( v:Vec1 )Return v.x - xEnd#rem monkeydoc Normalizes the vector and returns the result.#endMethod Normalize:Vec1()Return Self/LengthEnd#rem monkeydoc Blends the vector with another vector and returns the result.#endMethod Blend:Vec1( v:Vec1,alpha:Double )Return New Vec1( (v.x-x)*alpha+x )End#rem monkeydoc Gets a string representation for the vector.#endMethod ToString:String()Return SelfEndEnd'#rem monkeydoc Transforms a Vec1\<Int\> by an AffineMat3.'#end'Function TransformVec1i<T>:Vec1i( vec:Vec1i,matrix:AffineMat3<T> )'' Local tmp:=matrix * New Vec1<T>( rect.min.x )'' Return New Vec1i( Round( tmp.x ),Round( tmp.y ) )'End - 
		AuthorPosts
 
You must be logged in to reply to this topic.