【问题标题】:How do I access a value from a method of my simple type?如何从我的简单类型的方法中访问值?
【发布时间】:2020-01-24 11:05:09
【问题描述】:

如果我想创建我的简单类型,如何从 type 方法访问它的值?
例如:

[IntegerType (rank = 6, signed = true, width = 32)]
[SimpleType]
[CCode (has_type_id = false)]
struct foo_t {
    public string say_hello(){
        return(@"Hello from new foo_t type");
    }
    public int x10(){
        return this.value * 10;
    }
}

这里this.value 抛出错误The name 'value' does not exist
say_hello 工作正常。

【问题讨论】:

    标签: glib vala


    【解决方案1】:

    答案非常简单,没有隐藏的value 字段,只有this 是当前值。

    [IntegerType (rank = 6, signed = true, width = 32)]
    [SimpleType]
    [CCode (has_type_id = false)]
    struct foo_t {
        public int x10(){
            return this * 10;
        }
    }
    void main () {
        foo_t foo = 5;
        prin(foo.x10());
    }
    
    
    [Print]
    inline void prin (string str) {
        stdout.printf (str + "\n");
    }
    

    工作正常!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-15
      • 2011-09-08
      • 1970-01-01
      • 2017-07-03
      相关资源
      最近更新 更多