C
C#2y ago
Theos

❔ Switch case on Int type

Hey, so I have a class with variable _type and I want to store a variable type in it (long, int, short, ubyte etc etc). Later I want to do a switch on _type something like
switch(_type)
{
case long:
case int:
}
switch(_type)
{
case long:
case int:
}
How can I do this?
23 Replies
nukleer bomb
nukleer bomb2y ago
_type variable is of type Type?
Theos
TheosOP2y ago
yea and when I create this class I do something like new Class(typeof(int));
nukleer bomb
nukleer bomb2y ago
You can't switch on typeof() since it's not a constant You have to use good ol' ifs (also, can you use generics instead of typeof in your class?)
Theos
TheosOP2y ago
how exactly?
sibber
sibber2y ago
if you had a variable of the type you can do that switch syntax
MODiX
MODiX2y ago
nukleer bomb
REPL Result: Failure
Type _type = null!;
switch(_type)
{
case typeof(int):
Console.WriteLine("int");
break;
default:
Console.WriteLine("???");
break;
}
Type _type = null!;
switch(_type)
{
case typeof(int):
Console.WriteLine("int");
break;
default:
Console.WriteLine("???");
break;
}
Exception: CompilationErrorException
- A constant value is expected
- A constant value is expected
Quoted by
<@536372826161283072> from #bot-spam (click here)
Compile: 579.576ms | Execution: 0.000ms | React with ❌ to remove this embed.
ero
ero2y ago
new Class<int>()
Theos
TheosOP2y ago
ero
ero2y ago
yeah, they were showing that it doesn't work
Theos
TheosOP2y ago
ah oke
ero
ero2y ago
the modix eval has an error itself you can see it
Theos
TheosOP2y ago
yep, true i'm not exactly familiar with generics how would I then store this type in my class?
ero
ero2y ago
you wouldn't you have it as the type parameter there's not need to store it in any way
Theos
TheosOP2y ago
but how would I do a switch case on it later?
ero
ero2y ago
that depends on your use case it'd be good to have more detail
Theos
TheosOP2y ago
alright so let me explain it in more details
Theos
TheosOP2y ago
i am working on a tokenizer i can have tokens like i8, i16 ,i32 and i64 (for signed ints) and u8 u16 u32 u64 (for unsigned ints)
switch(numberType)
{
case "i64":
long _long;
if(long.TryParse(value, out _long)) return new NumberAST(value, typeof(long));
else ValueMatchTypeException.Log(value, numberType);
break;
case "i32":
int _int;
if (int.TryParse(value, out _int)) return new NumberAST(value, typeof(int));
else ValueMatchTypeException.Log(value, numberType);
break;
case "i16":
short _short;
if (short.TryParse(value, out _short)) return new NumberAST(value, typeof(short));
else ValueMatchTypeException.Log(value, numberType);
break;
case "i8":
sbyte _sbyte;
if (sbyte.TryParse(value, out _sbyte)) return new NumberAST(value, typeof(sbyte));
else ValueMatchTypeException.Log(value, numberType);
break;
default:
WrongNumberTypeException.Log(number);
break;
}
switch(numberType)
{
case "i64":
long _long;
if(long.TryParse(value, out _long)) return new NumberAST(value, typeof(long));
else ValueMatchTypeException.Log(value, numberType);
break;
case "i32":
int _int;
if (int.TryParse(value, out _int)) return new NumberAST(value, typeof(int));
else ValueMatchTypeException.Log(value, numberType);
break;
case "i16":
short _short;
if (short.TryParse(value, out _short)) return new NumberAST(value, typeof(short));
else ValueMatchTypeException.Log(value, numberType);
break;
case "i8":
sbyte _sbyte;
if (sbyte.TryParse(value, out _sbyte)) return new NumberAST(value, typeof(sbyte));
else ValueMatchTypeException.Log(value, numberType);
break;
default:
WrongNumberTypeException.Log(number);
break;
}
public class NumberAST : ASTNode
{
public string Value;
public System.Type Type;
public NumberAST(string value, System.Type type)
{
Value = value;
Type = type;
}
}
public class NumberAST : ASTNode
{
public string Value;
public System.Type Type;
public NumberAST(string value, System.Type type)
{
Value = value;
Type = type;
}
}
ero
ero2y ago
how do you mean "tokens"?
Theos
TheosOP2y ago
tokens are strings
ero
ero2y ago
where do they come from
Theos
TheosOP2y ago
i'm working on a simple programming language and they come from my lexer so
variable = 1000i8;
variable = 1000i8;
would return these tokens:
ID ASSIGN i8
ID ASSIGN i8
so, later using my NumberAST class I want to create LLVM consts
LLVMValueRef.CreateConstInt(LLVMTypeRef.Int64, val);
LLVMValueRef.CreateConstInt(LLVMTypeRef.Int64, val);
thats why i need to do switch case to change the LLVMTypeRef
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?