Implementing a generic incrementable trait in Rust

I'm trying to understand how to implement a generic trait in Rust. While I've seen a number of examples, the examples are too tied to a specific use (e.g. genomic mutators) for me to be able to understand at this point in my Rust development. Instead, here's a simple example based on something fairly universal--incrementing: trait Incrementable { fn post_inc(&mut self) -> Self; fn post_inc_by(&mut self, n: usize) -> Self; } impl Incrementable for usize { fn post_inc(&mut self) -> Self { let tmp = *self; *self += 1; tmp } //"Overload" for full generalizability fn post_inc_by(&mut self, n: usize

2022-01-18 16:36:40    分类:问答    rust   traits   generic-programming

Java: Integer obj can't cast to Comparable

I'm having problems trying to pass an Integer object from a driver class as an argument for function of a SortedArray Generic class I created. From my driver class, I convert the user's int input into an Integer object to be cast onto Comparable of my SortedArray class. I continue to receive the error: "Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to Comparable". I took a look at some of my classmates' source codes only to find little difference in setting the arguments/parameters yet they have their code working just fine. I've been looking for

2022-01-18 12:06:16    分类:问答    java   generic-programming   comparable

Ambiguous type variable

Related to my earlier question on traversing data structures, I'm having a problem making my code generic when I use it along with the uniplate package. I'm dealing with the data structures in the Language.Exts.Annotated.Syntax module, which are all generic with a type parameter l. This l is the same throughout the tree. The kind of code I'm writing is like this: doInt :: Child1 l -> Child1 l doInt (Child1 l n) = Child1 l (n + 1) doString :: Child2 l -> Child2 l doString (Child2 l (_:s)) = Child2 l ('j' : s) replace :: Data l => Parent l -> Parent l replace = transformBi doInt . transformBi

2022-01-18 07:23:11    分类:问答    haskell   generic-programming

C++ Concepts: Can I define a concept that is itself a template?

