C
C#16mo 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 bomb16mo ago
_type variable is of type Type?
Theos
TheosOP16mo ago
yea and when I create this class I do something like new Class(typeof(int));
nukleer bomb
nukleer bomb16mo 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
TheosOP16mo ago
how exactly?
sibber
sibber16mo ago
if you had a variable of the type you can do that switch syntax
MODiX
MODiX16mo 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
ero16mo ago
new Class<int>()
Theos
TheosOP16mo ago
ero
ero16mo ago
yeah, they were showing that it doesn't work
Theos
TheosOP16mo ago
ah oke
ero
ero16mo ago
the modix eval has an error itself you can see it
Theos
TheosOP16mo ago
yep, true i'm not exactly familiar with generics how would I then store this type in my class?
ero
ero16mo ago
you wouldn't you have it as the type parameter there's not need to store it in any way
Theos
TheosOP16mo ago
but how would I do a switch case on it later?
ero
ero16mo ago
that depends on your use case it'd be good to have more detail
Theos
TheosOP16mo ago
alright so let me explain it in more details
Theos
TheosOP16mo 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
ero16mo ago
how do you mean "tokens"?
Theos
TheosOP16mo ago
tokens are strings
ero
ero16mo ago
where do they come from
Theos
TheosOP16mo 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
Accord16mo 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.
Want results from more Discord servers?
Add your server