标签: forward-reference

为什么我必须使用"this"关键字进行前向引用?

当我使用this关键字访问类中的非静态变量时,Java不会给出任何错误.但是当我不使用它时,Java会出错.我为什么要用this

我知道通常应该什么时候使用this,但这个例子与正常用法非常不同.

例:

class Foo {
//  int a = b; // gives error. why ?
    int a = this.b; // no error. why ?
    int b;
    int c = b;

    int var1 = this.var2; // very interesting
    int var2 = this.var1; // very interesting
}
Run Code Online (Sandbox Code Playgroud)

java class this forward-reference

59
推荐指数
2
解决办法
5241
查看次数

是否有通用的方法来记忆Scala?

我想记住这个:

def fib(n: Int) = if(n <= 1) 1 else fib(n-1) + fib(n-2)
println(fib(100)) // times out
Run Code Online (Sandbox Code Playgroud)

所以我写了这个,这令人惊讶地编译和工作(我很惊讶,因为fib在其声明中引用自己):

case class Memo[A,B](f: A => B) extends (A => B) {
  private val cache = mutable.Map.empty[A, B]
  def apply(x: A) = cache getOrElseUpdate (x, f(x))
}

val fib: Memo[Int, BigInt] = Memo {
  case 0 => 0
  case 1 => 1
  case n => fib(n-1) + fib(n-2) 
}

println(fib(100))     // prints 100th fibonacci number instantly
Run Code Online (Sandbox Code Playgroud)

但是当我尝试在a中声明fib时def,我得到一个编译器错误:

def foo(n: …
Run Code Online (Sandbox Code Playgroud)

scope scala memoization dynamic-programming forward-reference

48
推荐指数
3
解决办法
2万
查看次数

前向参考与前向声明

我有点困惑.前向声明和前向引用有什么区别?在我的脑海中,当你声明一个尚未实现的函数时,前向声明是不正确的?您是否必须查看指定的情况,以声明案例"前向引用"或"前向声明"?

forward-reference forward-declaration

12
推荐指数
2
解决办法
2万
查看次数

为什么在Scala中允许使用未初始化的变量?

可能重复:
转发引用 - 为什么这段代码会编译?
Scala和前向引用

object Main extends App {
  val a = 4
  val b = a + c
  val c = 5
  println(b) // => 4
}
Run Code Online (Sandbox Code Playgroud)

这将打印4作为c显然是0b被分配.a并且c是价值观,因此它们不应该0在一个时刻,而5在下一个时刻.在Scala中,它们应该是不可变的,对吧?

我不应该至少得到某种警告吗?当然,在上面的例子中,你必须是一个想念的白痴,但在更复杂的情况下,很难说出它的顺序.

我当然知道我可以使用,lazy val b = ...但如果我认为我把它按正确的顺序排列,当我真的没有时.不是很危险,因为根本没有得到任何警告吗?运行得很好!?这是如何获得通过两者的Scala IDE 编译器?这是故意的吗?或者这是一个无法修复的错误?:/

:)

jvm scala forward-reference

10
推荐指数
0
解决办法
135
查看次数

使用angular2组件实现递归结构(没有forwardRef)?

我有一个递归结构,由两个组件组成:

  • OptionsMenuComponent(A菜单)
  • MenuItemComponent(菜单项)

OptionsMenuComponent :( 模板+组件)

Template:
    <menu-item *ngFor="let menuItem of menu.menuItems" [menuItem]="menuItem"> </menu-item>

Component:
    import { Component, Input } from '@angular/core';
    import { Menu } from '../models/menu.model';
    import { MenuItemComponent } from '../menu-item/menu-item.component';

    @Component({
        moduleId: __moduleName,
        selector: 'options-menu',
        templateUrl: 'options-menu.component.html',
        styleUrls: ['options-menu.component.css'],
        directives: [MenuItemComponent],
    })
    export class OptionsMenuComponent {
        @Input() menu: Menu;

        constructor() { }
    }
Run Code Online (Sandbox Code Playgroud)

菜单项:(模板+组件)

Template:
    <span class="menu-item-title">{{menuItem.title}}</span>

    <options-menu *ngIf="menuItem.subMenu != null" [menu]="menuItem.subMenu" class="sub-menu"> </options-menu>

