标签: variable-assignment

空非零字符串的合并 Swift 运算符

我在 Swift 中有一个字符串变量:

let foo = "bar"
Run Code Online (Sandbox Code Playgroud)

但它也可以是任何其他任意字符串。现在我想做以下事情:

let bar = ... // either the value of foo, or if foo is "", it will be "default"
Run Code Online (Sandbox Code Playgroud)

我查找了“合并运算符”,这似乎是正确的方向。我用下面的代码试了一下:

let bar = foo ?? "default"
Run Code Online (Sandbox Code Playgroud)

但是好像什么时候foo为空,还是会取这个,不会设置default为值。

我假设合并运算符仅适用于nil值而不适用于空字符串。但是,是否有解决方案可以测试我的值是否为空字符串并分配默认值?我不能使用 if/else,因为它将位于一个类中。

string variable-assignment swift

4
推荐指数
2
解决办法
1506
查看次数

为什么可以将整数值分配给未初始化的指针

当我这样做时,它完美地打印出“2”。

int main()
{
    int *p;
    int x = 2;

    *p = x;

    cout << *p;

}
Run Code Online (Sandbox Code Playgroud)

但是当我第一次将 *p 初始化为 null 时,程序崩溃了。

int main()
{
    int *p=0;
    int x = 2;

    *p = x;

     cout << *p;

}
Run Code Online (Sandbox Code Playgroud)

我想问一下,第一个程序到底成功运行了什么,为什么可以给一个未初始化的指针赋值?

[编辑] 我的问题实际上与我得到的这个过去的考试问题有关。您可以勾选多个答案,看来 (b) 和 (c) 都是正确的。但现在我知道 (c) 是否有效纯粹是因为运气。

在此处输入图片说明

c++ pointers variable-assignment

4
推荐指数
1
解决办法
769
查看次数

使用 C API 覆盖 Lua 中的赋值运算符

我的 C++ 程序中有对象作为用户数据传递给 Lua,并且我覆盖了此用户数据的元表,以便对对象索引的赋值(通过 __newindex 和 __index)导致对 C 的调用,从而转换赋值影响 C++ 对象或将 C++ 元素转换为 Lua 值(另一个用户数据或基本类型,如 bool、number、string)。用户数据作为参数传递给从我的 C++ 程序调用的类似事件的 Lua 函数。

luaL_newmetatable(L, "object");
lua_pushstring(L, "__index");
lua_pushvalue(L, -2);  /* pushes the metatable */
lua_settable(L, -3);  /* metatable.__index = metatable */

luaL_openlib(L, NULL, vallib_m, 0);
luaL_openlib(L, "internal", vallib_f, 0);

lua_pushstring(L, "__index");
lua_pushstring(L, "get");
lua_gettable(L, 2);  /* get val.get */
lua_settable(L, 1);  /* metatable.__index = val.get */

lua_pushstring(L, "__newindex");
lua_pushstring(L, "set");
lua_gettable(L, 2); /* get array.set */
lua_settable(L, 1); /* metatable.__newindex = val.set */ …
Run Code Online (Sandbox Code Playgroud)

c c++ lua variable-assignment metatable

4
推荐指数
1
解决办法
1027
查看次数

PHP:在if语句中分配多个变量

我似乎无法在"if"语句中分配多个变量。以下代码:

<?php

function a ($var)
{
    if ($var == 1)
    {
        return 'foo';
    }

    return false;
}

function b ($var)
{
    if ($var == 1)
    {
        return 'bar';
    }

    return false;
}

if ($result1 = a(1) && $result2 = b(1))
{
    echo $result1 . ' ' . $result2;
}

?>
Run Code Online (Sandbox Code Playgroud)

返回“1 bar”而不是“foo bar”。如果我删除第二个条件/赋值,它会返回“foo”。

有没有办法在“if”语句中分配多个变量,还是仅限于一个?

php if-statement variable-assignment

4
推荐指数
2
解决办法
2765
查看次数

为什么扩展切片分配不如常规切片分配灵活?

根据有关扩展切片的 Python 文档:

如果您有一个可变序列,例如列表或数组,您可以分配或删除扩展切片,但分配给扩展切片和常规切片之间存在一些差异。对常规切片的赋值可用于更改序列的长度:

