casting

How to avoid calling asInstanceOf in Scala

Deadly 提交于 2021-02-08 14:08:52
问题 Here is a simplified version of my code. How can I avoid to call asInstanceOf (because it is a smell for a poorly design solution) ? sealed trait Location final case class Single(bucket: String) extends Location final case class Multi(buckets: Seq[String]) extends Location @SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf")) class Log[L <: Location](location: L, path: String) { // I prefer composition over inheritance // I don't want to pass location to this method because it's a

C# Is there any better way to use Switch-Case / If-Else on generic types

主宰稳场 提交于 2021-02-08 11:38:03
问题 I am currently trying to get some casting on generic types done. So the base idea is to have a method which accepts a generic Type and does different stuff depending on which type gets passed in. For simplicity reasons I will just showcase the use of float , bool and default The setup looks something like this (where T is a generic type defined by the class itself): protected T DoStuff(T value) { switch (value) { case float floatValue: float result = DoFloatStuff(floatValue); switch (result)

C# Is there any better way to use Switch-Case / If-Else on generic types

拥有回忆 提交于 2021-02-08 11:37:47
问题 I am currently trying to get some casting on generic types done. So the base idea is to have a method which accepts a generic Type and does different stuff depending on which type gets passed in. For simplicity reasons I will just showcase the use of float , bool and default The setup looks something like this (where T is a generic type defined by the class itself): protected T DoStuff(T value) { switch (value) { case float floatValue: float result = DoFloatStuff(floatValue); switch (result)

Casting subclass to superclass and calling a function that both classes have

谁说胖子不能爱 提交于 2021-02-08 11:10:38
问题 public class Test3 { public static void main(String[] args) { Derived04 b = new Derived04(); Base04 a = (Base04)b; System.out.println(a.i); System.out.println(a.f()); } } class Base04 { int i=1; public int f() {return i;} } class Derived04 extends Base04 { int i = 2; public int f(){return -i;} } Both classes include the f() method so when I cast d onto a which method does it go to when I call a.f()? I thinking since a is declared as Base04 type when you do a.i it gives 1 but then why does it

Casting subclass to superclass and calling a function that both classes have

十年热恋 提交于 2021-02-08 11:02:10
问题 public class Test3 { public static void main(String[] args) { Derived04 b = new Derived04(); Base04 a = (Base04)b; System.out.println(a.i); System.out.println(a.f()); } } class Base04 { int i=1; public int f() {return i;} } class Derived04 extends Base04 { int i = 2; public int f(){return -i;} } Both classes include the f() method so when I cast d onto a which method does it go to when I call a.f()? I thinking since a is declared as Base04 type when you do a.i it gives 1 but then why does it

Dynamic LINQ Cast issue

廉价感情. 提交于 2021-02-08 08:13:14
问题 When I try to execute this ESQL (Cast int to string) with dynamic linq (from this link) queryable.Where("CAST(PositionID AS Edm.String).Contains(@0)", paramsObj); //PositionID is Int32 it throw exception ')' or ',' expected My Entity Framework version is 4.0. Any idea how to resolve this problem ? Thanks in advance, Brian 回答1: you cann't use as inside function call, try change your code like this queryable.Where("(PositionID.ToString().Contains(@0))", paramsObj); //PositionID is Int32 UPDATE:

Dynamic LINQ Cast issue

泄露秘密 提交于 2021-02-08 08:06:01
问题 When I try to execute this ESQL (Cast int to string) with dynamic linq (from this link) queryable.Where("CAST(PositionID AS Edm.String).Contains(@0)", paramsObj); //PositionID is Int32 it throw exception ')' or ',' expected My Entity Framework version is 4.0. Any idea how to resolve this problem ? Thanks in advance, Brian 回答1: you cann't use as inside function call, try change your code like this queryable.Where("(PositionID.ToString().Contains(@0))", paramsObj); //PositionID is Int32 UPDATE:

Dynamic LINQ Cast issue

会有一股神秘感。 提交于 2021-02-08 08:04:27
问题 When I try to execute this ESQL (Cast int to string) with dynamic linq (from this link) queryable.Where("CAST(PositionID AS Edm.String).Contains(@0)", paramsObj); //PositionID is Int32 it throw exception ')' or ',' expected My Entity Framework version is 4.0. Any idea how to resolve this problem ? Thanks in advance, Brian 回答1: you cann't use as inside function call, try change your code like this queryable.Where("(PositionID.ToString().Contains(@0))", paramsObj); //PositionID is Int32 UPDATE:

Getting incompatible error for parent child classes java

蹲街弑〆低调 提交于 2021-02-08 07:34:03
问题 I have a HashMap defined: HashMap<String,ArrayList<Thing>> hashmap = new HashMap<>(); I do need an ArrayList since I want one key to have multiple values. This was the best solution I found for that problem. The Thing class is a parent class for a bunch of other classes. Problem is when I try to add I get an error. hashmap.put(b, world.ports); This is the error: no suitable method found for put(String,ArrayList<SeaPort>) method Map.put(String,ArrayList<Thing>) is not applicable (argument

Getting incompatible error for parent child classes java

久未见 提交于 2021-02-08 07:32:00
问题 I have a HashMap defined: HashMap<String,ArrayList<Thing>> hashmap = new HashMap<>(); I do need an ArrayList since I want one key to have multiple values. This was the best solution I found for that problem. The Thing class is a parent class for a bunch of other classes. Problem is when I try to add I get an error. hashmap.put(b, world.ports); This is the error: no suitable method found for put(String,ArrayList<SeaPort>) method Map.put(String,ArrayList<Thing>) is not applicable (argument