Component:
    import { Component, Input, forwardRef } from '@angular/core';
    import { …
Run Code Online (Sandbox Code Playgroud)

forward-reference typescript angular2-directives angular2-template angular

8
推荐指数
1
解决办法
610
查看次数

什么是C中的前向参考?

C中关于指针的前向引用是什么?

我能举个例子吗?

c pointers forward-reference

6
推荐指数
3
解决办法
2万
查看次数

what does do forwardRef in angular?

What does forwardRef do in angular, and what is its usage?

here is an example:

import {Component, Injectable, forwardRef} from '@angular/core';

export class ClassCL { value; }

@Component({
    selector: 'my-app',
    template: '<h1>{{ text }}</h1>',
    providers: [{provide: ClassCL, useClass: forwardRef(() => ForwardRefS)}]
})
export class AppComponent {
    text;

    constructor( myClass: ClassCL ) {
        this.text = myClass.value;
    }
}

Injectable()
export class ForwardRefS { value = 'forwardRef works!' }
Run Code Online (Sandbox Code Playgroud)

forward-reference angular

6
推荐指数
2
解决办法
3215
查看次数

使用 TypeScript React Refs:无法读取未定义的属性“当前”

我正在使用 TypeScript 构建 React 应用程序。

我想创建按钮,滚动到主页上子组件的标题。

我已经在子组件中创建了一个引用,遵循此堆栈溢出答案,并(尝试)使用前向引用在我的父组件上访问它。

export class Parent extends Component {
  private testTitleRef!: RefObject<HTMLHeadingElement>;

  scrollToTestTitleRef = () => {
    if (this.testTitleRef.current !== null) {
      window.scrollTo({
        behavior: "smooth",
        top: this.testTitleRef.current.offsetTop
      });
    }
  };

  render() {
    return <Child ref={this.testTitleRef} />
  }
}

interface Props {
  ref: RefObject<HTMLHeadingElement>;
}

export class Child extends Component<Props> {
  render() {
    return <h1 ref={this.props.ref}>Header<h1 />
  }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,当我触发时,scrollToTestTitleRef我收到错误:

Cannot read property 'current' of undefined
Run Code Online (Sandbox Code Playgroud)

这意味着 ref 未定义。这是为什么?我究竟做错了什么?

编辑: Estus …

forward-reference typescript reactjs react-ref

6
推荐指数
1
解决办法
1万
查看次数

const转发参考给出错误C2440:“正在初始化”:无法从“ const std :: string”转换为“ const std :: string &amp;&amp;”

以下给出了编译器错误:

  #include <string>

  const std::string& get_name();

  int main(){
    auto&& name1 = get_name();//should bind to whatever
    const auto& name2 = get_name();//also ok
    const auto&& name3 = get_name();//<-not ok, why ?
    return 0;
  }
Run Code Online (Sandbox Code Playgroud)

链接到Godbolt:https://godbolt.org/z/l6IQQ7

如果我使用const auto&它可以编译-但这不会绑定到值。 auto&&将绑定到任何东西,以便自然也可以工作。但是,const auto&&在这种情况下不绑定背后的逻辑是什么?我知道auto&&会保留constness-但是有没有办法使之const明确,同时又与引用/值无关

动机:

对于函数等内部的“正常编程工作”,可以说类似这样的东西很棒:“我不在乎它是值还是引用-但我不会在其余函数中更改它”

给定当前语言,这应该是可能的。

相关问题: 为什么添加const会使通用引用成为右值

c++ forward-reference c++17

6
推荐指数
1
解决办法
86
查看次数

如何在 Nestjs 中的猫鼬模式中进行双重引用而不形成循环依赖?

作者.module.ts

@Module({
  imports: [
    forwardRef(() => ArticleModule),
    MongooseModule.forFeature([{ name: Author.name, schema: AuthorSchema }]),
  ],
  controllers: [AuthorController],
  providers: [AuthorService, AuthorsRepository, UtilService],
  exports: [AuthorService],
})
export class AuthorModule {}
Run Code Online (Sandbox Code Playgroud)

文章.module.ts

  @Module({
  imports: [
   forwardRef(() => AuthorModule),
    CityModule,
    // BookmarkModule,
    MongooseModule.forFeature([{ name: Article.name, schema: ArticleSchema }]),
  ],
  controllers: [ArticleController],
  providers: [ArticleService, ArticlesRepository],
})
export class ArticleModule {}
Run Code Online (Sandbox Code Playgroud)

author.schema.ts(部分文件)

@Schema(DefaultSchemaConfig)
export class Author extends BaseSchema {
  @ApiPropertyOptional()
  @IsString()
  @Prop()
  name?: string;

  @ApiPropertyOptional({ type: ArticleRefDto, isArray: true })
  @IsArray()
  @Prop({ type: [{ type: MongooseSchema.Types.ObjectId, ref: …
Run Code Online (Sandbox Code Playgroud)

circular-dependency forward-reference mongoose nestjs

5
推荐指数
0
解决办法
517
查看次数