Sorry if the question isn't too clear. I'm not sure the best way to phrase it (feel free to edit!). I think an example would be the most clear: I am attempting to define a Monad concept based off of the Haskell definition. The bind operator (>>=) requires that a Monad of type A can be bound to a function that takes an A and returns a Monad of type B. I can define A in terms of a value_type typedef but how do I define type B in my concept? template <typename M> concept bool Monad() { return requires(M m, Function<_1, ValueType<M>> f) { // (>>=) :: m a -> (a -> m b) -> m b { m >>= f } -> M } }

2022-01-17 07:07:25    分类:问答    c++   generic-programming   c++17   c++-concepts

使用 Spring Boot Data JPA 按需创建表(Creating tables on demand with Spring Boot Data JPA)

问题 我正在尝试创建一个 Spring Boot 应用程序,我想在其中按需创建和使用数据库表,而不为它们定义实体/存储库。 我有一个基础业务实体和业务类型实体来保留业务类型。 我希望能够用自己的表为业务类型(比如 Coffeeshop)动态创建一个表(同时我可以将表的名称保留在 BusinessType 表上并决定从那里查询哪个表)。 因此,在不定义实体和/或为 Coffeeshop 创建 CrudRepository 的情况下,我可以创建一个名为 Coffeeshop 的表,然后对其进行一些查询吗? 如果没有,我的替代方案是什么? 在典型的 Spring Data JPA 应用程序中,您如何处理具有基本类型并允许系统具有更多具体类型(持久并与基本类型相关联)的情况? 谢谢 回答1 您描述的持久性策略不适合 JPA。 JPA 是一种 ORM(对象关系映射)策略,其中框架将数据从基于类的对象映射到模式定义的关系表中的行。 如果表没有对应的类,或者类没有特定的表,这会使注释驱动的实现复杂化。 要使用 JPA 解决您的问题,一个惯用的解决方案是将您的所有业务保存在一个表中(映射到业务实体类,如您所描述的),并在表上有一个(外)键以允许每一行具有一种业务类型。 然后,您可以通过查询(而不是针对不同的表)选择具有特定类型的业务集。 Business id | type | name |

2022-01-16 06:25:48    分类:技术分享    java   spring-boot   spring-data-jpa   ddl   generic-programming

Creating tables on demand with Spring Boot Data JPA

I'm trying to create a Spring Boot application where I want to create and use database tables on demand, without defining entities / repositories for them. I have got a base Business entity, and BusinessType entity to keep the type of business. I want to be able to dynamically create a table for a business type (say Coffeeshop) with its own table (while I can keep the name of the table on the BusinessType table and decide which table to query from there). So without defining an entity and/or creating a CrudRepository for Coffeeshop can I create a table named Coffeeshop and later do some

2022-01-16 01:35:30    分类:问答    java   spring-boot   spring-data-jpa   ddl   generic-programming

在考虑对齐要求时一般重载 operator new(Generically overloading operator new while considering alignment requirements)

问题 情况 我正在为动态内存(取消)分配编写内存管理器。 对于在调用operator new (或delete )时class A使用它, class A继承自class CustomAllocate就足够了,它本身以使用内存管理器的方式重载new和delete 。 问题 但是,显然我完全错过了对齐要求。 不幸的是, CustomAllocate::new没有关于继承自它的class A应该如何对齐的信息,因为唯一的参数是所请求内存的大小。 我正在寻找一种方法来包含对齐信息,而不必在每个class A中重载new (和delete )来使用内存管理器。 想法 1(以及为什么它不起作用) 使用表示对齐要求的整数值模板class CustomAllocate并像这样继承: class A : public CustomAllocate< alignof(A) > 。 不可能,因为alignof(A)在必须作为模板参数传递时不知道,即使传递的参数永远不应该改变class A的对齐要求。 想法 2(以及为什么它不起作用) 拥有一个纯虚函数virtual int CustomAllocate::getAlignment() = 0 ,通过复制粘贴类似return alignof(A);的方式在每个class A中实现。 . 不可能,因为new是静态的,因此永远无法访问虚函数。

2022-01-15 19:02:03    分类:技术分享    c++   alignment   operator-overloading   new-operator   generic-programming

Amazon API Gateway Import From Swagger 错误 - 不采用泛型(Amazon API Gateway Import From Swagger Error - Not taking Generics)

问题 我正在尝试通过从 Swagger 导入创建新的 APIGateway,但出现验证错误: 导致问题的特定类是我们的 PaginationModel 类。 代码模型定义: public class PaginationModel<T> { public IEnumerable<T> items { get; set; } public int offset { get; set; } public int totalCount { get; set; } } 代表特定类型的 Generic PaginationModel 的 Swagger 文件部分: *"PaginationModel[DepartmentUIModel]":{"type":"object","properties":{"items": {"type":"array","items":{"$ref":"#/definitions/DepartmentUIModel"}},"offset": {"format":"int32","type":"integer"},"totalCount":{"format":"int32","type":"integer"}}}* 将 Swagger 文件导入 Amazon API Gateway 时出错: 无法为“PaginationModel

2022-01-13 23:57:25    分类:技术分享    amazon-web-services   generics   swagger   generic-programming   aws-api-gateway

Generically overloading operator new while considering alignment requirements

Situation I am writing a memory manager for dynamic memory (de)allocations. For a class A to use it when operator new (or delete) is called, it is sufficient for class A to inherit from a class CustomAllocate, which itself overloads new and delete in a way that uses the memory manager. Problem However, apparently I completely missed out on alignment requirements. Unfortunately, CustomAllocate::new has no information about how a class A inheriting from it should be aligned as the only parameter is the size of the requested memory. I am searching for a way to include alignment information

2022-01-13 12:04:57    分类:问答    c++   alignment   operator-overloading   new-operator   generic-programming

如何使 VB.NET 的参数作为通用类型起作用?(How to make Parameters of VB.NET function as Generic type?)

问题 我有一个如下的VB.NET函数,传递给函数的参数'x'是'Single'类型。 但是,我想编写函数以便它可以接受任何数字类型,例如“单”、“双”和“整数”。 我知道这样做的一种方法是编写 3 个具有相同名称的函数,但这会非常乏味。 任何人都可以提出任何想法吗? 谢谢你。 Public Function Square(x As Single) As Single Return x * x End Function 回答1 试试下面的方法 Public Function Square(Of T)(ByVal x As Object) As T Dim b As Object = Val(x) * Val(x) Return CType(b, T) End Function 您可以像这样使用上述功能 Dim p As Integer = Square(Of Integer)(10) Dim d As Double = Square(Of Double)(1.5) 回答2 您可以通过 IConvertible 和Structure约束泛型类型。 以下数据类型实现了 IConvertible 接口: System.Boolean 系统字节系统字符系统日期时间 System.DBNull System.Decimal 系统.Double 系统枚举 System.Int16 System

2022-01-12 16:41:31    分类:技术分享    vb.net   function   generics   generic-programming