[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Deafening
From: |
Sather User |
Subject: |
Re: Deafening |
Date: |
Thu, 5 May 2011 03:00:54 +0930 (CST) |
On Fri, 29 Apr 2011, Marek Janukowicz wrote:
> What I'm doing with Sather is writing a library called MUSH (hopefully this
> doesn't mean anything offensive in English :)) that can be found here:
> https://bitbucket.org/starware/mush
Heh. Not quite. :-)
Marek, your site is impressive.
Regarding enums the approach I take is to define an enum type and so
get an error message if an incorrect value is used. There is a file
enum.sa:
class ENUM_RANGE_EXC < $STR is
readonly attr str:STR;
create(ty:$ENUM):SAME is
r::=new;
r.str:="enum out of range "+ty.minval+" .. "+ty.maxval;
return r
end
end;
abstract class $ENUM < $STR is
int: INT;
str: STR;
maxval: SAME;
minval: SAME;
create(i: INT): SAME
end;
And a class containing the enum constants which is included both in
the class that defines the particular enum class and the classes that
use it:
partial class GETOPT_MODE_ENUM_CONSTS is
const getopt_long, getopt_long_only;
end;
And then the enum class is something like this:
immutable class GETOPT_MODE_ENUM < $ENUM is
include GETOPT_MODE_ENUM_CONSTS;
private attr val:INT;
int:INT is return val end;
c_int:C_INT is return #C_INT(val) end;
str:STR is return val.str end;
minval:SAME is return create(getopt_long) end;
maxval:SAME is return create(getopt_long_only) end;
create(i:INT):SAME is
r:SAME;
case i
when getopt_long, getopt_long_only
then
r := r.val(i);
else
raise #ENUM_RANGE_EXC(r)
end;
return r
end;
is_eq(f:SAME):BOOL is
return int.is_eq(f.int)
end;
is_eq(arg: $OB): BOOL is
-- Overloaded version of the is_eq routine that works with an argument
-- of any type. If the type of the 'arg' is not the same as they
-- type of 'self' then return false. Otherwise, delegate to
-- the 'real' is_eq(SAME):BOOL routine (Comment found in Library.)
typecase arg when SAME then return is_eq(arg) else return false end;
end;
end;
Then it is used something like this:
class GETOPT is
include GETOPT_MODE_ENUM_CONSTS;
include GETOPT_HAS_ARG_ENUM_CONSTS;
shared No_Argument, Required_Argument:GETOPT_HAS_ARG_ENUM;
-- In effect constants, but need a create.
private attr mode, mode_long, mode_long_only:GETOPT_MODE_ENUM;
...
create(oppies:ARRAY{OPTION},
args:ARRAY{STR},
mode:GETOPT_MODE_ENUM)
is
r::=new;
r.me := args[0];
r.mode := mode;
r.mode_long := #(getopt_long);
r.mode_long_only := #(getopt_long_only);
...
Then programs that use the class are something like
include GETOPT_MODE_ENUM_CONSTS;
include GETOPT_HAS_ARG_ENUM_CONSTS;
...
optsarray: ARRAY{OPTION} :=
|
#("person-override", 'O', #(required_argument), bind(psno(_,_))),
... |;
...
p ::= #GETOPT(optsarray, args, #(getopt_long));
etc. So, something like
p ::= #GETOPT(optsarray, args, #(3));
raises an error.
A bit over-elaborate, I grant you.
Regards,
Mike
--
Michael Talbot-Wilson
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Re: Deafening,
Sather User <=