Questions tagged [type-erasure]

In Java, type erasure is the process where the compiler removes all information related to type parameters and type arguments within a class or method when a generic type is instantiated. In C++ type erasure refers to a technique for hiding some or all of the type information regarding a class.

Filter by
Sorted by
Tagged with
0 votes
1 answer
23 views

Is there any way to type erase only the parameter of closure in Swift?

Here's my code: init<T: RandomAccessCollection>( items: T, build: @escaping (T.Element) -> any View ) where T.Element : Hashable { self.items = ...
shipty's user avatar
  • 1
0 votes
0 answers
43 views

Why java raw type erase types of concretized members [duplicate]

Given below example: import java.util.ArrayList; import java.util.List; public class MyTemplate<T> { T parameterizedObj; List<String> stringList; public static void main(...
Fengtao Ding's user avatar
1 vote
1 answer
70 views

Is it true that overloading must differ in parameter types or number of parameters but not erasure in Java?

Erasure is decided at runtime and overloaded method to call is resolved at compile time. Is it correct to say that overloading must differ in parameter types or number of parameters but not erasure in ...
banan3'14's user avatar
  • 4,112
0 votes
0 answers
75 views

Type erasure into a void*

I'm seeking help in validating the correctness of the following code: #include <iostream> // Implementing a kind of 'reference' to any functor with a void(void) call signature struct ...
quarky02's user avatar
1 vote
1 answer
105 views

How to define a type-erased ranges::view?

I have a class with a predicate, for example, Object::isValid(). I need to iterate over only valid objects, so I'm using ranges/views: struct Object { bool isValid() const; // ... }; // Usage:...
Dmitry Kuzminov's user avatar
0 votes
1 answer
34 views

A proper way to identify a genericly-typed contents on an Any collection in Scala

first of all, I know many resources that explain the issue with type erasure, how to use ClassTags, TypeTags, etc... Yet, in this case I would kindly ask to focus on an exact problem, and suggest the ...
Uko's user avatar
  • 13.2k
2 votes
2 answers
57 views

How to use Generics with Java Functional code

I am trying to implement a simple method using Generics that first finds all records of specific type from repository, then we call getId() method to collect the ids and then print all ids for that ...
J Dev's user avatar
  • 23
0 votes
0 answers
38 views

How is the array created at runtime: Generics and Type Erasure [duplicate]

While going through some questions on Generics, I came accross the question- Why can't we have Generic Arrays in Java? Next, I explored and came accross following article- https://www.baeldung.com/...
ayush's user avatar
  • 534
0 votes
1 answer
79 views

Passing a generic Type as responseType in RestTemplate.exchange() in Spring Rest Template

I have a candy factory that exposed various endpoints to get candies in a bag. candy such as name: m&m ; endpoint: candy-factory/mnms name: skittle; endpoint: candy-factory/skittles name: ...
barun's user avatar
  • 393
2 votes
2 answers
99 views

How is Spring able to see type arguments at runtime? What about the whole type erasure thing?

I don't understand how it is possible to see exception messages like this one Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.List<...
JoreJoh's user avatar
  • 321
0 votes
1 answer
30 views

Confusion regarding the use of Type Erasure by the Compiler in certain situation [duplicate]

I am trying to learn Java Generics. I wrote the following code and was trying to understand how compiler apply Type erasure rules on different scenarios of codes. Here is the code: package ...
Dipto Paul Dip's user avatar
0 votes
1 answer
46 views

How to print a nested ArrayList?

I want to print a nested ArrayList in Java as the title suggests and I'm unable to do that because of conflict with another method. Here's my code: import java.util.*; class TBF { <T> void ...
THE BLUE FRIEND's user avatar
0 votes
0 answers
82 views

Extending struct with type erasure apprach to avoid major refactoring

I am writing from existing (not my) code. It uses Path (i.e. using Path = std::vector<Point>;) everywhere. Point has no functionality. It just stores data. One of the methods takes Path, filters ...
theateist's user avatar
  • 14k
2 votes
1 answer
127 views

Access class member array of unknown size through pointer to first member

I want to be able to handle pointers to objects with an array member of unknown size, and access that array through a type-erased pointer to their common first member. My current attempt is the ...
Lukas Lang's user avatar
0 votes
1 answer
56 views

Casting object from erasure-type object to concrete type

I'm trying to use Type Erasure concept. Based on the object type I'd like to cast it to concrete type. I attempted to do this as shown below, but it didn't work. It prints some number instead of 11. ...
theateist's user avatar
  • 14k
0 votes
1 answer
52 views

Type-erase owner in self-referential crates

I have a struct, and want to define another struct that references its field. struct Tweet { text: Text } #[ouroboros::self_referencing] struct BorrowedText<Owner> { owner: Arc<Owner>,...
mq7's user avatar
  • 1,125
0 votes
2 answers
102 views

How to return one of two possible templated object types in C++?

I have created a minimal example based on my complicated project that is using boost: template<typename T> struct Data { T attribute; void print() {} }; typedef Data<int> ...
Oleg's user avatar
  • 1,055
0 votes
1 answer
68 views

Type erased on implicit evidence check

I'm trying to create a poor man version of refined types implementation with a simple validation of string content and check the return type via Implicit evidence on another function. It seems that ...
Led's user avatar
  • 615
1 vote
1 answer
137 views

JSON Object in Swift

For the purpose of my app I'm using JSON objects that are defined like: typealias JSONObject = [String:JSONValue] Where JSON Value is a protocol to which possible types are conforming to: protocol ...
Thomas Albert's user avatar
1 vote
1 answer
131 views

Is that possible to upcast erased type without RTTI?

I have a custom implementation of std::expected, but with a type-erased error. It makes the expected type looks similar to exceptions. We just can use the next code: Expected<int> V = 123; V....
Artem Selivanov's user avatar
1 vote
0 answers
81 views

How Spring's ResolvableType resolves generics while java compiler does type erasure?

We know in Java that generic types are for compile time only and because of type erasure we will not have this generic type parameter info at runtime. But I came across Spring's ResolvableType which ...
Jayavardhan Gange's user avatar
3 votes
1 answer
44 views

What is the meaning of template parameters in boost::any_range?

I've been surfing the internet in search of some well-documented reference for stuff like boost::any_range, but all I've managed to find is an official source which only gives a brief explanation of ...
Kaiyakha's user avatar
  • 1,525
1 vote
0 answers
90 views

Is type erasure of getters and their return types (introspection) possible without heap allocations?

I am trying to write some form of type erased introspection for member getters and haven't been able to do it without heap allocations. Is there a way to do this while eliminating "new" in ...
Klypto's user avatar
  • 444
0 votes
1 answer
71 views

In Scala 2.13, why is it possible to summon unqualified TypeTag for abstract type?

Considering the following code: import scala.reflect.api.Universe object UnqualifiedTypeTag { val RuntimeUniverse = scala.reflect.runtime.universe trait HasUniverse { val universe: ...
tribbloid's user avatar
  • 3,756
-1 votes
2 answers
37 views

swift type erasure understanding, Cannot convert value of type 'AnyMapper<Payload, [Post]>' to expected element type 'AnyMapper<Payload, User>

struct Payload {} struct User {} struct Post {} protocol Mapper { associatedtype PayloadType associatedtype ResultType func map(_ payload: PayloadType) -> ResultType } class ...
GeX's user avatar
  • 3
1 vote
1 answer
324 views

In Scala 3, is it possible to use declared type of an object in runtime?

In Scala 2, most generic type information of an object is erased at runtime. At this moment, all 3 binary execution environments (JVM, javascript, and LLVM) abide this behaviour, they only differs in ...
tribbloid's user avatar
  • 3,756
1 vote
1 answer
72 views

Why doesn't Java erase types in an inheritance chain?

Type erasure only seems to occur when instantiating a generic type, but if you extend a generic type and provide a concrete type in place of T, the type is available at runtime. It seems to be the ...
Matthew Layton's user avatar
0 votes
0 answers
78 views

How can I convert type-erasure wrappers or use them as another wrapper

I created sets of functions using the type-erasure design pattern: Encodable: encode(), decode() Printable: print() If I overload these functions with MyStruct1 and MyStruct2, I'll be able to wrap ...
Sylvester's user avatar
  • 101
0 votes
1 answer
187 views

In Scala 3, what's the canonical method for pattern match that uses an erased type?

Here is a simple example: object MatchErasedType { trait Supe { self: Singleton => type T1 lazy val default: T1 def process(v: Any): T1 = { v match { case vv: T1 ...
tribbloid's user avatar
  • 3,756
0 votes
1 answer
111 views

swift: AnyCollection with different generic types

AnyCollection([1, 2, 3]) // AnyCollection<Int> AnyCollection(["a", "b", "c"]) // AnyCollection<String> These two AnyCollection is great, but they have ...
LiYanan2004's user avatar
0 votes
1 answer
36 views

What type should be pointed to for the result of cuDeviceGetGraphMemAttribute()?

cuDeviceGetGraphMemAttribute() takes a void pointer to a result variable. But - what type does it expect the pointed-to value to be? The documentation (for CUDA v12.0) doesn't say. I'm guessing it's ...
einpoklum's user avatar
  • 121k
0 votes
1 answer
63 views

Why can't you initialize objects with the assignment init but can with the parentheses init when taking std::function in the constructor?

Let's say you have a class taking callable in its constructor. class SomeClass { public: SomeClass(const std::function<int(void)>& callable) { ... } }; and a free ...
Jozef Nagy's user avatar
0 votes
0 answers
96 views

Scala match on generic type

I am trying to do a match on a value based on its type, and there is a case where the expected type is a generic, like below: def foo(bar: Matchable) = bar match case (bar: Vector[String]) => ...
Weier's user avatar
  • 1,359
0 votes
0 answers
58 views

How to reduce compiled binary size of type erasure

I am developing firmware in C++. As part of the firmware, I wrote my own, lightweight version of std::shared_ptr (without weak pointers and some other features) and std::make_shared. Without weak ...
Patrick Wright's user avatar
2 votes
0 answers
67 views

Reuse Natvis STL visualization fields in custom visualization

Question When writing custom Natvis visualizations for Visual Studio, (how) can I access fields of a lambda wrapped in a std::function? Concrete example I have a class looks somewhat like this (very ...
Robin's user avatar
  • 393
0 votes
1 answer
66 views

Scala type eraser

case class Thing[T](value: T) def processThing(thing: Thing[_]) = { thing match { case Thing(value: Int) => "Thing of int" case Thing(value: String) => "Thing of string&...
Bostonian's user avatar
  • 627
0 votes
1 answer
113 views

Confused about Type Erasure in Hands-On Design Patterns with C++ by Fedor G. Pikus

In his book, Fedor G. Pikus talks about type erasure in C++. Specifically, he mentions the example of a smart pointer which has its own deleter whose type is "abstracted away". This is the ...
m_ognjen's user avatar
  • 265
0 votes
3 answers
313 views

Templates and type erasure - Why does this program compile?

I have written an example type erasure program, but I noticed something that seemed strange. The code compiles - and I believe it shouldn't. More likely, it does something that I don't understand ...
FreelanceConsultant's user avatar
-2 votes
2 answers
160 views

Pass generic type with actual literal value in type parameter

I'm trying to pass a type parameter to a function public void foo(Class<T> class){ // do something } it's easy if i have to pass Integer or any simple Class by just passing foo(ClassName.class) ...
Akash Jain's user avatar
-1 votes
1 answer
69 views

C++ How to Refer to a Class' Members' Members Without Accessing Original Member

Hi I know the title is a little hard to understand, and that's just because I have no idea how to phrase this problem. Fortunately, I can provide an easy-to-understand example of my problem. Imagine a ...
Thicc Theo's user avatar
-1 votes
1 answer
90 views

Why java type erasure is needed at runtime at all?

Below is an excerpt from java documentation here Because of type erasure, List<Number> and List<String> both become List. Consequently, the compiler allows the assignment of the object l, ...
timedout's user avatar
  • 541
1 vote
1 answer
141 views

Why doesn't copy elision work in my static functional implementation?

I am trying to implement a "static" sized function, that uses preallocated store, unlike std::function that uses dynamic heap allocations. #include <utility> #include <cstddef> ...
Sergey Kolesnik's user avatar
2 votes
3 answers
250 views

Can static polymorphism (templates) be used despite type erasure?

Having returned relatively recently to C++ after decades of Java, I am currently struggling with a template-based approach to data conversion for instances where type erasure has been applied. Please ...
hennejg's user avatar
  • 23
-1 votes
1 answer
134 views

Why do you need type erasure in this case?

I have a simple protocol Provider like this: protocol Provider { func get() -> String } And a struct S that has an existential Provider as a field: struct S { var provider: any Provider } ...
user avatar
0 votes
2 answers
78 views

Java Comporator Of Integer Array [duplicate]

I'm trying to make a priority queue that orders integer arrays by the value of the first element, but I am running into an issue that the complier is complaining that an array is requred in my ...
pdewilde's user avatar
  • 176
0 votes
0 answers
48 views

Optimization-friendly Polymorphic NVI adapter class for Runtime Polymorphism (customizable ownership)

I have a polymorphic NVI class to provide an adapter for Runtime-Polymorphism. Type-erasure is used for loose coupling. The non-owning implementation can be optimized out in a compile-time context: // ...
Sergey Kolesnik's user avatar
3 votes
0 answers
196 views

Implementation-specific questions about type erasure via external polymorphism

I have recently watched a CppCon 2021 talk by Klaus Iglberger about type erasure pattern and tried to implement it based on the slides. Despite the code from slides not compiling, after a lot of ...
Ave Milia's user avatar
  • 599
2 votes
1 answer
426 views

Java generics: getting rid unchecked cast warning (case described inside)

Please forgive the bad naming, the problem described below is a very crude simplification and alteration of real code I have the following types defined: interface Bundle { // omitted } interface ...
Maksim Zelenov's user avatar
1 vote
1 answer
159 views

Generics type erasure

I have the concept that in generics the type perimeter's type is decided at run time and since the data type of an object has to be decided at compile time that is why an error is given: T obj = new T(...
Wahab Khaddim's user avatar
0 votes
2 answers
310 views

Type erasure Generics

An error occurs at new T[5] during compile-time saying => error: generic array creation and according to my understanding, the array is created during compile-time and since we don't know the type ...
Wahab Khaddim's user avatar

1
2 3 4 5
16