[nem-en] Re: conversion of int to uint

Kannan Goundan cakoose at yahoo.com
Thu Feb 23 09:14:20 CET 2006


It looks like Nemerle, unlike C++ templates, doesn't instantiate a
function multiple times for different argument types.  It attempts to
find a single type signature that works for all callers of the
function.  Since there is no way to do that (?) for the checkMask
function, you get an error.

Maybe Nemerle can introduce a common supertype of "Int32" and
"UInt32" called "Bits32".  That type would behave exactly like
"UInt32" except it's just a way of telling the compiler to implicitly
convert from both "Int32" and "UInt32".

Currently, there are a couple not-so-clean ways to accomplish what
you want (given below).  I'm not a Nemerle expert, though, so there
might be a better way.

class IntCast
{
   static umask : uint = 1;
   static smask : int  = 1;
   static modes : uint = 1;

   static f() : void
   {
      def checkMask(mask : uint, modes)
      {
         if ((mask %& modes) == 0)
            System.Console.WriteLine("0");
         else
            System.Console.WriteLine("1");
      }

      def checkMaskS(mask : int, modes)
      {
         checkMask(mask :> uint, modes);
      }

      checkMask(umask, modes);
      checkMaskS(smask, modes);
   }

   static g() : void
   {
      def checkMask(mask : uint, modes)
      {
         if (((mask : uint) %& modes) == 0)
            System.Console.WriteLine("0");
         else
            System.Console.WriteLine("1");
      }

      checkMask(umask, modes);
      checkMask(smask :> uint, modes);
   }

   static Main() : void
   {
      f();
      g();
   }

}


--- Valient Gough <valient at gmail.com> wrote:

> Sorry, GMail got confused...  I wasn't ready to send.
> 
> Hi,
> 
> I'm having trouble converting from an int to uint.  I have to
> convert
> to a common type because I need to apply a bit-field operation, and
> I'm constrained on the different types from external interfaces.
> 
> My expectation was for something like this to work:
> 
> def checkMask( mask, modes )
> {
>     if((mask :> uint) %& modes)
>         0;
>     else
>         ErrorCode.EACCES;
> }
> 
> But that gives an error "System.Int32 is not a subtype of
> System.UInt32 [simple require]"
> 
> So I searched around the nemerle site and didn't see anything. I
> tried
> other variations without success, and then I tried casting the
> uints
> to ints and got an interesting error:  "common super type of types
> [System.UInt32, int] is just `System.ValueType', please upcast one
> of
> the types to `System.ValueType' if this is desired
> "
> 
> So, I tried to force a conversion through ValueType:
> 
> def checkMask( mask : ValueType , modes )
> {
>     if((mask :> uint) %& modes)
> ...
> 
> That resulted in an ICE:
> error: internal compiler error: assertion failed in file
> generation/ILEmitter.n, line 778:
> Sulf.GenericNode::Access: failed to convert non-value type
> System.ValueType to a value type System.UInt32
> 
> 
> While I realize that an ICE should go in a bug report, I don't
> really
> want to cast to ValueType anyway, all I want to do is cast between
> an
> int and uint.. There must be an easy way that I'm missing.
> 
> regards,
> Valient
> 
> _______________________________________________
> https://nemerle.org/mailman/listinfo/devel-en
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the devel-en mailing list