>>> a = range(3)
>>> a
[0, 1, 2]
>>> a[1:3] = [4, 5, 6]
>>> a
[0, 4, 5, 6]
Run Code Online (Sandbox Code Playgroud)

扩展切片不是那么灵活。分配给扩展切片时,语句右侧的列表必须包含与其要替换的切片相同数量的项目:

>>> a = range(4)
>>> a
[0, 1, 2, 3]
>>> a[::2]
[0, 2]
>>> a[::2] = [0, -1]
>>> a
[0, 1, -1, 3]
>>> a[::2] = [0,1,2]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: attempt to assign sequence of size 3 to extended slice of …
Run Code Online (Sandbox Code Playgroud)

python list variable-assignment slice python-3.x

4
推荐指数
1
解决办法
1526
查看次数

已声明但未使用 - 如何更新 IF 语句中的变量

我试图在 Go Lang 中设置一个带有默认值的 bool 变量并根据条件更新它。func foo编译,但函数bar未编译并给出错误“f 已声明且未使用”

有一个相关的答案- 它没有解释下一个问题

Go 中 this(bar 函数)的正确模式是什么?

这是代码:

package main

import (
"fmt"
"strconv"
)

func foo(m map[string]string) bool {
    f := false

if _, exists := m["READWRITE"]; exists {
    fmt.Println("Before Updating f : ", f)
    f, _ = strconv.ParseBool(m["READWRITE"])
    //if err != nil {
    //  panic(err)
    //}
}

fmt.Println("After Updating f : ", f)

return f
}

func bar(m map[string]string) bool {
    f := false

    if _, …
Run Code Online (Sandbox Code Playgroud)

if-statement variable-assignment go

4
推荐指数
2
解决办法
4601
查看次数

限制 Typescript 中 if 语句的赋值

最近我发现了一个由 If 语句中的赋值引起的令人讨厌的错误。我认为 if 赋值对我来说是一个糟糕的模式,我想完全关闭它以获得编译器 varnings 而不是实际的错误。

我怎么能在打字稿中做到这一点?

variable-assignment compiler-options typescript

4
推荐指数
1
解决办法
236
查看次数

设置一个等于 {} 的向量;

以下代码始终有效还是依赖于编译器/平台?显然,我可以edges使用值构造函数进行初始化,但是我很好奇operator=在edges初始化为大小 0时复制赋值是否在此处工作,然后设置为等于带括号的 r 值。

它适用于我的 macbook。

std::vector<std::vector<int>> edges;
edges = {{1,2,3},{4},{5,6}};
Run Code Online (Sandbox Code Playgroud)

c++ vector variable-assignment initializer-list c++11

4
推荐指数
1
解决办法
983
查看次数

为什么要避免使用递增赋值运算符 (+=) 来创建集合

StackOverflow 站点的问答中+=经常使用递增赋值运算符 ( )[PowerShell]来构造集合对象,例如:

$Collection = @()
1..$Size | ForEach-Object {
    $Collection += [PSCustomObject]@{Index = $_; Name = "Name$_"}
}
Run Code Online (Sandbox Code Playgroud)

然而,它似乎是一种非常低效的操作。

一般情况下,+=在 PowerShell 中构建对象集合时应该避免使用递增赋值运算符 ( ) 是否可以?

collections powershell variable-assignment operator-keyword

4
推荐指数
1
解决办法
529
查看次数

map with function for each element?

Very often I process single elements of tuples like this:

size, duration, name = some_external_function()
size = int(size)
duration = float(duration)
name = name.strip().lower()
Run Code Online (Sandbox Code Playgroud)

If some_external_function would return some equally typed tuple I could use map in order to have a (more functional) closed expression:

size, duration, name = map(magic, some_external_function())
Run Code Online (Sandbox Code Playgroud)

Is there something like an element wise map? Something I could run like this:

size, duration, name = map2((int, float, strip), some_external_function())
Run Code Online (Sandbox Code Playgroud)

Update: I know I can …

python functional-programming variable-assignment python-3.x iterable-unpacking

4
推荐指数
1
解决办法
260
查看次数