将runtimeType作为泛型函数参数传递

时间:2018-05-14 08:54:26

标签: swift dart generic-programming

我是Swift开发人员,也是Dart的新手。我正在尝试编写一些通用方法。

我想知道我是否可以在Dart中达到类似的效果。

     RULES = {
  ol: {
    1 => ->(index) { "#{index + 1}. " },
    2 => ->(index) { "#{}" },
    3 => ->(index) { "#{}" },
    4 => ->(index) { "#{}" }
  },
  ul: {
    1 => ->(_) { "\u2022 " },
    2 => ->(_) { "" },
    3 => ->(_) { "" },
    4 => ->(_) { "" },
  }
}

def ol_rule(group, deepness: 1)
  group.search('> li').each_with_index do |item, i|
    prefix = RULES[:ol][deepness].call(i)
    item.prepend_child(prefix)
    descend(item, deepness + 1)
  end
end

def ul_rule(group, deepness: 1)
  group.search('> li').each_with_index do |item, i|
    prefix = RULES[:ul][deepness].call(i)
    item.prepend_child(prefix)
    descend(item, deepness + 1)
  end
end

def descend(item, deepness)
  item.search('> ol').each do |ol|
    ol_rule(ol, deepness: deepness)
  end
  item.search('> ul').each do |ul|
    ul_rule(ul, deepness: deepness)
  end
end

doc = Nokogiri::HTML.fragment(text)

doc.search('ol').each do |group|
  ol_rule(group, deepness: 1)
end

doc.search('ul').each do |group|
  ul_rule(group, deepness: 1)
end


  puts doc.inner_text


1. One
2. Two

1. Inner One
2. inner Two

• hey

1. hiiiiiiiii
2. why
3. hiiiiiiiii


• aniket 


3. sup 
4. there 

3. hey 
4. Three



1. Introduction

1. Introduction

2. Description
3. Observation
4. Results
5. Summary



• Introduction
• Description

• Observation

• Results

• Summary






• Overview

对于Dart,我可以转换泛型类的类型吗?像Swift中的//Swift version public func modelFrom<T: Mappable>(response: Encodable?, model: T.Type) -> T? { if let response = response, let string = response.jsonString { return T(JSONString: string) } return nil } // The method can be called like this let responseModel = modelFrom(response: response, model: FoodLogModel.self) 一样?

Dart中的<T: Mappable>相当于Swift中的runtimeType吗?

非常感谢

这是我尝试过的,

<ClassName>.self

但我不知道如何传递import 'package:dartson/dartson.dart'; import 'package:mobile_corelib/base/model.dart'; T requestFrom<T>(BaseModel model, Type T) { try { var dson = new Dartson.JSON(); var object = dson.map(dson.encode(model), T.runtimeType); return object; } catch(error) { return null; } } var dick = requestFrom(model, AccessTokenRequest().runtimeType) 。我应该使用Class type吗?或dynamic

1 个答案:

答案 0 :(得分:0)

Dart中的

Typenew List<String>()类型的值,这与在泛型类型参数中编写类型时发生的情况不同。

例如,new List<''.runtimeType>()有效,但Type是语法错误。

通常,SQL = "SELECT [SCHED_SURGERY_DATETIME] " SQL = SQL & "FROM Raw_Data " SQL = SQL & "WHERE [PROCEDURE_NM_WID] = '" & size_system & "' " SQL = SQL & "ORDER BY [SCHED_SURGERY_DATETIME];" 非常有限。您无法使用它来创建新实例 - 除非您在虚拟机中使用镜像。